Merge "Revert "lowmemorykiller: make perms 0220""
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 74ec29d..7f9536e 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -51,3 +51,7 @@
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/init.rc)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/init.rc)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/bin/reboot)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/default.prop)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/recovery/root/default.prop)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/EXECUTABLES/lmkd_intermediates/import_includes)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libsysutils_intermediates/import_includes)
diff --git a/README b/README
deleted file mode 100644
index 0083247..0000000
--- a/README
+++ /dev/null
@@ -1,20 +0,0 @@
-
-The system/ directory is intended for pieces of the world that are the
-core of the embedded linux platform at the heart of Android. These
-essential bits are required for basic booting, operation, and debugging.
-
-They should not depend on libraries outside of system/... (some of them
-do currently -- they need to be updated or changed) and they should not
-be required for the simulator build.
-
-The license for all these pieces should be clean (Apache2, BSD, or MIT).
-
-Currently system/bluetooth/... and system/extra/... have some pieces
-with GPL/LGPL licensed code.
-
-Assorted Issues:
-
-- pppd depends on libutils for logging
-- pppd depends on libcrypt/libcrypto
-- init, linker, debuggerd, toolbox, usbd depend on libcutils
-- should probably rename bionic to libc
diff --git a/ThirdPartyProject.prop b/ThirdPartyProject.prop
deleted file mode 100644
index 18b0594..0000000
--- a/ThirdPartyProject.prop
+++ /dev/null
@@ -1,10 +0,0 @@
-# Copyright 2010 Google Inc. All Rights Reserved.
-#Fri Jul 16 10:03:09 PDT 2010
-currentVersion=2.6.32
-version=2.6.32
-isNative=true
-feedurl=http\://kernel.org/pub/linux/kernel/v2.6/
-name=linux
-keywords=linux
-onDevice=true
-homepage=http\://kernel.org
diff --git a/adb/.clang-format b/adb/.clang-format
new file mode 100644
index 0000000..0395c8e
--- /dev/null
+++ b/adb/.clang-format
@@ -0,0 +1,11 @@
+BasedOnStyle: Google
+AllowShortBlocksOnASingleLine: false
+AllowShortFunctionsOnASingleLine: false
+
+CommentPragmas: NOLINT:.*
+DerivePointerAlignment: false
+IndentWidth: 4
+PointerAlignment: Left
+TabWidth: 4
+UseTab: Never
+PenaltyExcessCharacter: 32
diff --git a/adb/Android.mk b/adb/Android.mk
index 62f012c..8d38077 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -5,85 +5,163 @@
LOCAL_PATH:= $(call my-dir)
+ADB_CLANG :=
+
+# libadb
+# =========================================================
+
+# Much of adb is duplicated in bootable/recovery/minadb and fastboot. Changes
+# made to adb rarely get ported to the other two, so the trees have diverged a
+# bit. We'd like to stop this because it is a maintenance nightmare, but the
+# divergence makes this difficult to do all at once. For now, we will start
+# small by moving common files into a static library. Hopefully some day we can
+# get enough of adb in here that we no longer need minadb. https://b/17626262
+LIBADB_SRC_FILES := \
+ adb.cpp \
+ adb_auth.cpp \
+ adb_io.cpp \
+ adb_listeners.cpp \
+ sockets.cpp \
+ transport.cpp \
+ transport_local.cpp \
+ transport_usb.cpp \
+
+LIBADB_CFLAGS := \
+ -Wall -Werror \
+ -Wno-unused-parameter \
+ -Wno-missing-field-initializers \
+ -fvisibility=hidden \
+
+LIBADB_darwin_SRC_FILES := \
+ fdevent.cpp \
+ get_my_path_darwin.c \
+ usb_osx.c \
+
+LIBADB_linux_SRC_FILES := \
+ fdevent.cpp \
+ get_my_path_linux.cpp \
+ usb_linux.cpp \
+
+LIBADB_windows_SRC_FILES := \
+ get_my_path_windows.cpp \
+ sysdeps_win32.c \
+ usb_windows.cpp \
+
+include $(CLEAR_VARS)
+LOCAL_CLANG := $(ADB_CLANG)
+LOCAL_MODULE := libadbd
+LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=0
+LOCAL_SRC_FILES := \
+ $(LIBADB_SRC_FILES) \
+ adb_auth_client.cpp \
+ fdevent.cpp \
+ jdwp_service.cpp \
+ qemu_tracing.cpp \
+ usb_linux_client.c \
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_CLANG := $(ADB_CLANG)
+LOCAL_MODULE := libadb
+LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=1
+LOCAL_SRC_FILES := \
+ $(LIBADB_SRC_FILES) \
+ $(LIBADB_$(HOST_OS)_SRC_FILES) \
+ adb_auth_host.cpp \
+
+# Even though we're building a static library (and thus there's no link step for
+# this to take effect), this adds the SSL includes to our path.
+LOCAL_STATIC_LIBRARIES := libcrypto_static
+
+ifeq ($(HOST_OS),windows)
+ LOCAL_C_INCLUDES += development/host/windows/usb/api/
+endif
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+LIBADB_TEST_SRCS := \
+ adb_io_test.cpp \
+ transport_test.cpp \
+
+include $(CLEAR_VARS)
+LOCAL_CLANG := $(ADB_CLANG)
+LOCAL_MODULE := adbd_test
+LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
+LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
+LOCAL_STATIC_LIBRARIES := libadbd
+LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
+include $(BUILD_NATIVE_TEST)
+
+include $(CLEAR_VARS)
+LOCAL_CLANG := $(ADB_CLANG)
+LOCAL_MODULE := adb_test
+LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
+LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp
+LOCAL_SHARED_LIBRARIES := liblog libbase
+LOCAL_STATIC_LIBRARIES := \
+ libadb \
+ libcrypto_static \
+ libcutils \
+
+ifeq ($(HOST_OS),linux)
+ LOCAL_LDLIBS += -lrt -ldl -lpthread
+endif
+
+include $(BUILD_HOST_NATIVE_TEST)
+
# adb host tool
# =========================================================
include $(CLEAR_VARS)
-# Default to a virtual (sockets) usb interface
-USB_SRCS :=
-EXTRA_SRCS :=
-
ifeq ($(HOST_OS),linux)
- USB_SRCS := usb_linux.c
- EXTRA_SRCS := get_my_path_linux.c
LOCAL_LDLIBS += -lrt -ldl -lpthread
LOCAL_CFLAGS += -DWORKAROUND_BUG6558362
endif
ifeq ($(HOST_OS),darwin)
- USB_SRCS := usb_osx.c
- EXTRA_SRCS := get_my_path_darwin.c
LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
-endif
-
-ifeq ($(HOST_OS),freebsd)
- USB_SRCS := usb_libusb.c
- EXTRA_SRCS := get_my_path_freebsd.c
- LOCAL_LDLIBS += -lpthread -lusb
+ LOCAL_CFLAGS += -Wno-sizeof-pointer-memaccess -Wno-unused-parameter
endif
ifeq ($(HOST_OS),windows)
- USB_SRCS := usb_windows.c
- EXTRA_SRCS := get_my_path_windows.c
EXTRA_STATIC_LIBS := AdbWinApi
- ifneq ($(strip $(USE_CYGWIN)),)
- # Pure cygwin case
- LOCAL_LDLIBS += -lpthread -lgdi32
- LOCAL_C_INCLUDES += /usr/include/w32api/ddk
- endif
ifneq ($(strip $(USE_MINGW)),)
# MinGW under Linux case
LOCAL_LDLIBS += -lws2_32 -lgdi32
USE_SYSDEPS_WIN32 := 1
- LOCAL_C_INCLUDES += /usr/i586-mingw32msvc/include/ddk
endif
- LOCAL_C_INCLUDES += development/host/windows/usb/api/
endif
+LOCAL_CLANG := $(ADB_CLANG)
+
LOCAL_SRC_FILES := \
- adb.c \
- console.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- commandline.c \
- adb_client.c \
- adb_auth_host.c \
- sockets.c \
- services.c \
- file_sync_client.c \
- $(EXTRA_SRCS) \
- $(USB_SRCS) \
- usb_vendors.c
+ adb_main.cpp \
+ console.cpp \
+ commandline.cpp \
+ adb_client.cpp \
+ services.cpp \
+ file_sync_client.cpp \
-LOCAL_C_INCLUDES += external/openssl/include
+LOCAL_CFLAGS += \
+ -Wall -Werror \
+ -Wno-unused-parameter \
+ -D_GNU_SOURCE \
+ -DADB_HOST=1 \
-ifneq ($(USE_SYSDEPS_WIN32),)
- LOCAL_SRC_FILES += sysdeps_win32.c
-else
- LOCAL_SRC_FILES += fdevent.c
-endif
-
-LOCAL_CFLAGS += -O2 -g -DADB_HOST=1 -Wall -Wno-unused-parameter
-LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
LOCAL_MODULE := adb
LOCAL_MODULE_TAGS := debug
-LOCAL_STATIC_LIBRARIES := libzipfile libunz libcrypto_static $(EXTRA_STATIC_LIBS)
+LOCAL_STATIC_LIBRARIES := \
+ libadb \
+ libcrypto_static \
+ $(EXTRA_STATIC_LIBS) \
+
ifeq ($(USE_SYSDEPS_WIN32),)
- LOCAL_STATIC_LIBRARIES += libcutils
+ LOCAL_STATIC_LIBRARIES += libcutils
endif
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_HOST_EXECUTABLE)
$(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
@@ -100,80 +178,48 @@
include $(CLEAR_VARS)
-LOCAL_SRC_FILES := \
- adb.c \
- backup_service.c \
- fdevent.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- adb_auth_client.c \
- sockets.c \
- services.c \
- file_sync_service.c \
- jdwp_service.c \
- framebuffer_service.c \
- remount_service.c \
- usb_linux_client.c
+LOCAL_CLANG := $(ADB_CLANG)
-LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter
-LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
+LOCAL_SRC_FILES := \
+ adb_main.cpp \
+ services.cpp \
+ file_sync_service.cpp \
+ framebuffer_service.cpp \
+ remount_service.cpp \
+ set_verity_enable_state_service.cpp \
+
+LOCAL_CFLAGS := \
+ -DADB_HOST=0 \
+ -D_GNU_SOURCE \
+ -Wall -Werror \
+ -Wno-unused-parameter \
+ -Wno-deprecated-declarations \
ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
endif
+ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
+LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
+endif
+
LOCAL_MODULE := adbd
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
+LOCAL_C_INCLUDES += system/extras/ext4_utils system/core/fs_mgr/include
-LOCAL_STATIC_LIBRARIES := liblog libcutils libc libmincrypt libselinux
-include $(BUILD_EXECUTABLE)
+LOCAL_STATIC_LIBRARIES := \
+ libadbd \
+ libfs_mgr \
+ liblog \
+ libcutils \
+ libc \
+ libmincrypt \
+ libselinux \
+ libext4_utils_static \
-
-# adb host tool for device-as-host
-# =========================================================
-ifneq ($(SDK_ONLY),true)
-include $(CLEAR_VARS)
-
-LOCAL_LDLIBS := -lrt -ldl -lpthread
-
-LOCAL_SRC_FILES := \
- adb.c \
- console.c \
- transport.c \
- transport_local.c \
- transport_usb.c \
- commandline.c \
- adb_client.c \
- adb_auth_host.c \
- sockets.c \
- services.c \
- file_sync_client.c \
- get_my_path_linux.c \
- usb_linux.c \
- usb_vendors.c \
- fdevent.c
-
-LOCAL_CFLAGS := \
- -O2 \
- -g \
- -DADB_HOST=1 \
- -DADB_HOST_ON_TARGET=1 \
- -Wall \
- -Wno-unused-parameter \
- -D_XOPEN_SOURCE \
- -D_GNU_SOURCE
-
-LOCAL_C_INCLUDES += external/openssl/include
-
-LOCAL_MODULE := adb
-
-LOCAL_STATIC_LIBRARIES := libzipfile libunz libcutils
-
-LOCAL_SHARED_LIBRARIES := libcrypto
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_EXECUTABLE)
-endif
diff --git a/adb/CPPLINT.cfg b/adb/CPPLINT.cfg
new file mode 100644
index 0000000..9b906e8
--- /dev/null
+++ b/adb/CPPLINT.cfg
@@ -0,0 +1,2 @@
+set noparent
+filter=-build/header_guard,-build/include,-readability/function
diff --git a/adb/SERVICES.TXT b/adb/SERVICES.TXT
index 7f85dc3..63000f2 100644
--- a/adb/SERVICES.TXT
+++ b/adb/SERVICES.TXT
@@ -240,3 +240,20 @@
This starts the file synchronisation service, used to implement "adb push"
and "adb pull". Since this service is pretty complex, it will be detailed
in a companion document named SYNC.TXT
+
+reverse:<forward-command>
+ This implements the 'adb reverse' feature, i.e. the ability to reverse
+ socket connections from a device to the host. <forward-command> is one
+ of the forwarding commands that are described above, as in:
+
+ list-forward
+ forward:<local>;<remote>
+ forward:norebind:<local>;<remote>
+ killforward-all
+ killforward:<local>
+
+ Note that in this case, <local> corresponds to the socket on the device
+ and <remote> corresponds to the socket on the host.
+
+ The output of reverse:list-forward is the same as host:list-forward
+ except that <serial> will be just 'host'.
diff --git a/adb/SYNC.TXT b/adb/SYNC.TXT
new file mode 100644
index 0000000..e74d217
--- /dev/null
+++ b/adb/SYNC.TXT
@@ -0,0 +1,84 @@
+This file tries to document file related requests a client can make
+to the ADB server of an adbd daemon. See the OVERVIEW.TXT document
+to understand what's going on here. See the SERVICES.TXT to learn more
+about the other requests that are possible.
+
+SYNC SERVICES:
+
+
+Requesting the sync service ("sync:") using the protocol as described in
+SERVICES.TXT sets the connection in sync mode. This mode is a binary mode that
+differ from the regular adb protocol. The connection stays in sync mode until
+explicitly terminated (see below).
+
+After the initial "sync:" command is sent the server must respond with either
+"OKAY" or "FAIL" as per usual.
+
+In sync mode both the server and the client will frequently use eight-byte
+packets to communicate in this document called sync request and sync
+responses. The first four bytes is an id and specifies sync request is
+represented by four utf-8 characters. The last four bytes is a Little-Endian
+integer, with various uses. This number will be called "length" below. In fact
+all binary integers are Little-Endian in the sync mode. Sync mode is
+implicitly exited after each sync request, and normal adb communication
+follows as described in SERVICES.TXT.
+
+The following sync requests are accepted:
+LIST - List the files in a folder
+SEND - Send a file to device
+RECV - Retreive a file from device
+
+Not yet documented:
+STAT - Stat a file
+ULNK - Unlink (remove) a file. (Not currently supported)
+
+For all of the sync request above the must be followed by length number of
+bytes containing an utf-8 string with a remote filename.
+
+LIST:
+Lists files in the directory specified by the remote filename. The server will
+respond with zero or more directory entries or "dents".
+
+The directory entries will be returned in the following form
+1. A four-byte sync response id beeing "DENT"
+2. A four-byte integer representing file mode.
+3. A four-byte integer representing file size.
+4. A four-byte integer representing last modified time.
+5. A four-byte integer representing file name length.
+6. length number of bytes containing an utf-8 string representing the file
+ name.
+
+When an sync response "DONE" is received the listing is done.
+
+SEND:
+The remote file name is split into two parts separated by the last
+comma (","). The first part is the actual path, while the second is a decimal
+encoded file mode containing the permissions of the file on device.
+
+Note that some file types will be deleted before the copying starts, and if
+the transfer fails. Some file types will not be deleted, which allows
+ adb push disk_image /some_block_device
+to work.
+
+After this the actual file is sent in chunks. Each chucks has the following
+format.
+A sync request with id "DATA" and length equal to the chunk size. After
+follows chunk size number of bytes. This is repeated until the file is
+transfered. Each chunk must not be larger than 64k.
+
+When the file is tranfered a sync request "DONE" is sent, where length is set
+to the last modified time for the file. The server responds to this last
+request (but not to chuck requests) with an "OKAY" sync response (length can
+be ignored).
+
+
+RECV:
+Retrieves a file from device to a local file. The remote path is the path to
+the file that will be returned. Just as for the SEND sync request the file
+received is split up into chunks. The sync response id is "DATA" and length is
+the chuck size. After follows chunk size number of bytes. This is repeated
+until the file is transfered. Each chuck will not be larger than 64k.
+
+When the file is transfered a sync resopnse "DONE" is retrieved where the
+length can be ignored.
+
diff --git a/adb/adb.c b/adb/adb.c
deleted file mode 100644
index e116414..0000000
--- a/adb/adb.c
+++ /dev/null
@@ -1,1690 +0,0 @@
-/*
- * 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.
- */
-
-#define TRACE_TAG TRACE_ADB
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <stddef.h>
-#include <string.h>
-#include <time.h>
-#include <sys/time.h>
-#include <stdint.h>
-
-#include "sysdeps.h"
-#include "adb.h"
-#include "adb_auth.h"
-
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
-
-#if !ADB_HOST
-#include <cutils/properties.h>
-#include <private/android_filesystem_config.h>
-#include <sys/capability.h>
-#include <linux/prctl.h>
-#include <sys/mount.h>
-#include <getopt.h>
-#include <selinux/selinux.h>
-#else
-#include "usb_vendors.h"
-#endif
-
-#if ADB_TRACE
-ADB_MUTEX_DEFINE( D_lock );
-#endif
-
-int HOST = 0;
-int gListenAll = 0;
-
-static int auth_enabled = 0;
-
-#if !ADB_HOST
-static const char *adb_device_banner = "device";
-static const char *root_seclabel = NULL;
-#endif
-
-void fatal(const char *fmt, ...)
-{
- va_list ap;
- va_start(ap, fmt);
- fprintf(stderr, "error: ");
- vfprintf(stderr, fmt, ap);
- fprintf(stderr, "\n");
- va_end(ap);
- exit(-1);
-}
-
-void fatal_errno(const char *fmt, ...)
-{
- va_list ap;
- va_start(ap, fmt);
- fprintf(stderr, "error: %s: ", strerror(errno));
- vfprintf(stderr, fmt, ap);
- fprintf(stderr, "\n");
- va_end(ap);
- exit(-1);
-}
-
-int adb_trace_mask;
-
-/* read a comma/space/colum/semi-column separated list of tags
- * from the ADB_TRACE environment variable and build the trace
- * mask from it. note that '1' and 'all' are special cases to
- * enable all tracing
- */
-void adb_trace_init(void)
-{
- const char* p = getenv("ADB_TRACE");
- const char* q;
-
- static const struct {
- const char* tag;
- int flag;
- } tags[] = {
- { "1", 0 },
- { "all", 0 },
- { "adb", TRACE_ADB },
- { "sockets", TRACE_SOCKETS },
- { "packets", TRACE_PACKETS },
- { "rwx", TRACE_RWX },
- { "usb", TRACE_USB },
- { "sync", TRACE_SYNC },
- { "sysdeps", TRACE_SYSDEPS },
- { "transport", TRACE_TRANSPORT },
- { "jdwp", TRACE_JDWP },
- { "services", TRACE_SERVICES },
- { "auth", TRACE_AUTH },
- { NULL, 0 }
- };
-
- if (p == NULL)
- return;
-
- /* use a comma/column/semi-colum/space separated list */
- while (*p) {
- int len, tagn;
-
- q = strpbrk(p, " ,:;");
- if (q == NULL) {
- q = p + strlen(p);
- }
- len = q - p;
-
- for (tagn = 0; tags[tagn].tag != NULL; tagn++)
- {
- int taglen = strlen(tags[tagn].tag);
-
- if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
- {
- int flag = tags[tagn].flag;
- if (flag == 0) {
- adb_trace_mask = ~0;
- return;
- }
- adb_trace_mask |= (1 << flag);
- break;
- }
- }
- p = q;
- if (*p)
- p++;
- }
-}
-
-#if !ADB_HOST
-/*
- * Implements ADB tracing inside the emulator.
- */
-
-#include <stdarg.h>
-
-/*
- * Redefine open and write for qemu_pipe.h that contains inlined references
- * to those routines. We will redifine them back after qemu_pipe.h inclusion.
- */
-
-#undef open
-#undef write
-#define open adb_open
-#define write adb_write
-#include <hardware/qemu_pipe.h>
-#undef open
-#undef write
-#define open ___xxx_open
-#define write ___xxx_write
-
-/* A handle to adb-debug qemud service in the emulator. */
-int adb_debug_qemu = -1;
-
-/* Initializes connection with the adb-debug qemud service in the emulator. */
-static int adb_qemu_trace_init(void)
-{
- char con_name[32];
-
- if (adb_debug_qemu >= 0) {
- return 0;
- }
-
- /* adb debugging QEMUD service connection request. */
- snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
- adb_debug_qemu = qemu_pipe_open(con_name);
- return (adb_debug_qemu >= 0) ? 0 : -1;
-}
-
-void adb_qemu_trace(const char* fmt, ...)
-{
- va_list args;
- va_start(args, fmt);
- char msg[1024];
-
- if (adb_debug_qemu >= 0) {
- vsnprintf(msg, sizeof(msg), fmt, args);
- adb_write(adb_debug_qemu, msg, strlen(msg));
- }
-}
-#endif /* !ADB_HOST */
-
-apacket *get_apacket(void)
-{
- apacket *p = malloc(sizeof(apacket));
- if(p == 0) fatal("failed to allocate an apacket");
- memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
- return p;
-}
-
-void put_apacket(apacket *p)
-{
- free(p);
-}
-
-void handle_online(atransport *t)
-{
- D("adb: online\n");
- t->online = 1;
-}
-
-void handle_offline(atransport *t)
-{
- D("adb: offline\n");
- //Close the associated usb
- t->online = 0;
- run_transport_disconnects(t);
-}
-
-#if DEBUG_PACKETS
-#define DUMPMAX 32
-void print_packet(const char *label, apacket *p)
-{
- char *tag;
- char *x;
- unsigned count;
-
- switch(p->msg.command){
- case A_SYNC: tag = "SYNC"; break;
- case A_CNXN: tag = "CNXN" ; break;
- case A_OPEN: tag = "OPEN"; break;
- case A_OKAY: tag = "OKAY"; break;
- case A_CLSE: tag = "CLSE"; break;
- case A_WRTE: tag = "WRTE"; break;
- case A_AUTH: tag = "AUTH"; break;
- default: tag = "????"; break;
- }
-
- fprintf(stderr, "%s: %s %08x %08x %04x \"",
- label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
- count = p->msg.data_length;
- x = (char*) p->data;
- if(count > DUMPMAX) {
- count = DUMPMAX;
- tag = "\n";
- } else {
- tag = "\"\n";
- }
- while(count-- > 0){
- if((*x >= ' ') && (*x < 127)) {
- fputc(*x, stderr);
- } else {
- fputc('.', stderr);
- }
- x++;
- }
- fputs(tag, stderr);
-}
-#endif
-
-static void send_ready(unsigned local, unsigned remote, atransport *t)
-{
- D("Calling send_ready \n");
- apacket *p = get_apacket();
- p->msg.command = A_OKAY;
- p->msg.arg0 = local;
- p->msg.arg1 = remote;
- send_packet(p, t);
-}
-
-static void send_close(unsigned local, unsigned remote, atransport *t)
-{
- D("Calling send_close \n");
- apacket *p = get_apacket();
- p->msg.command = A_CLSE;
- p->msg.arg0 = local;
- p->msg.arg1 = remote;
- send_packet(p, t);
-}
-
-static size_t fill_connect_data(char *buf, size_t bufsize)
-{
-#if ADB_HOST
- return snprintf(buf, bufsize, "host::") + 1;
-#else
- static const char *cnxn_props[] = {
- "ro.product.name",
- "ro.product.model",
- "ro.product.device",
- };
- static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
- int i;
- size_t remaining = bufsize;
- size_t len;
-
- len = snprintf(buf, remaining, "%s::", adb_device_banner);
- remaining -= len;
- buf += len;
- for (i = 0; i < num_cnxn_props; i++) {
- char value[PROPERTY_VALUE_MAX];
- property_get(cnxn_props[i], value, "");
- len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
- remaining -= len;
- buf += len;
- }
-
- return bufsize - remaining + 1;
-#endif
-}
-
-static void send_connect(atransport *t)
-{
- D("Calling send_connect \n");
- apacket *cp = get_apacket();
- cp->msg.command = A_CNXN;
- cp->msg.arg0 = A_VERSION;
- cp->msg.arg1 = MAX_PAYLOAD;
- cp->msg.data_length = fill_connect_data((char *)cp->data,
- sizeof(cp->data));
- send_packet(cp, t);
-}
-
-void send_auth_request(atransport *t)
-{
- D("Calling send_auth_request\n");
- apacket *p;
- int ret;
-
- ret = adb_auth_generate_token(t->token, sizeof(t->token));
- if (ret != sizeof(t->token)) {
- D("Error generating token ret=%d\n", ret);
- return;
- }
-
- p = get_apacket();
- memcpy(p->data, t->token, ret);
- p->msg.command = A_AUTH;
- p->msg.arg0 = ADB_AUTH_TOKEN;
- p->msg.data_length = ret;
- send_packet(p, t);
-}
-
-static void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
-{
- D("Calling send_auth_response\n");
- apacket *p = get_apacket();
- int ret;
-
- ret = adb_auth_sign(t->key, token, token_size, p->data);
- if (!ret) {
- D("Error signing the token\n");
- put_apacket(p);
- return;
- }
-
- p->msg.command = A_AUTH;
- p->msg.arg0 = ADB_AUTH_SIGNATURE;
- p->msg.data_length = ret;
- send_packet(p, t);
-}
-
-static void send_auth_publickey(atransport *t)
-{
- D("Calling send_auth_publickey\n");
- apacket *p = get_apacket();
- int ret;
-
- ret = adb_auth_get_userkey(p->data, sizeof(p->data));
- if (!ret) {
- D("Failed to get user public key\n");
- put_apacket(p);
- return;
- }
-
- p->msg.command = A_AUTH;
- p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
- p->msg.data_length = ret;
- send_packet(p, t);
-}
-
-void adb_auth_verified(atransport *t)
-{
- handle_online(t);
- send_connect(t);
-}
-
-static char *connection_state_name(atransport *t)
-{
- if (t == NULL) {
- return "unknown";
- }
-
- switch(t->connection_state) {
- case CS_BOOTLOADER:
- return "bootloader";
- case CS_DEVICE:
- return "device";
- case CS_RECOVERY:
- return "recovery";
- case CS_SIDELOAD:
- return "sideload";
- case CS_OFFLINE:
- return "offline";
- case CS_UNAUTHORIZED:
- return "unauthorized";
- default:
- return "unknown";
- }
-}
-
-/* qual_overwrite is used to overwrite a qualifier string. dst is a
- * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
- * was malloc'ed and needs to freed. *dst will be set to a dup of src.
- */
-static void qual_overwrite(char **dst, const char *src)
-{
- if (!dst)
- return;
-
- free(*dst);
- *dst = NULL;
-
- if (!src || !*src)
- return;
-
- *dst = strdup(src);
-}
-
-void parse_banner(char *banner, atransport *t)
-{
- static const char *prop_seps = ";";
- static const char key_val_sep = '=';
- char *cp;
- char *type;
-
- D("parse_banner: %s\n", banner);
- type = banner;
- cp = strchr(type, ':');
- if (cp) {
- *cp++ = 0;
- /* Nothing is done with second field. */
- cp = strchr(cp, ':');
- if (cp) {
- char *save;
- char *key;
- key = adb_strtok_r(cp + 1, prop_seps, &save);
- while (key) {
- cp = strchr(key, key_val_sep);
- if (cp) {
- *cp++ = '\0';
- if (!strcmp(key, "ro.product.name"))
- qual_overwrite(&t->product, cp);
- else if (!strcmp(key, "ro.product.model"))
- qual_overwrite(&t->model, cp);
- else if (!strcmp(key, "ro.product.device"))
- qual_overwrite(&t->device, cp);
- }
- key = adb_strtok_r(NULL, prop_seps, &save);
- }
- }
- }
-
- if(!strcmp(type, "bootloader")){
- D("setting connection_state to CS_BOOTLOADER\n");
- t->connection_state = CS_BOOTLOADER;
- update_transports();
- return;
- }
-
- if(!strcmp(type, "device")) {
- D("setting connection_state to CS_DEVICE\n");
- t->connection_state = CS_DEVICE;
- update_transports();
- return;
- }
-
- if(!strcmp(type, "recovery")) {
- D("setting connection_state to CS_RECOVERY\n");
- t->connection_state = CS_RECOVERY;
- update_transports();
- return;
- }
-
- if(!strcmp(type, "sideload")) {
- D("setting connection_state to CS_SIDELOAD\n");
- t->connection_state = CS_SIDELOAD;
- update_transports();
- return;
- }
-
- t->connection_state = CS_HOST;
-}
-
-void handle_packet(apacket *p, atransport *t)
-{
- asocket *s;
-
- D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
- ((char*) (&(p->msg.command)))[1],
- ((char*) (&(p->msg.command)))[2],
- ((char*) (&(p->msg.command)))[3]);
- print_packet("recv", p);
-
- switch(p->msg.command){
- case A_SYNC:
- if(p->msg.arg0){
- send_packet(p, t);
- if(HOST) send_connect(t);
- } else {
- t->connection_state = CS_OFFLINE;
- handle_offline(t);
- send_packet(p, t);
- }
- return;
-
- case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
- /* XXX verify version, etc */
- if(t->connection_state != CS_OFFLINE) {
- t->connection_state = CS_OFFLINE;
- handle_offline(t);
- }
-
- parse_banner((char*) p->data, t);
-
- if (HOST || !auth_enabled) {
- handle_online(t);
- if(!HOST) send_connect(t);
- } else {
- send_auth_request(t);
- }
- break;
-
- case A_AUTH:
- if (p->msg.arg0 == ADB_AUTH_TOKEN) {
- t->connection_state = CS_UNAUTHORIZED;
- t->key = adb_auth_nextkey(t->key);
- if (t->key) {
- send_auth_response(p->data, p->msg.data_length, t);
- } else {
- /* No more private keys to try, send the public key */
- send_auth_publickey(t);
- }
- } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
- if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
- adb_auth_verified(t);
- t->failed_auth_attempts = 0;
- } else {
- if (t->failed_auth_attempts++ > 10)
- adb_sleep_ms(1000);
- send_auth_request(t);
- }
- } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
- adb_auth_confirm_key(p->data, p->msg.data_length, t);
- }
- break;
-
- case A_OPEN: /* OPEN(local-id, 0, "destination") */
- if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
- char *name = (char*) p->data;
- name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
- s = create_local_service_socket(name);
- if(s == 0) {
- send_close(0, p->msg.arg0, t);
- } else {
- s->peer = create_remote_socket(p->msg.arg0, t);
- s->peer->peer = s;
- send_ready(s->id, s->peer->id, t);
- s->ready(s);
- }
- }
- break;
-
- case A_OKAY: /* READY(local-id, remote-id, "") */
- if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
- if((s = find_local_socket(p->msg.arg1, 0))) {
- if(s->peer == 0) {
- /* On first READY message, create the connection. */
- s->peer = create_remote_socket(p->msg.arg0, t);
- s->peer->peer = s;
- s->ready(s);
- } else if (s->peer->id == p->msg.arg0) {
- /* Other READY messages must use the same local-id */
- s->ready(s);
- } else {
- D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
- p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
- }
- }
- }
- break;
-
- case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
- if (t->online && p->msg.arg1 != 0) {
- if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
- /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
- * a failed OPEN only. However, due to a bug in previous ADB
- * versions, CLOSE(0, remote-id, "") was also used for normal
- * CLOSE() operations.
- *
- * This is bad because it means a compromised adbd could
- * send packets to close connections between the host and
- * other devices. To avoid this, only allow this if the local
- * socket has a peer on the same transport.
- */
- if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
- D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
- p->msg.arg1, t->serial, s->peer->transport->serial);
- } else {
- s->close(s);
- }
- }
- }
- break;
-
- case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
- if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
- if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
- unsigned rid = p->msg.arg0;
- p->len = p->msg.data_length;
-
- if(s->enqueue(s, p) == 0) {
- D("Enqueue the socket\n");
- send_ready(s->id, rid, t);
- }
- return;
- }
- }
- break;
-
- default:
- printf("handle_packet: what is %08x?!\n", p->msg.command);
- }
-
- put_apacket(p);
-}
-
-alistener listener_list = {
- .next = &listener_list,
- .prev = &listener_list,
-};
-
-static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
-{
- asocket *s;
-
- if(ev & FDE_READ) {
- struct sockaddr addr;
- socklen_t alen;
- int fd;
-
- alen = sizeof(addr);
- fd = adb_socket_accept(_fd, &addr, &alen);
- if(fd < 0) return;
-
- adb_socket_setbufsize(fd, CHUNK_SIZE);
-
- s = create_local_socket(fd);
- if(s) {
- connect_to_smartsocket(s);
- return;
- }
-
- adb_close(fd);
- }
-}
-
-static void listener_event_func(int _fd, unsigned ev, void *_l)
-{
- alistener *l = _l;
- asocket *s;
-
- if(ev & FDE_READ) {
- struct sockaddr addr;
- socklen_t alen;
- int fd;
-
- alen = sizeof(addr);
- fd = adb_socket_accept(_fd, &addr, &alen);
- if(fd < 0) return;
-
- s = create_local_socket(fd);
- if(s) {
- s->transport = l->transport;
- connect_to_remote(s, l->connect_to);
- return;
- }
-
- adb_close(fd);
- }
-}
-
-static void free_listener(alistener* l)
-{
- if (l->next) {
- l->next->prev = l->prev;
- l->prev->next = l->next;
- l->next = l->prev = l;
- }
-
- // closes the corresponding fd
- fdevent_remove(&l->fde);
-
- if (l->local_name)
- free((char*)l->local_name);
-
- if (l->connect_to)
- free((char*)l->connect_to);
-
- if (l->transport) {
- remove_transport_disconnect(l->transport, &l->disconnect);
- }
- free(l);
-}
-
-static void listener_disconnect(void* _l, atransport* t)
-{
- alistener* l = _l;
-
- free_listener(l);
-}
-
-int local_name_to_fd(const char *name)
-{
- int port;
-
- if(!strncmp("tcp:", name, 4)){
- int ret;
- port = atoi(name + 4);
-
- if (gListenAll > 0) {
- ret = socket_inaddr_any_server(port, SOCK_STREAM);
- } else {
- ret = socket_loopback_server(port, SOCK_STREAM);
- }
-
- return ret;
- }
-#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
- // It's non-sensical to support the "reserved" space on the adb host side
- if(!strncmp(name, "local:", 6)) {
- return socket_local_server(name + 6,
- ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
- } else if(!strncmp(name, "localabstract:", 14)) {
- return socket_local_server(name + 14,
- ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
- } else if(!strncmp(name, "localfilesystem:", 16)) {
- return socket_local_server(name + 16,
- ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
- }
-
-#endif
- printf("unknown local portname '%s'\n", name);
- return -1;
-}
-
-// Write a single line describing a listener to a user-provided buffer.
-// Appends a trailing zero, even in case of truncation, but the function
-// returns the full line length.
-// If |buffer| is NULL, does not write but returns required size.
-static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
- // Format is simply:
- //
- // <device-serial> " " <local-name> " " <remote-name> "\n"
- //
- int local_len = strlen(l->local_name);
- int connect_len = strlen(l->connect_to);
- int serial_len = strlen(l->transport->serial);
-
- if (buffer != NULL) {
- snprintf(buffer, buffer_len, "%s %s %s\n",
- l->transport->serial, l->local_name, l->connect_to);
- }
- // NOTE: snprintf() on Windows returns -1 in case of truncation, so
- // return the computed line length instead.
- return local_len + connect_len + serial_len + 3;
-}
-
-// Write the list of current listeners (network redirections) into a
-// user-provided buffer. Appends a trailing zero, even in case of
-// trunctaion, but return the full size in bytes.
-// If |buffer| is NULL, does not write but returns required size.
-static int format_listeners(char* buf, size_t buflen)
-{
- alistener* l;
- int result = 0;
- for (l = listener_list.next; l != &listener_list; l = l->next) {
- // Ignore special listeners like those for *smartsocket*
- if (l->connect_to[0] == '*')
- continue;
- int len = format_listener(l, buf, buflen);
- // Ensure there is space for the trailing zero.
- result += len;
- if (buf != NULL) {
- buf += len;
- buflen -= len;
- if (buflen <= 0)
- break;
- }
- }
- return result;
-}
-
-static int remove_listener(const char *local_name, atransport* transport)
-{
- alistener *l;
-
- for (l = listener_list.next; l != &listener_list; l = l->next) {
- if (!strcmp(local_name, l->local_name)) {
- listener_disconnect(l, l->transport);
- return 0;
- }
- }
- return -1;
-}
-
-static void remove_all_listeners(void)
-{
- alistener *l, *l_next;
- for (l = listener_list.next; l != &listener_list; l = l_next) {
- l_next = l->next;
- // Never remove smart sockets.
- if (l->connect_to[0] == '*')
- continue;
- listener_disconnect(l, l->transport);
- }
-}
-
-// error/status codes for install_listener.
-typedef enum {
- INSTALL_STATUS_OK = 0,
- INSTALL_STATUS_INTERNAL_ERROR = -1,
- INSTALL_STATUS_CANNOT_BIND = -2,
- INSTALL_STATUS_CANNOT_REBIND = -3,
-} install_status_t;
-
-static install_status_t install_listener(const char *local_name,
- const char *connect_to,
- atransport* transport,
- int no_rebind)
-{
- alistener *l;
-
- //printf("install_listener('%s','%s')\n", local_name, connect_to);
-
- for(l = listener_list.next; l != &listener_list; l = l->next){
- if(strcmp(local_name, l->local_name) == 0) {
- char *cto;
-
- /* can't repurpose a smartsocket */
- if(l->connect_to[0] == '*') {
- return INSTALL_STATUS_INTERNAL_ERROR;
- }
-
- /* can't repurpose a listener if 'no_rebind' is true */
- if (no_rebind) {
- return INSTALL_STATUS_CANNOT_REBIND;
- }
-
- cto = strdup(connect_to);
- if(cto == 0) {
- return INSTALL_STATUS_INTERNAL_ERROR;
- }
-
- //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
- free((void*) l->connect_to);
- l->connect_to = cto;
- if (l->transport != transport) {
- remove_transport_disconnect(l->transport, &l->disconnect);
- l->transport = transport;
- add_transport_disconnect(l->transport, &l->disconnect);
- }
- return INSTALL_STATUS_OK;
- }
- }
-
- if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
- if((l->local_name = strdup(local_name)) == 0) goto nomem;
- if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
-
-
- l->fd = local_name_to_fd(local_name);
- if(l->fd < 0) {
- free((void*) l->local_name);
- free((void*) l->connect_to);
- free(l);
- printf("cannot bind '%s'\n", local_name);
- return -2;
- }
-
- close_on_exec(l->fd);
- if(!strcmp(l->connect_to, "*smartsocket*")) {
- fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
- } else {
- fdevent_install(&l->fde, l->fd, listener_event_func, l);
- }
- fdevent_set(&l->fde, FDE_READ);
-
- l->next = &listener_list;
- l->prev = listener_list.prev;
- l->next->prev = l;
- l->prev->next = l;
- l->transport = transport;
-
- if (transport) {
- l->disconnect.opaque = l;
- l->disconnect.func = listener_disconnect;
- add_transport_disconnect(transport, &l->disconnect);
- }
- return INSTALL_STATUS_OK;
-
-nomem:
- fatal("cannot allocate listener");
- return INSTALL_STATUS_INTERNAL_ERROR;
-}
-
-#ifdef HAVE_WIN32_PROC
-static BOOL WINAPI ctrlc_handler(DWORD type)
-{
- exit(STATUS_CONTROL_C_EXIT);
- return TRUE;
-}
-#endif
-
-static void adb_cleanup(void)
-{
- usb_cleanup();
-}
-
-void start_logging(void)
-{
-#ifdef HAVE_WIN32_PROC
- char temp[ MAX_PATH ];
- FILE* fnul;
- FILE* flog;
-
- GetTempPath( sizeof(temp) - 8, temp );
- strcat( temp, "adb.log" );
-
- /* Win32 specific redirections */
- fnul = fopen( "NUL", "rt" );
- if (fnul != NULL)
- stdin[0] = fnul[0];
-
- flog = fopen( temp, "at" );
- if (flog == NULL)
- flog = fnul;
-
- setvbuf( flog, NULL, _IONBF, 0 );
-
- stdout[0] = flog[0];
- stderr[0] = flog[0];
- fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
-#else
- int fd;
-
- fd = unix_open("/dev/null", O_RDONLY);
- dup2(fd, 0);
- adb_close(fd);
-
- fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
- if(fd < 0) {
- fd = unix_open("/dev/null", O_WRONLY);
- }
- dup2(fd, 1);
- dup2(fd, 2);
- adb_close(fd);
- fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
-#endif
-}
-
-#if !ADB_HOST
-void start_device_log(void)
-{
- int fd;
- char path[PATH_MAX];
- struct tm now;
- time_t t;
- char value[PROPERTY_VALUE_MAX];
-
- // read the trace mask from persistent property persist.adb.trace_mask
- // give up if the property is not set or cannot be parsed
- property_get("persist.adb.trace_mask", value, "");
- if (sscanf(value, "%x", &adb_trace_mask) != 1)
- return;
-
- adb_mkdir("/data/adb", 0775);
- tzset();
- time(&t);
- localtime_r(&t, &now);
- strftime(path, sizeof(path),
- "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
- &now);
- fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
- if (fd < 0)
- return;
-
- // redirect stdout and stderr to the log file
- dup2(fd, 1);
- dup2(fd, 2);
- fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
- adb_close(fd);
-
- fd = unix_open("/dev/null", O_RDONLY);
- dup2(fd, 0);
- adb_close(fd);
-}
-#endif
-
-#if ADB_HOST
-
-#ifdef WORKAROUND_BUG6558362
-#include <sched.h>
-#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
-void adb_set_affinity(void)
-{
- cpu_set_t cpu_set;
- const char* cpunum_str = getenv(AFFINITY_ENVVAR);
- char* strtol_res;
- int cpu_num;
-
- if (!cpunum_str || !*cpunum_str)
- return;
- cpu_num = strtol(cpunum_str, &strtol_res, 0);
- if (*strtol_res != '\0')
- fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
-
- sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
- D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
- CPU_ZERO(&cpu_set);
- CPU_SET(cpu_num, &cpu_set);
- sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
- sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
- D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
-}
-#endif
-
-int launch_server(int server_port)
-{
-#ifdef HAVE_WIN32_PROC
- /* we need to start the server in the background */
- /* we create a PIPE that will be used to wait for the server's "OK" */
- /* message since the pipe handles must be inheritable, we use a */
- /* security attribute */
- HANDLE pipe_read, pipe_write;
- HANDLE stdout_handle, stderr_handle;
- SECURITY_ATTRIBUTES sa;
- STARTUPINFO startup;
- PROCESS_INFORMATION pinfo;
- char program_path[ MAX_PATH ];
- int ret;
-
- sa.nLength = sizeof(sa);
- sa.lpSecurityDescriptor = NULL;
- sa.bInheritHandle = TRUE;
-
- /* create pipe, and ensure its read handle isn't inheritable */
- ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
- if (!ret) {
- fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
- return -1;
- }
-
- SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
-
- /* Some programs want to launch an adb command and collect its output by
- * calling CreateProcess with inheritable stdout/stderr handles, then
- * using read() to get its output. When this happens, the stdout/stderr
- * handles passed to the adb client process will also be inheritable.
- * When starting the adb server here, care must be taken to reset them
- * to non-inheritable.
- * Otherwise, something bad happens: even if the adb command completes,
- * the calling process is stuck while read()-ing from the stdout/stderr
- * descriptors, because they're connected to corresponding handles in the
- * adb server process (even if the latter never uses/writes to them).
- */
- stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
- stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
- if (stdout_handle != INVALID_HANDLE_VALUE) {
- SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
- }
- if (stderr_handle != INVALID_HANDLE_VALUE) {
- SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
- }
-
- ZeroMemory( &startup, sizeof(startup) );
- startup.cb = sizeof(startup);
- startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
- startup.hStdOutput = pipe_write;
- startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
- startup.dwFlags = STARTF_USESTDHANDLES;
-
- ZeroMemory( &pinfo, sizeof(pinfo) );
-
- /* get path of current program */
- GetModuleFileName( NULL, program_path, sizeof(program_path) );
-
- ret = CreateProcess(
- program_path, /* program path */
- "adb fork-server server",
- /* the fork-server argument will set the
- debug = 2 in the child */
- NULL, /* process handle is not inheritable */
- NULL, /* thread handle is not inheritable */
- TRUE, /* yes, inherit some handles */
- DETACHED_PROCESS, /* the new process doesn't have a console */
- NULL, /* use parent's environment block */
- NULL, /* use parent's starting directory */
- &startup, /* startup info, i.e. std handles */
- &pinfo );
-
- CloseHandle( pipe_write );
-
- if (!ret) {
- fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
- CloseHandle( pipe_read );
- return -1;
- }
-
- CloseHandle( pinfo.hProcess );
- CloseHandle( pinfo.hThread );
-
- /* wait for the "OK\n" message */
- {
- char temp[3];
- DWORD count;
-
- ret = ReadFile( pipe_read, temp, 3, &count, NULL );
- CloseHandle( pipe_read );
- if ( !ret ) {
- fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
- return -1;
- }
- if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
- fprintf(stderr, "ADB server didn't ACK\n" );
- return -1;
- }
- }
-#elif defined(HAVE_FORKEXEC)
- char path[PATH_MAX];
- int fd[2];
-
- // set up a pipe so the child can tell us when it is ready.
- // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
- if (pipe(fd)) {
- fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
- return -1;
- }
- get_my_path(path, PATH_MAX);
- pid_t pid = fork();
- if(pid < 0) return -1;
-
- if (pid == 0) {
- // child side of the fork
-
- // redirect stderr to the pipe
- // we use stderr instead of stdout due to stdout's buffering behavior.
- adb_close(fd[0]);
- dup2(fd[1], STDERR_FILENO);
- adb_close(fd[1]);
-
- char str_port[30];
- snprintf(str_port, sizeof(str_port), "%d", server_port);
- // child process
- int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
- // this should not return
- fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
- } else {
- // parent side of the fork
-
- char temp[3];
-
- temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
- // wait for the "OK\n" message
- adb_close(fd[1]);
- int ret = adb_read(fd[0], temp, 3);
- int saved_errno = errno;
- adb_close(fd[0]);
- if (ret < 0) {
- fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
- return -1;
- }
- if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
- fprintf(stderr, "ADB server didn't ACK\n" );
- return -1;
- }
-
- setsid();
- }
-#else
-#error "cannot implement background server start on this platform"
-#endif
- return 0;
-}
-#endif
-
-/* Constructs a local name of form tcp:port.
- * target_str points to the target string, it's content will be overwritten.
- * target_size is the capacity of the target string.
- * server_port is the port number to use for the local name.
- */
-void build_local_name(char* target_str, size_t target_size, int server_port)
-{
- snprintf(target_str, target_size, "tcp:%d", server_port);
-}
-
-#if !ADB_HOST
-
-static void drop_capabilities_bounding_set_if_needed() {
-#ifdef ALLOW_ADBD_ROOT
- char value[PROPERTY_VALUE_MAX];
- property_get("ro.debuggable", value, "");
- if (strcmp(value, "1") == 0) {
- return;
- }
-#endif
- int i;
- for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
- if (i == CAP_SETUID || i == CAP_SETGID) {
- // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
- continue;
- }
- int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
-
- // Some kernels don't have file capabilities compiled in, and
- // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
- // die when we see such misconfigured kernels.
- if ((err < 0) && (errno != EINVAL)) {
- exit(1);
- }
- }
-}
-
-static int should_drop_privileges() {
-#ifndef ALLOW_ADBD_ROOT
- return 1;
-#else /* ALLOW_ADBD_ROOT */
- int secure = 0;
- char value[PROPERTY_VALUE_MAX];
-
- /* run adbd in secure mode if ro.secure is set and
- ** we are not in the emulator
- */
- property_get("ro.kernel.qemu", value, "");
- if (strcmp(value, "1") != 0) {
- property_get("ro.secure", value, "1");
- if (strcmp(value, "1") == 0) {
- // don't run as root if ro.secure is set...
- secure = 1;
-
- // ... except we allow running as root in userdebug builds if the
- // service.adb.root property has been set by the "adb root" command
- property_get("ro.debuggable", value, "");
- if (strcmp(value, "1") == 0) {
- property_get("service.adb.root", value, "");
- if (strcmp(value, "1") == 0) {
- secure = 0;
- }
- }
- }
- }
- return secure;
-#endif /* ALLOW_ADBD_ROOT */
-}
-#endif /* !ADB_HOST */
-
-int adb_main(int is_daemon, int server_port)
-{
-#if !ADB_HOST
- int port;
- char value[PROPERTY_VALUE_MAX];
-
- umask(000);
-#endif
-
- atexit(adb_cleanup);
-#ifdef HAVE_WIN32_PROC
- SetConsoleCtrlHandler( ctrlc_handler, TRUE );
-#elif defined(HAVE_FORKEXEC)
- // No SIGCHLD. Let the service subproc handle its children.
- signal(SIGPIPE, SIG_IGN);
-#endif
-
- init_transport_registration();
-
-#if ADB_HOST
- HOST = 1;
-
-#ifdef WORKAROUND_BUG6558362
- if(is_daemon) adb_set_affinity();
-#endif
- usb_vendors_init();
- usb_init();
- local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
- adb_auth_init();
-
- char local_name[30];
- build_local_name(local_name, sizeof(local_name), server_port);
- if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
- exit(1);
- }
-#else
- property_get("ro.adb.secure", value, "0");
- auth_enabled = !strcmp(value, "1");
- if (auth_enabled)
- adb_auth_init();
-
- // Our external storage path may be different than apps, since
- // we aren't able to bind mount after dropping root.
- const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
- if (NULL != adb_external_storage) {
- setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
- } else {
- D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
- " unchanged.\n");
- }
-
- /* don't listen on a port (default 5037) if running in secure mode */
- /* don't run as root if we are running in secure mode */
- if (should_drop_privileges()) {
- drop_capabilities_bounding_set_if_needed();
-
- /* add extra groups:
- ** AID_ADB to access the USB driver
- ** AID_LOG to read system logs (adb logcat)
- ** AID_INPUT to diagnose input issues (getevent)
- ** AID_INET to diagnose network issues (netcfg, ping)
- ** AID_GRAPHICS to access the frame buffer
- ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
- ** AID_SDCARD_R to allow reading from the SD card
- ** AID_SDCARD_RW to allow writing to the SD card
- ** AID_NET_BW_STATS to read out qtaguid statistics
- */
- gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
- AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
- AID_NET_BW_STATS };
- if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
- exit(1);
- }
-
- /* then switch user and group to "shell" */
- if (setgid(AID_SHELL) != 0) {
- exit(1);
- }
- if (setuid(AID_SHELL) != 0) {
- exit(1);
- }
-
- D("Local port disabled\n");
- } else {
- char local_name[30];
- if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
- // b/12587913: fix setcon to allow const pointers
- if (setcon((char *)root_seclabel) < 0) {
- exit(1);
- }
- }
- build_local_name(local_name, sizeof(local_name), server_port);
- if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
- exit(1);
- }
- }
-
- int usb = 0;
- if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
- // listen on USB
- usb_init();
- usb = 1;
- }
-
- // If one of these properties is set, also listen on that port
- // If one of the properties isn't set and we couldn't listen on usb,
- // listen on the default port.
- property_get("service.adb.tcp.port", value, "");
- if (!value[0]) {
- property_get("persist.adb.tcp.port", value, "");
- }
- if (sscanf(value, "%d", &port) == 1 && port > 0) {
- printf("using port=%d\n", port);
- // listen on TCP port specified by service.adb.tcp.port property
- local_init(port);
- } else if (!usb) {
- // listen on default port
- local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
- }
-
- D("adb_main(): pre init_jdwp()\n");
- init_jdwp();
- D("adb_main(): post init_jdwp()\n");
-#endif
-
- if (is_daemon)
- {
- // inform our parent that we are up and running.
-#ifdef HAVE_WIN32_PROC
- DWORD count;
- WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
-#elif defined(HAVE_FORKEXEC)
- fprintf(stderr, "OK\n");
-#endif
- start_logging();
- }
- D("Event loop starting\n");
-
- fdevent_loop();
-
- usb_cleanup();
-
- return 0;
-}
-
-int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
-{
- atransport *transport = NULL;
- char buf[4096];
-
- if(!strcmp(service, "kill")) {
- fprintf(stderr,"adb server killed by remote request\n");
- fflush(stdout);
- adb_write(reply_fd, "OKAY", 4);
- usb_cleanup();
- exit(0);
- }
-
-#if ADB_HOST
- // "transport:" is used for switching transport with a specified serial number
- // "transport-usb:" is used for switching transport to the only USB transport
- // "transport-local:" is used for switching transport to the only local transport
- // "transport-any:" is used for switching transport to the only transport
- if (!strncmp(service, "transport", strlen("transport"))) {
- char* error_string = "unknown failure";
- transport_type type = kTransportAny;
-
- if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
- type = kTransportUsb;
- } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
- type = kTransportLocal;
- } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
- type = kTransportAny;
- } else if (!strncmp(service, "transport:", strlen("transport:"))) {
- service += strlen("transport:");
- serial = service;
- }
-
- transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
-
- if (transport) {
- s->transport = transport;
- adb_write(reply_fd, "OKAY", 4);
- } else {
- sendfailmsg(reply_fd, error_string);
- }
- return 1;
- }
-
- // return a list of all connected devices
- if (!strncmp(service, "devices", 7)) {
- char buffer[4096];
- int use_long = !strcmp(service+7, "-l");
- if (use_long || service[7] == 0) {
- memset(buf, 0, sizeof(buf));
- memset(buffer, 0, sizeof(buffer));
- D("Getting device list \n");
- list_transports(buffer, sizeof(buffer), use_long);
- snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);
- D("Wrote device list \n");
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
- }
-
- // remove TCP transport
- if (!strncmp(service, "disconnect:", 11)) {
- char buffer[4096];
- memset(buffer, 0, sizeof(buffer));
- char* serial = service + 11;
- if (serial[0] == 0) {
- // disconnect from all TCP devices
- unregister_all_tcp_transports();
- } else {
- char hostbuf[100];
- // assume port 5555 if no port is specified
- if (!strchr(serial, ':')) {
- snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
- serial = hostbuf;
- }
- atransport *t = find_transport(serial);
-
- if (t) {
- unregister_transport(t);
- } else {
- snprintf(buffer, sizeof(buffer), "No such device %s", serial);
- }
- }
-
- snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
-
- // returns our value for ADB_SERVER_VERSION
- if (!strcmp(service, "version")) {
- char version[12];
- snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
- snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version);
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
-
- if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
- char *out = "unknown";
- transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
- if (transport && transport->serial) {
- out = transport->serial;
- }
- snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
- if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
- char *out = "unknown";
- transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
- if (transport && transport->devpath) {
- out = transport->devpath;
- }
- snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out);
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
- // indicates a new emulator instance has started
- if (!strncmp(service,"emulator:",9)) {
- int port = atoi(service+9);
- local_connect(port);
- /* we don't even need to send a reply */
- return 0;
- }
-#endif // ADB_HOST
-
- if(!strcmp(service,"list-forward")) {
- // Create the list of forward redirections.
- char header[9];
- int buffer_size = format_listeners(NULL, 0);
- // Add one byte for the trailing zero.
- char* buffer = malloc(buffer_size+1);
- (void) format_listeners(buffer, buffer_size+1);
- snprintf(header, sizeof header, "OKAY%04x", buffer_size);
- writex(reply_fd, header, 8);
- writex(reply_fd, buffer, buffer_size);
- free(buffer);
- return 0;
- }
-
- if (!strcmp(service,"killforward-all")) {
- remove_all_listeners();
- adb_write(reply_fd, "OKAYOKAY", 8);
- return 0;
- }
-
- if(!strncmp(service,"forward:",8) ||
- !strncmp(service,"killforward:",12)) {
- char *local, *remote, *err;
- int r;
- atransport *transport;
-
- int createForward = strncmp(service,"kill",4);
- int no_rebind = 0;
-
- local = strchr(service, ':') + 1;
-
- // Handle forward:norebind:<local>... here
- if (createForward && !strncmp(local, "norebind:", 9)) {
- no_rebind = 1;
- local = strchr(local, ':') + 1;
- }
-
- remote = strchr(local,';');
-
- if (createForward) {
- // Check forward: parameter format: '<local>;<remote>'
- if(remote == 0) {
- sendfailmsg(reply_fd, "malformed forward spec");
- return 0;
- }
-
- *remote++ = 0;
- if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
- sendfailmsg(reply_fd, "malformed forward spec");
- return 0;
- }
- } else {
- // Check killforward: parameter format: '<local>'
- if (local[0] == 0) {
- sendfailmsg(reply_fd, "malformed forward spec");
- return 0;
- }
- }
-
- transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
- if (!transport) {
- sendfailmsg(reply_fd, err);
- return 0;
- }
-
- if (createForward) {
- r = install_listener(local, remote, transport, no_rebind);
- } else {
- r = remove_listener(local, transport);
- }
- if(r == 0) {
- /* 1st OKAY is connect, 2nd OKAY is status */
- writex(reply_fd, "OKAYOKAY", 8);
- return 0;
- }
-
- if (createForward) {
- const char* message;
- switch (r) {
- case INSTALL_STATUS_CANNOT_BIND:
- message = "cannot bind to socket";
- break;
- case INSTALL_STATUS_CANNOT_REBIND:
- message = "cannot rebind existing socket";
- break;
- default:
- message = "internal error";
- }
- sendfailmsg(reply_fd, message);
- } else {
- sendfailmsg(reply_fd, "cannot remove listener");
- }
- return 0;
- }
-
- if(!strncmp(service,"get-state",strlen("get-state"))) {
- transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
- char *state = connection_state_name(transport);
- snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(state),state);
- writex(reply_fd, buf, strlen(buf));
- return 0;
- }
- return -1;
-}
-
-int main(int argc, char **argv)
-{
-#if ADB_HOST
- adb_sysdeps_init();
- adb_trace_init();
- D("Handling commandline()\n");
- return adb_commandline(argc - 1, argv + 1);
-#else
- /* If adbd runs inside the emulator this will enable adb tracing via
- * adb-debug qemud service in the emulator. */
- adb_qemu_trace_init();
- while(1) {
- int c;
- int option_index = 0;
- static struct option opts[] = {
- {"root_seclabel", required_argument, 0, 's' },
- {"device_banner", required_argument, 0, 'b' }
- };
- c = getopt_long(argc, argv, "", opts, &option_index);
- if (c == -1)
- break;
- switch (c) {
- case 's':
- root_seclabel = optarg;
- break;
- case 'b':
- adb_device_banner = optarg;
- break;
- default:
- break;
- }
- }
-
- start_device_log();
- D("Handling main()\n");
- return adb_main(0, DEFAULT_ADB_PORT);
-#endif
-}
diff --git a/adb/adb.cpp b/adb/adb.cpp
new file mode 100644
index 0000000..ffa93f4
--- /dev/null
+++ b/adb/adb.cpp
@@ -0,0 +1,955 @@
+/*
+ * 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.
+ */
+
+#define TRACE_TAG TRACE_ADB
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <stddef.h>
+#include <string.h>
+#include <time.h>
+#include <sys/time.h>
+#include <stdint.h>
+
+#include "sysdeps.h"
+#include "adb.h"
+#include "adb_auth.h"
+#include "adb_io.h"
+#include "adb_listeners.h"
+#include "transport.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+#if !ADB_HOST
+#include <cutils/properties.h>
+#include <sys/capability.h>
+#include <sys/mount.h>
+#endif
+
+#if ADB_TRACE
+ADB_MUTEX_DEFINE( D_lock );
+#endif
+
+int HOST = 0;
+
+#if !ADB_HOST
+const char *adb_device_banner = "device";
+#endif
+
+void fatal(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ fprintf(stderr, "error: ");
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ va_end(ap);
+ exit(-1);
+}
+
+void fatal_errno(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ fprintf(stderr, "error: %s: ", strerror(errno));
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ va_end(ap);
+ exit(-1);
+}
+
+int adb_trace_mask;
+
+/* read a comma/space/colum/semi-column separated list of tags
+ * from the ADB_TRACE environment variable and build the trace
+ * mask from it. note that '1' and 'all' are special cases to
+ * enable all tracing
+ */
+void adb_trace_init(void)
+{
+ const char* p = getenv("ADB_TRACE");
+ const char* q;
+
+ static const struct {
+ const char* tag;
+ int flag;
+ } tags[] = {
+ { "1", 0 },
+ { "all", 0 },
+ { "adb", TRACE_ADB },
+ { "sockets", TRACE_SOCKETS },
+ { "packets", TRACE_PACKETS },
+ { "rwx", TRACE_RWX },
+ { "usb", TRACE_USB },
+ { "sync", TRACE_SYNC },
+ { "sysdeps", TRACE_SYSDEPS },
+ { "transport", TRACE_TRANSPORT },
+ { "jdwp", TRACE_JDWP },
+ { "services", TRACE_SERVICES },
+ { "auth", TRACE_AUTH },
+ { NULL, 0 }
+ };
+
+ if (p == NULL)
+ return;
+
+ /* use a comma/column/semi-colum/space separated list */
+ while (*p) {
+ int len, tagn;
+
+ q = strpbrk(p, " ,:;");
+ if (q == NULL) {
+ q = p + strlen(p);
+ }
+ len = q - p;
+
+ for (tagn = 0; tags[tagn].tag != NULL; tagn++)
+ {
+ int taglen = strlen(tags[tagn].tag);
+
+ if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
+ {
+ int flag = tags[tagn].flag;
+ if (flag == 0) {
+ adb_trace_mask = ~0;
+ return;
+ }
+ adb_trace_mask |= (1 << flag);
+ break;
+ }
+ }
+ p = q;
+ if (*p)
+ p++;
+ }
+}
+
+apacket* get_apacket(void)
+{
+ apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket)));
+ if (p == nullptr) {
+ fatal("failed to allocate an apacket");
+ }
+
+ memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
+ return p;
+}
+
+void put_apacket(apacket *p)
+{
+ free(p);
+}
+
+void handle_online(atransport *t)
+{
+ D("adb: online\n");
+ t->online = 1;
+}
+
+void handle_offline(atransport *t)
+{
+ D("adb: offline\n");
+ //Close the associated usb
+ t->online = 0;
+ run_transport_disconnects(t);
+}
+
+#if DEBUG_PACKETS
+#define DUMPMAX 32
+void print_packet(const char *label, apacket *p)
+{
+ char *tag;
+ char *x;
+ unsigned count;
+
+ switch(p->msg.command){
+ case A_SYNC: tag = "SYNC"; break;
+ case A_CNXN: tag = "CNXN" ; break;
+ case A_OPEN: tag = "OPEN"; break;
+ case A_OKAY: tag = "OKAY"; break;
+ case A_CLSE: tag = "CLSE"; break;
+ case A_WRTE: tag = "WRTE"; break;
+ case A_AUTH: tag = "AUTH"; break;
+ default: tag = "????"; break;
+ }
+
+ fprintf(stderr, "%s: %s %08x %08x %04x \"",
+ label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
+ count = p->msg.data_length;
+ x = (char*) p->data;
+ if(count > DUMPMAX) {
+ count = DUMPMAX;
+ tag = "\n";
+ } else {
+ tag = "\"\n";
+ }
+ while(count-- > 0){
+ if((*x >= ' ') && (*x < 127)) {
+ fputc(*x, stderr);
+ } else {
+ fputc('.', stderr);
+ }
+ x++;
+ }
+ fputs(tag, stderr);
+}
+#endif
+
+static void send_ready(unsigned local, unsigned remote, atransport *t)
+{
+ D("Calling send_ready \n");
+ apacket *p = get_apacket();
+ p->msg.command = A_OKAY;
+ p->msg.arg0 = local;
+ p->msg.arg1 = remote;
+ send_packet(p, t);
+}
+
+static void send_close(unsigned local, unsigned remote, atransport *t)
+{
+ D("Calling send_close \n");
+ apacket *p = get_apacket();
+ p->msg.command = A_CLSE;
+ p->msg.arg0 = local;
+ p->msg.arg1 = remote;
+ send_packet(p, t);
+}
+
+static size_t fill_connect_data(char *buf, size_t bufsize)
+{
+#if ADB_HOST
+ return snprintf(buf, bufsize, "host::") + 1;
+#else
+ static const char *cnxn_props[] = {
+ "ro.product.name",
+ "ro.product.model",
+ "ro.product.device",
+ };
+ static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
+ int i;
+ size_t remaining = bufsize;
+ size_t len;
+
+ len = snprintf(buf, remaining, "%s::", adb_device_banner);
+ remaining -= len;
+ buf += len;
+ for (i = 0; i < num_cnxn_props; i++) {
+ char value[PROPERTY_VALUE_MAX];
+ property_get(cnxn_props[i], value, "");
+ len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
+ remaining -= len;
+ buf += len;
+ }
+
+ return bufsize - remaining + 1;
+#endif
+}
+
+#if !ADB_HOST
+static void send_msg_with_header(int fd, const char* msg, size_t msglen) {
+ char header[5];
+ if (msglen > 0xffff)
+ msglen = 0xffff;
+ snprintf(header, sizeof(header), "%04x", (unsigned)msglen);
+ WriteFdExactly(fd, header, 4);
+ WriteFdExactly(fd, msg, msglen);
+}
+#endif
+
+#if ADB_HOST
+static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
+ char header[9];
+ if (msglen > 0xffff)
+ msglen = 0xffff;
+ snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
+ WriteFdExactly(fd, header, 8);
+ WriteFdExactly(fd, msg, msglen);
+}
+#endif // ADB_HOST
+
+void send_connect(atransport *t)
+{
+ D("Calling send_connect \n");
+ apacket *cp = get_apacket();
+ cp->msg.command = A_CNXN;
+ cp->msg.arg0 = A_VERSION;
+ cp->msg.arg1 = MAX_PAYLOAD;
+ cp->msg.data_length = fill_connect_data((char *)cp->data,
+ sizeof(cp->data));
+ send_packet(cp, t);
+}
+
+#if ADB_HOST
+static const char* connection_state_name(atransport *t)
+{
+ if (t == NULL) {
+ return "unknown";
+ }
+
+ switch(t->connection_state) {
+ case CS_BOOTLOADER:
+ return "bootloader";
+ case CS_DEVICE:
+ return "device";
+ case CS_RECOVERY:
+ return "recovery";
+ case CS_SIDELOAD:
+ return "sideload";
+ case CS_OFFLINE:
+ return "offline";
+ case CS_UNAUTHORIZED:
+ return "unauthorized";
+ default:
+ return "unknown";
+ }
+}
+#endif // ADB_HOST
+
+/* qual_overwrite is used to overwrite a qualifier string. dst is a
+ * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
+ * was malloc'ed and needs to freed. *dst will be set to a dup of src.
+ */
+static void qual_overwrite(char **dst, const char *src)
+{
+ if (!dst)
+ return;
+
+ free(*dst);
+ *dst = NULL;
+
+ if (!src || !*src)
+ return;
+
+ *dst = strdup(src);
+}
+
+void parse_banner(char *banner, atransport *t)
+{
+ static const char *prop_seps = ";";
+ static const char key_val_sep = '=';
+ char *cp;
+ char *type;
+
+ D("parse_banner: %s\n", banner);
+ type = banner;
+ cp = strchr(type, ':');
+ if (cp) {
+ *cp++ = 0;
+ /* Nothing is done with second field. */
+ cp = strchr(cp, ':');
+ if (cp) {
+ char *save;
+ char *key;
+ key = adb_strtok_r(cp + 1, prop_seps, &save);
+ while (key) {
+ cp = strchr(key, key_val_sep);
+ if (cp) {
+ *cp++ = '\0';
+ if (!strcmp(key, "ro.product.name"))
+ qual_overwrite(&t->product, cp);
+ else if (!strcmp(key, "ro.product.model"))
+ qual_overwrite(&t->model, cp);
+ else if (!strcmp(key, "ro.product.device"))
+ qual_overwrite(&t->device, cp);
+ }
+ key = adb_strtok_r(NULL, prop_seps, &save);
+ }
+ }
+ }
+
+ if(!strcmp(type, "bootloader")){
+ D("setting connection_state to CS_BOOTLOADER\n");
+ t->connection_state = CS_BOOTLOADER;
+ update_transports();
+ return;
+ }
+
+ if(!strcmp(type, "device")) {
+ D("setting connection_state to CS_DEVICE\n");
+ t->connection_state = CS_DEVICE;
+ update_transports();
+ return;
+ }
+
+ if(!strcmp(type, "recovery")) {
+ D("setting connection_state to CS_RECOVERY\n");
+ t->connection_state = CS_RECOVERY;
+ update_transports();
+ return;
+ }
+
+ if(!strcmp(type, "sideload")) {
+ D("setting connection_state to CS_SIDELOAD\n");
+ t->connection_state = CS_SIDELOAD;
+ update_transports();
+ return;
+ }
+
+ t->connection_state = CS_HOST;
+}
+
+void handle_packet(apacket *p, atransport *t)
+{
+ asocket *s;
+
+ D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
+ ((char*) (&(p->msg.command)))[1],
+ ((char*) (&(p->msg.command)))[2],
+ ((char*) (&(p->msg.command)))[3]);
+ print_packet("recv", p);
+
+ switch(p->msg.command){
+ case A_SYNC:
+ if(p->msg.arg0){
+ send_packet(p, t);
+ if(HOST) send_connect(t);
+ } else {
+ t->connection_state = CS_OFFLINE;
+ handle_offline(t);
+ send_packet(p, t);
+ }
+ return;
+
+ case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
+ /* XXX verify version, etc */
+ if(t->connection_state != CS_OFFLINE) {
+ t->connection_state = CS_OFFLINE;
+ handle_offline(t);
+ }
+
+ parse_banner((char*) p->data, t);
+
+ if (HOST || !auth_enabled) {
+ handle_online(t);
+ if(!HOST) send_connect(t);
+ } else {
+ send_auth_request(t);
+ }
+ break;
+
+ case A_AUTH:
+ if (p->msg.arg0 == ADB_AUTH_TOKEN) {
+ t->connection_state = CS_UNAUTHORIZED;
+ t->key = adb_auth_nextkey(t->key);
+ if (t->key) {
+ send_auth_response(p->data, p->msg.data_length, t);
+ } else {
+ /* No more private keys to try, send the public key */
+ send_auth_publickey(t);
+ }
+ } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
+ if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
+ adb_auth_verified(t);
+ t->failed_auth_attempts = 0;
+ } else {
+ if (t->failed_auth_attempts++ > 10)
+ adb_sleep_ms(1000);
+ send_auth_request(t);
+ }
+ } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
+ adb_auth_confirm_key(p->data, p->msg.data_length, t);
+ }
+ break;
+
+ case A_OPEN: /* OPEN(local-id, 0, "destination") */
+ if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
+ char *name = (char*) p->data;
+ name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
+ s = create_local_service_socket(name);
+ if(s == 0) {
+ send_close(0, p->msg.arg0, t);
+ } else {
+ s->peer = create_remote_socket(p->msg.arg0, t);
+ s->peer->peer = s;
+ send_ready(s->id, s->peer->id, t);
+ s->ready(s);
+ }
+ }
+ break;
+
+ case A_OKAY: /* READY(local-id, remote-id, "") */
+ if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
+ if((s = find_local_socket(p->msg.arg1, 0))) {
+ if(s->peer == 0) {
+ /* On first READY message, create the connection. */
+ s->peer = create_remote_socket(p->msg.arg0, t);
+ s->peer->peer = s;
+ s->ready(s);
+ } else if (s->peer->id == p->msg.arg0) {
+ /* Other READY messages must use the same local-id */
+ s->ready(s);
+ } else {
+ D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
+ p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
+ }
+ }
+ }
+ break;
+
+ case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
+ if (t->online && p->msg.arg1 != 0) {
+ if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
+ /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
+ * a failed OPEN only. However, due to a bug in previous ADB
+ * versions, CLOSE(0, remote-id, "") was also used for normal
+ * CLOSE() operations.
+ *
+ * This is bad because it means a compromised adbd could
+ * send packets to close connections between the host and
+ * other devices. To avoid this, only allow this if the local
+ * socket has a peer on the same transport.
+ */
+ if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
+ D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
+ p->msg.arg1, t->serial, s->peer->transport->serial);
+ } else {
+ s->close(s);
+ }
+ }
+ }
+ break;
+
+ case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
+ if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
+ if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
+ unsigned rid = p->msg.arg0;
+ p->len = p->msg.data_length;
+
+ if(s->enqueue(s, p) == 0) {
+ D("Enqueue the socket\n");
+ send_ready(s->id, rid, t);
+ }
+ return;
+ }
+ }
+ break;
+
+ default:
+ printf("handle_packet: what is %08x?!\n", p->msg.command);
+ }
+
+ put_apacket(p);
+}
+
+#if ADB_HOST
+
+int launch_server(int server_port)
+{
+#if defined(_WIN32)
+ /* we need to start the server in the background */
+ /* we create a PIPE that will be used to wait for the server's "OK" */
+ /* message since the pipe handles must be inheritable, we use a */
+ /* security attribute */
+ HANDLE pipe_read, pipe_write;
+ HANDLE stdout_handle, stderr_handle;
+ SECURITY_ATTRIBUTES sa;
+ STARTUPINFO startup;
+ PROCESS_INFORMATION pinfo;
+ char program_path[ MAX_PATH ];
+ int ret;
+
+ sa.nLength = sizeof(sa);
+ sa.lpSecurityDescriptor = NULL;
+ sa.bInheritHandle = TRUE;
+
+ /* create pipe, and ensure its read handle isn't inheritable */
+ ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
+ if (!ret) {
+ fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
+ return -1;
+ }
+
+ SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
+
+ /* Some programs want to launch an adb command and collect its output by
+ * calling CreateProcess with inheritable stdout/stderr handles, then
+ * using read() to get its output. When this happens, the stdout/stderr
+ * handles passed to the adb client process will also be inheritable.
+ * When starting the adb server here, care must be taken to reset them
+ * to non-inheritable.
+ * Otherwise, something bad happens: even if the adb command completes,
+ * the calling process is stuck while read()-ing from the stdout/stderr
+ * descriptors, because they're connected to corresponding handles in the
+ * adb server process (even if the latter never uses/writes to them).
+ */
+ stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
+ stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
+ if (stdout_handle != INVALID_HANDLE_VALUE) {
+ SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
+ }
+ if (stderr_handle != INVALID_HANDLE_VALUE) {
+ SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
+ }
+
+ ZeroMemory( &startup, sizeof(startup) );
+ startup.cb = sizeof(startup);
+ startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
+ startup.hStdOutput = pipe_write;
+ startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
+ startup.dwFlags = STARTF_USESTDHANDLES;
+
+ ZeroMemory( &pinfo, sizeof(pinfo) );
+
+ /* get path of current program */
+ GetModuleFileName( NULL, program_path, sizeof(program_path) );
+ char args[64];
+ snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port);
+ ret = CreateProcess(
+ program_path, /* program path */
+ args,
+ /* the fork-server argument will set the
+ debug = 2 in the child */
+ NULL, /* process handle is not inheritable */
+ NULL, /* thread handle is not inheritable */
+ TRUE, /* yes, inherit some handles */
+ DETACHED_PROCESS, /* the new process doesn't have a console */
+ NULL, /* use parent's environment block */
+ NULL, /* use parent's starting directory */
+ &startup, /* startup info, i.e. std handles */
+ &pinfo );
+
+ CloseHandle( pipe_write );
+
+ if (!ret) {
+ fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
+ CloseHandle( pipe_read );
+ return -1;
+ }
+
+ CloseHandle( pinfo.hProcess );
+ CloseHandle( pinfo.hThread );
+
+ /* wait for the "OK\n" message */
+ {
+ char temp[3];
+ DWORD count;
+
+ ret = ReadFile( pipe_read, temp, 3, &count, NULL );
+ CloseHandle( pipe_read );
+ if ( !ret ) {
+ fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
+ return -1;
+ }
+ if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
+ fprintf(stderr, "ADB server didn't ACK\n" );
+ return -1;
+ }
+ }
+#else /* !defined(_WIN32) */
+ char path[PATH_MAX];
+ int fd[2];
+
+ // set up a pipe so the child can tell us when it is ready.
+ // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
+ if (pipe(fd)) {
+ fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
+ return -1;
+ }
+ get_my_path(path, PATH_MAX);
+ pid_t pid = fork();
+ if(pid < 0) return -1;
+
+ if (pid == 0) {
+ // child side of the fork
+
+ // redirect stderr to the pipe
+ // we use stderr instead of stdout due to stdout's buffering behavior.
+ adb_close(fd[0]);
+ dup2(fd[1], STDERR_FILENO);
+ adb_close(fd[1]);
+
+ char str_port[30];
+ snprintf(str_port, sizeof(str_port), "%d", server_port);
+ // child process
+ int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
+ // this should not return
+ fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
+ } else {
+ // parent side of the fork
+
+ char temp[3];
+
+ temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
+ // wait for the "OK\n" message
+ adb_close(fd[1]);
+ int ret = adb_read(fd[0], temp, 3);
+ int saved_errno = errno;
+ adb_close(fd[0]);
+ if (ret < 0) {
+ fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
+ return -1;
+ }
+ if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
+ fprintf(stderr, "ADB server didn't ACK\n" );
+ return -1;
+ }
+
+ setsid();
+ }
+#endif /* !defined(_WIN32) */
+ return 0;
+}
+#endif /* ADB_HOST */
+
+// Try to handle a network forwarding request.
+// This returns 1 on success, 0 on failure, and -1 to indicate this is not
+// a forwarding-related request.
+int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd)
+{
+ if (!strcmp(service, "list-forward")) {
+ // Create the list of forward redirections.
+ int buffer_size = format_listeners(NULL, 0);
+ // Add one byte for the trailing zero.
+ char* buffer = reinterpret_cast<char*>(malloc(buffer_size + 1));
+ if (buffer == nullptr) {
+ sendfailmsg(reply_fd, "not enough memory");
+ return 1;
+ }
+ (void) format_listeners(buffer, buffer_size + 1);
+#if ADB_HOST
+ send_msg_with_okay(reply_fd, buffer, buffer_size);
+#else
+ send_msg_with_header(reply_fd, buffer, buffer_size);
+#endif
+ free(buffer);
+ return 1;
+ }
+
+ if (!strcmp(service, "killforward-all")) {
+ remove_all_listeners();
+#if ADB_HOST
+ /* On the host: 1st OKAY is connect, 2nd OKAY is status */
+ adb_write(reply_fd, "OKAY", 4);
+#endif
+ adb_write(reply_fd, "OKAY", 4);
+ return 1;
+ }
+
+ if (!strncmp(service, "forward:",8) ||
+ !strncmp(service, "killforward:",12)) {
+ char *local, *remote;
+ int r;
+ atransport *transport;
+
+ int createForward = strncmp(service, "kill", 4);
+ int no_rebind = 0;
+
+ local = strchr(service, ':') + 1;
+
+ // Handle forward:norebind:<local>... here
+ if (createForward && !strncmp(local, "norebind:", 9)) {
+ no_rebind = 1;
+ local = strchr(local, ':') + 1;
+ }
+
+ remote = strchr(local,';');
+
+ if (createForward) {
+ // Check forward: parameter format: '<local>;<remote>'
+ if(remote == 0) {
+ sendfailmsg(reply_fd, "malformed forward spec");
+ return 1;
+ }
+
+ *remote++ = 0;
+ if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
+ sendfailmsg(reply_fd, "malformed forward spec");
+ return 1;
+ }
+ } else {
+ // Check killforward: parameter format: '<local>'
+ if (local[0] == 0) {
+ sendfailmsg(reply_fd, "malformed forward spec");
+ return 1;
+ }
+ }
+
+ const char* err;
+ transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
+ if (!transport) {
+ sendfailmsg(reply_fd, err);
+ return 1;
+ }
+
+ if (createForward) {
+ r = install_listener(local, remote, transport, no_rebind);
+ } else {
+ r = remove_listener(local, transport);
+ }
+ if(r == 0) {
+#if ADB_HOST
+ /* On the host: 1st OKAY is connect, 2nd OKAY is status */
+ WriteFdExactly(reply_fd, "OKAY", 4);
+#endif
+ WriteFdExactly(reply_fd, "OKAY", 4);
+ return 1;
+ }
+
+ if (createForward) {
+ const char* message;
+ switch (r) {
+ case INSTALL_STATUS_CANNOT_BIND:
+ message = "cannot bind to socket";
+ break;
+ case INSTALL_STATUS_CANNOT_REBIND:
+ message = "cannot rebind existing socket";
+ break;
+ default:
+ message = "internal error";
+ }
+ sendfailmsg(reply_fd, message);
+ } else {
+ sendfailmsg(reply_fd, "cannot remove listener");
+ }
+ return 1;
+ }
+ return 0;
+}
+
+int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
+{
+ if(!strcmp(service, "kill")) {
+ fprintf(stderr,"adb server killed by remote request\n");
+ fflush(stdout);
+ adb_write(reply_fd, "OKAY", 4);
+ usb_cleanup();
+ exit(0);
+ }
+
+#if ADB_HOST
+ atransport *transport = NULL;
+ // "transport:" is used for switching transport with a specified serial number
+ // "transport-usb:" is used for switching transport to the only USB transport
+ // "transport-local:" is used for switching transport to the only local transport
+ // "transport-any:" is used for switching transport to the only transport
+ if (!strncmp(service, "transport", strlen("transport"))) {
+ transport_type type = kTransportAny;
+
+ if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
+ type = kTransportUsb;
+ } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
+ type = kTransportLocal;
+ } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
+ type = kTransportAny;
+ } else if (!strncmp(service, "transport:", strlen("transport:"))) {
+ service += strlen("transport:");
+ serial = service;
+ }
+
+ const char* error_string = "unknown failure";
+ transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
+
+ if (transport) {
+ s->transport = transport;
+ adb_write(reply_fd, "OKAY", 4);
+ } else {
+ sendfailmsg(reply_fd, error_string);
+ }
+ return 1;
+ }
+
+ // return a list of all connected devices
+ if (!strncmp(service, "devices", 7)) {
+ char buffer[4096];
+ int use_long = !strcmp(service+7, "-l");
+ if (use_long || service[7] == 0) {
+ memset(buffer, 0, sizeof(buffer));
+ D("Getting device list \n");
+ list_transports(buffer, sizeof(buffer), use_long);
+ D("Wrote device list \n");
+ send_msg_with_okay(reply_fd, buffer, strlen(buffer));
+ return 0;
+ }
+ }
+
+ // remove TCP transport
+ if (!strncmp(service, "disconnect:", 11)) {
+ char buffer[4096];
+ memset(buffer, 0, sizeof(buffer));
+ char* serial = service + 11;
+ if (serial[0] == 0) {
+ // disconnect from all TCP devices
+ unregister_all_tcp_transports();
+ } else {
+ char hostbuf[100];
+ // assume port 5555 if no port is specified
+ if (!strchr(serial, ':')) {
+ snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
+ serial = hostbuf;
+ }
+ atransport *t = find_transport(serial);
+
+ if (t) {
+ unregister_transport(t);
+ } else {
+ snprintf(buffer, sizeof(buffer), "No such device %s", serial);
+ }
+ }
+
+ send_msg_with_okay(reply_fd, buffer, strlen(buffer));
+ return 0;
+ }
+
+ // returns our value for ADB_SERVER_VERSION
+ if (!strcmp(service, "version")) {
+ char version[12];
+ snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
+ send_msg_with_okay(reply_fd, version, strlen(version));
+ return 0;
+ }
+
+ if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
+ const char *out = "unknown";
+ transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
+ if (transport && transport->serial) {
+ out = transport->serial;
+ }
+ send_msg_with_okay(reply_fd, out, strlen(out));
+ return 0;
+ }
+ if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
+ const char *out = "unknown";
+ transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
+ if (transport && transport->devpath) {
+ out = transport->devpath;
+ }
+ send_msg_with_okay(reply_fd, out, strlen(out));
+ return 0;
+ }
+ // indicates a new emulator instance has started
+ if (!strncmp(service,"emulator:",9)) {
+ int port = atoi(service+9);
+ local_connect(port);
+ /* we don't even need to send a reply */
+ return 0;
+ }
+
+ if(!strncmp(service,"get-state",strlen("get-state"))) {
+ transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
+ const char *state = connection_state_name(transport);
+ send_msg_with_okay(reply_fd, state, strlen(state));
+ return 0;
+ }
+#endif // ADB_HOST
+
+ int ret = handle_forward_request(service, ttype, serial, reply_fd);
+ if (ret >= 0)
+ return ret - 1;
+ return -1;
+}
diff --git a/adb/adb.h b/adb/adb.h
index 6f5c93c..749515c 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -18,8 +18,14 @@
#define __ADB_H
#include <limits.h>
+#include <sys/types.h>
-#include "transport.h" /* readx(), writex() */
+#include "adb_trace.h"
+#include "fdevent.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
#define MAX_PAYLOAD 4096
@@ -31,12 +37,15 @@
#define A_WRTE 0x45545257
#define A_AUTH 0x48545541
-#define A_VERSION 0x01000000 // ADB protocol version
+// ADB protocol version.
+#define A_VERSION 0x01000000
-#define ADB_VERSION_MAJOR 1 // Used for help/version information
-#define ADB_VERSION_MINOR 0 // Used for help/version information
+// Used for help/version information.
+#define ADB_VERSION_MAJOR 1
+#define ADB_VERSION_MINOR 0
-#define ADB_SERVER_VERSION 31 // Increment this when we want to force users to start a new adb server
+// Increment this when we want to force users to start a new adb server.
+#define ADB_SERVER_VERSION 32
typedef struct amessage amessage;
typedef struct apacket apacket;
@@ -230,8 +239,8 @@
fdevent fde;
int fd;
- const char *local_name;
- const char *connect_to;
+ char *local_name;
+ char *connect_to;
atransport *transport;
adisconnect disconnect;
};
@@ -257,33 +266,11 @@
void fatal_errno(const char *fmt, ...);
void handle_packet(apacket *p, atransport *t);
-void send_packet(apacket *p, atransport *t);
void get_my_path(char *s, size_t maxLen);
int launch_server(int server_port);
int adb_main(int is_daemon, int server_port);
-
-/* transports are ref-counted
-** get_device_transport does an acquire on your behalf before returning
-*/
-void init_transport_registration(void);
-int list_transports(char *buf, size_t bufsize, int long_listing);
-void update_transports(void);
-
-asocket* create_device_tracker(void);
-
-/* Obtain a transport from the available transports.
-** If state is != CS_ANY, only transports in that state are considered.
-** If serial is non-NULL then only the device with that serial will be chosen.
-** If no suitable transport is found, error is set.
-*/
-atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
-void add_transport_disconnect( atransport* t, adisconnect* dis );
-void remove_transport_disconnect( atransport* t, adisconnect* dis );
-void run_transport_disconnects( atransport* t );
-void kick_transport( atransport* t );
-
/* initialize a transport object's func pointers and state */
#if ADB_HOST
int get_available_local_transport_index();
@@ -291,22 +278,6 @@
int init_socket_transport(atransport *t, int s, int port, int local);
void init_usb_transport(atransport *t, usb_handle *usb, int state);
-/* for MacOS X cleanup */
-void close_usb_devices();
-
-/* cause new transports to be init'd and added to the list */
-int register_socket_transport(int s, const char *serial, int port, int local);
-
-/* these should only be used for the "adb disconnect" command */
-void unregister_transport(atransport *t);
-void unregister_all_tcp_transports();
-
-void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
-
-/* this should only be used for transports with connection_state == CS_NOPERM */
-void unregister_usb_transport(usb_handle *usb);
-
-atransport *find_transport(const char *serial);
#if ADB_HOST
atransport* find_emulator_transport_by_adb_port(int adb_port);
#endif
@@ -323,101 +294,19 @@
int create_jdwp_connection_fd(int jdwp_pid);
#endif
+int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
+
#if !ADB_HOST
-typedef enum {
- BACKUP,
- RESTORE
-} BackupOperation;
-int backup_service(BackupOperation operation, char* args);
void framebuffer_service(int fd, void *cookie);
-void remount_service(int fd, void *cookie);
+void set_verity_enabled_state_service(int fd, void* cookie);
#endif
/* packet allocator */
apacket *get_apacket(void);
void put_apacket(apacket *p);
-int check_header(apacket *p);
-int check_data(apacket *p);
-
-/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
-
-#define ADB_TRACE 1
-
-/* IMPORTANT: if you change the following list, don't
- * forget to update the corresponding 'tags' table in
- * the adb_trace_init() function implemented in adb.c
- */
-typedef enum {
- TRACE_ADB = 0, /* 0x001 */
- TRACE_SOCKETS,
- TRACE_PACKETS,
- TRACE_TRANSPORT,
- TRACE_RWX, /* 0x010 */
- TRACE_USB,
- TRACE_SYNC,
- TRACE_SYSDEPS,
- TRACE_JDWP, /* 0x100 */
- TRACE_SERVICES,
- TRACE_AUTH,
-} AdbTrace;
-
-#if ADB_TRACE
-
-#if !ADB_HOST
-/*
- * When running inside the emulator, guest's adbd can connect to 'adb-debug'
- * qemud service that can display adb trace messages (on condition that emulator
- * has been started with '-debug adb' option).
- */
-
-/* Delivers a trace message to the emulator via QEMU pipe. */
-void adb_qemu_trace(const char* fmt, ...);
-/* Macro to use to send ADB trace messages to the emulator. */
-#define DQ(...) adb_qemu_trace(__VA_ARGS__)
-#else
-#define DQ(...) ((void)0)
-#endif /* !ADB_HOST */
-
- extern int adb_trace_mask;
- extern unsigned char adb_trace_output_count;
- void adb_trace_init(void);
-
-# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
-
- /* you must define TRACE_TAG before using this macro */
-# define D(...) \
- do { \
- if (ADB_TRACING) { \
- int save_errno = errno; \
- adb_mutex_lock(&D_lock); \
- fprintf(stderr, "%s::%s():", \
- __FILE__, __FUNCTION__); \
- errno = save_errno; \
- fprintf(stderr, __VA_ARGS__ ); \
- fflush(stderr); \
- adb_mutex_unlock(&D_lock); \
- errno = save_errno; \
- } \
- } while (0)
-# define DR(...) \
- do { \
- if (ADB_TRACING) { \
- int save_errno = errno; \
- adb_mutex_lock(&D_lock); \
- errno = save_errno; \
- fprintf(stderr, __VA_ARGS__ ); \
- fflush(stderr); \
- adb_mutex_unlock(&D_lock); \
- errno = save_errno; \
- } \
- } while (0)
-#else
-# define D(...) ((void)0)
-# define DR(...) ((void)0)
-# define ADB_TRACING 0
-#endif
-
+// Define it if you want to dump packets.
+#define DEBUG_PACKETS 0
#if !DEBUG_PACKETS
#define print_packet(tag,p) do {} while (0)
@@ -456,8 +345,7 @@
int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
#endif
-unsigned host_to_le32(unsigned n);
-int adb_commandline(int argc, char **argv);
+int adb_commandline(int argc, const char **argv);
int connection_state(atransport *t);
@@ -471,9 +359,15 @@
#define CS_SIDELOAD 6
#define CS_UNAUTHORIZED 7
+extern const char *adb_device_banner;
extern int HOST;
extern int SHELL_EXIT_NOTIFY_FD;
+typedef enum {
+ SUBPROC_PTY = 0,
+ SUBPROC_RAW = 1,
+} subproc_mode;
+
#define CHUNK_SIZE (64*1024)
#if !ADB_HOST
@@ -490,4 +384,13 @@
int sendfailmsg(int fd, const char *reason);
int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
+void handle_online(atransport *t);
+void handle_offline(atransport *t);
+
+void send_connect(atransport *t);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/adb/adb_auth.cpp b/adb/adb_auth.cpp
new file mode 100644
index 0000000..c236b64
--- /dev/null
+++ b/adb/adb_auth.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#define TRACE_TAG TRACE_ADB
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "adb.h"
+#include "adb_auth.h"
+#include "transport.h"
+#include "sysdeps.h"
+
+int auth_enabled = 0;
+
+void send_auth_request(atransport *t)
+{
+ D("Calling send_auth_request\n");
+ apacket *p;
+ int ret;
+
+ ret = adb_auth_generate_token(t->token, sizeof(t->token));
+ if (ret != sizeof(t->token)) {
+ D("Error generating token ret=%d\n", ret);
+ return;
+ }
+
+ p = get_apacket();
+ memcpy(p->data, t->token, ret);
+ p->msg.command = A_AUTH;
+ p->msg.arg0 = ADB_AUTH_TOKEN;
+ p->msg.data_length = ret;
+ send_packet(p, t);
+}
+
+void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
+{
+ D("Calling send_auth_response\n");
+ apacket *p = get_apacket();
+ int ret;
+
+ ret = adb_auth_sign(t->key, token, token_size, p->data);
+ if (!ret) {
+ D("Error signing the token\n");
+ put_apacket(p);
+ return;
+ }
+
+ p->msg.command = A_AUTH;
+ p->msg.arg0 = ADB_AUTH_SIGNATURE;
+ p->msg.data_length = ret;
+ send_packet(p, t);
+}
+
+void send_auth_publickey(atransport *t)
+{
+ D("Calling send_auth_publickey\n");
+ apacket *p = get_apacket();
+ int ret;
+
+ ret = adb_auth_get_userkey(p->data, sizeof(p->data));
+ if (!ret) {
+ D("Failed to get user public key\n");
+ put_apacket(p);
+ return;
+ }
+
+ p->msg.command = A_AUTH;
+ p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
+ p->msg.data_length = ret;
+ send_packet(p, t);
+}
+
+void adb_auth_verified(atransport *t)
+{
+ handle_online(t);
+ send_connect(t);
+}
diff --git a/adb/adb_auth.h b/adb/adb_auth.h
index b24c674..e0425ad 100644
--- a/adb/adb_auth.h
+++ b/adb/adb_auth.h
@@ -17,10 +17,18 @@
#ifndef __ADB_AUTH_H
#define __ADB_AUTH_H
-void adb_auth_init(void);
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int auth_enabled;
+
+int adb_auth_keygen(const char* filename);
void adb_auth_verified(atransport *t);
void send_auth_request(atransport *t);
+void send_auth_response(uint8_t *token, size_t token_size, atransport *t);
+void send_auth_publickey(atransport *t);
/* AUTH packets first argument */
/* Request */
@@ -31,7 +39,9 @@
#if ADB_HOST
-int adb_auth_sign(void *key, void *token, size_t token_size, void *sig);
+void adb_auth_init(void);
+int adb_auth_sign(void *key, const unsigned char* token, size_t token_size,
+ unsigned char* sig);
void *adb_auth_nextkey(void *current);
int adb_auth_get_userkey(unsigned char *data, size_t len);
@@ -41,14 +51,23 @@
#else // !ADB_HOST
-static inline int adb_auth_sign(void* key, void *token, size_t token_size, void *sig) { return 0; }
+static inline int adb_auth_sign(void* key, const unsigned char* token,
+ size_t token_size, unsigned char* sig) {
+ return 0;
+}
static inline void *adb_auth_nextkey(void *current) { return NULL; }
static inline int adb_auth_get_userkey(unsigned char *data, size_t len) { return 0; }
+void adbd_auth_init(void);
+void adbd_cloexec_auth_socket();
int adb_auth_generate_token(void *token, size_t token_size);
-int adb_auth_verify(void *token, void *sig, int siglen);
+int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen);
void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t);
#endif // ADB_HOST
+#ifdef __cplusplus
+}
+#endif
+
#endif // __ADB_AUTH_H
diff --git a/adb/adb_auth_client.c b/adb/adb_auth_client.cpp
similarity index 86%
rename from adb/adb_auth_client.c
rename to adb/adb_auth_client.cpp
index f8d7306..5dadcd9 100644
--- a/adb/adb_auth_client.c
+++ b/adb/adb_auth_client.cpp
@@ -14,18 +14,20 @@
* limitations under the License.
*/
+#include <resolv.h>
#include <stdio.h>
#include <string.h>
-#include <resolv.h>
-#include <cutils/list.h>
-#include <cutils/sockets.h>
#include "sysdeps.h"
+
#include "adb.h"
#include "adb_auth.h"
+#include "cutils/list.h"
+#include "cutils/sockets.h"
#include "fdevent.h"
#include "mincrypt/rsa.h"
#include "mincrypt/sha.h"
+#include "transport.h"
#define TRACE_TAG TRACE_AUTH
@@ -35,7 +37,7 @@
RSAPublicKey key;
};
-static char *key_paths[] = {
+static const char *key_paths[] = {
"/adb_keys",
"/data/misc/adb/adb_keys",
NULL
@@ -51,13 +53,12 @@
static void read_keys(const char *file, struct listnode *list)
{
- struct adb_public_key *key;
FILE *f;
char buf[MAX_PAYLOAD];
char *sep;
int ret;
- f = fopen(file, "r");
+ f = fopen(file, "re");
if (!f) {
D("Can't open '%s'\n", file);
return;
@@ -65,8 +66,9 @@
while (fgets(buf, sizeof(buf), f)) {
/* Allocate 4 extra bytes to decode the base64 data in-place */
- key = calloc(1, sizeof(*key) + 4);
- if (!key) {
+ auto key = reinterpret_cast<adb_public_key*>(
+ calloc(1, sizeof(adb_public_key) + 4));
+ if (key == nullptr) {
D("Can't malloc key\n");
break;
}
@@ -107,8 +109,8 @@
static void load_keys(struct listnode *list)
{
- char *path;
- char **paths = key_paths;
+ const char* path;
+ const char** paths = key_paths;
struct stat buf;
list_init(list);
@@ -126,7 +128,7 @@
FILE *f;
int ret;
- f = fopen("/dev/urandom", "r");
+ f = fopen("/dev/urandom", "re");
if (!f)
return 0;
@@ -136,10 +138,9 @@
return ret * token_size;
}
-int adb_auth_verify(void *token, void *sig, int siglen)
+int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen)
{
struct listnode *item;
- struct adb_public_key *key;
struct listnode key_list;
int ret = 0;
@@ -149,7 +150,7 @@
load_keys(&key_list);
list_for_each(item, &key_list) {
- key = node_to_item(item, struct adb_public_key, node);
+ adb_public_key* key = node_to_item(item, struct adb_public_key, node);
ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE);
if (ret)
break;
@@ -175,7 +176,7 @@
if (events & FDE_READ) {
ret = unix_read(fd, response, sizeof(response));
- if (ret < 0) {
+ if (ret <= 0) {
D("Framework disconnect\n");
if (usb_transport)
fdevent_remove(&usb_transport->auth_fde);
@@ -248,18 +249,23 @@
}
}
-void adb_auth_init(void)
-{
- int fd, ret;
+void adbd_cloexec_auth_socket() {
+ int fd = android_get_control_socket("adbd");
+ if (fd == -1) {
+ D("Failed to get adbd socket\n");
+ return;
+ }
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+}
- fd = android_get_control_socket("adbd");
- if (fd < 0) {
+void adbd_auth_init(void) {
+ int fd = android_get_control_socket("adbd");
+ if (fd == -1) {
D("Failed to get adbd socket\n");
return;
}
- ret = listen(fd, 4);
- if (ret < 0) {
+ if (listen(fd, 4) == -1) {
D("Failed to listen on '%d'\n", fd);
return;
}
diff --git a/adb/adb_auth_host.c b/adb/adb_auth_host.cpp
similarity index 73%
rename from adb/adb_auth_host.c
rename to adb/adb_auth_host.cpp
index 9039d42..aba23d4 100644
--- a/adb/adb_auth_host.c
+++ b/adb/adb_auth_host.cpp
@@ -15,9 +15,12 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#ifdef _WIN32
-# define WIN32_LEAN_AND_MEAN
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
# include "windows.h"
# include "shlobj.h"
#else
@@ -45,6 +48,10 @@
#include <openssl/rsa.h>
#include <openssl/sha.h>
+#if defined(OPENSSL_IS_BORINGSSL)
+#include <openssl/base64.h>
+#endif
+
#define TRACE_TAG TRACE_AUTH
#define ANDROID_PATH ".android"
@@ -110,18 +117,34 @@
static void get_user_info(char *buf, size_t len)
{
char hostname[1024], username[1024];
- int ret;
+ int ret = -1;
+
+ if (getenv("HOSTNAME") != NULL) {
+ strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname));
+ hostname[sizeof(hostname)-1] = '\0';
+ ret = 0;
+ }
#ifndef _WIN32
- ret = gethostname(hostname, sizeof(hostname));
if (ret < 0)
+ ret = gethostname(hostname, sizeof(hostname));
#endif
+ if (ret < 0)
strcpy(hostname, "unknown");
+ ret = -1;
+
+ if (getenv("LOGNAME") != NULL) {
+ strncpy(username, getenv("LOGNAME"), sizeof(username));
+ username[sizeof(username)-1] = '\0';
+ ret = 0;
+ }
+
#if !defined _WIN32 && !defined ADB_HOST_ON_TARGET
- ret = getlogin_r(username, sizeof(username));
if (ret < 0)
+ ret = getlogin_r(username, sizeof(username));
#endif
+ if (ret < 0)
strcpy(username, "unknown");
ret = snprintf(buf, len, " %s@%s", username, hostname);
@@ -132,43 +155,67 @@
static int write_public_keyfile(RSA *private_key, const char *private_key_path)
{
RSAPublicKey pkey;
- BIO *bio, *b64, *bfile;
+ FILE *outfile = NULL;
char path[PATH_MAX], info[MAX_PAYLOAD];
- int ret;
+ uint8_t* encoded = nullptr;
+ size_t encoded_length;
+ int ret = 0;
- ret = snprintf(path, sizeof(path), "%s.pub", private_key_path);
- if (ret >= (signed)sizeof(path))
+ if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
+ (int)sizeof(path)) {
+ D("Path too long while writing public key\n");
return 0;
+ }
- ret = RSA_to_RSAPublicKey(private_key, &pkey);
- if (!ret) {
+ if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
D("Failed to convert to publickey\n");
return 0;
}
- bfile = BIO_new_file(path, "w");
- if (!bfile) {
+ outfile = fopen(path, "w");
+ if (!outfile) {
D("Failed to open '%s'\n", path);
return 0;
}
D("Writing public key to '%s'\n", path);
- b64 = BIO_new(BIO_f_base64());
- BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
+#if defined(OPENSSL_IS_BORINGSSL)
+ if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
+ D("Public key too large to base64 encode");
+ goto out;
+ }
+#else
+ /* While we switch from OpenSSL to BoringSSL we have to implement
+ * |EVP_EncodedLength| here. */
+ encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
+#endif
- bio = BIO_push(b64, bfile);
- BIO_write(bio, &pkey, sizeof(pkey));
- BIO_flush(bio);
- BIO_pop(b64);
- BIO_free(b64);
+ encoded = reinterpret_cast<uint8_t*>(malloc(encoded_length));
+ if (encoded == nullptr) {
+ D("Allocation failure");
+ goto out;
+ }
+ encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
get_user_info(info, sizeof(info));
- BIO_write(bfile, info, strlen(info));
- BIO_flush(bfile);
- BIO_free_all(bfile);
- return 1;
+ if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
+ fwrite(info, strlen(info), 1, outfile) != 1) {
+ D("Write error while writing public key");
+ goto out;
+ }
+
+ ret = 1;
+
+ out:
+ if (outfile != NULL) {
+ fclose(outfile);
+ }
+ if (encoded != NULL) {
+ free(encoded);
+ }
+ return ret;
}
static int generate_key(const char *file)
@@ -225,18 +272,16 @@
static int read_key(const char *file, struct listnode *list)
{
- struct adb_private_key *key;
- FILE *f;
-
D("read_key '%s'\n", file);
- f = fopen(file, "r");
+ FILE* f = fopen(file, "r");
if (!f) {
D("Failed to open '%s'\n", file);
return 0;
}
- key = malloc(sizeof(*key));
+ adb_private_key* key = reinterpret_cast<adb_private_key*>(
+ malloc(sizeof(adb_private_key)));
if (!key) {
D("Failed to alloc key\n");
fclose(f);
@@ -343,11 +388,17 @@
}
}
-int adb_auth_sign(void *node, void *token, size_t token_size, void *sig)
+int adb_auth_sign(void *node, const unsigned char* token, size_t token_size,
+ unsigned char* sig)
{
unsigned int len;
struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
+ if (token_size != TOKEN_SIZE) {
+ D("Unexpected token size %zd\n", token_size);
+ return 0;
+ }
+
if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
return 0;
}
@@ -381,31 +432,38 @@
int adb_auth_get_userkey(unsigned char *data, size_t len)
{
char path[PATH_MAX];
- char *file;
- int ret;
-
- ret = get_user_keyfilepath(path, sizeof(path) - 4);
+ int ret = get_user_keyfilepath(path, sizeof(path) - 4);
if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
D("Error getting user key filename");
return 0;
}
strcat(path, ".pub");
- file = load_file(path, (unsigned*)&ret);
- if (!file) {
+ // TODO(danalbert): ReadFileToString
+ unsigned size;
+ char* file_data = reinterpret_cast<char*>(load_file(path, &size));
+ if (file_data == nullptr) {
D("Can't load '%s'\n", path);
return 0;
}
- if (len < (size_t)(ret + 1)) {
- D("%s: Content too large ret=%d\n", path, ret);
+ if (len < (size_t)(size + 1)) {
+ D("%s: Content too large ret=%d\n", path, size);
+ free(file_data);
return 0;
}
- memcpy(data, file, ret);
- data[ret] = '\0';
+ memcpy(data, file_data, size);
+ free(file_data);
+ file_data = nullptr;
+ data[size] = '\0';
- return ret + 1;
+ return size + 1;
+}
+
+int adb_auth_keygen(const char* filename) {
+ adb_trace_mask |= (1 << TRACE_AUTH);
+ return (generate_key(filename) == 0);
}
void adb_auth_init(void)
diff --git a/adb/adb_client.c b/adb/adb_client.cpp
similarity index 84%
rename from adb/adb_client.c
rename to adb/adb_client.cpp
index 586cd7b..a485aa2 100644
--- a/adb/adb_client.c
+++ b/adb/adb_client.cpp
@@ -1,17 +1,33 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * Copyright (C) 2015 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 <errno.h>
#include <limits.h>
#include <stdarg.h>
-#include <zipfile/zipfile.h>
-#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_ADB
#include "adb_client.h"
+#include "adb_io.h"
static transport_type __adb_transport = kTransportAny;
static const char* __adb_serial = NULL;
@@ -98,7 +114,7 @@
if (__adb_serial)
snprintf(service, sizeof service, "host:transport:%s", __adb_serial);
else {
- char* transport_type = "???";
+ const char* transport_type = "???";
switch (__adb_transport) {
case kTransportUsb:
@@ -121,7 +137,7 @@
len = strlen(service);
snprintf(tmp, sizeof tmp, "%04x", len);
- if(writex(fd, tmp, 4) || writex(fd, service, len)) {
+ if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
strcpy(__adb_error, "write failure during connection");
adb_close(fd);
return -1;
@@ -142,7 +158,7 @@
unsigned char buf[5];
unsigned len;
- if(readx(fd, buf, 4)) {
+ if(!ReadFdExactly(fd, buf, 4)) {
strcpy(__adb_error, "protocol fault (no status)");
return -1;
}
@@ -158,14 +174,14 @@
return -1;
}
- if(readx(fd, buf, 4)) {
+ if(!ReadFdExactly(fd, buf, 4)) {
strcpy(__adb_error, "protocol fault (status len)");
return -1;
}
buf[4] = 0;
len = strtoul((char*)buf, 0, 16);
if(len > 255) len = 255;
- if(readx(fd, __adb_error, len)) {
+ if(!ReadFdExactly(fd, __adb_error, len)) {
strcpy(__adb_error, "protocol fault (status read)");
return -1;
}
@@ -201,7 +217,7 @@
return -1;
}
- if(writex(fd, tmp, 4) || writex(fd, service, len)) {
+ if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
strcpy(__adb_error, "write failure during connection");
adb_close(fd);
return -1;
@@ -246,12 +262,12 @@
// if we have a file descriptor, then parse version result
if(fd >= 0) {
- if(readx(fd, buf, 4)) goto error;
+ if(!ReadFdExactly(fd, buf, 4)) goto error;
buf[4] = 0;
n = strtoul(buf, 0, 16);
if(n > sizeof(buf)) goto error;
- if(readx(fd, buf, n)) goto error;
+ if(!ReadFdExactly(fd, buf, n)) goto error;
adb_close(fd);
if (sscanf(buf, "%04x", &version) != 1) goto error;
@@ -279,7 +295,7 @@
fd = _adb_connect(service);
if(fd == -1) {
- fprintf(stderr,"error: %s\n", __adb_error);
+ D("_adb_connect error: %s", __adb_error);
} else if(fd == -2) {
fprintf(stderr,"** daemon still not running\n");
}
@@ -296,6 +312,7 @@
{
int fd = adb_connect(service);
if(fd < 0) {
+ fprintf(stderr, "error: %s\n", adb_error());
return -1;
}
@@ -310,8 +327,8 @@
char *adb_query(const char *service)
{
char buf[5];
- unsigned n;
- char *tmp;
+ unsigned long n;
+ char* tmp;
D("adb_query: %s\n", service);
int fd = adb_connect(service);
@@ -320,16 +337,19 @@
return 0;
}
- if(readx(fd, buf, 4)) goto oops;
+ if(!ReadFdExactly(fd, buf, 4)) goto oops;
buf[4] = 0;
n = strtoul(buf, 0, 16);
- if(n > 1024) goto oops;
+ if(n >= 0xffff) {
+ strcpy(__adb_error, "reply is too long (>= 64kB)");
+ goto oops;
+ }
- tmp = malloc(n + 1);
+ tmp = reinterpret_cast<char*>(malloc(n + 1));
if(tmp == 0) goto oops;
- if(readx(fd, tmp, n) == 0) {
+ if(!ReadFdExactly(fd, tmp, n) == 0) {
tmp[n] = 0;
adb_close(fd);
return tmp;
diff --git a/adb/adb_client.h b/adb/adb_client.h
index 0ec47ca..9af176f 100644
--- a/adb/adb_client.h
+++ b/adb/adb_client.h
@@ -3,6 +3,10 @@
#include "adb.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/* connect to adb, connect to the named service, and return
** a valid fd for interacting with that service upon success
** or a negative number on failure
@@ -43,7 +47,7 @@
* is zero, or more than one emulator connected (or if you use -s <serial>
* with a <serial> that does not designate an emulator)
*/
-int adb_send_emulator_command(int argc, char** argv);
+int adb_send_emulator_command(int argc, const char** argv);
/* return verbose error string from last operation */
const char *adb_error(void);
@@ -54,4 +58,8 @@
*/
int adb_status(int fd);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp
new file mode 100644
index 0000000..4dd9f4d
--- /dev/null
+++ b/adb/adb_io.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#define TRACE_TAG TRACE_RWX
+
+#include "sysdeps.h"
+#include "adb_io.h"
+
+#include <unistd.h>
+
+#include "adb_trace.h"
+#include "transport.h"
+
+bool ReadFdExactly(int fd, void* buf, size_t len) {
+ char* p = reinterpret_cast<char*>(buf);
+
+#if ADB_TRACE
+ size_t len0 = len;
+#endif
+
+ D("readx: fd=%d wanted=%zu\n", fd, len);
+ while (len > 0) {
+ int r = adb_read(fd, p, len);
+ if (r > 0) {
+ len -= r;
+ p += r;
+ } else if (r == -1) {
+ D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno));
+ return false;
+ } else {
+ D("readx: fd=%d disconnected\n", fd);
+ errno = 0;
+ return false;
+ }
+ }
+
+#if ADB_TRACE
+ D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
+ if (ADB_TRACING) {
+ dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
+ }
+#endif
+
+ return true;
+}
+
+bool WriteFdExactly(int fd, const void* buf, size_t len) {
+ const char* p = reinterpret_cast<const char*>(buf);
+ int r;
+
+#if ADB_TRACE
+ D("writex: fd=%d len=%d: ", fd, (int)len);
+ if (ADB_TRACING) {
+ dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
+ }
+#endif
+
+ while (len > 0) {
+ r = adb_write(fd, p, len);
+ if (r == -1) {
+ D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno));
+ if (errno == EAGAIN) {
+ adb_sleep_ms(1); // just yield some cpu time
+ continue;
+ } else if (errno == EPIPE) {
+ D("writex: fd=%d disconnected\n", fd);
+ errno = 0;
+ return false;
+ }
+ } else {
+ len -= r;
+ p += r;
+ }
+ }
+ return true;
+}
+
+bool WriteStringFully(int fd, const char* str) {
+ return WriteFdExactly(fd, str, strlen(str));
+}
diff --git a/adb/adb_io.h b/adb/adb_io.h
new file mode 100644
index 0000000..7d09e7b
--- /dev/null
+++ b/adb/adb_io.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2015 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 ADB_IO_H
+#define ADB_IO_H
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Reads exactly len bytes from fd into buf.
+ *
+ * Returns false if there is an error or if EOF was reached before len bytes
+ * were read. If EOF was found, errno will be set to 0.
+ *
+ * If this function fails, the contents of buf are undefined.
+ */
+bool ReadFdExactly(int fd, void *buf, size_t len);
+
+/*
+ * Writes exactly len bytes from buf to fd.
+ *
+ * Returns false if there is an error or if the fd was closed before the write
+ * completed. If the other end of the fd (such as in a socket, pipe, or fifo),
+ * is closed, errno will be set to 0.
+ */
+bool WriteFdExactly(int fd, const void *buf, size_t len);
+
+/* Same as WriteFdExactly, but with an implicit len = strlen(buf). */
+bool WriteStringFully(int fd, const char* str);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ADB_IO_H */
diff --git a/adb/adb_io_test.cpp b/adb/adb_io_test.cpp
new file mode 100644
index 0000000..0c69bc9
--- /dev/null
+++ b/adb/adb_io_test.cpp
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2015 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 "adb_io.h"
+
+#include <gtest/gtest.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <string>
+
+#include "base/file.h"
+
+class TemporaryFile {
+ public:
+ TemporaryFile() {
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
+ }
+ }
+
+ ~TemporaryFile() {
+ close(fd);
+ unlink(filename);
+ }
+
+ int fd;
+ char filename[1024];
+
+ private:
+ void init(const char* tmp_dir) {
+ snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
+ fd = mkstemp(filename);
+ }
+};
+
+TEST(io, ReadFdExactly_whole) {
+ const char expected[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ // Test reading the whole file.
+ char buf[sizeof(expected)] = {};
+ ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1)) << strerror(errno);
+ EXPECT_STREQ(expected, buf);
+}
+
+TEST(io, ReadFdExactly_eof) {
+ const char expected[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ // Test that not having enough data will fail.
+ char buf[sizeof(expected) + 1] = {};
+ ASSERT_FALSE(ReadFdExactly(tf.fd, buf, sizeof(buf)));
+ EXPECT_EQ(0, errno) << strerror(errno);
+}
+
+TEST(io, ReadFdExactly_partial) {
+ const char input[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ // Test reading a partial file.
+ char buf[sizeof(input) - 1] = {};
+ ASSERT_TRUE(ReadFdExactly(tf.fd, buf, sizeof(buf) - 1));
+
+ std::string expected(input);
+ expected.pop_back();
+ EXPECT_STREQ(expected.c_str(), buf);
+}
+
+TEST(io, WriteFdExactly_whole) {
+ const char expected[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ // Test writing the whole string to the file.
+ ASSERT_TRUE(WriteFdExactly(tf.fd, expected, sizeof(expected)))
+ << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
+ EXPECT_STREQ(expected, s.c_str());
+}
+
+TEST(io, WriteFdExactly_partial) {
+ const char buf[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ // Test writing a partial string to the file.
+ ASSERT_TRUE(WriteFdExactly(tf.fd, buf, sizeof(buf) - 2)) << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ std::string expected(buf);
+ expected.pop_back();
+
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
+ EXPECT_EQ(expected, s);
+}
+
+TEST(io, WriteStringFully) {
+ const char str[] = "Foobar";
+ TemporaryFile tf;
+ ASSERT_NE(-1, tf.fd);
+
+ // Test writing a partial string to the file.
+ ASSERT_TRUE(WriteStringFully(tf.fd, str)) << strerror(errno);
+ ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
+
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
+ EXPECT_STREQ(str, s.c_str());
+}
diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp
new file mode 100644
index 0000000..84b9c64
--- /dev/null
+++ b/adb/adb_listeners.cpp
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2015 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 "adb_listeners.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sysdeps.h"
+#include "transport.h"
+
+int gListenAll = 0; /* Not static because it is used in commandline.c. */
+
+alistener listener_list = {
+ .next = &listener_list,
+ .prev = &listener_list,
+};
+
+void ss_listener_event_func(int _fd, unsigned ev, void *_l)
+{
+ asocket *s;
+
+ if(ev & FDE_READ) {
+ struct sockaddr addr;
+ socklen_t alen;
+ int fd;
+
+ alen = sizeof(addr);
+ fd = adb_socket_accept(_fd, &addr, &alen);
+ if(fd < 0) return;
+
+ adb_socket_setbufsize(fd, CHUNK_SIZE);
+
+ s = create_local_socket(fd);
+ if(s) {
+ connect_to_smartsocket(s);
+ return;
+ }
+
+ adb_close(fd);
+ }
+}
+
+void listener_event_func(int _fd, unsigned ev, void* _l)
+{
+ alistener* listener = reinterpret_cast<alistener*>(_l);
+ asocket *s;
+
+ if (ev & FDE_READ) {
+ struct sockaddr addr;
+ socklen_t alen;
+ int fd;
+
+ alen = sizeof(addr);
+ fd = adb_socket_accept(_fd, &addr, &alen);
+ if (fd < 0) {
+ return;
+ }
+
+ s = create_local_socket(fd);
+ if (s) {
+ s->transport = listener->transport;
+ connect_to_remote(s, listener->connect_to);
+ return;
+ }
+
+ adb_close(fd);
+ }
+}
+
+static void free_listener(alistener* l)
+{
+ if (l->next) {
+ l->next->prev = l->prev;
+ l->prev->next = l->next;
+ l->next = l->prev = l;
+ }
+
+ // closes the corresponding fd
+ fdevent_remove(&l->fde);
+
+ if (l->local_name)
+ free((char*)l->local_name);
+
+ if (l->connect_to)
+ free((char*)l->connect_to);
+
+ if (l->transport) {
+ remove_transport_disconnect(l->transport, &l->disconnect);
+ }
+ free(l);
+}
+
+void listener_disconnect(void* listener, atransport* t)
+{
+ free_listener(reinterpret_cast<alistener*>(listener));
+}
+
+int local_name_to_fd(const char *name)
+{
+ int port;
+
+ if(!strncmp("tcp:", name, 4)){
+ int ret;
+ port = atoi(name + 4);
+
+ if (gListenAll > 0) {
+ ret = socket_inaddr_any_server(port, SOCK_STREAM);
+ } else {
+ ret = socket_loopback_server(port, SOCK_STREAM);
+ }
+
+ return ret;
+ }
+#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
+ // It's non-sensical to support the "reserved" space on the adb host side
+ if(!strncmp(name, "local:", 6)) {
+ return socket_local_server(name + 6,
+ ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+ } else if(!strncmp(name, "localabstract:", 14)) {
+ return socket_local_server(name + 14,
+ ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+ } else if(!strncmp(name, "localfilesystem:", 16)) {
+ return socket_local_server(name + 16,
+ ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
+ }
+
+#endif
+ printf("unknown local portname '%s'\n", name);
+ return -1;
+}
+
+// Write a single line describing a listener to a user-provided buffer.
+// Appends a trailing zero, even in case of truncation, but the function
+// returns the full line length.
+// If |buffer| is NULL, does not write but returns required size.
+static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
+ // Format is simply:
+ //
+ // <device-serial> " " <local-name> " " <remote-name> "\n"
+ //
+ int local_len = strlen(l->local_name);
+ int connect_len = strlen(l->connect_to);
+ int serial_len = strlen(l->transport->serial);
+
+ if (buffer != NULL) {
+ snprintf(buffer, buffer_len, "%s %s %s\n",
+ l->transport->serial, l->local_name, l->connect_to);
+ }
+ // NOTE: snprintf() on Windows returns -1 in case of truncation, so
+ // return the computed line length instead.
+ return local_len + connect_len + serial_len + 3;
+}
+
+// Write the list of current listeners (network redirections) into a
+// user-provided buffer. Appends a trailing zero, even in case of
+// trunctaion, but return the full size in bytes.
+// If |buffer| is NULL, does not write but returns required size.
+int format_listeners(char* buf, size_t buflen)
+{
+ alistener* l;
+ int result = 0;
+ for (l = listener_list.next; l != &listener_list; l = l->next) {
+ // Ignore special listeners like those for *smartsocket*
+ if (l->connect_to[0] == '*')
+ continue;
+ int len = format_listener(l, buf, buflen);
+ // Ensure there is space for the trailing zero.
+ result += len;
+ if (buf != NULL) {
+ buf += len;
+ buflen -= len;
+ if (buflen <= 0)
+ break;
+ }
+ }
+ return result;
+}
+
+int remove_listener(const char *local_name, atransport* transport)
+{
+ alistener *l;
+
+ for (l = listener_list.next; l != &listener_list; l = l->next) {
+ if (!strcmp(local_name, l->local_name)) {
+ listener_disconnect(l, l->transport);
+ return 0;
+ }
+ }
+ return -1;
+}
+
+void remove_all_listeners(void)
+{
+ alistener *l, *l_next;
+ for (l = listener_list.next; l != &listener_list; l = l_next) {
+ l_next = l->next;
+ // Never remove smart sockets.
+ if (l->connect_to[0] == '*')
+ continue;
+ listener_disconnect(l, l->transport);
+ }
+}
+
+install_status_t install_listener(const char *local_name,
+ const char *connect_to,
+ atransport* transport,
+ int no_rebind)
+{
+ for (alistener* l = listener_list.next; l != &listener_list; l = l->next) {
+ if (strcmp(local_name, l->local_name) == 0) {
+ char* cto;
+
+ /* can't repurpose a smartsocket */
+ if(l->connect_to[0] == '*') {
+ return INSTALL_STATUS_INTERNAL_ERROR;
+ }
+
+ /* can't repurpose a listener if 'no_rebind' is true */
+ if (no_rebind) {
+ return INSTALL_STATUS_CANNOT_REBIND;
+ }
+
+ cto = strdup(connect_to);
+ if(cto == 0) {
+ return INSTALL_STATUS_INTERNAL_ERROR;
+ }
+
+ free((void*) l->connect_to);
+ l->connect_to = cto;
+ if (l->transport != transport) {
+ remove_transport_disconnect(l->transport, &l->disconnect);
+ l->transport = transport;
+ add_transport_disconnect(l->transport, &l->disconnect);
+ }
+ return INSTALL_STATUS_OK;
+ }
+ }
+
+ alistener* listener = reinterpret_cast<alistener*>(
+ calloc(1, sizeof(alistener)));
+ if (listener == nullptr) {
+ goto nomem;
+ }
+
+ listener->local_name = strdup(local_name);
+ if (listener->local_name == nullptr) {
+ goto nomem;
+ }
+
+ listener->connect_to = strdup(connect_to);
+ if (listener->connect_to == nullptr) {
+ goto nomem;
+ }
+
+ listener->fd = local_name_to_fd(local_name);
+ if (listener->fd < 0) {
+ free(listener->local_name);
+ free(listener->connect_to);
+ free(listener);
+ printf("cannot bind '%s'\n", local_name);
+ return INSTALL_STATUS_CANNOT_BIND;
+ }
+
+ close_on_exec(listener->fd);
+ if (!strcmp(listener->connect_to, "*smartsocket*")) {
+ fdevent_install(&listener->fde, listener->fd, ss_listener_event_func,
+ listener);
+ } else {
+ fdevent_install(&listener->fde, listener->fd, listener_event_func,
+ listener);
+ }
+ fdevent_set(&listener->fde, FDE_READ);
+
+ listener->next = &listener_list;
+ listener->prev = listener_list.prev;
+ listener->next->prev = listener;
+ listener->prev->next = listener;
+ listener->transport = transport;
+
+ if (transport) {
+ listener->disconnect.opaque = listener;
+ listener->disconnect.func = listener_disconnect;
+ add_transport_disconnect(transport, &listener->disconnect);
+ }
+ return INSTALL_STATUS_OK;
+
+nomem:
+ fatal("cannot allocate listener");
+ return INSTALL_STATUS_INTERNAL_ERROR;
+}
diff --git a/adb/adb_listeners.h b/adb/adb_listeners.h
new file mode 100644
index 0000000..14fdcd6
--- /dev/null
+++ b/adb/adb_listeners.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2015 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 __ADB_LISTENERS_H
+#define __ADB_LISTENERS_H
+
+#include "adb.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// error/status codes for install_listener.
+typedef enum {
+ INSTALL_STATUS_OK = 0,
+ INSTALL_STATUS_INTERNAL_ERROR = -1,
+ INSTALL_STATUS_CANNOT_BIND = -2,
+ INSTALL_STATUS_CANNOT_REBIND = -3,
+} install_status_t;
+
+extern alistener listener_list;
+
+void listener_disconnect(void* _l, atransport* t);
+void listener_event_func(int _fd, unsigned ev, void *_l);
+void ss_listener_event_func(int _fd, unsigned ev, void *_l);
+
+install_status_t install_listener(const char *local_name,
+ const char *connect_to,
+ atransport* transport,
+ int no_rebind);
+
+int format_listeners(char* buf, size_t buflen);
+
+int remove_listener(const char *local_name, atransport* transport);
+void remove_all_listeners(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADB_LISTENERS_H */
diff --git a/adb/adb_main.cpp b/adb/adb_main.cpp
new file mode 100644
index 0000000..1d9cc3b
--- /dev/null
+++ b/adb/adb_main.cpp
@@ -0,0 +1,425 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#define TRACE_TAG TRACE_ADB
+
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sysdeps.h"
+
+#include "adb.h"
+#include "adb_auth.h"
+#include "adb_listeners.h"
+#include "transport.h"
+
+#if !ADB_HOST
+#include <getopt.h>
+#include <sys/prctl.h>
+
+#include "cutils/properties.h"
+#include "private/android_filesystem_config.h"
+#include "selinux/selinux.h"
+
+#include "qemu_tracing.h"
+#endif
+
+static void adb_cleanup(void)
+{
+ usb_cleanup();
+}
+
+#if defined(_WIN32)
+static BOOL WINAPI ctrlc_handler(DWORD type)
+{
+ exit(STATUS_CONTROL_C_EXIT);
+ return TRUE;
+}
+#endif
+
+#if ADB_HOST
+#ifdef WORKAROUND_BUG6558362
+#include <sched.h>
+#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
+void adb_set_affinity(void)
+{
+ cpu_set_t cpu_set;
+ const char* cpunum_str = getenv(AFFINITY_ENVVAR);
+ char* strtol_res;
+ int cpu_num;
+
+ if (!cpunum_str || !*cpunum_str)
+ return;
+ cpu_num = strtol(cpunum_str, &strtol_res, 0);
+ if (*strtol_res != '\0')
+ fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
+
+ sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
+ D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
+ CPU_ZERO(&cpu_set);
+ CPU_SET(cpu_num, &cpu_set);
+ sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
+ sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
+ D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
+}
+#endif
+#else /* ADB_HOST */
+static const char *root_seclabel = NULL;
+
+static void drop_capabilities_bounding_set_if_needed() {
+#ifdef ALLOW_ADBD_ROOT
+ char value[PROPERTY_VALUE_MAX];
+ property_get("ro.debuggable", value, "");
+ if (strcmp(value, "1") == 0) {
+ return;
+ }
+#endif
+ int i;
+ for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
+ if (i == CAP_SETUID || i == CAP_SETGID) {
+ // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
+ continue;
+ }
+ int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
+
+ // Some kernels don't have file capabilities compiled in, and
+ // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
+ // die when we see such misconfigured kernels.
+ if ((err < 0) && (errno != EINVAL)) {
+ exit(1);
+ }
+ }
+}
+
+static bool should_drop_privileges() {
+#if defined(ALLOW_ADBD_ROOT)
+ char value[PROPERTY_VALUE_MAX];
+
+ // The properties that affect `adb root` and `adb unroot` are ro.secure and
+ // ro.debuggable. In this context the names don't make the expected behavior
+ // particularly obvious.
+ //
+ // ro.debuggable:
+ // Allowed to become root, but not necessarily the default. Set to 1 on
+ // eng and userdebug builds.
+ //
+ // ro.secure:
+ // Drop privileges by default. Set to 1 on userdebug and user builds.
+ property_get("ro.secure", value, "1");
+ bool ro_secure = (strcmp(value, "1") == 0);
+
+ property_get("ro.debuggable", value, "");
+ bool ro_debuggable = (strcmp(value, "1") == 0);
+
+ // Drop privileges if ro.secure is set...
+ bool drop = ro_secure;
+
+ property_get("service.adb.root", value, "");
+ bool adb_root = (strcmp(value, "1") == 0);
+ bool adb_unroot = (strcmp(value, "0") == 0);
+
+ // ...except "adb root" lets you keep privileges in a debuggable build.
+ if (ro_debuggable && adb_root) {
+ drop = false;
+ }
+
+ // ...and "adb unroot" lets you explicitly drop privileges.
+ if (adb_unroot) {
+ drop = true;
+ }
+
+ return drop;
+#else
+ return true; // "adb root" not allowed, always drop privileges.
+#endif /* ALLOW_ADBD_ROOT */
+}
+
+void start_device_log(void)
+{
+ int fd;
+ char path[PATH_MAX];
+ struct tm now;
+ time_t t;
+ char value[PROPERTY_VALUE_MAX];
+
+ // read the trace mask from persistent property persist.adb.trace_mask
+ // give up if the property is not set or cannot be parsed
+ property_get("persist.adb.trace_mask", value, "");
+ if (sscanf(value, "%x", &adb_trace_mask) != 1)
+ return;
+
+ adb_mkdir("/data/adb", 0775);
+ tzset();
+ time(&t);
+ localtime_r(&t, &now);
+ strftime(path, sizeof(path),
+ "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
+ &now);
+ fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+ if (fd < 0)
+ return;
+
+ // redirect stdout and stderr to the log file
+ dup2(fd, 1);
+ dup2(fd, 2);
+ fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
+ adb_close(fd);
+
+ fd = unix_open("/dev/null", O_RDONLY);
+ dup2(fd, 0);
+ adb_close(fd);
+}
+#endif /* ADB_HOST */
+
+/* Constructs a local name of form tcp:port.
+ * target_str points to the target string, it's content will be overwritten.
+ * target_size is the capacity of the target string.
+ * server_port is the port number to use for the local name.
+ */
+void build_local_name(char* target_str, size_t target_size, int server_port)
+{
+ snprintf(target_str, target_size, "tcp:%d", server_port);
+}
+
+void start_logging(void)
+{
+#if defined(_WIN32)
+ char temp[ MAX_PATH ];
+ FILE* fnul;
+ FILE* flog;
+
+ GetTempPath( sizeof(temp) - 8, temp );
+ strcat( temp, "adb.log" );
+
+ /* Win32 specific redirections */
+ fnul = fopen( "NUL", "rt" );
+ if (fnul != NULL)
+ stdin[0] = fnul[0];
+
+ flog = fopen( temp, "at" );
+ if (flog == NULL)
+ flog = fnul;
+
+ setvbuf( flog, NULL, _IONBF, 0 );
+
+ stdout[0] = flog[0];
+ stderr[0] = flog[0];
+ fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
+#else
+ int fd;
+
+ fd = unix_open("/dev/null", O_RDONLY);
+ dup2(fd, 0);
+ adb_close(fd);
+
+ fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
+ if(fd < 0) {
+ fd = unix_open("/dev/null", O_WRONLY);
+ }
+ dup2(fd, 1);
+ dup2(fd, 2);
+ adb_close(fd);
+ fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
+#endif
+}
+
+int adb_main(int is_daemon, int server_port)
+{
+#if !ADB_HOST
+ int port;
+ char value[PROPERTY_VALUE_MAX];
+
+ umask(000);
+#endif
+
+ atexit(adb_cleanup);
+#if defined(_WIN32)
+ SetConsoleCtrlHandler( ctrlc_handler, TRUE );
+#else
+ // No SIGCHLD. Let the service subproc handle its children.
+ signal(SIGPIPE, SIG_IGN);
+#endif
+
+ init_transport_registration();
+
+#if ADB_HOST
+ HOST = 1;
+
+#ifdef WORKAROUND_BUG6558362
+ if(is_daemon) adb_set_affinity();
+#endif
+ usb_init();
+ local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
+ adb_auth_init();
+
+ char local_name[30];
+ build_local_name(local_name, sizeof(local_name), server_port);
+ if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
+ exit(1);
+ }
+#else
+ // We need to call this even if auth isn't enabled because the file
+ // descriptor will always be open.
+ adbd_cloexec_auth_socket();
+
+ property_get("ro.adb.secure", value, "0");
+ auth_enabled = !strcmp(value, "1");
+ if (auth_enabled)
+ adbd_auth_init();
+
+ // Our external storage path may be different than apps, since
+ // we aren't able to bind mount after dropping root.
+ const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
+ if (NULL != adb_external_storage) {
+ setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
+ } else {
+ D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
+ " unchanged.\n");
+ }
+
+ /* add extra groups:
+ ** AID_ADB to access the USB driver
+ ** AID_LOG to read system logs (adb logcat)
+ ** AID_INPUT to diagnose input issues (getevent)
+ ** AID_INET to diagnose network issues (ping)
+ ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
+ ** AID_SDCARD_R to allow reading from the SD card
+ ** AID_SDCARD_RW to allow writing to the SD card
+ ** AID_NET_BW_STATS to read out qtaguid statistics
+ */
+ gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
+ AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
+ AID_NET_BW_STATS };
+ if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
+ exit(1);
+ }
+
+ /* don't listen on a port (default 5037) if running in secure mode */
+ /* don't run as root if we are running in secure mode */
+ if (should_drop_privileges()) {
+ drop_capabilities_bounding_set_if_needed();
+
+ /* then switch user and group to "shell" */
+ if (setgid(AID_SHELL) != 0) {
+ exit(1);
+ }
+ if (setuid(AID_SHELL) != 0) {
+ exit(1);
+ }
+
+ D("Local port disabled\n");
+ } else {
+ char local_name[30];
+ if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
+ // b/12587913: fix setcon to allow const pointers
+ if (setcon((char *)root_seclabel) < 0) {
+ exit(1);
+ }
+ }
+ build_local_name(local_name, sizeof(local_name), server_port);
+ if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
+ exit(1);
+ }
+ }
+
+ int usb = 0;
+ if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
+ // listen on USB
+ usb_init();
+ usb = 1;
+ }
+
+ // If one of these properties is set, also listen on that port
+ // If one of the properties isn't set and we couldn't listen on usb,
+ // listen on the default port.
+ property_get("service.adb.tcp.port", value, "");
+ if (!value[0]) {
+ property_get("persist.adb.tcp.port", value, "");
+ }
+ if (sscanf(value, "%d", &port) == 1 && port > 0) {
+ printf("using port=%d\n", port);
+ // listen on TCP port specified by service.adb.tcp.port property
+ local_init(port);
+ } else if (!usb) {
+ // listen on default port
+ local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
+ }
+
+ D("adb_main(): pre init_jdwp()\n");
+ init_jdwp();
+ D("adb_main(): post init_jdwp()\n");
+#endif
+
+ if (is_daemon)
+ {
+ // inform our parent that we are up and running.
+#if defined(_WIN32)
+ DWORD count;
+ WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
+#else
+ fprintf(stderr, "OK\n");
+#endif
+ start_logging();
+ }
+ D("Event loop starting\n");
+
+ fdevent_loop();
+
+ usb_cleanup();
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+#if ADB_HOST
+ adb_sysdeps_init();
+ adb_trace_init();
+ D("Handling commandline()\n");
+ return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
+#else
+ /* If adbd runs inside the emulator this will enable adb tracing via
+ * adb-debug qemud service in the emulator. */
+ adb_qemu_trace_init();
+ while(1) {
+ int c;
+ int option_index = 0;
+ static struct option opts[] = {
+ {"root_seclabel", required_argument, 0, 's' },
+ {"device_banner", required_argument, 0, 'b' }
+ };
+ c = getopt_long(argc, argv, "", opts, &option_index);
+ if (c == -1)
+ break;
+ switch (c) {
+ case 's':
+ root_seclabel = optarg;
+ break;
+ case 'b':
+ adb_device_banner = optarg;
+ break;
+ default:
+ break;
+ }
+ }
+
+ start_device_log();
+ D("Handling main()\n");
+ return adb_main(0, DEFAULT_ADB_PORT);
+#endif
+}
diff --git a/adb/adb_trace.h b/adb/adb_trace.h
new file mode 100644
index 0000000..ef5dc24
--- /dev/null
+++ b/adb/adb_trace.h
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2014 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 __ADB_TRACE_H
+#define __ADB_TRACE_H
+
+#if !ADB_HOST
+#include <android/log.h>
+#else
+#include <stdio.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
+#define ADB_TRACE 1
+
+/* IMPORTANT: if you change the following list, don't
+ * forget to update the corresponding 'tags' table in
+ * the adb_trace_init() function implemented in adb.c
+ */
+typedef enum {
+ TRACE_ADB = 0, /* 0x001 */
+ TRACE_SOCKETS,
+ TRACE_PACKETS,
+ TRACE_TRANSPORT,
+ TRACE_RWX, /* 0x010 */
+ TRACE_USB,
+ TRACE_SYNC,
+ TRACE_SYSDEPS,
+ TRACE_JDWP, /* 0x100 */
+ TRACE_SERVICES,
+ TRACE_AUTH,
+ TRACE_FDEVENT,
+} AdbTrace;
+
+#if ADB_TRACE
+
+#if !ADB_HOST
+/*
+ * When running inside the emulator, guest's adbd can connect to 'adb-debug'
+ * qemud service that can display adb trace messages (on condition that emulator
+ * has been started with '-debug adb' option).
+ */
+
+/* Delivers a trace message to the emulator via QEMU pipe. */
+void adb_qemu_trace(const char* fmt, ...);
+/* Macro to use to send ADB trace messages to the emulator. */
+#define DQ(...) adb_qemu_trace(__VA_ARGS__)
+#else
+#define DQ(...) ((void)0)
+#endif /* !ADB_HOST */
+
+extern int adb_trace_mask;
+extern unsigned char adb_trace_output_count;
+void adb_trace_init(void);
+
+# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
+
+/* you must define TRACE_TAG before using this macro */
+#if ADB_HOST
+# define D(...) \
+ do { \
+ if (ADB_TRACING) { \
+ int save_errno = errno; \
+ adb_mutex_lock(&D_lock); \
+ fprintf(stderr, "%16s: %5d:%5lu | ", \
+ __FUNCTION__, \
+ getpid(), adb_thread_id()); \
+ errno = save_errno; \
+ fprintf(stderr, __VA_ARGS__ ); \
+ fflush(stderr); \
+ adb_mutex_unlock(&D_lock); \
+ errno = save_errno; \
+ } \
+ } while (0)
+# define DR(...) \
+ do { \
+ if (ADB_TRACING) { \
+ int save_errno = errno; \
+ adb_mutex_lock(&D_lock); \
+ errno = save_errno; \
+ fprintf(stderr, __VA_ARGS__ ); \
+ fflush(stderr); \
+ adb_mutex_unlock(&D_lock); \
+ errno = save_errno; \
+ } \
+ } while (0)
+# define DD(...) \
+ do { \
+ int save_errno = errno; \
+ adb_mutex_lock(&D_lock); \
+ fprintf(stderr, "%16s: %5d:%5lu | ", \
+ __FUNCTION__, \
+ getpid(), adb_thread_id()); \
+ errno = save_errno; \
+ fprintf(stderr, __VA_ARGS__ ); \
+ fflush(stderr); \
+ adb_mutex_unlock(&D_lock); \
+ errno = save_errno; \
+ } while (0)
+#else
+# define D(...) \
+ do { \
+ if (ADB_TRACING) { \
+ __android_log_print( \
+ ANDROID_LOG_INFO, \
+ __FUNCTION__, \
+ __VA_ARGS__ ); \
+ } \
+ } while (0)
+# define DR(...) \
+ do { \
+ if (ADB_TRACING) { \
+ __android_log_print( \
+ ANDROID_LOG_INFO, \
+ __FUNCTION__, \
+ __VA_ARGS__ ); \
+ } \
+ } while (0)
+# define DD(...) \
+ do { \
+ __android_log_print( \
+ ANDROID_LOG_INFO, \
+ __FUNCTION__, \
+ __VA_ARGS__ ); \
+ } while (0)
+#endif /* ADB_HOST */
+#else
+# define D(...) ((void)0)
+# define DR(...) ((void)0)
+# define DD(...) ((void)0)
+# define ADB_TRACING 0
+#endif /* ADB_TRACE */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADB_TRACE_H */
diff --git a/adb/backup_service.c b/adb/backup_service.c
deleted file mode 100644
index 669ff86..0000000
--- a/adb/backup_service.c
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * 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.
- */
-
-#include <unistd.h>
-#include <stdio.h>
-
-#include "sysdeps.h"
-
-#define TRACE_TAG TRACE_ADB
-#include "adb.h"
-
-typedef struct {
- pid_t pid;
- int fd;
-} backup_harvest_params;
-
-// socketpair but do *not* mark as close_on_exec
-static int backup_socketpair(int sv[2]) {
- int rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
- if (rc < 0)
- return -1;
-
- return 0;
-}
-
-// harvest the child process then close the read end of the socketpair
-static void* backup_child_waiter(void* args) {
- int status;
- backup_harvest_params* params = (backup_harvest_params*) args;
-
- waitpid(params->pid, &status, 0);
- adb_close(params->fd);
- free(params);
- return NULL;
-}
-
-/* returns the data socket passing the backup data here for forwarding */
-int backup_service(BackupOperation op, char* args) {
- pid_t pid;
- int s[2];
- char* operation;
- int socketnum;
-
- // Command string and choice of stdin/stdout for the pipe depend on our invocation
- if (op == BACKUP) {
- operation = "backup";
- socketnum = STDOUT_FILENO;
- } else {
- operation = "restore";
- socketnum = STDIN_FILENO;
- }
-
- D("backup_service(%s, %s)\n", operation, args);
-
- // set up the pipe from the subprocess to here
- // parent will read s[0]; child will write s[1]
- if (backup_socketpair(s)) {
- D("can't create backup/restore socketpair\n");
- fprintf(stderr, "unable to create backup/restore socketpair\n");
- return -1;
- }
-
- D("Backup/restore socket pair: (send=%d, receive=%d)\n", s[1], s[0]);
- close_on_exec(s[0]); // only the side we hold on to
-
- // spin off the child process to run the backup command
- pid = fork();
- if (pid < 0) {
- // failure
- D("can't fork for %s\n", operation);
- fprintf(stderr, "unable to fork for %s\n", operation);
- adb_close(s[0]);
- adb_close(s[1]);
- return -1;
- }
-
- // Great, we're off and running.
- if (pid == 0) {
- // child -- actually run the backup here
- char* p;
- int argc;
- char portnum[16];
- char** bu_args;
-
- // fixed args: [0] is 'bu', [1] is the port number, [2] is the 'operation' string
- argc = 3;
- for (p = (char*)args; p && *p; ) {
- argc++;
- while (*p && *p != ':') p++;
- if (*p == ':') p++;
- }
-
- bu_args = (char**) alloca(argc*sizeof(char*) + 1);
-
- // run through again to build the argv array
- argc = 0;
- bu_args[argc++] = "bu";
- snprintf(portnum, sizeof(portnum), "%d", s[1]);
- bu_args[argc++] = portnum;
- bu_args[argc++] = operation;
- for (p = (char*)args; p && *p; ) {
- bu_args[argc++] = p;
- while (*p && *p != ':') p++;
- if (*p == ':') {
- *p = 0;
- p++;
- }
- }
- bu_args[argc] = NULL;
-
- // Close the half of the socket that we don't care about, route 'bu's console
- // to the output socket, and off we go
- adb_close(s[0]);
-
- // off we go
- execvp("/system/bin/bu", (char * const *)bu_args);
- // oops error - close up shop and go home
- fprintf(stderr, "Unable to exec 'bu', bailing\n");
- exit(-1);
- } else {
- adb_thread_t t;
- backup_harvest_params* params;
-
- // parent, i.e. adbd -- close the sending half of the socket
- D("fork() returned pid %d\n", pid);
- adb_close(s[1]);
-
- // spin a thread to harvest the child process
- params = (backup_harvest_params*) malloc(sizeof(backup_harvest_params));
- params->pid = pid;
- params->fd = s[0];
- if (adb_thread_create(&t, backup_child_waiter, params)) {
- adb_close(s[0]);
- free(params);
- D("Unable to create child harvester\n");
- return -1;
- }
- }
-
- // we'll be reading from s[0] as the data is sent by the child process
- return s[0];
-}
diff --git a/adb/commandline.c b/adb/commandline.cpp
similarity index 63%
rename from adb/commandline.c
rename to adb/commandline.cpp
index c9bb437..4538b04 100644
--- a/adb/commandline.c
+++ b/adb/commandline.cpp
@@ -14,53 +14,57 @@
* limitations under the License.
*/
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <limits.h>
-#include <stdarg.h>
-#include <sys/types.h>
#include <sys/stat.h>
-#include <ctype.h>
-#include <assert.h>
+#include <sys/types.h>
+
+#if !defined(_WIN32)
+#include <termios.h>
+#include <unistd.h>
+#endif
#include "sysdeps.h"
-#ifdef HAVE_TERMIO_H
-#include <termios.h>
-#endif
-
#define TRACE_TAG TRACE_ADB
#include "adb.h"
+#include "adb_auth.h"
#include "adb_client.h"
+#include "adb_io.h"
#include "file_sync_service.h"
-static int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
+static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...);
-void get_my_path(char *s, size_t maxLen);
int find_sync_dirs(const char *srcarg,
- char **android_srcdir_out, char **data_srcdir_out);
-int install_app(transport_type transport, char* serial, int argc, char** argv);
-int uninstall_app(transport_type transport, char* serial, int argc, char** argv);
+ char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out,
+ char **oem_srcdir_out);
+int install_app(transport_type transport, const char* serial, int argc,
+ const char** argv);
+int install_multiple_app(transport_type transport, const char* serial, int argc,
+ const char** argv);
+int uninstall_app(transport_type transport, const char* serial, int argc,
+ const char** argv);
static const char *gProductOutPath = NULL;
extern int gListenAll;
static char *product_file(const char *extra)
{
- int n;
- char *x;
-
if (gProductOutPath == NULL) {
fprintf(stderr, "adb: Product directory not specified; "
"use -p or define ANDROID_PRODUCT_OUT\n");
exit(1);
}
- n = strlen(gProductOutPath) + strlen(extra) + 2;
- x = malloc(n);
+ int n = strlen(gProductOutPath) + strlen(extra) + 2;
+ char* x = reinterpret_cast<char*>(malloc(n));
if (x == 0) {
fprintf(stderr, "adb: Out of memory (product_file())\n");
exit(1);
@@ -107,8 +111,13 @@
" will disconnect from all connected TCP/IP devices.\n"
"\n"
"device commands:\n"
- " adb push <local> <remote> - copy file/dir to device\n"
- " adb pull <remote> [<local>] - copy file/dir from device\n"
+ " adb push [-p] <local> <remote>\n"
+ " - copy file/dir to device\n"
+ " ('-p' to display the transfer progress)\n"
+ " adb pull [-p] [-a] <remote> [<local>]\n"
+ " - copy file/dir from device\n"
+ " ('-p' to display the transfer progress)\n"
+ " ('-a' means copy timestamp and mode)\n"
" adb sync [ <directory> ] - copy host->device only if changed\n"
" (-l means list but don't copy)\n"
" (see 'adb help all')\n"
@@ -132,13 +141,29 @@
" if <local> is already forwarded\n"
" adb forward --remove <local> - remove a specific forward socket connection\n"
" adb forward --remove-all - remove all forward socket connections\n"
+ " adb reverse --list - list all reverse socket connections from device\n"
+ " adb reverse <remote> <local> - reverse socket connections\n"
+ " reverse specs are one of:\n"
+ " tcp:<port>\n"
+ " localabstract:<unix domain socket name>\n"
+ " localreserved:<unix domain socket name>\n"
+ " localfilesystem:<unix domain socket name>\n"
+ " adb reverse --norebind <remote> <local>\n"
+ " - same as 'adb reverse <remote> <local>' but fails\n"
+ " if <remote> is already reversed.\n"
+ " adb reverse --remove <remote>\n"
+ " - remove a specific reversed socket connection\n"
+ " adb reverse --remove-all - remove all reversed socket connections from device\n"
" adb jdwp - list PIDs of processes hosting a JDWP transport\n"
- " adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --iv <hex-encoded iv>] <file>\n"
+ " adb install [-lrtsd] <file>\n"
+ " adb install-multiple [-lrtsdp] <file...>\n"
" - push this package file to the device and install it\n"
- " ('-l' means forward-lock the app)\n"
- " ('-r' means reinstall the app, keeping its data)\n"
- " ('-s' means install on SD card instead of internal storage)\n"
- " ('--algo', '--key', and '--iv' mean the file is encrypted already)\n"
+ " (-l: forward lock application)\n"
+ " (-r: replace existing application)\n"
+ " (-t: allow test packages)\n"
+ " (-s: install application on sdcard)\n"
+ " (-d: allow version code downgrade)\n"
+ " (-p: partial application install)\n"
" adb uninstall [-k] <package> - remove this app package from the device\n"
" ('-k' means keep the data and cache directories)\n"
" adb bugreport - return all information from the device\n"
@@ -166,6 +191,11 @@
"\n"
" adb restore <file> - restore device contents from the <file> backup archive\n"
"\n"
+ " adb disable-verity - disable dm-verity checking on USERDEBUG builds\n"
+ " adb enable-verity - re-enable dm-verity checking on USERDEBUG builds\n"
+ " adb keygen <file> - generate adb public/private key. The private key is stored in <file>,\n"
+ " and the public key is stored in <file>.pub. Any existing files\n"
+ " are overwritten.\n"
" adb help - show this help message\n"
" adb version - show version num\n"
"\n"
@@ -177,13 +207,13 @@
" adb get-serialno - prints: <serial-number>\n"
" adb get-devpath - prints: <device-path>\n"
" adb status-window - continuously print device status for a specified device\n"
- " adb remount - remounts the /system partition on the device read-write\n"
+ " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n"
" adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n"
" adb reboot-bootloader - reboots the device into the bootloader\n"
" adb root - restarts the adbd daemon with root permissions\n"
+ " adb unroot - restarts the adbd daemon without root permissions\n"
" adb usb - restarts the adbd daemon listening on USB\n"
- " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port"
- "\n"
+ " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port\n"
"networking:\n"
" adb ppp <tty> [parameters] - Run PPP over USB.\n"
" Note: you should not automatically start a PPP connection.\n"
@@ -193,9 +223,9 @@
"adb sync notes: adb sync [ <directory> ]\n"
" <localdir> can be interpreted in several ways:\n"
"\n"
- " - If <directory> is not specified, both /system and /data partitions will be updated.\n"
+ " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n"
"\n"
- " - If it is \"system\" or \"data\", only the corresponding partition\n"
+ " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n"
" is updated.\n"
"\n"
"environmental variables:\n"
@@ -212,7 +242,18 @@
return 1;
}
-#ifdef HAVE_TERMIO_H
+#if defined(_WIN32)
+
+// Windows does not have <termio.h>.
+static void stdin_raw_init(int fd) {
+
+}
+
+static void stdin_raw_restore(int fd) {
+
+}
+
+#else
static struct termios tio_save;
static void stdin_raw_init(int fd)
@@ -261,6 +302,24 @@
}
}
+static void read_status_line(int fd, char* buf, size_t count)
+{
+ count--;
+ while (count > 0) {
+ int len = adb_read(fd, buf, count);
+ if (len == 0) {
+ break;
+ } else if (len < 0) {
+ if (errno == EINTR) continue;
+ break;
+ }
+
+ buf += len;
+ count -= len;
+ }
+ *buf = '\0';
+}
+
static void copy_to_file(int inFd, int outFd) {
const size_t BUFSIZE = 32 * 1024;
char* buf = (char*) malloc(BUFSIZE);
@@ -268,8 +327,17 @@
long total = 0;
D("copy_to_file(%d -> %d)\n", inFd, outFd);
+
+ if (inFd == STDIN_FILENO) {
+ stdin_raw_init(STDIN_FILENO);
+ }
+
for (;;) {
- len = adb_read(inFd, buf, BUFSIZE);
+ if (inFd == STDIN_FILENO) {
+ len = unix_read(inFd, buf, BUFSIZE);
+ } else {
+ len = adb_read(inFd, buf, BUFSIZE);
+ }
if (len == 0) {
D("copy_to_file() : read 0 bytes; exiting\n");
break;
@@ -282,9 +350,19 @@
D("copy_to_file() : error %d\n", errno);
break;
}
- adb_write(outFd, buf, len);
+ if (outFd == STDOUT_FILENO) {
+ fwrite(buf, 1, len, stdout);
+ fflush(stdout);
+ } else {
+ adb_write(outFd, buf, len);
+ }
total += len;
}
+
+ if (inFd == STDIN_FILENO) {
+ stdin_raw_restore(STDIN_FILENO);
+ }
+
D("copy_to_file() finished after %lu bytes\n", total);
free(buf);
}
@@ -325,9 +403,7 @@
case '.':
if(state == 2) {
fprintf(stderr,"\n* disconnect *\n");
-#ifdef HAVE_TERMIO_H
stdin_raw_restore(fdi);
-#endif
exit(0);
}
default:
@@ -346,7 +422,6 @@
{
adb_thread_t thr;
int fdi, fd;
- int *fds;
fd = adb_connect("shell:");
if(fd < 0) {
@@ -355,18 +430,14 @@
}
fdi = 0; //dup(0);
- fds = malloc(sizeof(int) * 2);
+ int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2));
fds[0] = fd;
fds[1] = fdi;
-#ifdef HAVE_TERMIO_H
stdin_raw_init(fdi);
-#endif
adb_thread_create(&thr, stdin_read_thread, fds);
read_and_dump(fd);
-#ifdef HAVE_TERMIO_H
stdin_raw_restore(fdi);
-#endif
return 0;
}
@@ -392,7 +463,6 @@
char buf[4096];
unsigned total;
int fd;
- const unsigned char *ptr;
sprintf(buf,"%s:%d", service, sz);
fd = adb_connect(buf);
@@ -402,10 +472,10 @@
}
int opt = CHUNK_SIZE;
- opt = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
+ opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
total = sz;
- ptr = data;
+ const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
if(progress) {
char *x = strrchr(service, ':');
@@ -414,7 +484,7 @@
while(sz > 0) {
unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
- if(writex(fd, ptr, xfer)) {
+ if(!WriteFdExactly(fd, ptr, xfer)) {
adb_status(fd);
fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
return -1;
@@ -430,7 +500,7 @@
printf("\n");
}
- if(readx(fd, buf, 4)){
+ if(!ReadFdExactly(fd, buf, 4)){
fprintf(stderr,"* error reading response *\n");
adb_close(fd);
return -1;
@@ -463,6 +533,114 @@
return status;
}
+#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE)
+
+/*
+ * The sideload-host protocol serves the data in a file (given on the
+ * command line) to the client, using a simple protocol:
+ *
+ * - The connect message includes the total number of bytes in the
+ * file and a block size chosen by us.
+ *
+ * - The other side sends the desired block number as eight decimal
+ * digits (eg "00000023" for block 23). Blocks are numbered from
+ * zero.
+ *
+ * - We send back the data of the requested block. The last block is
+ * likely to be partial; when the last block is requested we only
+ * send the part of the block that exists, it's not padded up to the
+ * block size.
+ *
+ * - When the other side sends "DONEDONE" instead of a block number,
+ * we hang up.
+ */
+int adb_sideload_host(const char* fn) {
+ unsigned sz;
+ size_t xfer = 0;
+ int status;
+ int last_percent = -1;
+ int opt = SIDELOAD_HOST_BLOCK_SIZE;
+
+ printf("loading: '%s'", fn);
+ fflush(stdout);
+ uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz));
+ if (data == 0) {
+ printf("\n");
+ fprintf(stderr, "* cannot read '%s' *\n", fn);
+ return -1;
+ }
+
+ char buf[100];
+ sprintf(buf, "sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
+ int fd = adb_connect(buf);
+ if (fd < 0) {
+ // Try falling back to the older sideload method. Maybe this
+ // is an older device that doesn't support sideload-host.
+ printf("\n");
+ status = adb_download_buffer("sideload", fn, data, sz, 1);
+ goto done;
+ }
+
+ opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
+
+ for (;;) {
+ if (!ReadFdExactly(fd, buf, 8)) {
+ fprintf(stderr, "* failed to read command: %s\n", adb_error());
+ status = -1;
+ goto done;
+ }
+
+ if (strncmp("DONEDONE", buf, 8) == 0) {
+ status = 0;
+ break;
+ }
+
+ buf[8] = '\0';
+ int block = strtol(buf, NULL, 10);
+
+ size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
+ if (offset >= sz) {
+ fprintf(stderr, "* attempt to read past end: %s\n", adb_error());
+ status = -1;
+ goto done;
+ }
+ uint8_t* start = data + offset;
+ size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
+ size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
+ if (offset_end > sz) {
+ to_write = sz - offset;
+ }
+
+ if(!WriteFdExactly(fd, start, to_write)) {
+ adb_status(fd);
+ fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
+ status = -1;
+ goto done;
+ }
+ xfer += to_write;
+
+ // For normal OTA packages, we expect to transfer every byte
+ // twice, plus a bit of overhead (one read during
+ // verification, one read of each byte for installation, plus
+ // extra access to things like the zip central directory).
+ // This estimate of the completion becomes 100% when we've
+ // transferred ~2.13 (=100/47) times the package size.
+ int percent = (int)(xfer * 47LL / (sz ? sz : 1));
+ if (percent != last_percent) {
+ printf("\rserving: '%s' (~%d%%) ", fn, percent);
+ fflush(stdout);
+ last_percent = percent;
+ }
+ }
+
+ printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
+
+ done:
+ if (fd >= 0) adb_close(fd);
+ free(data);
+ return status;
+}
+
static void status_window(transport_type ttype, const char* serial)
{
char command[4096];
@@ -507,39 +685,45 @@
}
}
-/** duplicate string and quote all \ " ( ) chars + space character. */
-static char *
-dupAndQuote(const char *s)
+static int should_escape(const char c)
+{
+ return (c == ' ' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')');
+}
+
+/* Duplicate and escape given argument. */
+static char *escape_arg(const char *s)
{
const char *ts;
size_t alloc_len;
char *ret;
char *dest;
- ts = s;
-
alloc_len = 0;
-
- for( ;*ts != '\0'; ts++) {
+ for (ts = s; *ts != '\0'; ts++) {
alloc_len++;
- if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
+ if (should_escape(*ts)) {
alloc_len++;
}
}
- ret = (char *)malloc(alloc_len + 1);
-
- ts = s;
- dest = ret;
-
- for ( ;*ts != '\0'; ts++) {
- if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
- *dest++ = '\\';
- }
-
- *dest++ = *ts;
+ if (alloc_len == 0) {
+ // Preserve empty arguments
+ ret = (char *) malloc(3);
+ ret[0] = '\"';
+ ret[1] = '\"';
+ ret[2] = '\0';
+ return ret;
}
+ ret = (char *) malloc(alloc_len + 1);
+ dest = ret;
+
+ for (ts = s; *ts != '\0'; ts++) {
+ if (should_escape(*ts)) {
+ *dest++ = '\\';
+ }
+ *dest++ = *ts;
+ }
*dest++ = '\0';
return ret;
@@ -552,13 +736,12 @@
* ppp dev:/dev/omap_csmi_tty0 <ppp options>
*
*/
-int ppp(int argc, char **argv)
+int ppp(int argc, const char **argv)
{
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
return -1;
#else
- char *adb_service_name;
pid_t pid;
int fd;
@@ -569,8 +752,7 @@
return 1;
}
- adb_service_name = argv[1];
-
+ const char* adb_service_name = argv[1];
fd = adb_connect(adb_service_name);
if(fd < 0) {
@@ -617,10 +799,11 @@
adb_close(fd);
return 0;
}
-#endif /* !HAVE_WIN32_PROC */
+#endif /* !defined(_WIN32) */
}
-static int send_shellcommand(transport_type transport, char* serial, char* buf)
+static int send_shellcommand(transport_type transport, const char* serial,
+ char* buf)
{
int fd, ret;
@@ -641,35 +824,30 @@
return ret;
}
-static int logcat(transport_type transport, char* serial, int argc, char **argv)
+static int logcat(transport_type transport, const char* serial, int argc,
+ const char** argv)
{
char buf[4096];
char *log_tags;
- char *quoted_log_tags;
+ char *quoted;
log_tags = getenv("ANDROID_LOG_TAGS");
- quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags);
-
+ quoted = escape_arg(log_tags == NULL ? "" : log_tags);
snprintf(buf, sizeof(buf),
- "shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat",
- quoted_log_tags);
+ "shell:export ANDROID_LOG_TAGS=\"%s\"; exec logcat", quoted);
+ free(quoted);
- free(quoted_log_tags);
-
- if (!strcmp(argv[0],"longcat")) {
- strncat(buf, " -v long", sizeof(buf)-1);
+ if (!strcmp(argv[0], "longcat")) {
+ strncat(buf, " -v long", sizeof(buf) - 1);
}
argc -= 1;
argv += 1;
while(argc-- > 0) {
- char *quoted;
-
- quoted = dupAndQuote (*argv++);
-
- strncat(buf, " ", sizeof(buf)-1);
- strncat(buf, quoted, sizeof(buf)-1);
+ quoted = escape_arg(*argv++);
+ strncat(buf, " ", sizeof(buf) - 1);
+ strncat(buf, quoted, sizeof(buf) - 1);
free(quoted);
}
@@ -677,10 +855,10 @@
return 0;
}
-static int mkdirs(char *path)
+static int mkdirs(const char *path)
{
int ret;
- char *x = path + 1;
+ char *x = (char *)path + 1;
for(;;) {
x = adb_dirstart(x);
@@ -696,7 +874,7 @@
return 0;
}
-static int backup(int argc, char** argv) {
+static int backup(int argc, const char** argv) {
char buf[4096];
char default_name[32];
const char* filename = strcpy(default_name, "./backup.ab");
@@ -723,7 +901,7 @@
if (argc < 2) return usage();
adb_unlink(filename);
- mkdirs((char *)filename);
+ mkdirs(filename);
outFd = adb_creat(filename, 0640);
if (outFd < 0) {
fprintf(stderr, "adb: unable to open file %s\n", filename);
@@ -752,7 +930,7 @@
return 0;
}
-static int restore(int argc, char** argv) {
+static int restore(int argc, const char** argv) {
const char* filename;
int fd, tarFd;
@@ -921,7 +1099,36 @@
return path_buf;
}
-int adb_commandline(int argc, char **argv)
+static void parse_push_pull_args(const char **arg, int narg, char const **path1,
+ char const **path2, int *show_progress,
+ int *copy_attrs) {
+ *show_progress = 0;
+ *copy_attrs = 0;
+
+ while (narg > 0) {
+ if (!strcmp(*arg, "-p")) {
+ *show_progress = 1;
+ } else if (!strcmp(*arg, "-a")) {
+ *copy_attrs = 1;
+ } else {
+ break;
+ }
+ ++arg;
+ --narg;
+ }
+
+ if (narg > 0) {
+ *path1 = *arg;
+ ++arg;
+ --narg;
+ }
+
+ if (narg > 0) {
+ *path2 = *arg;
+ }
+}
+
+int adb_commandline(int argc, const char **argv)
{
char buf[4096];
int no_daemon = 0;
@@ -929,10 +1136,9 @@
int is_server = 0;
int persist = 0;
int r;
- int quote;
transport_type ttype = kTransportAny;
- char* serial = NULL;
- char* server_port_str = NULL;
+ const char* serial = NULL;
+ const char* server_port_str = NULL;
/* If defined, this should be an absolute path to
* the directory containing all of the various system images
@@ -962,17 +1168,17 @@
}
/* modifiers and flags */
- while(argc > 0) {
- if(!strcmp(argv[0],"server")) {
+ while (argc > 0) {
+ if (!strcmp(argv[0],"server")) {
is_server = 1;
- } else if(!strcmp(argv[0],"nodaemon")) {
+ } else if (!strcmp(argv[0],"nodaemon")) {
no_daemon = 1;
} else if (!strcmp(argv[0], "fork-server")) {
/* this is a special flag used only when the ADB client launches the ADB Server */
is_daemon = 1;
- } else if(!strcmp(argv[0],"persist")) {
+ } else if (!strcmp(argv[0],"persist")) {
persist = 1;
- } else if(!strncmp(argv[0], "-p", 2)) {
+ } else if (!strncmp(argv[0], "-p", 2)) {
const char *product = NULL;
if (argv[0][2] == '\0') {
if (argc < 2) return usage();
@@ -992,7 +1198,7 @@
if (isdigit(argv[0][2])) {
serial = argv[0] + 2;
} else {
- if(argc < 2 || argv[0][2] != '\0') return usage();
+ if (argc < 2 || argv[0][2] != '\0') return usage();
serial = argv[1];
argc--;
argv++;
@@ -1003,7 +1209,7 @@
ttype = kTransportLocal;
} else if (!strcmp(argv[0],"-a")) {
gListenAll = 1;
- } else if(!strncmp(argv[0], "-H", 2)) {
+ } else if (!strncmp(argv[0], "-H", 2)) {
const char *hostname = NULL;
if (argv[0][2] == '\0') {
if (argc < 2) return usage();
@@ -1015,7 +1221,7 @@
}
adb_set_tcp_name(hostname);
- } else if(!strncmp(argv[0], "-P", 2)) {
+ } else if (!strncmp(argv[0], "-P", 2)) {
if (argv[0][2] == '\0') {
if (argc < 2) return usage();
server_port_str = argv[1];
@@ -1054,196 +1260,19 @@
} else {
r = launch_server(server_port);
}
- if(r) {
+ if (r) {
fprintf(stderr,"* could not start server *\n");
}
return r;
}
-top:
- if(argc == 0) {
+ if (argc == 0) {
return usage();
}
- /* adb_connect() commands */
-
- if(!strcmp(argv[0], "devices")) {
- char *tmp;
- char *listopt;
- if (argc < 2)
- listopt = "";
- else if (argc == 2 && !strcmp(argv[1], "-l"))
- listopt = argv[1];
- else {
- fprintf(stderr, "Usage: adb devices [-l]\n");
- return 1;
- }
- snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
- tmp = adb_query(buf);
- if(tmp) {
- printf("List of devices attached \n");
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
-
- if(!strcmp(argv[0], "connect")) {
- char *tmp;
- if (argc != 2) {
- fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
- return 1;
- }
- snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
- tmp = adb_query(buf);
- if(tmp) {
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
-
- if(!strcmp(argv[0], "disconnect")) {
- char *tmp;
- if (argc > 2) {
- fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
- return 1;
- }
- if (argc == 2) {
- snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
- } else {
- snprintf(buf, sizeof buf, "host:disconnect:");
- }
- tmp = adb_query(buf);
- if(tmp) {
- printf("%s\n", tmp);
- return 0;
- } else {
- return 1;
- }
- }
-
- if (!strcmp(argv[0], "emu")) {
- return adb_send_emulator_command(argc, argv);
- }
-
- if(!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
- int r;
- int fd;
-
- char h = (argv[0][0] == 'h');
-
- if (h) {
- printf("\x1b[41;33m");
- fflush(stdout);
- }
-
- if(argc < 2) {
- D("starting interactive shell\n");
- r = interactive_shell();
- if (h) {
- printf("\x1b[0m");
- fflush(stdout);
- }
- return r;
- }
-
- snprintf(buf, sizeof buf, "shell:%s", argv[1]);
- argc -= 2;
- argv += 2;
- while(argc-- > 0) {
- strcat(buf, " ");
-
- /* quote empty strings and strings with spaces */
- quote = (**argv == 0 || strchr(*argv, ' '));
- if (quote)
- strcat(buf, "\"");
- strcat(buf, *argv++);
- if (quote)
- strcat(buf, "\"");
- }
-
- for(;;) {
- D("interactive shell loop. buff=%s\n", buf);
- fd = adb_connect(buf);
- if(fd >= 0) {
- D("about to read_and_dump(fd=%d)\n", fd);
- read_and_dump(fd);
- D("read_and_dump() done.\n");
- adb_close(fd);
- r = 0;
- } else {
- fprintf(stderr,"error: %s\n", adb_error());
- r = -1;
- }
-
- if(persist) {
- fprintf(stderr,"\n- waiting for device -\n");
- adb_sleep_ms(1000);
- do_cmd(ttype, serial, "wait-for-device", 0);
- } else {
- if (h) {
- printf("\x1b[0m");
- fflush(stdout);
- }
- D("interactive shell loop. return r=%d\n", r);
- return r;
- }
- }
- }
-
- if(!strcmp(argv[0], "kill-server")) {
- int fd;
- fd = _adb_connect("host:kill");
- if(fd == -1) {
- fprintf(stderr,"* server not running *\n");
- return 1;
- }
- return 0;
- }
-
- if(!strcmp(argv[0], "sideload")) {
- if(argc != 2) return usage();
- if(adb_download("sideload", argv[1], 1)) {
- return 1;
- } else {
- return 0;
- }
- }
-
- if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
- || !strcmp(argv[0], "reboot-bootloader")
- || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
- || !strcmp(argv[0], "root")) {
- char command[100];
- if (!strcmp(argv[0], "reboot-bootloader"))
- snprintf(command, sizeof(command), "reboot:bootloader");
- else if (argc > 1)
- snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
- else
- snprintf(command, sizeof(command), "%s:", argv[0]);
- int fd = adb_connect(command);
- if(fd >= 0) {
- read_and_dump(fd);
- adb_close(fd);
- return 0;
- }
- fprintf(stderr,"error: %s\n", adb_error());
- return 1;
- }
-
- if(!strcmp(argv[0], "bugreport")) {
- if (argc != 1) return usage();
- do_cmd(ttype, serial, "shell", "bugreport", 0);
- return 0;
- }
-
- /* adb_command() wrapper commands */
-
- if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
- char* service = argv[0];
+ /* handle wait-for-* prefix */
+ if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
+ const char* service = argv[0];
if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
if (ttype == kTransportUsb) {
service = "wait-for-usb";
@@ -1265,16 +1294,213 @@
/* Allow a command to be run after wait-for-device,
* e.g. 'adb wait-for-device shell'.
*/
- if(argc > 1) {
- argc--;
- argv++;
- goto top;
+ if (argc == 1) {
+ return 0;
+ }
+
+ /* Fall through */
+ argc--;
+ argv++;
+ }
+
+ /* adb_connect() commands */
+ if (!strcmp(argv[0], "devices")) {
+ char *tmp;
+ const char *listopt;
+ if (argc < 2)
+ listopt = "";
+ else if (argc == 2 && !strcmp(argv[1], "-l"))
+ listopt = argv[1];
+ else {
+ fprintf(stderr, "Usage: adb devices [-l]\n");
+ return 1;
+ }
+ snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
+ tmp = adb_query(buf);
+ if (tmp) {
+ printf("List of devices attached \n");
+ printf("%s\n", tmp);
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ else if (!strcmp(argv[0], "connect")) {
+ char *tmp;
+ if (argc != 2) {
+ fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
+ return 1;
+ }
+ snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
+ tmp = adb_query(buf);
+ if (tmp) {
+ printf("%s\n", tmp);
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ else if (!strcmp(argv[0], "disconnect")) {
+ char *tmp;
+ if (argc > 2) {
+ fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
+ return 1;
+ }
+ if (argc == 2) {
+ snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
+ } else {
+ snprintf(buf, sizeof buf, "host:disconnect:");
+ }
+ tmp = adb_query(buf);
+ if (tmp) {
+ printf("%s\n", tmp);
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ else if (!strcmp(argv[0], "emu")) {
+ return adb_send_emulator_command(argc, argv);
+ }
+ else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
+ int r;
+ int fd;
+
+ char h = (argv[0][0] == 'h');
+
+ if (h) {
+ printf("\x1b[41;33m");
+ fflush(stdout);
+ }
+
+ if (argc < 2) {
+ D("starting interactive shell\n");
+ r = interactive_shell();
+ if (h) {
+ printf("\x1b[0m");
+ fflush(stdout);
+ }
+ return r;
+ }
+
+ snprintf(buf, sizeof(buf), "shell:%s", argv[1]);
+ argc -= 2;
+ argv += 2;
+ while (argc-- > 0) {
+ char *quoted = escape_arg(*argv++);
+ strncat(buf, " ", sizeof(buf) - 1);
+ strncat(buf, quoted, sizeof(buf) - 1);
+ free(quoted);
+ }
+
+ for(;;) {
+ D("interactive shell loop. buff=%s\n", buf);
+ fd = adb_connect(buf);
+ if (fd >= 0) {
+ D("about to read_and_dump(fd=%d)\n", fd);
+ read_and_dump(fd);
+ D("read_and_dump() done.\n");
+ adb_close(fd);
+ r = 0;
+ } else {
+ fprintf(stderr,"error: %s\n", adb_error());
+ r = -1;
+ }
+
+ if (persist) {
+ fprintf(stderr,"\n- waiting for device -\n");
+ adb_sleep_ms(1000);
+ do_cmd(ttype, serial, "wait-for-device", 0);
+ } else {
+ if (h) {
+ printf("\x1b[0m");
+ fflush(stdout);
+ }
+ D("interactive shell loop. return r=%d\n", r);
+ return r;
+ }
+ }
+ }
+ else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
+ int exec_in = !strcmp(argv[0], "exec-in");
+ int fd;
+
+ snprintf(buf, sizeof buf, "exec:%s", argv[1]);
+ argc -= 2;
+ argv += 2;
+ while (argc-- > 0) {
+ char *quoted = escape_arg(*argv++);
+ strncat(buf, " ", sizeof(buf) - 1);
+ strncat(buf, quoted, sizeof(buf) - 1);
+ free(quoted);
+ }
+
+ fd = adb_connect(buf);
+ if (fd < 0) {
+ fprintf(stderr, "error: %s\n", adb_error());
+ return -1;
+ }
+
+ if (exec_in) {
+ copy_to_file(STDIN_FILENO, fd);
+ } else {
+ copy_to_file(fd, STDOUT_FILENO);
+ }
+
+ adb_close(fd);
+ return 0;
+ }
+ else if (!strcmp(argv[0], "kill-server")) {
+ int fd;
+ fd = _adb_connect("host:kill");
+ if (fd == -1) {
+ fprintf(stderr,"* server not running *\n");
+ return 1;
}
return 0;
}
-
- if(!strcmp(argv[0], "forward")) {
+ else if (!strcmp(argv[0], "sideload")) {
+ if (argc != 2) return usage();
+ if (adb_sideload_host(argv[1])) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ else if (!strcmp(argv[0], "remount") ||
+ !strcmp(argv[0], "reboot") ||
+ !strcmp(argv[0], "reboot-bootloader") ||
+ !strcmp(argv[0], "tcpip") ||
+ !strcmp(argv[0], "usb") ||
+ !strcmp(argv[0], "root") ||
+ !strcmp(argv[0], "unroot") ||
+ !strcmp(argv[0], "disable-verity") ||
+ !strcmp(argv[0], "enable-verity")) {
+ char command[100];
+ if (!strcmp(argv[0], "reboot-bootloader"))
+ snprintf(command, sizeof(command), "reboot:bootloader");
+ else if (argc > 1)
+ snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
+ else
+ snprintf(command, sizeof(command), "%s:", argv[0]);
+ int fd = adb_connect(command);
+ if (fd >= 0) {
+ read_and_dump(fd);
+ adb_close(fd);
+ return 0;
+ }
+ fprintf(stderr,"error: %s\n", adb_error());
+ return 1;
+ }
+ else if (!strcmp(argv[0], "bugreport")) {
+ if (argc != 1) return usage();
+ do_cmd(ttype, serial, "shell", "bugreport", 0);
+ return 0;
+ }
+ /* adb_command() wrapper commands */
+ else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
char host_prefix[64];
+ char reverse = (char) !strcmp(argv[0], "reverse");
char remove = 0;
char remove_all = 0;
char list = 0;
@@ -1303,15 +1529,19 @@
}
// Determine the <host-prefix> for this command.
- if (serial) {
- snprintf(host_prefix, sizeof host_prefix, "host-serial:%s",
- serial);
- } else if (ttype == kTransportUsb) {
- snprintf(host_prefix, sizeof host_prefix, "host-usb");
- } else if (ttype == kTransportLocal) {
- snprintf(host_prefix, sizeof host_prefix, "host-local");
+ if (reverse) {
+ snprintf(host_prefix, sizeof host_prefix, "reverse");
} else {
- snprintf(host_prefix, sizeof host_prefix, "host");
+ if (serial) {
+ snprintf(host_prefix, sizeof host_prefix, "host-serial:%s",
+ serial);
+ } else if (ttype == kTransportUsb) {
+ snprintf(host_prefix, sizeof host_prefix, "host-usb");
+ } else if (ttype == kTransportLocal) {
+ snprintf(host_prefix, sizeof host_prefix, "host-local");
+ } else {
+ snprintf(host_prefix, sizeof host_prefix, "host");
+ }
}
// Implement forward --list
@@ -1349,55 +1579,67 @@
{
if (argc != 3)
return usage();
- const char* command = no_rebind ? "forward:norebind:" : "forward";
+ const char* command = no_rebind ? "forward:norebind" : "forward";
snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
}
- if(adb_command(buf)) {
+ if (adb_command(buf)) {
fprintf(stderr,"error: %s\n", adb_error());
return 1;
}
return 0;
}
-
/* do_sync_*() commands */
-
- if(!strcmp(argv[0], "ls")) {
- if(argc != 2) return usage();
+ else if (!strcmp(argv[0], "ls")) {
+ if (argc != 2) return usage();
return do_sync_ls(argv[1]);
}
+ else if (!strcmp(argv[0], "push")) {
+ int show_progress = 0;
+ int copy_attrs = 0; // unused
+ const char* lpath = NULL, *rpath = NULL;
- if(!strcmp(argv[0], "push")) {
- if(argc != 3) return usage();
- return do_sync_push(argv[1], argv[2], 0 /* no verify APK */);
- }
+ parse_push_pull_args(&argv[1], argc - 1, &lpath, &rpath, &show_progress, ©_attrs);
- if(!strcmp(argv[0], "pull")) {
- if (argc == 2) {
- return do_sync_pull(argv[1], ".");
- } else if (argc == 3) {
- return do_sync_pull(argv[1], argv[2]);
- } else {
- return usage();
+ if ((lpath != NULL) && (rpath != NULL)) {
+ return do_sync_push(lpath, rpath, show_progress);
}
- }
- if(!strcmp(argv[0], "install")) {
+ return usage();
+ }
+ else if (!strcmp(argv[0], "pull")) {
+ int show_progress = 0;
+ int copy_attrs = 0;
+ const char* rpath = NULL, *lpath = ".";
+
+ parse_push_pull_args(&argv[1], argc - 1, &rpath, &lpath, &show_progress, ©_attrs);
+
+ if (rpath != NULL) {
+ return do_sync_pull(rpath, lpath, show_progress, copy_attrs);
+ }
+
+ return usage();
+ }
+ else if (!strcmp(argv[0], "install")) {
if (argc < 2) return usage();
return install_app(ttype, serial, argc, argv);
}
-
- if(!strcmp(argv[0], "uninstall")) {
+ else if (!strcmp(argv[0], "install-multiple")) {
+ if (argc < 2) return usage();
+ return install_multiple_app(ttype, serial, argc, argv);
+ }
+ else if (!strcmp(argv[0], "uninstall")) {
if (argc < 2) return usage();
return uninstall_app(ttype, serial, argc, argv);
}
+ else if (!strcmp(argv[0], "sync")) {
+ const char* srcarg;
+ char *system_srcpath, *data_srcpath, *vendor_srcpath, *oem_srcpath;
- if(!strcmp(argv[0], "sync")) {
- char *srcarg, *android_srcpath, *data_srcpath;
int listonly = 0;
int ret;
- if(argc < 2) {
+ if (argc < 2) {
/* No local path was specified. */
srcarg = NULL;
} else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
@@ -1407,28 +1649,33 @@
} else {
srcarg = NULL;
}
- } else if(argc == 2) {
+ } else if (argc == 2) {
/* A local path or "android"/"data" arg was specified. */
srcarg = argv[1];
} else {
return usage();
}
- ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath);
- if(ret != 0) return usage();
+ ret = find_sync_dirs(srcarg, &system_srcpath, &data_srcpath, &vendor_srcpath,
+ &oem_srcpath);
+ if (ret != 0) return usage();
- if(android_srcpath != NULL)
- ret = do_sync_sync(android_srcpath, "/system", listonly);
- if(ret == 0 && data_srcpath != NULL)
+ if (system_srcpath != NULL)
+ ret = do_sync_sync(system_srcpath, "/system", listonly);
+ if (ret == 0 && vendor_srcpath != NULL)
+ ret = do_sync_sync(vendor_srcpath, "/vendor", listonly);
+ if(ret == 0 && oem_srcpath != NULL)
+ ret = do_sync_sync(oem_srcpath, "/oem", listonly);
+ if (ret == 0 && data_srcpath != NULL)
ret = do_sync_sync(data_srcpath, "/data", listonly);
- free(android_srcpath);
+ free(system_srcpath);
+ free(vendor_srcpath);
+ free(oem_srcpath);
free(data_srcpath);
return ret;
}
-
/* passthrough commands */
-
- if(!strcmp(argv[0],"get-state") ||
+ else if (!strcmp(argv[0],"get-state") ||
!strcmp(argv[0],"get-serialno") ||
!strcmp(argv[0],"get-devpath"))
{
@@ -1436,42 +1683,38 @@
format_host_command(buf, sizeof buf, argv[0], ttype, serial);
tmp = adb_query(buf);
- if(tmp) {
+ if (tmp) {
printf("%s\n", tmp);
return 0;
} else {
return 1;
}
}
-
/* other commands */
-
- if(!strcmp(argv[0],"status-window")) {
+ else if (!strcmp(argv[0],"status-window")) {
status_window(ttype, serial);
return 0;
}
-
- if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
+ else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
return logcat(ttype, serial, argc, argv);
}
-
- if(!strcmp(argv[0],"ppp")) {
+ else if (!strcmp(argv[0],"ppp")) {
return ppp(argc, argv);
}
-
- if (!strcmp(argv[0], "start-server")) {
+ else if (!strcmp(argv[0], "start-server")) {
return adb_connect("host:start-server");
}
-
- if (!strcmp(argv[0], "backup")) {
+ else if (!strcmp(argv[0], "backup")) {
return backup(argc, argv);
}
-
- if (!strcmp(argv[0], "restore")) {
+ else if (!strcmp(argv[0], "restore")) {
return restore(argc, argv);
}
-
- if (!strcmp(argv[0], "jdwp")) {
+ else if (!strcmp(argv[0], "keygen")) {
+ if (argc < 2) return usage();
+ return adb_auth_keygen(argv[1]);
+ }
+ else if (!strcmp(argv[0], "jdwp")) {
int fd = adb_connect("jdwp");
if (fd >= 0) {
read_and_dump(fd);
@@ -1482,14 +1725,12 @@
return -1;
}
}
-
/* "adb /?" is a common idiom under Windows */
- if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
+ else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
help();
return 0;
}
-
- if(!strcmp(argv[0], "version")) {
+ else if (!strcmp(argv[0], "version")) {
version(stdout);
return 0;
}
@@ -1498,9 +1739,10 @@
return 1;
}
-static int do_cmd(transport_type ttype, char* serial, char *cmd, ...)
+#define MAX_ARGV_LENGTH 16
+static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...)
{
- char *argv[16];
+ const char *argv[MAX_ARGV_LENGTH];
int argc;
va_list ap;
@@ -1517,7 +1759,9 @@
}
argv[argc++] = cmd;
- while((argv[argc] = va_arg(ap, char*)) != 0) argc++;
+ while(argc < MAX_ARGV_LENGTH &&
+ (argv[argc] = va_arg(ap, char*)) != 0) argc++;
+ assert(argc < MAX_ARGV_LENGTH);
va_end(ap);
#if 0
@@ -1532,34 +1776,54 @@
}
int find_sync_dirs(const char *srcarg,
- char **android_srcdir_out, char **data_srcdir_out)
+ char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out,
+ char **oem_srcdir_out)
{
- char *android_srcdir, *data_srcdir;
+ char *system_srcdir = NULL, *data_srcdir = NULL, *vendor_srcdir = NULL, *oem_srcdir = NULL;
+ struct stat st;
if(srcarg == NULL) {
- android_srcdir = product_file("system");
+ system_srcdir = product_file("system");
data_srcdir = product_file("data");
+ vendor_srcdir = product_file("vendor");
+ oem_srcdir = product_file("oem");
+ // Check if vendor partition exists.
+ if (lstat(vendor_srcdir, &st) || !S_ISDIR(st.st_mode))
+ vendor_srcdir = NULL;
+ // Check if oem partition exists.
+ if (lstat(oem_srcdir, &st) || !S_ISDIR(st.st_mode))
+ oem_srcdir = NULL;
} else {
- /* srcarg may be "data", "system" or NULL.
- * if srcarg is NULL, then both data and system are synced
- */
+ // srcarg may be "data", "system", "vendor", "oem" or NULL.
+ // If srcarg is NULL, then all partitions are synced.
if(strcmp(srcarg, "system") == 0) {
- android_srcdir = product_file("system");
- data_srcdir = NULL;
+ system_srcdir = product_file("system");
} else if(strcmp(srcarg, "data") == 0) {
- android_srcdir = NULL;
data_srcdir = product_file("data");
+ } else if(strcmp(srcarg, "vendor") == 0) {
+ vendor_srcdir = product_file("vendor");
+ } else if(strcmp(srcarg, "oem") == 0) {
+ oem_srcdir = product_file("oem");
} else {
- /* It's not "system" or "data".
- */
+ // It's not "system", "data", "vendor", or "oem".
return 1;
}
}
- if(android_srcdir_out != NULL)
- *android_srcdir_out = android_srcdir;
+ if(system_srcdir_out != NULL)
+ *system_srcdir_out = system_srcdir;
else
- free(android_srcdir);
+ free(system_srcdir);
+
+ if(vendor_srcdir_out != NULL)
+ *vendor_srcdir_out = vendor_srcdir;
+ else
+ free(vendor_srcdir);
+
+ if(oem_srcdir_out != NULL)
+ *oem_srcdir_out = oem_srcdir;
+ else
+ free(oem_srcdir);
if(data_srcdir_out != NULL)
*data_srcdir_out = data_srcdir;
@@ -1569,20 +1833,17 @@
return 0;
}
-static int pm_command(transport_type transport, char* serial,
- int argc, char** argv)
+static int pm_command(transport_type transport, const char* serial,
+ int argc, const char** argv)
{
char buf[4096];
snprintf(buf, sizeof(buf), "shell:pm");
while(argc-- > 0) {
- char *quoted;
-
- quoted = dupAndQuote(*argv++);
-
- strncat(buf, " ", sizeof(buf)-1);
- strncat(buf, quoted, sizeof(buf)-1);
+ char *quoted = escape_arg(*argv++);
+ strncat(buf, " ", sizeof(buf) - 1);
+ strncat(buf, quoted, sizeof(buf) - 1);
free(quoted);
}
@@ -1590,7 +1851,8 @@
return 0;
}
-int uninstall_app(transport_type transport, char* serial, int argc, char** argv)
+int uninstall_app(transport_type transport, const char* serial, int argc,
+ const char** argv)
{
/* if the user choose the -k option, we refuse to do it until devices are
out with the option to uninstall the remaining data somehow (adb/ui) */
@@ -1608,13 +1870,13 @@
return pm_command(transport, serial, argc, argv);
}
-static int delete_file(transport_type transport, char* serial, char* filename)
+static int delete_file(transport_type transport, const char* serial, char* filename)
{
char buf[4096];
char* quoted;
- snprintf(buf, sizeof(buf), "shell:rm ");
- quoted = dupAndQuote(filename);
+ snprintf(buf, sizeof(buf), "shell:rm -f ");
+ quoted = escape_arg(filename);
strncat(buf, quoted, sizeof(buf)-1);
free(quoted);
@@ -1633,114 +1895,188 @@
}
}
-static int check_file(const char* filename)
-{
- struct stat st;
-
- if (filename == NULL) {
- return 0;
- }
-
- if (stat(filename, &st) != 0) {
- fprintf(stderr, "can't find '%s' to install\n", filename);
- return 1;
- }
-
- if (!S_ISREG(st.st_mode)) {
- fprintf(stderr, "can't install '%s' because it's not a file\n", filename);
- return 1;
- }
-
- return 0;
-}
-
-int install_app(transport_type transport, char* serial, int argc, char** argv)
+int install_app(transport_type transport, const char* serial, int argc,
+ const char** argv)
{
static const char *const DATA_DEST = "/data/local/tmp/%s";
static const char *const SD_DEST = "/sdcard/tmp/%s";
const char* where = DATA_DEST;
- char apk_dest[PATH_MAX];
- char verification_dest[PATH_MAX];
- char* apk_file;
- char* verification_file = NULL;
- int file_arg = -1;
- int err;
int i;
- int verify_apk = 1;
+ struct stat sb;
for (i = 1; i < argc; i++) {
- if (*argv[i] != '-') {
- file_arg = i;
- break;
- } else if (!strcmp(argv[i], "-i")) {
- // Skip the installer package name.
- i++;
- } else if (!strcmp(argv[i], "-s")) {
+ if (!strcmp(argv[i], "-s")) {
where = SD_DEST;
- } else if (!strcmp(argv[i], "--algo")) {
- verify_apk = 0;
- i++;
- } else if (!strcmp(argv[i], "--iv")) {
- verify_apk = 0;
- i++;
- } else if (!strcmp(argv[i], "--key")) {
- verify_apk = 0;
- i++;
}
}
- if (file_arg < 0) {
- fprintf(stderr, "can't find filename in arguments\n");
- return 1;
- } else if (file_arg + 2 < argc) {
- fprintf(stderr, "too many files specified; only takes APK file and verifier file\n");
- return 1;
+ // Find last APK argument.
+ // All other arguments passed through verbatim.
+ int last_apk = -1;
+ for (i = argc - 1; i >= 0; i--) {
+ const char* file = argv[i];
+ char* dot = strrchr(file, '.');
+ if (dot && !strcasecmp(dot, ".apk")) {
+ if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ fprintf(stderr, "Invalid APK file: %s\n", file);
+ return -1;
+ }
+
+ last_apk = i;
+ break;
+ }
}
- apk_file = argv[file_arg];
-
- if (file_arg != argc - 1) {
- verification_file = argv[file_arg + 1];
+ if (last_apk == -1) {
+ fprintf(stderr, "Missing APK file\n");
+ return -1;
}
- if (check_file(apk_file) || check_file(verification_file)) {
- return 1;
- }
-
+ const char* apk_file = argv[last_apk];
+ char apk_dest[PATH_MAX];
snprintf(apk_dest, sizeof apk_dest, where, get_basename(apk_file));
- if (verification_file != NULL) {
- snprintf(verification_dest, sizeof(verification_dest), where, get_basename(verification_file));
-
- if (!strcmp(apk_dest, verification_dest)) {
- fprintf(stderr, "APK and verification file can't have the same name\n");
- return 1;
- }
- }
-
- err = do_sync_push(apk_file, apk_dest, verify_apk);
+ int err = do_sync_push(apk_file, apk_dest, 0 /* no show progress */);
if (err) {
goto cleanup_apk;
} else {
- argv[file_arg] = apk_dest; /* destination name, not source location */
- }
-
- if (verification_file != NULL) {
- err = do_sync_push(verification_file, verification_dest, 0 /* no verify APK */);
- if (err) {
- goto cleanup_apk;
- } else {
- argv[file_arg + 1] = verification_dest; /* destination name, not source location */
- }
+ argv[last_apk] = apk_dest; /* destination name, not source location */
}
pm_command(transport, serial, argc, argv);
cleanup_apk:
- if (verification_file != NULL) {
- delete_file(transport, serial, verification_dest);
+ delete_file(transport, serial, apk_dest);
+ return err;
+}
+
+int install_multiple_app(transport_type transport, const char* serial, int argc,
+ const char** argv)
+{
+ char buf[1024];
+ int i;
+ struct stat sb;
+ unsigned long long total_size = 0;
+
+ // Find all APK arguments starting at end.
+ // All other arguments passed through verbatim.
+ int first_apk = -1;
+ for (i = argc - 1; i >= 0; i--) {
+ const char* file = argv[i];
+ char* dot = strrchr(file, '.');
+ if (dot && !strcasecmp(dot, ".apk")) {
+ if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) {
+ fprintf(stderr, "Invalid APK file: %s\n", file);
+ return -1;
+ }
+
+ total_size += sb.st_size;
+ first_apk = i;
+ } else {
+ break;
+ }
}
- delete_file(transport, serial, apk_dest);
+ if (first_apk == -1) {
+ fprintf(stderr, "Missing APK file\n");
+ return 1;
+ }
- return err;
+ snprintf(buf, sizeof(buf), "exec:pm install-create -S %lld", total_size);
+ for (i = 1; i < first_apk; i++) {
+ char *quoted = escape_arg(argv[i]);
+ strncat(buf, " ", sizeof(buf) - 1);
+ strncat(buf, quoted, sizeof(buf) - 1);
+ free(quoted);
+ }
+
+ // Create install session
+ int fd = adb_connect(buf);
+ if (fd < 0) {
+ fprintf(stderr, "Connect error for create: %s\n", adb_error());
+ return -1;
+ }
+ read_status_line(fd, buf, sizeof(buf));
+ adb_close(fd);
+
+ int session_id = -1;
+ if (!strncmp("Success", buf, 7)) {
+ char* start = strrchr(buf, '[');
+ char* end = strrchr(buf, ']');
+ if (start && end) {
+ *end = '\0';
+ session_id = strtol(start + 1, NULL, 10);
+ }
+ }
+ if (session_id < 0) {
+ fprintf(stderr, "Failed to create session\n");
+ fputs(buf, stderr);
+ return -1;
+ }
+
+ // Valid session, now stream the APKs
+ int success = 1;
+ for (i = first_apk; i < argc; i++) {
+ const char* file = argv[i];
+ if (stat(file, &sb) == -1) {
+ fprintf(stderr, "Failed to stat %s\n", file);
+ success = 0;
+ goto finalize_session;
+ }
+
+ snprintf(buf, sizeof(buf), "exec:pm install-write -S %lld %d %d_%s -",
+ (long long int) sb.st_size, session_id, i, get_basename(file));
+
+ int localFd = adb_open(file, O_RDONLY);
+ if (localFd < 0) {
+ fprintf(stderr, "Failed to open %s: %s\n", file, adb_error());
+ success = 0;
+ goto finalize_session;
+ }
+
+ int remoteFd = adb_connect(buf);
+ if (remoteFd < 0) {
+ fprintf(stderr, "Connect error for write: %s\n", adb_error());
+ adb_close(localFd);
+ success = 0;
+ goto finalize_session;
+ }
+
+ copy_to_file(localFd, remoteFd);
+ read_status_line(remoteFd, buf, sizeof(buf));
+
+ adb_close(localFd);
+ adb_close(remoteFd);
+
+ if (strncmp("Success", buf, 7)) {
+ fprintf(stderr, "Failed to write %s\n", file);
+ fputs(buf, stderr);
+ success = 0;
+ goto finalize_session;
+ }
+ }
+
+finalize_session:
+ // Commit session if we streamed everything okay; otherwise abandon
+ if (success) {
+ snprintf(buf, sizeof(buf), "exec:pm install-commit %d", session_id);
+ } else {
+ snprintf(buf, sizeof(buf), "exec:pm install-abandon %d", session_id);
+ }
+
+ fd = adb_connect(buf);
+ if (fd < 0) {
+ fprintf(stderr, "Connect error for finalize: %s\n", adb_error());
+ return -1;
+ }
+ read_status_line(fd, buf, sizeof(buf));
+ adb_close(fd);
+
+ if (!strncmp("Success", buf, 7)) {
+ fputs(buf, stderr);
+ return 0;
+ } else {
+ fprintf(stderr, "Failed to finalize session\n");
+ fputs(buf, stderr);
+ return -1;
+ }
}
diff --git a/adb/console.c b/adb/console.cpp
similarity index 93%
rename from adb/console.c
rename to adb/console.cpp
index b813d33..452ee41 100644
--- a/adb/console.c
+++ b/adb/console.cpp
@@ -24,7 +24,7 @@
}
-int adb_send_emulator_command(int argc, char** argv)
+int adb_send_emulator_command(int argc, const char** argv)
{
int fd, nn;
diff --git a/adb/fdevent.c b/adb/fdevent.cpp
similarity index 95%
rename from adb/fdevent.c
rename to adb/fdevent.cpp
index 5c374a7..eeb2a9c 100644
--- a/adb/fdevent.c
+++ b/adb/fdevent.cpp
@@ -28,10 +28,12 @@
#include <stdarg.h>
#include <stddef.h>
+#include "adb_io.h"
+#include "adb_trace.h"
#include "fdevent.h"
-#include "transport.h"
#include "sysdeps.h"
+#define TRACE_TAG TRACE_FDEVENT
/* !!! Do not enable DEBUG for the adb that will run as the server:
** both stdout and stderr are used to communicate between the client
@@ -57,16 +59,6 @@
#define FATAL(x...) fatal(__FUNCTION__, x)
#if DEBUG
-#define D(...) \
- do { \
- adb_mutex_lock(&D_lock); \
- int save_errno = errno; \
- fprintf(stderr, "%s::%s():", __FILE__, __FUNCTION__); \
- errno = save_errno; \
- fprintf(stderr, __VA_ARGS__); \
- adb_mutex_unlock(&D_lock); \
- errno = save_errno; \
- } while(0)
static void dump_fde(fdevent *fde, const char *info)
{
adb_mutex_lock(&D_lock);
@@ -78,7 +70,6 @@
adb_mutex_unlock(&D_lock);
}
#else
-#define D(...) ((void)0)
#define dump_fde(fde, info) do { } while(0)
#endif
@@ -97,6 +88,12 @@
static fdevent list_pending = {
.next = &list_pending,
.prev = &list_pending,
+ .fd = -1,
+ .force_eof = 0,
+ .state = 0,
+ .events = 0,
+ .func = nullptr,
+ .arg = nullptr,
};
static fdevent **fd_table = 0;
@@ -321,7 +318,7 @@
}
#if !DEBUG
-static inline void dump_all_fds(const char *extra_msg) {}
+static inline void dump_all_fds(const char* /* extra_msg */) {}
#else
static void dump_all_fds(const char *extra_msg)
{
@@ -443,7 +440,8 @@
while(fd_table_max <= fde->fd) {
fd_table_max *= 2;
}
- fd_table = realloc(fd_table, sizeof(fdevent*) * fd_table_max);
+ fd_table = reinterpret_cast<fdevent**>(
+ realloc(fd_table, sizeof(fdevent*) * fd_table_max));
if(fd_table == 0) {
FATAL("could not expand fd_table to %d entries\n", fd_table_max);
}
@@ -514,7 +512,8 @@
fde->func(fde->fd, events, fde->arg);
}
-static void fdevent_subproc_event_func(int fd, unsigned ev, void *userdata)
+static void fdevent_subproc_event_func(int fd, unsigned ev,
+ void* /* userdata */)
{
D("subproc handling on fd=%d ev=%04x\n", fd, ev);
@@ -529,7 +528,7 @@
if(ev & FDE_READ){
int subproc_fd;
- if(readx(fd, &subproc_fd, sizeof(subproc_fd))) {
+ if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
FATAL("Failed to read the subproc's fd from fd=%d\n", fd);
}
if((subproc_fd < 0) || (subproc_fd >= fd_table_max)) {
@@ -588,6 +587,7 @@
FATAL("fde %p not created by fdevent_create()\n", fde);
}
fdevent_remove(fde);
+ free(fde);
}
void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg)
@@ -670,6 +670,8 @@
if(adb_socketpair(s)) {
FATAL("cannot create shell-exit socket-pair\n");
}
+ D("socketpair: (%d,%d)", s[0], s[1]);
+
SHELL_EXIT_NOTIFY_FD = s[0];
fdevent *fde;
fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
diff --git a/adb/fdevent.h b/adb/fdevent.h
index a0ebe2a..a8102ca 100644
--- a/adb/fdevent.h
+++ b/adb/fdevent.h
@@ -19,6 +19,10 @@
#include <stdint.h> /* for int64_t */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/* events that may be observed */
#define FDE_READ 0x0001
#define FDE_WRITE 0x0002
@@ -64,20 +68,22 @@
*/
void fdevent_loop();
-struct fdevent
-{
+struct fdevent {
fdevent *next;
fdevent *prev;
int fd;
int force_eof;
- unsigned short state;
- unsigned short events;
+ uint16_t state;
+ uint16_t events;
fd_func func;
void *arg;
};
+#ifdef __cplusplus
+}
+#endif
#endif
diff --git a/adb/file_sync_client.c b/adb/file_sync_client.cpp
similarity index 71%
rename from adb/file_sync_client.c
rename to adb/file_sync_client.cpp
index 9fec081..4ba730b 100644
--- a/adb/file_sync_client.c
+++ b/adb/file_sync_client.cpp
@@ -14,24 +14,25 @@
* limitations under the License.
*/
+#include <dirent.h>
+#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
-#include <time.h>
-#include <dirent.h>
-#include <limits.h>
#include <sys/types.h>
-#include <zipfile/zipfile.h>
+#include <time.h>
+#include <utime.h>
#include "sysdeps.h"
+
#include "adb.h"
#include "adb_client.h"
+#include "adb_io.h"
#include "file_sync_service.h"
-
static unsigned long long total_bytes;
static long long start_time;
@@ -62,6 +63,22 @@
total_bytes, (t / 1000000LL), (t % 1000000LL) / 1000LL);
}
+static const char* transfer_progress_format = "\rTransferring: %llu/%llu (%d%%)";
+
+static void print_transfer_progress(unsigned long long bytes_current,
+ unsigned long long bytes_total) {
+ if (bytes_total == 0) return;
+
+ fprintf(stderr, transfer_progress_format, bytes_current, bytes_total,
+ (int) (bytes_current * 100 / bytes_total));
+
+ if (bytes_current == bytes_total) {
+ fputc('\n', stderr);
+ }
+
+ fflush(stderr);
+}
+
void sync_quit(int fd)
{
syncmsg msg;
@@ -69,7 +86,7 @@
msg.req.id = ID_QUIT;
msg.req.namelen = 0;
- writex(fd, &msg.req, sizeof(msg.req));
+ WriteFdExactly(fd, &msg.req, sizeof(msg.req));
}
typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie);
@@ -86,20 +103,20 @@
msg.req.id = ID_LIST;
msg.req.namelen = htoll(len);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, path, len)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, path, len)) {
goto fail;
}
for(;;) {
- if(readx(fd, &msg.dent, sizeof(msg.dent))) break;
+ if(!ReadFdExactly(fd, &msg.dent, sizeof(msg.dent))) break;
if(msg.dent.id == ID_DONE) return 0;
if(msg.dent.id != ID_DENT) break;
len = ltohl(msg.dent.namelen);
if(len > 256) break;
- if(readx(fd, buf, len)) break;
+ if(!ReadFdExactly(fd, buf, len)) break;
buf[len] = 0;
func(ltohl(msg.dent.mode),
@@ -123,7 +140,8 @@
static syncsendbuf send_buffer;
-int sync_readtime(int fd, const char *path, unsigned *timestamp)
+int sync_readtime(int fd, const char *path, unsigned int *timestamp,
+ unsigned int *mode)
{
syncmsg msg;
int len = strlen(path);
@@ -131,12 +149,12 @@
msg.req.id = ID_STAT;
msg.req.namelen = htoll(len);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, path, len)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, path, len)) {
return -1;
}
- if(readx(fd, &msg.stat, sizeof(msg.stat))) {
+ if(!ReadFdExactly(fd, &msg.stat, sizeof(msg.stat))) {
return -1;
}
@@ -145,6 +163,7 @@
}
*timestamp = ltohl(msg.stat.time);
+ *mode = ltohl(msg.stat.mode);
return 0;
}
@@ -156,8 +175,8 @@
msg.req.id = ID_STAT;
msg.req.namelen = htoll(len);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, path, len)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, path, len)) {
return -1;
}
@@ -169,7 +188,7 @@
{
syncmsg msg;
- if(readx(fd, &msg.stat, sizeof(msg.stat)))
+ if(!ReadFdExactly(fd, &msg.stat, sizeof(msg.stat)))
return -1;
if(msg.stat.id != ID_STAT)
@@ -190,12 +209,12 @@
msg.req.id = ID_STAT;
msg.req.namelen = htoll(len);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, path, len)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, path, len)) {
return -1;
}
- if(readx(fd, &msg.stat, sizeof(msg.stat))) {
+ if(!ReadFdExactly(fd, &msg.stat, sizeof(msg.stat))) {
return -1;
}
@@ -207,9 +226,10 @@
return 0;
}
-static int write_data_file(int fd, const char *path, syncsendbuf *sbuf)
+static int write_data_file(int fd, const char *path, syncsendbuf *sbuf, int show_progress)
{
int lfd, err = 0;
+ unsigned long long size = 0;
lfd = adb_open(path, O_RDONLY);
if(lfd < 0) {
@@ -217,6 +237,17 @@
return -1;
}
+ if (show_progress) {
+ // Determine local file size.
+ struct stat st;
+ if (stat(path, &st)) {
+ fprintf(stderr,"cannot stat '%s': %s\n", path, strerror(errno));
+ return -1;
+ }
+
+ size = st.st_size;
+ }
+
sbuf->id = ID_DATA;
for(;;) {
int ret;
@@ -233,18 +264,23 @@
}
sbuf->size = htoll(ret);
- if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){
+ if(!WriteFdExactly(fd, sbuf, sizeof(unsigned) * 2 + ret)){
err = -1;
break;
}
total_bytes += ret;
+
+ if (show_progress) {
+ print_transfer_progress(total_bytes, size);
+ }
}
adb_close(lfd);
return err;
}
-static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf)
+static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf,
+ int show_progress)
{
int err = 0;
int total = 0;
@@ -258,18 +294,24 @@
memcpy(sbuf->data, &file_buffer[total], count);
sbuf->size = htoll(count);
- if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){
+ if(!WriteFdExactly(fd, sbuf, sizeof(unsigned) * 2 + count)){
err = -1;
break;
}
total += count;
total_bytes += count;
+
+ if (show_progress) {
+ print_transfer_progress(total, size);
+ }
}
return err;
}
-#ifdef HAVE_SYMLINKS
+#if defined(_WIN32)
+extern int write_data_link(int fd, const char *path, syncsendbuf *sbuf) __attribute__((error("no symlinks on Windows")));
+#else
static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
{
int len, ret;
@@ -284,7 +326,7 @@
sbuf->size = htoll(len + 1);
sbuf->id = ID_DATA;
- ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
+ ret = !WriteFdExactly(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
if(ret)
return -1;
@@ -295,7 +337,7 @@
#endif
static int sync_send(int fd, const char *lpath, const char *rpath,
- unsigned mtime, mode_t mode, int verifyApk)
+ unsigned mtime, mode_t mode, int show_progress)
{
syncmsg msg;
int len, r;
@@ -310,97 +352,38 @@
snprintf(tmp, sizeof(tmp), ",%d", mode);
r = strlen(tmp);
- if (verifyApk) {
- int lfd;
- zipfile_t zip;
- zipentry_t entry;
- int amt;
-
- // if we are transferring an APK file, then sanity check to make sure
- // we have a real zip file that contains an AndroidManifest.xml
- // this requires that we read the entire file into memory.
- lfd = adb_open(lpath, O_RDONLY);
- if(lfd < 0) {
- fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
- return -1;
- }
-
- size = adb_lseek(lfd, 0, SEEK_END);
- if (size == -1 || -1 == adb_lseek(lfd, 0, SEEK_SET)) {
- fprintf(stderr, "error seeking in file '%s'\n", lpath);
- adb_close(lfd);
- return 1;
- }
-
- file_buffer = (char *)malloc(size);
- if (file_buffer == NULL) {
- fprintf(stderr, "could not allocate buffer for '%s'\n",
- lpath);
- adb_close(lfd);
- return 1;
- }
- amt = adb_read(lfd, file_buffer, size);
- if (amt != size) {
- fprintf(stderr, "error reading from file: '%s'\n", lpath);
- adb_close(lfd);
- free(file_buffer);
- return 1;
- }
-
- adb_close(lfd);
-
- zip = init_zipfile(file_buffer, size);
- if (zip == NULL) {
- fprintf(stderr, "file '%s' is not a valid zip file\n",
- lpath);
- free(file_buffer);
- return 1;
- }
-
- entry = lookup_zipentry(zip, "AndroidManifest.xml");
- release_zipfile(zip);
- if (entry == NULL) {
- fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n",
- lpath);
- free(file_buffer);
- return 1;
- }
- }
-
msg.req.id = ID_SEND;
msg.req.namelen = htoll(len + r);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, rpath, len) || writex(fd, tmp, r)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, rpath, len) || !WriteFdExactly(fd, tmp, r)) {
free(file_buffer);
goto fail;
}
if (file_buffer) {
- write_data_buffer(fd, file_buffer, size, sbuf);
+ write_data_buffer(fd, file_buffer, size, sbuf, show_progress);
free(file_buffer);
} else if (S_ISREG(mode))
- write_data_file(fd, lpath, sbuf);
-#ifdef HAVE_SYMLINKS
+ write_data_file(fd, lpath, sbuf, show_progress);
else if (S_ISLNK(mode))
write_data_link(fd, lpath, sbuf);
-#endif
else
goto fail;
msg.data.id = ID_DONE;
msg.data.size = htoll(mtime);
- if(writex(fd, &msg.data, sizeof(msg.data)))
+ if(!WriteFdExactly(fd, &msg.data, sizeof(msg.data)))
goto fail;
- if(readx(fd, &msg.status, sizeof(msg.status)))
+ if(!ReadFdExactly(fd, &msg.status, sizeof(msg.status)))
return -1;
if(msg.status.id != ID_OKAY) {
if(msg.status.id == ID_FAIL) {
len = ltohl(msg.status.msglen);
if(len > 256) len = 256;
- if(readx(fd, sbuf->data, len)) {
+ if(!ReadFdExactly(fd, sbuf->data, len)) {
return -1;
}
sbuf->data[len] = 0;
@@ -419,10 +402,10 @@
return -1;
}
-static int mkdirs(char *name)
+static int mkdirs(const char *name)
{
int ret;
- char *x = name + 1;
+ char *x = (char *)name + 1;
for(;;) {
x = adb_dirstart(x);
@@ -438,32 +421,53 @@
return 0;
}
-int sync_recv(int fd, const char *rpath, const char *lpath)
+int sync_recv(int fd, const char *rpath, const char *lpath, int show_progress)
{
syncmsg msg;
int len;
int lfd = -1;
char *buffer = send_buffer.data;
unsigned id;
+ unsigned long long size = 0;
len = strlen(rpath);
if(len > 1024) return -1;
+ if (show_progress) {
+ // Determine remote file size.
+ syncmsg stat_msg;
+ stat_msg.req.id = ID_STAT;
+ stat_msg.req.namelen = htoll(len);
+
+ if (!WriteFdExactly(fd, &stat_msg.req, sizeof(stat_msg.req)) ||
+ !WriteFdExactly(fd, rpath, len)) {
+ return -1;
+ }
+
+ if (!ReadFdExactly(fd, &stat_msg.stat, sizeof(stat_msg.stat))) {
+ return -1;
+ }
+
+ if (stat_msg.stat.id != ID_STAT) return -1;
+
+ size = ltohl(stat_msg.stat.size);
+ }
+
msg.req.id = ID_RECV;
msg.req.namelen = htoll(len);
- if(writex(fd, &msg.req, sizeof(msg.req)) ||
- writex(fd, rpath, len)) {
+ if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
+ !WriteFdExactly(fd, rpath, len)) {
return -1;
}
- if(readx(fd, &msg.data, sizeof(msg.data))) {
+ if(!ReadFdExactly(fd, &msg.data, sizeof(msg.data))) {
return -1;
}
id = msg.data.id;
if((id == ID_DATA) || (id == ID_DONE)) {
adb_unlink(lpath);
- mkdirs((char *)lpath);
+ mkdirs(lpath);
lfd = adb_creat(lpath, 0644);
if(lfd < 0) {
fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
@@ -475,7 +479,7 @@
}
for(;;) {
- if(readx(fd, &msg.data, sizeof(msg.data))) {
+ if(!ReadFdExactly(fd, &msg.data, sizeof(msg.data))) {
return -1;
}
id = msg.data.id;
@@ -490,18 +494,22 @@
return -1;
}
- if(readx(fd, buffer, len)) {
+ if(!ReadFdExactly(fd, buffer, len)) {
adb_close(lfd);
return -1;
}
- if(writex(lfd, buffer, len)) {
+ if(!WriteFdExactly(lfd, buffer, len)) {
fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
adb_close(lfd);
return -1;
}
total_bytes += len;
+
+ if (show_progress) {
+ print_transfer_progress(total_bytes, size);
+ }
}
adb_close(lfd);
@@ -514,24 +522,19 @@
if(id == ID_FAIL) {
len = ltohl(msg.data.size);
if(len > 256) len = 256;
- if(readx(fd, buffer, len)) {
+ if(!ReadFdExactly(fd, buffer, len)) {
return -1;
}
buffer[len] = 0;
} else {
memcpy(buffer, &id, 4);
buffer[4] = 0;
-// strcpy(buffer,"unknown reason");
}
fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
return 0;
}
-
-
/* --- */
-
-
static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
const char *name, void *cookie)
{
@@ -565,7 +568,6 @@
unsigned int mode;
unsigned int size;
int flag;
- //char data[0];
};
copyinfo *mkcopyinfo(const char *spath, const char *dpath,
@@ -577,7 +579,8 @@
int ssize = slen + nlen + 2;
int dsize = dlen + nlen + 2;
- copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize);
+ copyinfo *ci = reinterpret_cast<copyinfo*>(
+ malloc(sizeof(copyinfo) + ssize + dsize));
if(ci == 0) {
fprintf(stderr,"out of memory\n");
abort();
@@ -593,7 +596,6 @@
snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, name);
snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, name);
-// fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst);
return ci;
}
@@ -607,8 +609,6 @@
copyinfo *dirlist = 0;
copyinfo *ci, *next;
-// fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath);
-
d = opendir(lpath);
if(d == 0) {
fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
@@ -632,30 +632,33 @@
continue;
strcpy(stat_path, lpath);
strcat(stat_path, de->d_name);
- stat(stat_path, &st);
- if (S_ISDIR(st.st_mode)) {
- ci = mkcopyinfo(lpath, rpath, name, 1);
- ci->next = dirlist;
- dirlist = ci;
- } else {
- ci = mkcopyinfo(lpath, rpath, name, 0);
- if(lstat(ci->src, &st)) {
- fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
- free(ci);
- closedir(d);
- return -1;
- }
- if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
- fprintf(stderr, "skipping special file '%s'\n", ci->src);
- free(ci);
+ if(!lstat(stat_path, &st)) {
+ if (S_ISDIR(st.st_mode)) {
+ ci = mkcopyinfo(lpath, rpath, name, 1);
+ ci->next = dirlist;
+ dirlist = ci;
} else {
- ci->time = st.st_mtime;
- ci->mode = st.st_mode;
- ci->size = st.st_size;
- ci->next = *filelist;
- *filelist = ci;
+ ci = mkcopyinfo(lpath, rpath, name, 0);
+ if(lstat(ci->src, &st)) {
+ fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
+ free(ci);
+ closedir(d);
+ return -1;
+ }
+ if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
+ fprintf(stderr, "skipping special file '%s'\n", ci->src);
+ free(ci);
+ } else {
+ ci->time = st.st_mtime;
+ ci->mode = st.st_mode;
+ ci->size = st.st_size;
+ ci->next = *filelist;
+ *filelist = ci;
+ }
}
+ } else {
+ fprintf(stderr, "cannot lstat '%s': %s\n",stat_path , strerror(errno));
}
}
@@ -681,14 +684,14 @@
if((lpath[0] == 0) || (rpath[0] == 0)) return -1;
if(lpath[strlen(lpath) - 1] != '/') {
int tmplen = strlen(lpath)+2;
- char *tmp = malloc(tmplen);
+ char *tmp = reinterpret_cast<char*>(malloc(tmplen));
if(tmp == 0) return -1;
snprintf(tmp, tmplen, "%s/",lpath);
lpath = tmp;
}
if(rpath[strlen(rpath) - 1] != '/') {
int tmplen = strlen(rpath)+2;
- char *tmp = malloc(tmplen);
+ char *tmp = reinterpret_cast<char*>(malloc(tmplen));
if(tmp == 0) return -1;
snprintf(tmp, tmplen, "%s/",rpath);
rpath = tmp;
@@ -721,7 +724,8 @@
if(ci->flag == 0) {
fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst);
if(!listonly &&
- sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){
+ sync_send(fd, ci->src, ci->dst, ci->time, ci->mode,
+ 0 /* no show progress */)) {
return 1;
}
pushed++;
@@ -739,7 +743,7 @@
}
-int do_sync_push(const char *lpath, const char *rpath, int verifyApk)
+int do_sync_push(const char *lpath, const char *rpath, int show_progress)
{
struct stat st;
unsigned mode;
@@ -780,13 +784,14 @@
name++;
}
int tmplen = strlen(name) + strlen(rpath) + 2;
- char *tmp = malloc(strlen(name) + strlen(rpath) + 2);
+ char *tmp = reinterpret_cast<char*>(
+ malloc(strlen(name) + strlen(rpath) + 2));
if(tmp == 0) return 1;
snprintf(tmp, tmplen, "%s/%s", rpath, name);
rpath = tmp;
}
BEGIN();
- if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) {
+ if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, show_progress)) {
return 1;
} else {
END();
@@ -868,63 +873,81 @@
return 0;
}
+static int set_time_and_mode(const char *lpath, time_t time, unsigned int mode)
+{
+ struct utimbuf times = { time, time };
+ int r1 = utime(lpath, ×);
+
+ /* use umask for permissions */
+ mode_t mask=umask(0000);
+ umask(mask);
+ int r2 = chmod(lpath, mode & ~mask);
+
+ return r1 ? : r2;
+}
+
+/* Return a copy of the path string with / appended if needed */
+static char *add_slash_to_path(const char *path)
+{
+ if (path[strlen(path) - 1] != '/') {
+ size_t len = strlen(path) + 2;
+ char *path_with_slash = reinterpret_cast<char*>(malloc(len));
+ if (path_with_slash == NULL)
+ return NULL;
+ snprintf(path_with_slash, len, "%s/", path);
+ return path_with_slash;
+ } else {
+ return strdup(path);
+ }
+}
+
static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
- int checktimestamps)
+ int copy_attrs)
{
copyinfo *filelist = 0;
copyinfo *ci, *next;
int pulled = 0;
int skipped = 0;
+ char *rpath_clean = NULL;
+ char *lpath_clean = NULL;
+ int ret = 0;
+
+ if (rpath[0] == '\0' || lpath[0] == '\0') {
+ ret = -1;
+ goto finish;
+ }
/* Make sure that both directory paths end in a slash. */
- if (rpath[0] == 0 || lpath[0] == 0) return -1;
- if (rpath[strlen(rpath) - 1] != '/') {
- int tmplen = strlen(rpath) + 2;
- char *tmp = malloc(tmplen);
- if (tmp == 0) return -1;
- snprintf(tmp, tmplen, "%s/", rpath);
- rpath = tmp;
+ rpath_clean = add_slash_to_path(rpath);
+ if (!rpath_clean) {
+ ret = -1;
+ goto finish;
}
- if (lpath[strlen(lpath) - 1] != '/') {
- int tmplen = strlen(lpath) + 2;
- char *tmp = malloc(tmplen);
- if (tmp == 0) return -1;
- snprintf(tmp, tmplen, "%s/", lpath);
- lpath = tmp;
+ lpath_clean = add_slash_to_path(lpath);
+ if (!lpath_clean) {
+ ret = -1;
+ goto finish;
}
- fprintf(stderr, "pull: building file list...\n");
/* Recursively build the list of files to copy. */
- if (remote_build_list(fd, &filelist, rpath, lpath)) {
- return -1;
+ fprintf(stderr, "pull: building file list...\n");
+ if (remote_build_list(fd, &filelist, rpath_clean, lpath_clean)) {
+ ret = -1;
+ goto finish;
}
-#if 0
- if (checktimestamps) {
- for (ci = filelist; ci != 0; ci = ci->next) {
- if (sync_start_readtime(fd, ci->dst)) {
- return 1;
- }
- }
- for (ci = filelist; ci != 0; ci = ci->next) {
- unsigned int timestamp, mode, size;
- if (sync_finish_readtime(fd, ×tamp, &mode, &size))
- return 1;
- if (size == ci->size) {
- /* for links, we cannot update the atime/mtime */
- if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
- (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
- ci->flag = 1;
- }
- }
- }
-#endif
for (ci = filelist; ci != 0; ci = next) {
next = ci->next;
if (ci->flag == 0) {
fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
- if (sync_recv(fd, ci->src, ci->dst)) {
- return 1;
+ if (sync_recv(fd, ci->src, ci->dst, 0 /* no show progress */)) {
+ ret = -1;
+ goto finish;
+ }
+
+ if (copy_attrs && set_time_and_mode(ci->dst, ci->time, ci->mode)) {
+ ret = -1;
+ goto finish;
}
pulled++;
} else {
@@ -937,12 +960,15 @@
pulled, (pulled == 1) ? "" : "s",
skipped, (skipped == 1) ? "" : "s");
- return 0;
+finish:
+ free(lpath_clean);
+ free(rpath_clean);
+ return ret;
}
-int do_sync_pull(const char *rpath, const char *lpath)
+int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int copy_attrs)
{
- unsigned mode;
+ unsigned mode, time;
struct stat st;
int fd;
@@ -953,7 +979,7 @@
return 1;
}
- if(sync_readmode(fd, rpath, &mode)) {
+ if(sync_readtime(fd, rpath, &time, &mode)) {
return 1;
}
if(mode == 0) {
@@ -974,23 +1000,25 @@
name++;
}
int tmplen = strlen(name) + strlen(lpath) + 2;
- char *tmp = malloc(tmplen);
+ char *tmp = reinterpret_cast<char*>(malloc(tmplen));
if(tmp == 0) return 1;
snprintf(tmp, tmplen, "%s/%s", lpath, name);
lpath = tmp;
}
}
BEGIN();
- if(sync_recv(fd, rpath, lpath)) {
+ if (sync_recv(fd, rpath, lpath, show_progress)) {
return 1;
} else {
+ if (copy_attrs && set_time_and_mode(lpath, time, mode))
+ return 1;
END();
sync_quit(fd);
return 0;
}
} else if(S_ISDIR(mode)) {
BEGIN();
- if (copy_remote_dir_local(fd, rpath, lpath, 0)) {
+ if (copy_remote_dir_local(fd, rpath, lpath, copy_attrs)) {
return 1;
} else {
END();
diff --git a/adb/file_sync_service.c b/adb/file_sync_service.cpp
similarity index 76%
rename from adb/file_sync_service.c
rename to adb/file_sync_service.cpp
index 25bfdc9..ac01678 100644
--- a/adb/file_sync_service.c
+++ b/adb/file_sync_service.cpp
@@ -14,29 +14,30 @@
* limitations under the License.
*/
-#include <stdlib.h>
+#include <dirent.h>
+#include <errno.h>
+#include <selinux/android.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-
#include <sys/stat.h>
#include <sys/types.h>
-#include <dirent.h>
-#include <utime.h>
#include <unistd.h>
+#include <utime.h>
-#include <errno.h>
-#include <private/android_filesystem_config.h>
-#include <selinux/android.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_SYNC
#include "adb.h"
+#include "adb_io.h"
#include "file_sync_service.h"
+#include "private/android_filesystem_config.h"
-/* TODO: use fs_config to configure permissions on /data */
-static bool is_on_system(const char *name) {
- const char *SYSTEM = "/system/";
- return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0);
+static bool should_use_fs_config(const char* path) {
+ // TODO: use fs_config to configure permissions on /data.
+ return strncmp("/system/", path, strlen("/system/")) == 0 ||
+ strncmp("/vendor/", path, strlen("/vendor/")) == 0 ||
+ strncmp("/oem/", path, strlen("/oem/")) == 0;
}
static int mkdirs(char *name)
@@ -54,7 +55,7 @@
x = adb_dirstart(x);
if(x == 0) return 0;
*x = 0;
- if (is_on_system(name)) {
+ if (should_use_fs_config(name)) {
fs_config(name, 1, &uid, &gid, &mode, &cap);
}
ret = adb_mkdir(name, mode);
@@ -92,7 +93,7 @@
msg.stat.time = htoll(st.st_mtime);
}
- return writex(s, &msg.stat, sizeof(msg.stat));
+ return WriteFdExactly(s, &msg.stat, sizeof(msg.stat)) ? 0 : -1;
}
static int do_list(int s, const char *path)
@@ -130,8 +131,8 @@
msg.dent.time = htoll(st.st_mtime);
msg.dent.namelen = htoll(len);
- if(writex(s, &msg.dent, sizeof(msg.dent)) ||
- writex(s, de->d_name, len)) {
+ if(!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
+ !WriteFdExactly(s, de->d_name, len)) {
closedir(d);
return -1;
}
@@ -146,7 +147,7 @@
msg.dent.size = 0;
msg.dent.time = 0;
msg.dent.namelen = 0;
- return writex(s, &msg.dent, sizeof(msg.dent));
+ return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ? 0 : -1;
}
static int fail_message(int s, const char *reason)
@@ -158,8 +159,8 @@
msg.data.id = ID_FAIL;
msg.data.size = htoll(len);
- if(writex(s, &msg.data, sizeof(msg.data)) ||
- writex(s, reason, len)) {
+ if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) ||
+ !WriteFdExactly(s, reason, len)) {
return -1;
} else {
return 0;
@@ -172,24 +173,24 @@
}
static int handle_send_file(int s, char *path, uid_t uid,
- gid_t gid, mode_t mode, char *buffer)
+ gid_t gid, mode_t mode, char *buffer, bool do_unlink)
{
syncmsg msg;
unsigned int timestamp = 0;
int fd;
- fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+ fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
if(fd < 0 && errno == ENOENT) {
if(mkdirs(path) != 0) {
if(fail_errno(s))
return -1;
fd = -1;
} else {
- fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+ fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
}
}
if(fd < 0 && errno == EEXIST) {
- fd = adb_open_mode(path, O_WRONLY, mode);
+ fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
}
if(fd < 0) {
if(fail_errno(s))
@@ -212,7 +213,7 @@
for(;;) {
unsigned int len;
- if(readx(s, &msg.data, sizeof(msg.data)))
+ if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
goto fail;
if(msg.data.id != ID_DATA) {
@@ -228,15 +229,15 @@
fail_message(s, "oversize data message");
goto fail;
}
- if(readx(s, buffer, len))
+ if(!ReadFdExactly(s, buffer, len))
goto fail;
if(fd < 0)
continue;
- if(writex(fd, buffer, len)) {
+ if(!WriteFdExactly(fd, buffer, len)) {
int saved_errno = errno;
adb_close(fd);
- adb_unlink(path);
+ if (do_unlink) adb_unlink(path);
fd = -1;
errno = saved_errno;
if(fail_errno(s)) return -1;
@@ -253,7 +254,7 @@
msg.status.id = ID_OKAY;
msg.status.msglen = 0;
- if(writex(s, &msg.status, sizeof(msg.status)))
+ if(!WriteFdExactly(s, &msg.status, sizeof(msg.status)))
return -1;
}
return 0;
@@ -261,18 +262,20 @@
fail:
if(fd >= 0)
adb_close(fd);
- adb_unlink(path);
+ if (do_unlink) adb_unlink(path);
return -1;
}
-#ifdef HAVE_SYMLINKS
+#if defined(_WIN32)
+extern int handle_send_link(int s, char *path, char *buffer) __attribute__((error("no symlinks on Windows")));
+#else
static int handle_send_link(int s, char *path, char *buffer)
{
syncmsg msg;
unsigned int len;
int ret;
- if(readx(s, &msg.data, sizeof(msg.data)))
+ if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
return -1;
if(msg.data.id != ID_DATA) {
@@ -285,7 +288,7 @@
fail_message(s, "oversize data message");
return -1;
}
- if(readx(s, buffer, len))
+ if(!ReadFdExactly(s, buffer, len))
return -1;
ret = symlink(buffer, path);
@@ -301,13 +304,13 @@
return -1;
}
- if(readx(s, &msg.data, sizeof(msg.data)))
+ if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
return -1;
if(msg.data.id == ID_DONE) {
msg.status.id = ID_OKAY;
msg.status.msglen = 0;
- if(writex(s, &msg.status, sizeof(msg.status)))
+ if(!WriteFdExactly(s, &msg.status, sizeof(msg.status)))
return -1;
} else {
fail_message(s, "invalid data message: expected ID_DONE");
@@ -316,63 +319,55 @@
return 0;
}
-#endif /* HAVE_SYMLINKS */
+#endif
static int do_send(int s, char *path, char *buffer)
{
- char *tmp;
unsigned int mode;
- int is_link, ret;
+ bool is_link = false;
+ bool do_unlink;
- tmp = strrchr(path,',');
+ char* tmp = strrchr(path,',');
if(tmp) {
*tmp = 0;
errno = 0;
mode = strtoul(tmp + 1, NULL, 0);
-#ifndef HAVE_SYMLINKS
- is_link = 0;
-#else
is_link = S_ISLNK((mode_t) mode);
-#endif
mode &= 0777;
}
if(!tmp || errno) {
mode = 0644;
is_link = 0;
+ do_unlink = true;
} else {
struct stat st;
/* Don't delete files before copying if they are not "regular" */
- if(lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
+ do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
+ if (do_unlink) {
adb_unlink(path);
}
}
-#ifdef HAVE_SYMLINKS
- if(is_link)
- ret = handle_send_link(s, path, buffer);
- else {
-#else
- {
-#endif
- uid_t uid = -1;
- gid_t gid = -1;
- uint64_t cap = 0;
-
- /* copy user permission bits to "group" and "other" permissions */
- mode |= ((mode >> 3) & 0070);
- mode |= ((mode >> 3) & 0007);
-
- tmp = path;
- if(*tmp == '/') {
- tmp++;
- }
- if (is_on_system(path)) {
- fs_config(tmp, 0, &uid, &gid, &mode, &cap);
- }
- ret = handle_send_file(s, path, uid, gid, mode, buffer);
+ if (is_link) {
+ return handle_send_link(s, path, buffer);
}
- return ret;
+ uid_t uid = -1;
+ gid_t gid = -1;
+ uint64_t cap = 0;
+
+ /* copy user permission bits to "group" and "other" permissions */
+ mode |= ((mode >> 3) & 0070);
+ mode |= ((mode >> 3) & 0007);
+
+ tmp = path;
+ if(*tmp == '/') {
+ tmp++;
+ }
+ if (should_use_fs_config(path)) {
+ fs_config(tmp, 0, &uid, &gid, &mode, &cap);
+ }
+ return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
}
static int do_recv(int s, const char *path, char *buffer)
@@ -380,7 +375,7 @@
syncmsg msg;
int fd, r;
- fd = adb_open(path, O_RDONLY);
+ fd = adb_open(path, O_RDONLY | O_CLOEXEC);
if(fd < 0) {
if(fail_errno(s)) return -1;
return 0;
@@ -397,8 +392,8 @@
return r;
}
msg.data.size = htoll(r);
- if(writex(s, &msg.data, sizeof(msg.data)) ||
- writex(s, buffer, r)) {
+ if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) ||
+ !WriteFdExactly(s, buffer, r)) {
adb_close(fd);
return -1;
}
@@ -408,7 +403,7 @@
msg.data.id = ID_DONE;
msg.data.size = 0;
- if(writex(s, &msg.data, sizeof(msg.data))) {
+ if(!WriteFdExactly(s, &msg.data, sizeof(msg.data))) {
return -1;
}
@@ -421,13 +416,13 @@
char name[1025];
unsigned namelen;
- char *buffer = malloc(SYNC_DATA_MAX);
+ char *buffer = reinterpret_cast<char*>(malloc(SYNC_DATA_MAX));
if(buffer == 0) goto fail;
for(;;) {
D("sync: waiting for command\n");
- if(readx(fd, &msg.req, sizeof(msg.req))) {
+ if(!ReadFdExactly(fd, &msg.req, sizeof(msg.req))) {
fail_message(fd, "command read failure");
break;
}
@@ -436,7 +431,7 @@
fail_message(fd, "invalid namelen");
break;
}
- if(readx(fd, name, namelen)) {
+ if(!ReadFdExactly(fd, name, namelen)) {
fail_message(fd, "filename read failure");
break;
}
diff --git a/adb/file_sync_service.h b/adb/file_sync_service.h
index e402e06..5b69a63 100644
--- a/adb/file_sync_service.h
+++ b/adb/file_sync_service.h
@@ -17,22 +17,14 @@
#ifndef _FILE_SYNC_SERVICE_H_
#define _FILE_SYNC_SERVICE_H_
-#ifdef HAVE_BIG_ENDIAN
-static inline unsigned __swap_uint32(unsigned x)
-{
- return (((x) & 0xFF000000) >> 24)
- | (((x) & 0x00FF0000) >> 8)
- | (((x) & 0x0000FF00) << 8)
- | (((x) & 0x000000FF) << 24);
-}
-#define htoll(x) __swap_uint32(x)
-#define ltohl(x) __swap_uint32(x)
-#define MKID(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24))
-#else
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define htoll(x) (x)
#define ltohl(x) (x)
+
#define MKID(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
-#endif
#define ID_STAT MKID('S','T','A','T')
#define ID_LIST MKID('L','I','S','T')
@@ -72,16 +64,20 @@
struct {
unsigned id;
unsigned msglen;
- } status;
+ } status;
} syncmsg;
void file_sync_service(int fd, void *cookie);
int do_sync_ls(const char *path);
-int do_sync_push(const char *lpath, const char *rpath, int verifyApk);
+int do_sync_push(const char *lpath, const char *rpath, int show_progress);
int do_sync_sync(const char *lpath, const char *rpath, int listonly);
-int do_sync_pull(const char *rpath, const char *lpath);
+int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int pullTime);
#define SYNC_DATA_MAX (64*1024)
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/adb/framebuffer_service.c b/adb/framebuffer_service.cpp
similarity index 88%
rename from adb/framebuffer_service.c
rename to adb/framebuffer_service.cpp
index fa7fd98..7baad8b 100644
--- a/adb/framebuffer_service.c
+++ b/adb/framebuffer_service.cpp
@@ -14,21 +14,23 @@
* limitations under the License.
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
#include <errno.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include "fdevent.h"
-#include "adb.h"
-
+#include <fcntl.h>
#include <linux/fb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "sysdeps.h"
+
+#include "adb.h"
+#include "adb_io.h"
+#include "fdevent.h"
/* TODO:
** - sync with vsync to avoid tearing
@@ -60,28 +62,30 @@
int fd_screencap;
int w, h, f;
int fds[2];
+ pid_t pid;
- if (pipe(fds) < 0) goto pipefail;
+ if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
- pid_t pid = fork();
+ pid = fork();
if (pid < 0) goto done;
if (pid == 0) {
dup2(fds[1], STDOUT_FILENO);
- close(fds[0]);
- close(fds[1]);
+ adb_close(fds[0]);
+ adb_close(fds[1]);
const char* command = "screencap";
const char *args[2] = {command, NULL};
execvp(command, (char**)args);
exit(1);
}
+ adb_close(fds[1]);
fd_screencap = fds[0];
/* read w, h & format */
- if(readx(fd_screencap, &w, 4)) goto done;
- if(readx(fd_screencap, &h, 4)) goto done;
- if(readx(fd_screencap, &f, 4)) goto done;
+ if(!ReadFdExactly(fd_screencap, &w, 4)) goto done;
+ if(!ReadFdExactly(fd_screencap, &h, 4)) goto done;
+ if(!ReadFdExactly(fd_screencap, &f, 4)) goto done;
fbinfo.version = DDMS_RAWIMAGE_VERSION;
/* see hardware/hardware.h */
@@ -161,22 +165,21 @@
}
/* write header */
- if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;
+ if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
/* write data */
for(i = 0; i < fbinfo.size; i += bsize) {
bsize = sizeof(buf);
if (i + bsize > fbinfo.size)
bsize = fbinfo.size - i;
- if(readx(fd_screencap, buf, bsize)) goto done;
- if(writex(fd, buf, bsize)) goto done;
+ if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
+ if(!WriteFdExactly(fd, buf, bsize)) goto done;
}
done:
- TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
+ adb_close(fds[0]);
- close(fds[0]);
- close(fds[1]);
+ TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
pipefail:
- close(fd);
+ adb_close(fd);
}
diff --git a/adb/get_my_path_darwin.c b/adb/get_my_path_darwin.c
index 5b95d15..b0c962e 100644
--- a/adb/get_my_path_darwin.c
+++ b/adb/get_my_path_darwin.c
@@ -17,14 +17,16 @@
#import <Carbon/Carbon.h>
#include <unistd.h>
+#include "adb.h"
+
void get_my_path(char *s, size_t maxLen)
{
- ProcessSerialNumber psn;
- GetCurrentProcess(&psn);
- CFDictionaryRef dict;
- dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
- CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
- CFSTR("CFBundleExecutable"));
- CFStringGetCString(value, s, maxLen, kCFStringEncodingUTF8);
+ CFBundleRef mainBundle = CFBundleGetMainBundle();
+ CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
+ CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
+ CFRelease(executableURL);
+
+ CFStringGetFileSystemRepresentation(executablePathString, s, maxLen);
+ CFRelease(executablePathString);
}
diff --git a/adb/get_my_path_freebsd.c b/adb/get_my_path_freebsd.c
deleted file mode 100644
index b06ec66..0000000
--- a/adb/get_my_path_freebsd.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2009 bsdroid project
- * Alexey Tarasov <tarasov@dodologics.com>
- *
- * 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.
- */
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <limits.h>
-#include <stdio.h>
-
-void
-get_my_path(char *exe, size_t maxLen)
-{
- char proc[64];
-
- snprintf(proc, sizeof(proc), "/proc/%d/file", getpid());
-
- int err = readlink(proc, exe, maxLen - 1);
-
- exe[err > 0 ? err : 0] = '\0';
-}
-
diff --git a/adb/get_my_path_linux.c b/adb/get_my_path_linux.cpp
similarity index 97%
rename from adb/get_my_path_linux.c
rename to adb/get_my_path_linux.cpp
index 179c3dd..11c0b21 100644
--- a/adb/get_my_path_linux.c
+++ b/adb/get_my_path_linux.cpp
@@ -14,10 +14,12 @@
* limitations under the License.
*/
-#include <sys/types.h>
-#include <unistd.h>
#include <limits.h>
#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "adb.h"
void get_my_path(char *exe, size_t maxLen)
{
diff --git a/adb/get_my_path_windows.c b/adb/get_my_path_windows.cpp
similarity index 97%
rename from adb/get_my_path_windows.c
rename to adb/get_my_path_windows.cpp
index ddf2816..9d23e1c 100644
--- a/adb/get_my_path_windows.c
+++ b/adb/get_my_path_windows.cpp
@@ -14,10 +14,12 @@
* limitations under the License.
*/
-#include <limits.h>
#include <assert.h>
+#include <limits.h>
#include <windows.h>
+#include "adb.h"
+
void get_my_path(char *exe, size_t maxLen)
{
char *r;
diff --git a/adb/jdwp_service.c b/adb/jdwp_service.cpp
similarity index 97%
rename from adb/jdwp_service.c
rename to adb/jdwp_service.cpp
index cd62b55..f0b4ba7 100644
--- a/adb/jdwp_service.c
+++ b/adb/jdwp_service.cpp
@@ -194,7 +194,8 @@
static JdwpProcess*
jdwp_process_alloc( int socket )
{
- JdwpProcess* proc = calloc(1,sizeof(*proc));
+ JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(
+ calloc(1, sizeof(*proc)));
if (proc == NULL) {
D("not enough memory to create new JDWP process\n");
@@ -234,7 +235,7 @@
static void
jdwp_process_event( int socket, unsigned events, void* _proc )
{
- JdwpProcess* proc = _proc;
+ JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
if (events & FDE_READ) {
if (proc->pid < 0) {
@@ -415,6 +416,7 @@
__FUNCTION__, strerror(errno));
return -1;
}
+ D("socketpair: (%d,%d)", fds[0], fds[1]);
proc->out_fds[ proc->out_count ] = fds[1];
if (++proc->out_count == 1)
@@ -600,7 +602,7 @@
asocket*
create_jdwp_service_socket( void )
{
- JdwpSocket* s = calloc(sizeof(*s),1);
+ JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
if (s == NULL)
return NULL;
@@ -695,7 +697,7 @@
asocket*
create_jdwp_tracker_service_socket( void )
{
- JdwpTracker* t = calloc(sizeof(*t),1);
+ JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1));
if (t == NULL)
return NULL;
diff --git a/adb/qemu_tracing.cpp b/adb/qemu_tracing.cpp
new file mode 100644
index 0000000..f31eae8
--- /dev/null
+++ b/adb/qemu_tracing.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/*
+ * Implements ADB tracing inside the emulator.
+ */
+
+#include <stdarg.h>
+
+#include "sysdeps.h"
+#include "qemu_tracing.h"
+
+/*
+ * Redefine open and write for qemu_pipe.h that contains inlined references
+ * to those routines. We will redifine them back after qemu_pipe.h inclusion.
+ */
+
+#undef open
+#undef write
+#define open adb_open
+#define write adb_write
+#include <hardware/qemu_pipe.h>
+#undef open
+#undef write
+#define open ___xxx_open
+#define write ___xxx_write
+
+/* A handle to adb-debug qemud service in the emulator. */
+int adb_debug_qemu = -1;
+
+/* Initializes connection with the adb-debug qemud service in the emulator. */
+int adb_qemu_trace_init(void)
+{
+ char con_name[32];
+
+ if (adb_debug_qemu >= 0) {
+ return 0;
+ }
+
+ /* adb debugging QEMUD service connection request. */
+ snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
+ adb_debug_qemu = qemu_pipe_open(con_name);
+ return (adb_debug_qemu >= 0) ? 0 : -1;
+}
+
+void adb_qemu_trace(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ char msg[1024];
+
+ if (adb_debug_qemu >= 0) {
+ vsnprintf(msg, sizeof(msg), fmt, args);
+ adb_write(adb_debug_qemu, msg, strlen(msg));
+ }
+}
diff --git a/include/cutils/cpu_info.h b/adb/qemu_tracing.h
similarity index 61%
rename from include/cutils/cpu_info.h
rename to adb/qemu_tracing.h
index 78c1884..bf80457 100644
--- a/include/cutils/cpu_info.h
+++ b/adb/qemu_tracing.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2015 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.
@@ -14,21 +14,23 @@
* limitations under the License.
*/
-#ifndef __CUTILS_CPU_INFO_H
-#define __CUTILS_CPU_INFO_H
+/*
+ * Implements ADB tracing inside the emulator.
+ */
+
+#ifndef __QEMU_TRACING_H
+#define __QEMU_TRACING_H
#ifdef __cplusplus
extern "C" {
#endif
-/* returns a string contiaining an ASCII representation of the CPU serial number,
-** or NULL if cpu info not available.
-** The string is a static variable, so don't call free() on it.
-*/
-extern const char* get_cpu_serial_number(void);
-
+/* Initializes connection with the adb-debug qemud service in the emulator. */
+int adb_qemu_trace_init(void);
+void adb_qemu_trace(const char* fmt, ...);
+
#ifdef __cplusplus
}
#endif
-#endif /* __CUTILS_CPU_INFO_H */
+#endif /* __QEMU_TRACING_H */
diff --git a/adb/remount_service.c b/adb/remount_service.c
deleted file mode 100644
index ad61284..0000000
--- a/adb/remount_service.c
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright (C) 2008 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 <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
-#include <sys/mount.h>
-#include <errno.h>
-
-#include "sysdeps.h"
-
-#define TRACE_TAG TRACE_ADB
-#include "adb.h"
-
-
-static int system_ro = 1;
-
-/* Returns the device used to mount a directory in /proc/mounts */
-static char *find_mount(const char *dir)
-{
- int fd;
- int res;
- int size;
- char *token = NULL;
- const char delims[] = "\n";
- char buf[4096];
-
- fd = unix_open("/proc/mounts", O_RDONLY);
- if (fd < 0)
- return NULL;
-
- buf[sizeof(buf) - 1] = '\0';
- size = adb_read(fd, buf, sizeof(buf) - 1);
- adb_close(fd);
-
- token = strtok(buf, delims);
-
- while (token) {
- char mount_dev[256];
- char mount_dir[256];
- int mount_freq;
- int mount_passno;
-
- res = sscanf(token, "%255s %255s %*s %*s %d %d\n",
- mount_dev, mount_dir, &mount_freq, &mount_passno);
- mount_dev[255] = 0;
- mount_dir[255] = 0;
- if (res == 4 && (strcmp(dir, mount_dir) == 0))
- return strdup(mount_dev);
-
- token = strtok(NULL, delims);
- }
- return NULL;
-}
-
-/* Init mounts /system as read only, remount to enable writes. */
-static int remount_system()
-{
- char *dev;
- int fd;
- int OFF = 0;
-
- if (system_ro == 0) {
- return 0;
- }
-
- dev = find_mount("/system");
-
- if (!dev)
- return -1;
-
- fd = unix_open(dev, O_RDONLY);
- if (fd < 0)
- return -1;
-
- ioctl(fd, BLKROSET, &OFF);
- adb_close(fd);
-
- system_ro = mount(dev, "/system", "none", MS_REMOUNT, NULL);
-
- free(dev);
-
- return system_ro;
-}
-
-static void write_string(int fd, const char* str)
-{
- writex(fd, str, strlen(str));
-}
-
-void remount_service(int fd, void *cookie)
-{
- int ret = remount_system();
-
- if (!ret)
- write_string(fd, "remount succeeded\n");
- else {
- char buffer[200];
- snprintf(buffer, sizeof(buffer), "remount failed: %s\n", strerror(errno));
- write_string(fd, buffer);
- }
-
- adb_close(fd);
-}
-
diff --git a/adb/remount_service.cpp b/adb/remount_service.cpp
new file mode 100644
index 0000000..a83d5b1
--- /dev/null
+++ b/adb/remount_service.cpp
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2008 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 <errno.h>
+#include <fcntl.h>
+#include <mntent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <unistd.h>
+
+#include <string>
+
+#include "sysdeps.h"
+
+#define TRACE_TAG TRACE_ADB
+#include "adb.h"
+#include "adb_io.h"
+#include "cutils/properties.h"
+
+static int system_ro = 1;
+static int vendor_ro = 1;
+static int oem_ro = 1;
+
+/* Returns the device used to mount a directory in /proc/mounts */
+static std::string find_mount(const char *dir) {
+ FILE* fp;
+ struct mntent* mentry;
+ char* device = NULL;
+
+ if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
+ return NULL;
+ }
+ while ((mentry = getmntent(fp)) != NULL) {
+ if (strcmp(dir, mentry->mnt_dir) == 0) {
+ device = mentry->mnt_fsname;
+ break;
+ }
+ }
+ endmntent(fp);
+ return device;
+}
+
+static bool has_partition(const char* path) {
+ struct stat sb;
+ return (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
+}
+
+int make_block_device_writable(const std::string& dev) {
+ int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
+ if (fd == -1) {
+ return -1;
+ }
+
+ int result = -1;
+ int OFF = 0;
+ if (!ioctl(fd, BLKROSET, &OFF)) {
+ result = 0;
+ }
+ adb_close(fd);
+
+ return result;
+}
+
+// Init mounts /system as read only, remount to enable writes.
+static int remount(const char* dir, int* dir_ro) {
+ std::string dev(find_mount(dir));
+ if (dev.empty() || make_block_device_writable(dev)) {
+ return -1;
+ }
+
+ int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
+ *dir_ro = rc;
+ return rc;
+}
+
+static bool remount_partition(int fd, const char* partition, int* ro) {
+ if (!has_partition(partition)) {
+ return true;
+ }
+ if (remount(partition, ro)) {
+ char buf[200];
+ snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
+ WriteStringFully(fd, buf);
+ return false;
+ }
+ return true;
+}
+
+void remount_service(int fd, void* cookie) {
+ char prop_buf[PROPERTY_VALUE_MAX];
+
+ if (getuid() != 0) {
+ WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
+ adb_close(fd);
+ return;
+ }
+
+ bool system_verified = false, vendor_verified = false;
+ property_get("partition.system.verified", prop_buf, "0");
+ if (!strcmp(prop_buf, "1")) {
+ system_verified = true;
+ }
+
+ property_get("partition.vendor.verified", prop_buf, "0");
+ if (!strcmp(prop_buf, "1")) {
+ vendor_verified = true;
+ }
+
+ if (system_verified || vendor_verified) {
+ // Allow remount but warn of likely bad effects
+ bool both = system_verified && vendor_verified;
+ char buffer[200];
+ snprintf(buffer, sizeof(buffer),
+ "dm_verity is enabled on the %s%s%s partition%s.\n",
+ system_verified ? "system" : "",
+ both ? " and " : "",
+ vendor_verified ? "vendor" : "",
+ both ? "s" : "");
+ WriteStringFully(fd, buffer);
+ snprintf(buffer, sizeof(buffer),
+ "Use \"adb disable-verity\" to disable verity.\n"
+ "If you do not, remount may succeed, however, you will still "
+ "not be able to write to these volumes.\n");
+ WriteStringFully(fd, buffer);
+ }
+
+ bool success = true;
+ success &= remount_partition(fd, "/system", &system_ro);
+ success &= remount_partition(fd, "/vendor", &vendor_ro);
+ success &= remount_partition(fd, "/oem", &oem_ro);
+
+ WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n");
+
+ adb_close(fd);
+}
diff --git a/adb/usb_vendors.h b/adb/remount_service.h
similarity index 72%
copy from adb/usb_vendors.h
copy to adb/remount_service.h
index cee23a1..e1763cf 100644
--- a/adb/usb_vendors.h
+++ b/adb/remount_service.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2015 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.
@@ -14,12 +14,12 @@
* limitations under the License.
*/
-#ifndef __USB_VENDORS_H
-#define __USB_VENDORS_H
+#ifndef _REMOUNT_SERVICE_H_
+#define _REMOUNT_SERVICE_H_
-extern int vendorIds[];
-extern unsigned vendorIdCount;
+#include <string>
-void usb_vendors_init(void);
+int make_block_device_writable(const std::string&);
+void remount_service(int, void*);
#endif
diff --git a/adb/services.c b/adb/services.cpp
similarity index 67%
rename from adb/services.c
rename to adb/services.cpp
index dcaf276..e7bf6b0 100644
--- a/adb/services.c
+++ b/adb/services.cpp
@@ -14,29 +14,32 @@
* limitations under the License.
*/
-#include <stddef.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef _WIN32
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#endif
+
+#if !ADB_HOST
+#include "cutils/android_reboot.h"
+#include "cutils/properties.h"
+#endif
#include "sysdeps.h"
#define TRACE_TAG TRACE_SERVICES
#include "adb.h"
+#include "adb_io.h"
#include "file_sync_service.h"
-
-#if ADB_HOST
-# ifndef HAVE_WINSOCK
-# include <netinet/in.h>
-# include <netdb.h>
-# include <sys/ioctl.h>
-# endif
-#else
-# include <cutils/android_reboot.h>
-# include <cutils/properties.h>
-#endif
+#include "remount_service.h"
+#include "transport.h"
typedef struct stinfo stinfo;
@@ -49,7 +52,7 @@
void *service_bootstrap_func(void *x)
{
- stinfo *sti = x;
+ stinfo* sti = reinterpret_cast<stinfo*>(x);
sti->func(sti->fd, sti->cookie);
free(sti);
return 0;
@@ -64,20 +67,36 @@
if (getuid() == 0) {
snprintf(buf, sizeof(buf), "adbd is already running as root\n");
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
} else {
property_get("ro.debuggable", value, "");
if (strcmp(value, "1") != 0) {
snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
return;
}
property_set("service.adb.root", "1");
snprintf(buf, sizeof(buf), "restarting adbd as root\n");
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
+ adb_close(fd);
+ }
+}
+
+void restart_unroot_service(int fd, void *cookie)
+{
+ char buf[100];
+
+ if (getuid() != 0) {
+ snprintf(buf, sizeof(buf), "adbd not running as root\n");
+ WriteFdExactly(fd, buf, strlen(buf));
+ adb_close(fd);
+ } else {
+ property_set("service.adb.root", "0");
+ snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
}
}
@@ -90,7 +109,7 @@
if (port <= 0) {
snprintf(buf, sizeof(buf), "invalid port\n");
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
return;
}
@@ -98,7 +117,7 @@
snprintf(value, sizeof(value), "%d", port);
property_set("service.adb.tcp.port", value);
snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
}
@@ -108,7 +127,7 @@
property_set("service.adb.tcp.port", "0");
snprintf(buf, sizeof(buf), "restarting in USB mode\n");
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
adb_close(fd);
}
@@ -123,14 +142,14 @@
ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
if (ret >= (int) sizeof(property_val)) {
snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
goto cleanup;
}
ret = property_set(ANDROID_RB_PROPERTY, property_val);
if (ret < 0) {
snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
- writex(fd, buf, strlen(buf));
+ WriteFdExactly(fd, buf, strlen(buf));
goto cleanup;
}
// Don't return early. Give the reboot command time to take effect
@@ -141,26 +160,38 @@
adb_close(fd);
}
+void reverse_service(int fd, void* arg)
+{
+ const char* command = reinterpret_cast<const char*>(arg);
+
+ if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
+ sendfailmsg(fd, "not a reverse forwarding command");
+ }
+ free(arg);
+ adb_close(fd);
+}
+
#endif
static int create_service_thread(void (*func)(int, void *), void *cookie)
{
- stinfo *sti;
- adb_thread_t t;
int s[2];
-
- if(adb_socketpair(s)) {
+ if (adb_socketpair(s)) {
printf("cannot create service socket pair\n");
return -1;
}
+ D("socketpair: (%d,%d)", s[0], s[1]);
- sti = malloc(sizeof(stinfo));
- if(sti == 0) fatal("cannot allocate stinfo");
+ stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
+ if (sti == nullptr) {
+ fatal("cannot allocate stinfo");
+ }
sti->func = func;
sti->cookie = cookie;
sti->fd = s[1];
- if(adb_thread_create( &t, service_bootstrap_func, sti)){
+ adb_thread_t t;
+ if (adb_thread_create(&t, service_bootstrap_func, sti)) {
free(sti);
adb_close(s[0]);
adb_close(s[1]);
@@ -173,25 +204,38 @@
}
#if !ADB_HOST
-static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
+
+static void init_subproc_child()
{
-#ifdef HAVE_WIN32_PROC
- D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
- fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
+ setsid();
+
+ // Set OOM score adjustment to prevent killing
+ int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
+ if (fd >= 0) {
+ adb_write(fd, "0", 1);
+ adb_close(fd);
+ } else {
+ D("adb: unable to update oom_score_adj\n");
+ }
+}
+
+static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
+{
+ D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
+#if defined(_WIN32)
+ fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
return -1;
-#else /* !HAVE_WIN32_PROC */
- char *devname;
+#else
int ptm;
- ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
+ ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
if(ptm < 0){
printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
return -1;
}
- fcntl(ptm, F_SETFD, FD_CLOEXEC);
- if(grantpt(ptm) || unlockpt(ptm) ||
- ((devname = (char*) ptsname(ptm)) == 0)){
+ char devname[64];
+ if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
adb_close(ptm);
return -1;
@@ -204,46 +248,75 @@
return -1;
}
- if(*pid == 0){
- int pts;
+ if (*pid == 0) {
+ init_subproc_child();
- setsid();
-
- pts = unix_open(devname, O_RDWR);
- if(pts < 0) {
+ int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
+ if (pts < 0) {
fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
exit(-1);
}
- dup2(pts, 0);
- dup2(pts, 1);
- dup2(pts, 2);
+ dup2(pts, STDIN_FILENO);
+ dup2(pts, STDOUT_FILENO);
+ dup2(pts, STDERR_FILENO);
adb_close(pts);
adb_close(ptm);
- // set OOM adjustment to zero
- char text[64];
- snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
- int fd = adb_open(text, O_WRONLY);
- if (fd >= 0) {
- adb_write(fd, "0", 1);
- adb_close(fd);
- } else {
- D("adb: unable to open %s\n", text);
- }
execl(cmd, cmd, arg0, arg1, NULL);
fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
cmd, strerror(errno), errno);
exit(-1);
} else {
- // Don't set child's OOM adjustment to zero.
- // Let the child do it itself, as sometimes the parent starts
- // running before the child has a /proc/pid/oom_adj.
- // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
return ptm;
}
-#endif /* !HAVE_WIN32_PROC */
+#endif /* !defined(_WIN32) */
+}
+
+static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
+{
+ D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
+#if defined(_WIN32)
+ fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
+ return -1;
+#else
+
+ // 0 is parent socket, 1 is child socket
+ int sv[2];
+ if (adb_socketpair(sv) < 0) {
+ printf("[ cannot create socket pair - %s ]\n", strerror(errno));
+ return -1;
+ }
+ D("socketpair: (%d,%d)", sv[0], sv[1]);
+
+ *pid = fork();
+ if (*pid < 0) {
+ printf("- fork failed: %s -\n", strerror(errno));
+ adb_close(sv[0]);
+ adb_close(sv[1]);
+ return -1;
+ }
+
+ if (*pid == 0) {
+ adb_close(sv[0]);
+ init_subproc_child();
+
+ dup2(sv[1], STDIN_FILENO);
+ dup2(sv[1], STDOUT_FILENO);
+ dup2(sv[1], STDERR_FILENO);
+
+ adb_close(sv[1]);
+
+ execl(cmd, cmd, arg0, arg1, NULL);
+ fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
+ cmd, strerror(errno), errno);
+ exit(-1);
+ } else {
+ adb_close(sv[1]);
+ return sv[0];
+ }
+#endif /* !defined(_WIN32) */
}
#endif /* !ABD_HOST */
@@ -279,39 +352,52 @@
D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
if (SHELL_EXIT_NOTIFY_FD >=0) {
int res;
- res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
+ res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
SHELL_EXIT_NOTIFY_FD, pid, res, errno);
}
}
-static int create_subproc_thread(const char *name)
+static int create_subproc_thread(const char *name, const subproc_mode mode)
{
- stinfo *sti;
adb_thread_t t;
int ret_fd;
- pid_t pid;
- if(name) {
- ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
- } else {
- ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
- }
- D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
+ pid_t pid = -1;
- sti = malloc(sizeof(stinfo));
+ const char *arg0, *arg1;
+ if (name == 0 || *name == 0) {
+ arg0 = "-"; arg1 = 0;
+ } else {
+ arg0 = "-c"; arg1 = name;
+ }
+
+ switch (mode) {
+ case SUBPROC_PTY:
+ ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
+ break;
+ case SUBPROC_RAW:
+ ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
+ break;
+ default:
+ fprintf(stderr, "invalid subproc_mode %d\n", mode);
+ return -1;
+ }
+ D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
+
+ stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
if(sti == 0) fatal("cannot allocate stinfo");
sti->func = subproc_waiter_service;
sti->cookie = (void*) (uintptr_t) pid;
sti->fd = ret_fd;
- if(adb_thread_create( &t, service_bootstrap_func, sti)){
+ if (adb_thread_create(&t, service_bootstrap_func, sti)) {
free(sti);
adb_close(ret_fd);
- printf("cannot create service thread\n");
+ fprintf(stderr, "cannot create service thread\n");
return -1;
}
- D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
+ D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
return ret_fd;
}
#endif
@@ -350,41 +436,65 @@
#endif
#if !ADB_HOST
} else if(!strncmp("dev:", name, 4)) {
- ret = unix_open(name + 4, O_RDWR);
+ ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
} else if(!strncmp(name, "framebuffer:", 12)) {
ret = create_service_thread(framebuffer_service, 0);
} else if (!strncmp(name, "jdwp:", 5)) {
ret = create_jdwp_connection_fd(atoi(name+5));
} else if(!HOST && !strncmp(name, "shell:", 6)) {
- if(name[6]) {
- ret = create_subproc_thread(name + 6);
- } else {
- ret = create_subproc_thread(0);
- }
+ ret = create_subproc_thread(name + 6, SUBPROC_PTY);
+ } else if(!HOST && !strncmp(name, "exec:", 5)) {
+ ret = create_subproc_thread(name + 5, SUBPROC_RAW);
} else if(!strncmp(name, "sync:", 5)) {
ret = create_service_thread(file_sync_service, NULL);
} else if(!strncmp(name, "remount:", 8)) {
ret = create_service_thread(remount_service, NULL);
} else if(!strncmp(name, "reboot:", 7)) {
void* arg = strdup(name + 7);
- if(arg == 0) return -1;
+ if (arg == NULL) return -1;
ret = create_service_thread(reboot_service, arg);
} else if(!strncmp(name, "root:", 5)) {
ret = create_service_thread(restart_root_service, NULL);
+ } else if(!strncmp(name, "unroot:", 7)) {
+ ret = create_service_thread(restart_unroot_service, NULL);
} else if(!strncmp(name, "backup:", 7)) {
- char* arg = strdup(name+7);
+ char* arg = strdup(name + 7);
if (arg == NULL) return -1;
- ret = backup_service(BACKUP, arg);
+ char* c = arg;
+ for (; *c != '\0'; c++) {
+ if (*c == ':')
+ *c = ' ';
+ }
+ char* cmd;
+ if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
+ ret = create_subproc_thread(cmd, SUBPROC_RAW);
+ free(cmd);
+ }
+ free(arg);
} else if(!strncmp(name, "restore:", 8)) {
- ret = backup_service(RESTORE, NULL);
+ ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
} else if(!strncmp(name, "tcpip:", 6)) {
int port;
- if (sscanf(name + 6, "%d", &port) == 0) {
+ if (sscanf(name + 6, "%d", &port) != 1) {
port = 0;
}
ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
} else if(!strncmp(name, "usb:", 4)) {
ret = create_service_thread(restart_usb_service, NULL);
+ } else if (!strncmp(name, "reverse:", 8)) {
+ char* cookie = strdup(name + 8);
+ if (cookie == NULL) {
+ ret = -1;
+ } else {
+ ret = create_service_thread(reverse_service, cookie);
+ if (ret < 0) {
+ free(cookie);
+ }
+ }
+ } else if(!strncmp(name, "disable-verity:", 15)) {
+ ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
+ } else if(!strncmp(name, "enable-verity:", 15)) {
+ ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
#endif
}
if (ret >= 0) {
@@ -402,14 +512,14 @@
static void wait_for_state(int fd, void* cookie)
{
- struct state_info* sinfo = cookie;
- char* err = "unknown error";
+ state_info* sinfo = reinterpret_cast<state_info*>(cookie);
D("wait_for_state %d\n", sinfo->state);
+ const char* err = "unknown error";
atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
if(t != 0) {
- writex(fd, "OKAY", 4);
+ WriteFdExactly(fd, "OKAY", 4);
} else {
sendfailmsg(fd, err);
}
@@ -437,7 +547,7 @@
}
// zero terminate the host at the point we found the colon
hostbuf[portstr - host] = 0;
- if (sscanf(portstr + 1, "%d", &port) == 0) {
+ if (sscanf(portstr + 1, "%d", &port) != 1) {
snprintf(buffer, buffer_size, "bad port number %s", portstr);
return;
}
@@ -447,7 +557,7 @@
snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
- fd = socket_network_client(hostbuf, port, SOCK_STREAM);
+ fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
if (fd < 0) {
snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
return;
@@ -525,7 +635,7 @@
{
char buf[4096];
char resp[4096];
- char *host = cookie;
+ char *host = reinterpret_cast<char*>(cookie);
if (!strncmp(host, "emu:", 4)) {
connect_emulator(host + 4, buf, sizeof(buf));
@@ -535,7 +645,7 @@
// Send response for emulator and device
snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
- writex(fd, resp, strlen(resp));
+ WriteFdExactly(fd, resp, strlen(resp));
adb_close(fd);
}
#endif
@@ -546,7 +656,7 @@
if (!strcmp(name,"track-devices")) {
return create_device_tracker();
} else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
- struct state_info* sinfo = malloc(sizeof(struct state_info));
+ auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
if (serial)
sinfo->serial = strdup(serial);
diff --git a/adb/set_verity_enable_state_service.cpp b/adb/set_verity_enable_state_service.cpp
new file mode 100644
index 0000000..139b074
--- /dev/null
+++ b/adb/set_verity_enable_state_service.cpp
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2014 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 <fcntl.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <sys/stat.h>
+
+#define TRACE_TAG TRACE_ADB
+#include "adb.h"
+#include "cutils/properties.h"
+#include "ext4_sb.h"
+#include "fs_mgr.h"
+#include "remount_service.h"
+#include "sysdeps.h"
+
+#define FSTAB_PREFIX "/fstab."
+struct fstab *fstab;
+
+#ifdef ALLOW_ADBD_DISABLE_VERITY
+static const bool kAllowDisableVerity = true;
+#else
+static const bool kAllowDisableVerity = false;
+#endif
+
+__attribute__((__format__(printf, 2, 3))) __nonnull((2))
+static void write_console(int fd, const char* format, ...)
+{
+ char buffer[256];
+ va_list args;
+ va_start (args, format);
+ vsnprintf (buffer, sizeof(buffer), format, args);
+ va_end (args);
+
+ adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
+}
+
+static int get_target_device_size(int fd, const char *blk_device,
+ uint64_t *device_size)
+{
+ int data_device;
+ struct ext4_super_block sb;
+ struct fs_info info;
+
+ info.len = 0; /* Only len is set to 0 to ask the device for real size. */
+
+ data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
+ if (data_device < 0) {
+ write_console(fd, "Error opening block device (%s)\n", strerror(errno));
+ return -1;
+ }
+
+ if (lseek64(data_device, 1024, SEEK_SET) < 0) {
+ write_console(fd, "Error seeking to superblock\n");
+ adb_close(data_device);
+ return -1;
+ }
+
+ if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
+ write_console(fd, "Error reading superblock\n");
+ adb_close(data_device);
+ return -1;
+ }
+
+ ext4_parse_sb(&sb, &info);
+ *device_size = info.len;
+
+ adb_close(data_device);
+ return 0;
+}
+
+/* Turn verity on/off */
+static int set_verity_enabled_state(int fd, const char *block_device,
+ const char* mount_point, bool enable)
+{
+ uint32_t magic_number;
+ const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
+ : VERITY_METADATA_MAGIC_DISABLE;
+ uint64_t device_length = 0;
+ int device = -1;
+ int retval = -1;
+
+ if (make_block_device_writable(block_device)) {
+ write_console(fd, "Could not make block device %s writable (%s).\n",
+ block_device, strerror(errno));
+ goto errout;
+ }
+
+ device = adb_open(block_device, O_RDWR | O_CLOEXEC);
+ if (device == -1) {
+ write_console(fd, "Could not open block device %s (%s).\n",
+ block_device, strerror(errno));
+ write_console(fd, "Maybe run adb remount?\n");
+ goto errout;
+ }
+
+ // find the start of the verity metadata
+ if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
+ write_console(fd, "Could not get target device size.\n");
+ goto errout;
+ }
+
+ if (lseek64(device, device_length, SEEK_SET) < 0) {
+ write_console(fd,
+ "Could not seek to start of verity metadata block.\n");
+ goto errout;
+ }
+
+ // check the magic number
+ if (adb_read(device, &magic_number, sizeof(magic_number))
+ != sizeof(magic_number)) {
+ write_console(fd, "Couldn't read magic number!\n");
+ goto errout;
+ }
+
+ if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
+ write_console(fd, "Verity already disabled on %s\n", mount_point);
+ goto errout;
+ }
+
+ if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
+ write_console(fd, "Verity already enabled on %s\n", mount_point);
+ goto errout;
+ }
+
+ if (magic_number != VERITY_METADATA_MAGIC_NUMBER
+ && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
+ write_console(fd,
+ "Couldn't find verity metadata at offset %" PRIu64 "!\n",
+ device_length);
+ goto errout;
+ }
+
+ if (lseek64(device, device_length, SEEK_SET) < 0) {
+ write_console(fd,
+ "Could not seek to start of verity metadata block.\n");
+ goto errout;
+ }
+
+ if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
+ write_console(
+ fd, "Could not set verity %s flag on device %s with error %s\n",
+ enable ? "enabled" : "disabled",
+ block_device, strerror(errno));
+ goto errout;
+ }
+
+ write_console(fd, "Verity %s on %s\n",
+ enable ? "enabled" : "disabled",
+ mount_point);
+ retval = 0;
+errout:
+ if (device != -1)
+ adb_close(device);
+ return retval;
+}
+
+void set_verity_enabled_state_service(int fd, void* cookie)
+{
+ bool enable = (cookie != NULL);
+ if (kAllowDisableVerity) {
+ char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+ char propbuf[PROPERTY_VALUE_MAX];
+ int i;
+ bool any_changed = false;
+
+ property_get("ro.secure", propbuf, "0");
+ if (strcmp(propbuf, "1")) {
+ write_console(fd, "verity not enabled - ENG build\n");
+ goto errout;
+ }
+
+ property_get("ro.debuggable", propbuf, "0");
+ if (strcmp(propbuf, "1")) {
+ write_console(
+ fd, "verity cannot be disabled/enabled - USER build\n");
+ goto errout;
+ }
+
+ property_get("ro.hardware", propbuf, "");
+ snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
+ propbuf);
+
+ fstab = fs_mgr_read_fstab(fstab_filename);
+ if (!fstab) {
+ write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
+ fstab_filename);
+ goto errout;
+ }
+
+ /* Loop through entries looking for ones that vold manages */
+ for (i = 0; i < fstab->num_entries; i++) {
+ if(fs_mgr_is_verified(&fstab->recs[i])) {
+ if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
+ fstab->recs[i].mount_point,
+ enable)) {
+ any_changed = true;
+ }
+ }
+ }
+
+ if (any_changed) {
+ write_console(
+ fd, "Now reboot your device for settings to take effect\n");
+ }
+ } else {
+ write_console(fd, "%s-verity only works for userdebug builds\n",
+ enable ? "enable" : "disable");
+ }
+
+errout:
+ adb_close(fd);
+}
diff --git a/adb/sockets.c b/adb/sockets.cpp
similarity index 89%
rename from adb/sockets.c
rename to adb/sockets.cpp
index de14a22..12bc8d8 100644
--- a/adb/sockets.c
+++ b/adb/sockets.cpp
@@ -14,17 +14,22 @@
* limitations under the License.
*/
+#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
#include <string.h>
-#include <ctype.h>
+#include <unistd.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_SOCKETS
#include "adb.h"
+#include "adb_io.h"
+#if !ADB_HOST
+#include "cutils/properties.h"
+#endif
+#include "transport.h"
ADB_MUTEX_DEFINE( socket_list_lock );
@@ -35,13 +40,17 @@
char buf[9];
int len;
len = strlen(reason);
- if(len > 0xffff) len = 0xffff;
- snprintf(buf, sizeof buf, "FAIL%04x", len);
- if(writex(fd, buf, 8)) return -1;
- return writex(fd, reason, len);
-}
+ if (len > 0xffff) {
+ len = 0xffff;
+ }
-//extern int online;
+ snprintf(buf, sizeof buf, "FAIL%04x", len);
+ if (!WriteFdExactly(fd, buf, 8)) {
+ return -1;
+ }
+
+ return WriteFdExactly(fd, reason, len) ? 0 : -1;
+}
static unsigned local_socket_next_id = 1;
@@ -192,10 +201,9 @@
static void local_socket_ready(asocket *s)
{
- /* far side is ready for data, pay attention to
- readable events */
+ /* far side is ready for data, pay attention to
+ readable events */
fdevent_add(&s->fde, FDE_READ);
-// D("LS(%d): ready()\n", s->id);
}
static void local_socket_close(asocket *s)
@@ -236,7 +244,7 @@
static void local_socket_close_locked(asocket *s)
{
- D("entered. LS(%d) fd=%d\n", s->id, s->fd);
+ D("entered local_socket_close_locked. LS(%d) fd=%d\n", s->id, s->fd);
if(s->peer) {
D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
s->id, s->peer->id, s->peer->fd);
@@ -276,98 +284,101 @@
insert_local_socket(s, &local_socket_closing_list);
}
-static void local_socket_event_func(int fd, unsigned ev, void *_s)
+static void local_socket_event_func(int fd, unsigned ev, void* _s)
{
- asocket *s = _s;
-
+ asocket* s = reinterpret_cast<asocket*>(_s);
D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
/* put the FDE_WRITE processing before the FDE_READ
** in order to simplify the code.
*/
- if(ev & FDE_WRITE){
- apacket *p;
-
- while((p = s->pkt_first) != 0) {
- while(p->len > 0) {
+ if (ev & FDE_WRITE) {
+ apacket* p;
+ while ((p = s->pkt_first) != nullptr) {
+ while (p->len > 0) {
int r = adb_write(fd, p->ptr, p->len);
- if(r > 0) {
+ if (r == -1) {
+ /* returning here is ok because FDE_READ will
+ ** be processed in the next iteration loop
+ */
+ if (errno == EAGAIN) {
+ return;
+ }
+ } else if (r > 0) {
p->ptr += r;
p->len -= r;
continue;
}
- if(r < 0) {
- /* returning here is ok because FDE_READ will
- ** be processed in the next iteration loop
- */
- if(errno == EAGAIN) return;
- if(errno == EINTR) continue;
- }
+
D(" closing after write because r=%d and errno is %d\n", r, errno);
s->close(s);
return;
}
- if(p->len == 0) {
+ if (p->len == 0) {
s->pkt_first = p->next;
- if(s->pkt_first == 0) s->pkt_last = 0;
+ if (s->pkt_first == 0) {
+ s->pkt_last = 0;
+ }
put_apacket(p);
}
}
- /* if we sent the last packet of a closing socket,
- ** we can now destroy it.
- */
+ /* if we sent the last packet of a closing socket,
+ ** we can now destroy it.
+ */
if (s->closing) {
D(" closing because 'closing' is set after write\n");
s->close(s);
return;
}
- /* no more packets queued, so we can ignore
- ** writable events again and tell our peer
- ** to resume writing
- */
+ /* no more packets queued, so we can ignore
+ ** writable events again and tell our peer
+ ** to resume writing
+ */
fdevent_del(&s->fde, FDE_WRITE);
s->peer->ready(s->peer);
}
- if(ev & FDE_READ){
+ if (ev & FDE_READ) {
apacket *p = get_apacket();
unsigned char *x = p->data;
size_t avail = MAX_PAYLOAD;
int r;
int is_eof = 0;
- while(avail > 0) {
+ while (avail > 0) {
r = adb_read(fd, x, avail);
- D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail);
- if(r > 0) {
+ D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n",
+ s->id, s->fd, r, r < 0 ? errno : 0, avail);
+ if (r == -1) {
+ if (errno == EAGAIN) {
+ break;
+ }
+ } else if (r > 0) {
avail -= r;
x += r;
continue;
}
- if(r < 0) {
- if(errno == EAGAIN) break;
- if(errno == EINTR) continue;
- }
- /* r = 0 or unhandled error */
+ /* r = 0 or unhandled error */
is_eof = 1;
break;
}
D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
s->id, s->fd, r, is_eof, s->fde.force_eof);
- if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
+ if ((avail == MAX_PAYLOAD) || (s->peer == 0)) {
put_apacket(p);
} else {
p->len = MAX_PAYLOAD - avail;
r = s->peer->enqueue(s->peer, p);
- D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
+ D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd,
+ r);
- if(r < 0) {
+ if (r < 0) {
/* error return means they closed us as a side-effect
** and we must return immediately.
**
@@ -379,7 +390,7 @@
return;
}
- if(r > 0) {
+ if (r > 0) {
/* if the remote cannot accept further events,
** we disable notification of READs. They'll
** be enabled again when we get a call to ready()
@@ -388,18 +399,18 @@
}
}
/* Don't allow a forced eof if data is still there */
- if((s->fde.force_eof && !r) || is_eof) {
- D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
+ if ((s->fde.force_eof && !r) || is_eof) {
+ D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n",
+ is_eof, r, s->fde.force_eof);
s->close(s);
}
}
- if(ev & FDE_ERROR){
+ if (ev & FDE_ERROR){
/* this should be caught be the next read or write
** catching it here means we may skip the last few
** bytes of readable data.
*/
-// s->close(s);
D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
return;
@@ -408,7 +419,7 @@
asocket *create_local_socket(int fd)
{
- asocket *s = calloc(1, sizeof(asocket));
+ asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
if (s == NULL) fatal("cannot allocate socket");
s->fd = fd;
s->enqueue = local_socket_enqueue;
@@ -418,17 +429,12 @@
install_local_socket(s);
fdevent_install(&s->fde, fd, local_socket_event_func, s);
-/* fdevent_add(&s->fde, FDE_ERROR); */
- //fprintf(stderr, "Created local socket in create_local_socket \n");
D("LS(%d): created (fd=%d)\n", s->id, s->fd);
return s;
}
asocket *create_local_service_socket(const char *name)
{
- asocket *s;
- int fd;
-
#if !ADB_HOST
if (!strcmp(name,"jdwp")) {
return create_jdwp_service_socket();
@@ -437,14 +443,19 @@
return create_jdwp_tracker_service_socket();
}
#endif
- fd = service_to_fd(name);
+ int fd = service_to_fd(name);
if(fd < 0) return 0;
- s = create_local_socket(fd);
+ asocket* s = create_local_socket(fd);
D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
#if !ADB_HOST
- if ((!strncmp(name, "root:", 5) && getuid() != 0)
+ char debug[PROPERTY_VALUE_MAX];
+ if (!strncmp(name, "root:", 5))
+ property_get("ro.debuggable", debug, "");
+
+ if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
+ || (!strncmp(name, "unroot:", 7) && getuid() == 0)
|| !strncmp(name, "usb:", 4)
|| !strncmp(name, "tcpip:", 6)) {
D("LS(%d): enabling exit_on_close\n", s->id);
@@ -532,8 +543,8 @@
static void remote_socket_disconnect(void* _s, atransport* t)
{
- asocket* s = _s;
- asocket* peer = s->peer;
+ asocket* s = reinterpret_cast<asocket*>(_s);
+ asocket* peer = s->peer;
D("remote_socket_disconnect RS(%d)\n", s->id);
if (peer) {
@@ -550,12 +561,9 @@
Returns a new non-NULL asocket handle. */
asocket *create_remote_socket(unsigned id, atransport *t)
{
- asocket* s;
- adisconnect* dis;
-
if (id == 0) fatal("invalid remote socket id (0)");
- s = calloc(1, sizeof(aremotesocket));
- dis = &((aremotesocket*)s)->disconnect;
+ asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(aremotesocket)));
+ adisconnect* dis = &reinterpret_cast<aremotesocket*>(s)->disconnect;
if (s == NULL) fatal("cannot allocate socket");
s->id = id;
@@ -817,7 +825,7 @@
}
#else /* !ADB_HOST */
if (s->transport == NULL) {
- char* error_string = "unknown failure";
+ const char* error_string = "unknown failure";
s->transport = acquire_one_transport (CS_ANY,
kTransportAny, NULL, &error_string);
@@ -885,7 +893,7 @@
static asocket *create_smart_socket(void)
{
D("Creating smart socket \n");
- asocket *s = calloc(1, sizeof(asocket));
+ asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
if (s == NULL) fatal("cannot allocate socket");
s->enqueue = smart_socket_enqueue;
s->ready = smart_socket_ready;
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 4033b72..c317e3a 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -24,17 +24,39 @@
# undef _WIN32
#endif
+/*
+ * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
+ * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
+ * not already defined, then define it here.
+ */
+#ifndef TEMP_FAILURE_RETRY
+/* Used to retry syscalls that can return EINTR. */
+#define TEMP_FAILURE_RETRY(exp) ({ \
+ typeof (exp) _rc; \
+ do { \
+ _rc = (exp); \
+ } while (_rc == -1 && errno == EINTR); \
+ _rc; })
+#endif
+
#ifdef _WIN32
-#include <windows.h>
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <process.h>
+#include <ctype.h>
+#include <direct.h>
+#include <errno.h>
#include <fcntl.h>
#include <io.h>
+#include <process.h>
#include <sys/stat.h>
-#include <errno.h>
-#include <ctype.h>
+#include <winsock2.h>
+#include <windows.h>
+#include <ws2tcpip.h>
+
+#include "fdevent.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
#define OS_PATH_SEPARATOR '\\'
#define OS_PATH_SEPARATOR_STR "\\"
@@ -76,13 +98,16 @@
return 0;
}
+static __inline__ unsigned long adb_thread_id()
+{
+ return GetCurrentThreadId();
+}
+
static __inline__ void close_on_exec(int fd)
{
/* nothing really */
}
-extern void disable_tcp_nagle(int fd);
-
#define lstat stat /* no symlinks on Win32 */
#define S_ISLNK(m) 0 /* no symlinks on Win32 */
@@ -169,6 +194,8 @@
/* normally provided by <cutils/sockets.h> */
extern int socket_loopback_client(int port, int type);
extern int socket_network_client(const char *host, int port, int type);
+extern int socket_network_client_timeout(const char *host, int port, int type,
+ int timeout);
extern int socket_loopback_server(int port, int type);
extern int socket_inaddr_any_server(int port, int type);
@@ -179,8 +206,6 @@
#define FDE_ERROR 0x0004
#define FDE_DONT_CLOSE 0x0080
-typedef struct fdevent fdevent;
-
typedef void (*fd_func)(int fd, unsigned events, void *userdata);
fdevent *fdevent_create(int fd, fd_func func, void *arg);
@@ -192,20 +217,6 @@
void fdevent_del(fdevent *fde, unsigned events);
void fdevent_loop();
-struct fdevent {
- fdevent *next;
- fdevent *prev;
-
- int fd;
- int force_eof;
-
- unsigned short state;
- unsigned short events;
-
- fd_func func;
- void *arg;
-};
-
static __inline__ void adb_sleep_ms( int mseconds )
{
Sleep( mseconds );
@@ -216,10 +227,21 @@
#undef accept
#define accept ___xxx_accept
+extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
+
+#undef setsockopt
+#define setsockopt ___xxx_setsockopt
+
static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
{
int opt = bufsize;
- return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
+ return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
+}
+
+static __inline__ void disable_tcp_nagle( int fd )
+{
+ int on = 1;
+ adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
}
extern int adb_socketpair( int sv[2] );
@@ -276,19 +298,8 @@
#include <string.h>
#include <unistd.h>
-/*
- * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
- * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
- * not already defined, then define it here.
- */
-#ifndef TEMP_FAILURE_RETRY
-/* Used to retry syscalls that can return EINTR. */
-#define TEMP_FAILURE_RETRY(exp) ({ \
- typeof (exp) _rc; \
- do { \
- _rc = (exp); \
- } while (_rc == -1 && errno == EINTR); \
- _rc; })
+#ifdef __cplusplus
+extern "C" {
#endif
#define OS_PATH_SEPARATOR '/'
@@ -456,6 +467,13 @@
setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
}
+static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
+{
+ return setsockopt( fd, level, optname, optval, optlen );
+}
+
+#undef setsockopt
+#define setsockopt ___xxx_setsockopt
static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
{
@@ -513,9 +531,19 @@
{
return strtok_r(str, delim, saveptr);
}
+
+static __inline__ unsigned long adb_thread_id()
+{
+ return (unsigned long)pthread_self();
+}
+
#undef strtok_r
#define strtok_r ___xxx_strtok_r
#endif /* !_WIN32 */
+#ifdef __cplusplus
+}
+#endif
+
#endif /* _ADB_SYSDEPS_H */
diff --git a/adb/sysdeps_win32.c b/adb/sysdeps_win32.c
index 2105b16..f132b8c 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -1,7 +1,8 @@
#include "sysdeps.h"
-#include <windows.h>
#include <winsock2.h>
+#include <windows.h>
#include <stdio.h>
+#include <stdlib.h>
#include <errno.h>
#define TRACE_TAG TRACE_SYSDEPS
#include "adb.h"
@@ -439,7 +440,8 @@
{
FH f = _fh_from_int(fd);
- if (!f) {
+ if (!f || f->clazz != &_fh_socket_class) {
+ D("adb_shutdown: invalid fd %d\n", fd);
return -1;
}
@@ -470,6 +472,8 @@
/**************************************************************************/
/**************************************************************************/
+#undef setsockopt
+
static void
_socket_set_errno( void )
{
@@ -701,6 +705,13 @@
}
+int socket_network_client_timeout(const char *host, int port, int type, int timeout)
+{
+ // TODO: implement timeouts for Windows.
+ return socket_network_client(host, port, type);
+}
+
+
int socket_inaddr_any_server(int port, int type)
{
FH f = _fh_alloc( &_fh_socket_class );
@@ -778,15 +789,16 @@
}
-void disable_tcp_nagle(int fd)
+int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
{
FH fh = _fh_from_int(fd);
- int on = 1;
- if ( !fh || fh->clazz != &_fh_socket_class )
- return;
+ if ( !fh || fh->clazz != &_fh_socket_class ) {
+ D("adb_setsockopt: invalid fd %d\n", fd);
+ return -1;
+ }
- setsockopt( fh->fh_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on) );
+ return setsockopt( fh->fh_socket, level, optname, optval, optlen );
}
/**************************************************************************/
@@ -956,7 +968,7 @@
avail = len;
memcpy( bip->buff + bip->a_end, src, avail );
- src += avail;
+ src = (const char *)src + avail;
count += avail;
len -= avail;
@@ -1049,7 +1061,7 @@
avail = len;
memcpy( dst, bip->buff + bip->a_start, avail );
- dst += avail;
+ dst = (char *)dst + avail;
count += avail;
len -= avail;
@@ -1542,7 +1554,7 @@
* reset" event that will remain set once it was set. */
main_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (main_event == NULL) {
- D("Unable to create main event. Error: %d", GetLastError());
+ D("Unable to create main event. Error: %d", (int)GetLastError());
free(threads);
return (int)WAIT_FAILED;
}
diff --git a/adb/test_track_devices.c b/adb/test_track_devices.cpp
similarity index 100%
rename from adb/test_track_devices.c
rename to adb/test_track_devices.cpp
diff --git a/adb/test_track_jdwp.c b/adb/test_track_jdwp.cpp
similarity index 100%
rename from adb/test_track_jdwp.c
rename to adb/test_track_jdwp.cpp
diff --git a/adb/tests/test_adb.py b/adb/tests/test_adb.py
new file mode 100755
index 0000000..f111b043
--- /dev/null
+++ b/adb/tests/test_adb.py
@@ -0,0 +1,400 @@
+#!/usr/bin/env python2
+"""Simple conformance test for adb.
+
+This script will use the available adb in path and run simple
+tests that attempt to touch all accessible attached devices.
+"""
+import hashlib
+import os
+import random
+import re
+import shlex
+import subprocess
+import sys
+import tempfile
+import unittest
+
+
+def trace(cmd):
+ """Print debug message if tracing enabled."""
+ if False:
+ print >> sys.stderr, cmd
+
+
+def call(cmd_str):
+ """Run process and return output tuple (stdout, stderr, ret code)."""
+ trace(cmd_str)
+ process = subprocess.Popen(shlex.split(cmd_str),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ stdout, stderr = process.communicate()
+ return stdout, stderr, process.returncode
+
+
+def call_combined(cmd_str):
+ """Run process and return output tuple (stdout+stderr, ret code)."""
+ trace(cmd_str)
+ process = subprocess.Popen(shlex.split(cmd_str),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ stdout, _ = process.communicate()
+ return stdout, process.returncode
+
+
+def call_checked(cmd_str):
+ """Run process and get stdout+stderr, raise an exception on trouble."""
+ trace(cmd_str)
+ return subprocess.check_output(shlex.split(cmd_str),
+ stderr=subprocess.STDOUT)
+
+
+def call_checked_list(cmd_str):
+ return call_checked(cmd_str).split('\n')
+
+
+def call_checked_list_skip(cmd_str):
+ out_list = call_checked_list(cmd_str)
+
+ def is_init_line(line):
+ if (len(line) >= 3) and (line[0] == "*") and (line[-2] == "*"):
+ return True
+ else:
+ return False
+
+ return [line for line in out_list if not is_init_line(line)]
+
+
+def get_device_list():
+ output = call_checked_list_skip("adb devices")
+ dev_list = []
+ for line in output[1:]:
+ if line.strip() == "":
+ continue
+ device, _ = line.split()
+ dev_list.append(device)
+ return dev_list
+
+
+def get_attached_device_count():
+ return len(get_device_list())
+
+
+def compute_md5(string):
+ hsh = hashlib.md5()
+ hsh.update(string)
+ return hsh.hexdigest()
+
+
+class HostFile(object):
+ def __init__(self, handle, md5):
+ self.handle = handle
+ self.md5 = md5
+ self.full_path = handle.name
+ self.base_name = os.path.basename(self.full_path)
+
+
+class DeviceFile(object):
+ def __init__(self, md5, full_path):
+ self.md5 = md5
+ self.full_path = full_path
+ self.base_name = os.path.basename(self.full_path)
+
+
+def make_random_host_files(in_dir, num_files, rand_size=True):
+ files = {}
+ min_size = 1 * (1 << 10)
+ max_size = 16 * (1 << 10)
+ fixed_size = min_size
+
+ for _ in range(num_files):
+ file_handle = tempfile.NamedTemporaryFile(dir=in_dir)
+
+ if rand_size:
+ size = random.randrange(min_size, max_size, 1024)
+ else:
+ size = fixed_size
+ rand_str = os.urandom(size)
+ file_handle.write(rand_str)
+ file_handle.flush()
+
+ md5 = compute_md5(rand_str)
+ files[file_handle.name] = HostFile(file_handle, md5)
+ return files
+
+
+def make_random_device_files(adb, in_dir, num_files, rand_size=True):
+ files = {}
+ min_size = 1 * (1 << 10)
+ max_size = 16 * (1 << 10)
+ fixed_size = min_size
+
+ for i in range(num_files):
+ if rand_size:
+ size = random.randrange(min_size, max_size, 1024)
+ else:
+ size = fixed_size
+
+ base_name = "device_tmpfile" + str(i)
+ full_path = in_dir + "/" + base_name
+
+ adb.shell("dd if=/dev/urandom of={} bs={} count=1".format(full_path,
+ size))
+ dev_md5, _ = adb.shell("md5sum {}".format(full_path)).split()
+
+ files[full_path] = DeviceFile(dev_md5, full_path)
+ return files
+
+
+class AdbWrapper(object):
+ """Convenience wrapper object for the adb command."""
+ def __init__(self, device=None, out_dir=None):
+ self.device = device
+ self.out_dir = out_dir
+ self.adb_cmd = "adb "
+ if self.device:
+ self.adb_cmd += "-s {} ".format(device)
+ if self.out_dir:
+ self.adb_cmd += "-p {} ".format(out_dir)
+
+ def shell(self, cmd):
+ return call_checked(self.adb_cmd + "shell " + cmd)
+
+ def shell_nocheck(self, cmd):
+ return call_combined(self.adb_cmd + "shell " + cmd)
+
+ def push(self, local, remote):
+ return call_checked(self.adb_cmd + "push {} {}".format(local, remote))
+
+ def pull(self, remote, local):
+ return call_checked(self.adb_cmd + "pull {} {}".format(remote, local))
+
+ def sync(self, directory=""):
+ return call_checked(self.adb_cmd + "sync {}".format(directory))
+
+ def forward(self, local, remote):
+ return call_checked(self.adb_cmd + "forward {} {}".format(local,
+ remote))
+
+ def tcpip(self, port):
+ return call_checked(self.adb_cmd + "tcpip {}".format(port))
+
+ def usb(self):
+ return call_checked(self.adb_cmd + "usb")
+
+ def root(self):
+ return call_checked(self.adb_cmd + "root")
+
+ def unroot(self):
+ return call_checked(self.adb_cmd + "unroot")
+
+ def forward_remove(self, local):
+ return call_checked(self.adb_cmd + "forward --remove {}".format(local))
+
+ def forward_remove_all(self):
+ return call_checked(self.adb_cmd + "forward --remove-all")
+
+ def connect(self, host):
+ return call_checked(self.adb_cmd + "connect {}".format(host))
+
+ def disconnect(self, host):
+ return call_checked(self.adb_cmd + "disconnect {}".format(host))
+
+ def reverse(self, remote, local):
+ return call_checked(self.adb_cmd + "reverse {} {}".format(remote,
+ local))
+
+ def reverse_remove_all(self):
+ return call_checked(self.adb_cmd + "reverse --remove-all")
+
+ def reverse_remove(self, remote):
+ return call_checked(
+ self.adb_cmd + "reverse --remove {}".format(remote))
+
+ def wait(self):
+ return call_checked(self.adb_cmd + "wait-for-device")
+
+
+class AdbBasic(unittest.TestCase):
+ def test_shell(self):
+ """Check that we can at least cat a file."""
+ adb = AdbWrapper()
+ out = adb.shell("cat /proc/uptime")
+ self.assertEqual(len(out.split()), 2)
+ self.assertGreater(float(out.split()[0]), 0.0)
+ self.assertGreater(float(out.split()[1]), 0.0)
+
+ def test_help(self):
+ """Make sure we get _something_ out of help."""
+ out = call_checked("adb help")
+ self.assertTrue(len(out) > 0)
+
+ def test_version(self):
+ """Get a version number out of the output of adb."""
+ out = call_checked("adb version").split()
+ version_num = False
+ for item in out:
+ if re.match(r"[\d+\.]*\d", item):
+ version_num = True
+ self.assertTrue(version_num)
+
+ def _test_root(self):
+ adb = AdbWrapper()
+ adb.root()
+ adb.wait()
+ self.assertEqual("root", adb.shell("id -un").strip())
+
+ def _test_unroot(self):
+ adb = AdbWrapper()
+ adb.unroot()
+ adb.wait()
+ self.assertEqual("shell", adb.shell("id -un").strip())
+
+ def test_root_unroot(self):
+ """Make sure that adb root and adb unroot work, using id(1)."""
+ adb = AdbWrapper()
+ original_user = adb.shell("id -un").strip()
+ try:
+ if original_user == "root":
+ self._test_unroot()
+ self._test_root()
+ elif original_user == "shell":
+ self._test_root()
+ self._test_unroot()
+ finally:
+ if original_user == "root":
+ adb.root()
+ else:
+ adb.unroot()
+ adb.wait()
+
+
+class AdbFile(unittest.TestCase):
+ SCRATCH_DIR = "/data/local/tmp"
+ DEVICE_TEMP_FILE = SCRATCH_DIR + "/adb_test_file"
+ DEVICE_TEMP_DIR = SCRATCH_DIR + "/adb_test_dir"
+
+ def test_push(self):
+ """Push a randomly generated file to specified device."""
+ kbytes = 512
+ adb = AdbWrapper()
+ with tempfile.NamedTemporaryFile(mode="w") as tmp:
+ rand_str = os.urandom(1024 * kbytes)
+ tmp.write(rand_str)
+ tmp.flush()
+
+ host_md5 = compute_md5(rand_str)
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
+ try:
+ adb.push(local=tmp.name, remote=AdbFile.DEVICE_TEMP_FILE)
+ dev_md5, _ = adb.shell(
+ "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
+ self.assertEqual(host_md5, dev_md5)
+ finally:
+ adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
+
+ # TODO: write push directory test.
+
+ def test_pull(self):
+ """Pull a randomly generated file from specified device."""
+ kbytes = 512
+ adb = AdbWrapper()
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
+ try:
+ adb.shell("dd if=/dev/urandom of={} bs=1024 count={}".format(
+ AdbFile.DEVICE_TEMP_FILE, kbytes))
+ dev_md5, _ = adb.shell(
+ "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
+
+ with tempfile.NamedTemporaryFile(mode="w") as tmp_write:
+ adb.pull(remote=AdbFile.DEVICE_TEMP_FILE, local=tmp_write.name)
+ with open(tmp_write.name) as tmp_read:
+ host_contents = tmp_read.read()
+ host_md5 = compute_md5(host_contents)
+ self.assertEqual(dev_md5, host_md5)
+ finally:
+ adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
+
+ def test_pull_dir(self):
+ """Pull a randomly generated directory of files from the device."""
+ adb = AdbWrapper()
+ temp_files = {}
+ host_dir = None
+ try:
+ # create temporary host directory
+ host_dir = tempfile.mkdtemp()
+
+ # create temporary dir on device
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+ adb.shell("mkdir -p {}".format(AdbFile.DEVICE_TEMP_DIR))
+
+ # populate device dir with random files
+ temp_files = make_random_device_files(
+ adb, in_dir=AdbFile.DEVICE_TEMP_DIR, num_files=32)
+
+ adb.pull(remote=AdbFile.DEVICE_TEMP_DIR, local=host_dir)
+
+ for device_full_path in temp_files:
+ host_path = os.path.join(
+ host_dir, temp_files[device_full_path].base_name)
+ with open(host_path) as host_file:
+ host_md5 = compute_md5(host_file.read())
+ self.assertEqual(host_md5,
+ temp_files[device_full_path].md5)
+ finally:
+ for dev_file in temp_files.values():
+ host_path = os.path.join(host_dir, dev_file.base_name)
+ os.remove(host_path)
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+ if host_dir:
+ os.removedirs(host_dir)
+
+ def test_sync(self):
+ """Sync a randomly generated directory of files to specified device."""
+ try:
+ adb = AdbWrapper()
+ temp_files = {}
+
+ # create temporary host directory
+ base_dir = tempfile.mkdtemp()
+
+ # create mirror device directory hierarchy within base_dir
+ full_dir_path = base_dir + AdbFile.DEVICE_TEMP_DIR
+ os.makedirs(full_dir_path)
+
+ # create 32 random files within the host mirror
+ temp_files = make_random_host_files(in_dir=full_dir_path,
+ num_files=32)
+
+ # clean up any trash on the device
+ adb = AdbWrapper(out_dir=base_dir)
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+
+ # issue the sync
+ adb.sync("data")
+
+ # confirm that every file on the device mirrors that on the host
+ for host_full_path in temp_files.keys():
+ device_full_path = os.path.join(
+ AdbFile.DEVICE_TEMP_DIR,
+ temp_files[host_full_path].base_name)
+ dev_md5, _ = adb.shell(
+ "md5sum {}".format(device_full_path)).split()
+ self.assertEqual(temp_files[host_full_path].md5, dev_md5)
+
+ finally:
+ adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+ if temp_files:
+ for tf in temp_files.values():
+ tf.handle.close()
+ if base_dir:
+ os.removedirs(base_dir + AdbFile.DEVICE_TEMP_DIR)
+
+
+if __name__ == '__main__':
+ random.seed(0)
+ dev_count = get_attached_device_count()
+ if dev_count:
+ suite = unittest.TestLoader().loadTestsFromName(__name__)
+ unittest.TextTestRunner(verbosity=3).run(suite)
+ else:
+ print "Test suite must be run with attached devices"
diff --git a/adb/transport.c b/adb/transport.cpp
similarity index 92%
rename from adb/transport.c
rename to adb/transport.cpp
index 6002530..1f8ac03 100644
--- a/adb/transport.c
+++ b/adb/transport.cpp
@@ -14,13 +14,16 @@
* limitations under the License.
*/
+#include "sysdeps.h"
+
+#include "transport.h"
+
+#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <errno.h>
-
-#include "sysdeps.h"
+#include <unistd.h>
#define TRACE_TAG TRACE_TRANSPORT
#include "adb.h"
@@ -41,7 +44,7 @@
#if ADB_TRACE
#define MAX_DUMP_HEX_LEN 16
-static void dump_hex( const unsigned char* ptr, size_t len )
+void dump_hex(const unsigned char* ptr, size_t len)
{
int nn, len2 = len;
// Build a string instead of logging each character.
@@ -67,8 +70,7 @@
}
#endif
-void
-kick_transport(atransport* t)
+void kick_transport(atransport* t)
{
if (t && !t->kicked)
{
@@ -85,8 +87,25 @@
}
}
-void
-run_transport_disconnects(atransport* t)
+// Each atransport contains a list of adisconnects (t->disconnects).
+// An adisconnect contains a link to the next/prev adisconnect, a function
+// pointer to a disconnect callback which takes a void* piece of user data and
+// the atransport, and some user data for the callback (helpfully named
+// "opaque").
+//
+// The list is circular. New items are added to the entry member of the list
+// (t->disconnects) by add_transport_disconnect.
+//
+// run_transport_disconnects invokes each function in the list.
+//
+// Gotchas:
+// * run_transport_disconnects assumes that t->disconnects is non-null, so
+// this can't be run on a zeroed atransport.
+// * The callbacks in this list are not removed when called, and this function
+// is not guarded against running more than once. As such, ensure that this
+// function is not called multiple times on the same atransport.
+// TODO(danalbert): Just fix this so that it is guarded once you have tests.
+void run_transport_disconnects(atransport* t)
{
adisconnect* dis = t->disconnects.next;
@@ -202,7 +221,7 @@
static void transport_socket_events(int fd, unsigned events, void *_t)
{
- atransport *t = _t;
+ atransport *t = reinterpret_cast<atransport*>(_t);
D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events);
if(events & FDE_READ){
apacket *p = 0;
@@ -259,7 +278,7 @@
static void *output_thread(void *_t)
{
- atransport *t = _t;
+ atransport *t = reinterpret_cast<atransport*>(_t);
apacket *p;
D("%s: starting transport output thread on fd %d, SYNC online (%d)\n",
@@ -314,7 +333,7 @@
static void *input_thread(void *_t)
{
- atransport *t = _t;
+ atransport *t = reinterpret_cast<atransport*>(_t);
apacket *p;
int active = 0;
@@ -475,7 +494,8 @@
asocket*
create_device_tracker(void)
{
- device_tracker* tracker = calloc(1,sizeof(*tracker));
+ device_tracker* tracker = reinterpret_cast<device_tracker*>(
+ calloc(1, sizeof(*tracker)));
if(tracker == 0) fatal("cannot allocate device tracker");
@@ -494,8 +514,7 @@
/* call this function each time the transport list has changed */
-void update_transports(void)
-{
+void update_transports(void) {
char buffer[1024];
int len;
device_tracker* tracker;
@@ -629,7 +648,7 @@
fatal_errno("cannot open transport socketpair");
}
- D("transport: %s (%d,%d) starting\n", t->serial, s[0], s[1]);
+ D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
t->transport_socket = s[0];
t->fd = s[1];
@@ -673,6 +692,7 @@
if(adb_socketpair(s)){
fatal_errno("cannot open transport registration socketpair");
}
+ D("socketpair: (%d,%d)", s[0], s[1]);
transport_registration_send = s[0];
transport_registration_recv = s[1];
@@ -751,17 +771,6 @@
dis->next = dis->prev = dis;
}
-static int qual_char_is_invalid(char ch)
-{
- if ('A' <= ch && ch <= 'Z')
- return 0;
- if ('a' <= ch && ch <= 'z')
- return 0;
- if ('0' <= ch && ch <= '9')
- return 0;
- return 1;
-}
-
static int qual_match(const char *to_test,
const char *prefix, const char *qual, int sanitize_qual)
{
@@ -781,7 +790,7 @@
while (*qual) {
char ch = *qual++;
- if (sanitize_qual && qual_char_is_invalid(ch))
+ if (sanitize_qual && isalnum(ch))
ch = '_';
if (ch != *to_test++)
return 0;
@@ -791,7 +800,8 @@
return !*to_test;
}
-atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out)
+atransport *acquire_one_transport(int state, transport_type ttype,
+ const char* serial, const char** error_out)
{
atransport *t;
atransport *result = NULL;
@@ -921,7 +931,7 @@
if (sanitize_qual) {
char *cp;
for (cp = *buf + prefix_len; cp < *buf + len; cp++) {
- if (qual_char_is_invalid(*cp))
+ if (isalnum(*cp))
*cp = '_';
}
}
@@ -999,7 +1009,8 @@
int register_socket_transport(int s, const char *serial, int port, int local)
{
- atransport *t = calloc(1, sizeof(atransport));
+ atransport *t = reinterpret_cast<atransport*>(
+ calloc(1, sizeof(atransport)));
atransport *n;
char buff[32];
@@ -1098,7 +1109,8 @@
void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable)
{
- atransport *t = calloc(1, sizeof(atransport));
+ atransport *t = reinterpret_cast<atransport*>(
+ calloc(1, sizeof(atransport)));
D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb,
serial ? serial : "");
init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM));
@@ -1137,66 +1149,6 @@
#undef TRACE_TAG
#define TRACE_TAG TRACE_RWX
-int readx(int fd, void *ptr, size_t len)
-{
- char *p = ptr;
- int r;
-#if ADB_TRACE
- size_t len0 = len;
-#endif
- D("readx: fd=%d wanted=%zu\n", fd, len);
- while(len > 0) {
- r = adb_read(fd, p, len);
- if(r > 0) {
- len -= r;
- p += r;
- } else {
- if (r < 0) {
- D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno));
- if (errno == EINTR)
- continue;
- } else {
- D("readx: fd=%d disconnected\n", fd);
- }
- return -1;
- }
- }
-
-#if ADB_TRACE
- D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
- dump_hex( ptr, len0 );
-#endif
- return 0;
-}
-
-int writex(int fd, const void *ptr, size_t len)
-{
- char *p = (char*) ptr;
- int r;
-
-#if ADB_TRACE
- D("writex: fd=%d len=%d: ", fd, (int)len);
- dump_hex( ptr, len );
-#endif
- while(len > 0) {
- r = adb_write(fd, p, len);
- if(r > 0) {
- len -= r;
- p += r;
- } else {
- if (r < 0) {
- D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno));
- if (errno == EINTR)
- continue;
- } else {
- D("writex: fd=%d disconnected\n", fd);
- }
- return -1;
- }
- }
- return 0;
-}
-
int check_header(apacket *p)
{
if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
diff --git a/adb/transport.h b/adb/transport.h
index 992e052..36a0e40 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -17,10 +17,65 @@
#ifndef __TRANSPORT_H
#define __TRANSPORT_H
-/* convenience wrappers around read/write that will retry on
-** EINTR and/or short read/write. Returns 0 on success, -1
-** on error or EOF.
+#include <stdbool.h>
+#include <sys/types.h>
+
+#include "adb.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if ADB_TRACE
+void dump_hex(const unsigned char* ptr, size_t len);
+#endif
+
+/*
+ * Obtain a transport from the available transports.
+ * If state is != CS_ANY, only transports in that state are considered.
+ * If serial is non-NULL then only the device with that serial will be chosen.
+ * If no suitable transport is found, error is set.
+ */
+atransport* acquire_one_transport(int state, transport_type ttype,
+ const char* serial, const char** error_out);
+void add_transport_disconnect(atransport* t, adisconnect* dis);
+void remove_transport_disconnect(atransport* t, adisconnect* dis);
+void kick_transport(atransport* t);
+void run_transport_disconnects(atransport* t);
+void update_transports(void);
+
+/* transports are ref-counted
+** get_device_transport does an acquire on your behalf before returning
*/
-int readx(int fd, void *ptr, size_t len);
-int writex(int fd, const void *ptr, size_t len);
+void init_transport_registration(void);
+int list_transports(char* buf, size_t bufsize, int long_listing);
+atransport* find_transport(const char* serial);
+
+void register_usb_transport(usb_handle* h, const char* serial,
+ const char* devpath, unsigned writeable);
+
+/* cause new transports to be init'd and added to the list */
+int register_socket_transport(int s, const char* serial, int port, int local);
+
+/* this should only be used for transports with connection_state == CS_NOPERM */
+void unregister_usb_transport(usb_handle* usb);
+
+/* these should only be used for the "adb disconnect" command */
+void unregister_transport(atransport* t);
+void unregister_all_tcp_transports();
+
+int check_header(apacket* p);
+int check_data(apacket* p);
+
+/* for MacOS X cleanup */
+void close_usb_devices();
+
+void send_packet(apacket* p, atransport* t);
+
+asocket* create_device_tracker(void);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif /* __TRANSPORT_H */
diff --git a/adb/transport_local.c b/adb/transport_local.cpp
similarity index 90%
rename from adb/transport_local.c
rename to adb/transport_local.cpp
index d2dbca6..440d4c5 100644
--- a/adb/transport_local.c
+++ b/adb/transport_local.cpp
@@ -14,41 +14,28 @@
* limitations under the License.
*/
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
+#include <sys/types.h>
#include "sysdeps.h"
-#include <sys/types.h>
-#if !ADB_HOST
-#include <cutils/properties.h>
-#endif
#define TRACE_TAG TRACE_TRANSPORT
#include "adb.h"
-
-#ifdef HAVE_BIG_ENDIAN
-#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
-static inline void fix_endians(apacket *p)
-{
- p->msg.command = H4(p->msg.command);
- p->msg.arg0 = H4(p->msg.arg0);
- p->msg.arg1 = H4(p->msg.arg1);
- p->msg.data_length = H4(p->msg.data_length);
- p->msg.data_check = H4(p->msg.data_check);
- p->msg.magic = H4(p->msg.magic);
-}
-#else
-#define fix_endians(p) do {} while (0)
+#include "adb_io.h"
+#if !ADB_HOST
+#include "cutils/properties.h"
#endif
+#include "transport.h"
#if ADB_HOST
/* we keep a list of opened transports. The atransport struct knows to which
* local transport it is connected. The list is used to detect when we're
* trying to connect twice to a given local transport.
*/
-#define ADB_LOCAL_TRANSPORT_MAX 16
+#define ADB_LOCAL_TRANSPORT_MAX 64
ADB_MUTEX_DEFINE( local_transports_lock );
@@ -57,23 +44,17 @@
static int remote_read(apacket *p, atransport *t)
{
- if(readx(t->sfd, &p->msg, sizeof(amessage))){
+ if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
D("remote local: read terminated (message)\n");
return -1;
}
- fix_endians(p);
-
-#if 0 && defined HAVE_BIG_ENDIAN
- D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
- p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
-#endif
if(check_header(p)) {
D("bad header: terminated (data)\n");
return -1;
}
- if(readx(t->sfd, p->data, p->msg.data_length)){
+ if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
D("remote local: terminated (data)\n");
return -1;
}
@@ -90,13 +71,7 @@
{
int length = p->msg.data_length;
- fix_endians(p);
-
-#if 0 && defined HAVE_BIG_ENDIAN
- D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
- p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
-#endif
- if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
+ if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
D("remote local: write terminated\n");
return -1;
}
diff --git a/adb/transport_test.cpp b/adb/transport_test.cpp
new file mode 100644
index 0000000..2b3fe3c
--- /dev/null
+++ b/adb/transport_test.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2015 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 "transport.h"
+
+#include <gtest/gtest.h>
+
+#include "adb.h"
+
+TEST(transport, kick_transport) {
+ atransport t = {};
+ // Mutate some member so we can test that the function is run.
+ t.kick = [](atransport* trans) { trans->fd = 42; };
+ atransport expected = t;
+ expected.fd = 42;
+ expected.kicked = 1;
+ kick_transport(&t);
+ ASSERT_EQ(42, t.fd);
+ ASSERT_EQ(1, t.kicked);
+ ASSERT_EQ(0, memcmp(&expected, &t, sizeof(atransport)));
+}
+
+TEST(transport, kick_transport_already_kicked) {
+ // Ensure that the transport is not modified if the transport has already been
+ // kicked.
+ atransport t = {};
+ t.kicked = 1;
+ t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
+ atransport expected = t;
+ kick_transport(&t);
+ ASSERT_EQ(0, memcmp(&expected, &t, sizeof(atransport)));
+}
+
+// Disabled because the function currently segfaults for a zeroed atransport. I
+// want to make sure I understand how this is working at all before I try fixing
+// that.
+TEST(transport, DISABLED_run_transport_disconnects_zeroed_atransport) {
+ atransport t = {};
+ run_transport_disconnects(&t);
+}
diff --git a/adb/transport_usb.c b/adb/transport_usb.cpp
similarity index 69%
rename from adb/transport_usb.c
rename to adb/transport_usb.cpp
index ee6b637..37a8219 100644
--- a/adb/transport_usb.c
+++ b/adb/transport_usb.cpp
@@ -22,33 +22,7 @@
#define TRACE_TAG TRACE_TRANSPORT
#include "adb.h"
-
-#if ADB_HOST
-#include "usb_vendors.h"
-#endif
-
-#ifdef HAVE_BIG_ENDIAN
-#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
-static inline void fix_endians(apacket *p)
-{
- p->msg.command = H4(p->msg.command);
- p->msg.arg0 = H4(p->msg.arg0);
- p->msg.arg1 = H4(p->msg.arg1);
- p->msg.data_length = H4(p->msg.data_length);
- p->msg.data_check = H4(p->msg.data_check);
- p->msg.magic = H4(p->msg.magic);
-}
-unsigned host_to_le32(unsigned n)
-{
- return H4(n);
-}
-#else
-#define fix_endians(p) do {} while (0)
-unsigned host_to_le32(unsigned n)
-{
- return n;
-}
-#endif
+#include "transport.h"
static int remote_read(apacket *p, atransport *t)
{
@@ -57,8 +31,6 @@
return -1;
}
- fix_endians(p);
-
if(check_header(p)) {
D("remote usb: check_header failed\n");
return -1;
@@ -83,8 +55,6 @@
{
unsigned size = p->msg.data_length;
- fix_endians(p);
-
if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
D("remote usb: 1 - write terminated\n");
return -1;
@@ -131,18 +101,6 @@
#if ADB_HOST
int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
{
- unsigned i;
- for (i = 0; i < vendorIdCount; i++) {
- if (vid == vendorIds[i]) {
- if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
- usb_protocol == ADB_PROTOCOL) {
- return 1;
- }
-
- return 0;
- }
- }
-
- return 0;
+ return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
}
#endif
diff --git a/adb/usb_libusb.c b/adb/usb_libusb.c
deleted file mode 100644
index 06ff5dc..0000000
--- a/adb/usb_libusb.c
+++ /dev/null
@@ -1,657 +0,0 @@
-/*
- * Copyright (C) 2009 bsdroid project
- * Alexey Tarasov <tarasov@dodologics.com>
- *
- * 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.
- */
-
-#include <sys/endian.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-
-#include <err.h>
-#include <errno.h>
-#include <poll.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <strings.h>
-#include <string.h>
-#include <sysexits.h>
-#include <unistd.h>
-#include <libusb.h>
-#include "sysdeps.h"
-
-#define TRACE_TAG TRACE_USB
-#include "adb.h"
-
-static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER;
-static libusb_context *ctx = NULL;
-
-struct usb_handle
-{
- usb_handle *prev;
- usb_handle *next;
-
- libusb_device *dev;
- libusb_device_handle *devh;
- int interface;
- uint8_t dev_bus;
- uint8_t dev_addr;
-
- int zero_mask;
- unsigned char end_point_address[2];
- char serial[128];
-
- adb_cond_t notify;
- adb_mutex_t lock;
-};
-
-static struct usb_handle handle_list = {
- .prev = &handle_list,
- .next = &handle_list,
-};
-
-void
-usb_cleanup()
-{
- libusb_exit(ctx);
-}
-
-void
-report_bulk_libusb_error(int r)
-{
- switch (r) {
- case LIBUSB_ERROR_TIMEOUT:
- D("Transfer timeout\n");
- break;
-
- case LIBUSB_ERROR_PIPE:
- D("Control request is not supported\n");
- break;
-
- case LIBUSB_ERROR_OVERFLOW:
- D("Device offered more data\n");
- break;
-
- case LIBUSB_ERROR_NO_DEVICE :
- D("Device was disconnected\n");
- break;
-
- default:
- D("Error %d during transfer\n", r);
- break;
- };
-}
-
-static int
-usb_bulk_write(usb_handle *uh, const void *data, int len)
-{
- int r = 0;
- int transferred = 0;
-
- r = libusb_bulk_transfer(uh->devh, uh->end_point_address[1], (void *)data, len,
- &transferred, 0);
-
- if (r != 0) {
- D("usb_bulk_write(): ");
- report_bulk_libusb_error(r);
- return r;
- }
-
- return (transferred);
-}
-
-static int
-usb_bulk_read(usb_handle *uh, void *data, int len)
-{
- int r = 0;
- int transferred = 0;
-
- r = libusb_bulk_transfer(uh->devh, uh->end_point_address[0], data, len,
- &transferred, 0);
-
- if (r != 0) {
- D("usb_bulk_read(): ");
- report_bulk_libusb_error(r);
- return r;
- }
-
- return (transferred);
-}
-
-int
-usb_write(struct usb_handle *uh, const void *_data, int len)
-{
- unsigned char *data = (unsigned char*) _data;
- int n;
- int need_zero = 0;
-
- if (uh->zero_mask == 1) {
- if (!(len & uh->zero_mask)) {
- need_zero = 1;
- }
- }
-
- D("usb_write(): %p:%d -> transport %p\n", _data, len, uh);
-
- while (len > 0) {
- int xfer = (len > 4096) ? 4096 : len;
-
- n = usb_bulk_write(uh, data, xfer);
-
- if (n != xfer) {
- D("usb_write(): failed for transport %p (%d bytes left)\n", uh, len);
- return -1;
- }
-
- len -= xfer;
- data += xfer;
- }
-
- if (need_zero){
- n = usb_bulk_write(uh, _data, 0);
-
- if (n < 0) {
- D("usb_write(): failed to finish operation for transport %p\n", uh);
- }
- return n;
- }
-
- return 0;
-}
-
-int
-usb_read(struct usb_handle *uh, void *_data, int len)
-{
- unsigned char *data = (unsigned char*) _data;
- int n;
-
- D("usb_read(): %p:%d <- transport %p\n", _data, len, uh);
-
- while (len > 0) {
- int xfer = (len > 4096) ? 4096 : len;
-
- n = usb_bulk_read(uh, data, xfer);
-
- if (n != xfer) {
- if (n > 0) {
- data += n;
- len -= n;
- continue;
- }
-
- D("usb_read(): failed for transport %p (%d bytes left)\n", uh, len);
- return -1;
- }
-
- len -= xfer;
- data += xfer;
- }
-
- return 0;
- }
-
-int
-usb_close(struct usb_handle *h)
-{
- D("usb_close(): closing transport %p\n", h);
- adb_mutex_lock(&usb_lock);
-
- h->next->prev = h->prev;
- h->prev->next = h->next;
- h->prev = NULL;
- h->next = NULL;
-
- libusb_release_interface(h->devh, h->interface);
- libusb_close(h->devh);
- libusb_unref_device(h->dev);
-
- adb_mutex_unlock(&usb_lock);
-
- free(h);
-
- return (0);
-}
-
-void usb_kick(struct usb_handle *h)
-{
- D("usb_cick(): kicking transport %p\n", h);
-
- adb_mutex_lock(&h->lock);
- unregister_usb_transport(h);
- adb_mutex_unlock(&h->lock);
-
- h->next->prev = h->prev;
- h->prev->next = h->next;
- h->prev = NULL;
- h->next = NULL;
-
- libusb_release_interface(h->devh, h->interface);
- libusb_close(h->devh);
- libusb_unref_device(h->dev);
- free(h);
-}
-
-int
-check_usb_interface(libusb_interface *interface,
- libusb_device_descriptor *desc,
- struct usb_handle *uh)
-{
- int e;
-
- if (interface->num_altsetting == 0) {
- D("check_usb_interface(): No interface settings\n");
- return -1;
- }
-
- libusb_interface_descriptor *idesc = &interface->altsetting[0];
-
- if (idesc->bNumEndpoints != 2) {
- D("check_usb_interface(): Interface have not 2 endpoints, ignoring\n");
- return -1;
- }
-
- for (e = 0; e < idesc->bNumEndpoints; e++) {
- libusb_endpoint_descriptor *edesc = &idesc->endpoint[e];
-
- if (edesc->bmAttributes != LIBUSB_TRANSFER_TYPE_BULK) {
- D("check_usb_interface(): Endpoint (%u) is not bulk (%u), ignoring\n",
- edesc->bmAttributes, LIBUSB_TRANSFER_TYPE_BULK);
- return -1;
- }
-
- if (edesc->bEndpointAddress & LIBUSB_ENDPOINT_IN)
- uh->end_point_address[0] = edesc->bEndpointAddress;
- else
- uh->end_point_address[1] = edesc->bEndpointAddress;
-
- /* aproto 01 needs 0 termination */
- if (idesc->bInterfaceProtocol == 0x01) {
- uh->zero_mask = edesc->wMaxPacketSize - 1;
- D("check_usb_interface(): Forced Android interface protocol v.1\n");
- }
- }
-
- D("check_usb_interface(): Device: %04x:%04x "
- "iclass: %x, isclass: %x, iproto: %x ep: %x/%x-> ",
- desc->idVendor, desc->idProduct, idesc->bInterfaceClass,
- idesc->bInterfaceSubClass, idesc->bInterfaceProtocol,
- uh->end_point_address[0], uh->end_point_address[1]);
-
- if (!is_adb_interface(desc->idVendor, desc->idProduct,
- idesc->bInterfaceClass, idesc->bInterfaceSubClass,
- idesc->bInterfaceProtocol))
- {
- D("not matches\n");
- return -1;
- }
-
- D("matches\n");
- return 1;
-}
-
-int
-check_usb_interfaces(libusb_config_descriptor *config,
- libusb_device_descriptor *desc, struct usb_handle *uh)
-{
- int i;
-
- for (i = 0; i < config->bNumInterfaces; ++i) {
- if (check_usb_interface(&config->interface[i], desc, uh) != -1) {
- /* found some interface and saved information about it */
- D("check_usb_interfaces(): Interface %d of %04x:%04x "
- "matches Android device\n", i, desc->idVendor,
- desc->idProduct);
-
- return i;
- }
- }
-
- return -1;
-}
-
-int
-register_device(struct usb_handle *uh, const char *serial)
-{
- D("register_device(): Registering %p [%s] as USB transport\n",
- uh, serial);
-
- struct usb_handle *usb= NULL;
-
- usb = calloc(1, sizeof(struct usb_handle));
- memcpy(usb, uh, sizeof(struct usb_handle));
- strcpy(usb->serial, uh->serial);
-
- adb_cond_init(&usb->notify, 0);
- adb_mutex_init(&usb->lock, 0);
-
- adb_mutex_lock(&usb_lock);
-
- usb->next = &handle_list;
- usb->prev = handle_list.prev;
- usb->prev->next = usb;
- usb->next->prev = usb;
-
- adb_mutex_unlock(&usb_lock);
-
- register_usb_transport(usb, serial, NULL, 1);
-
- return (1);
-}
-
-int
-already_registered(usb_handle *uh)
-{
- struct usb_handle *usb= NULL;
- int exists = 0;
-
- adb_mutex_lock(&usb_lock);
-
- for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
- if ((usb->dev_bus == uh->dev_bus) &&
- (usb->dev_addr == uh->dev_addr))
- {
- exists = 1;
- break;
- }
- }
-
- adb_mutex_unlock(&usb_lock);
-
- return exists;
-}
-
-void
-check_device(libusb_device *dev)
-{
- struct usb_handle uh;
- int i = 0;
- int found = -1;
- char serial[256] = {0};
-
- libusb_device_descriptor desc;
- libusb_config_descriptor *config = NULL;
-
- int r = libusb_get_device_descriptor(dev, &desc);
-
- if (r != LIBUSB_SUCCESS) {
- D("check_device(): Failed to get device descriptor\n");
- return;
- }
-
- if ((desc.idVendor == 0) && (desc.idProduct == 0))
- return;
-
- D("check_device(): Probing usb device %04x:%04x\n",
- desc.idVendor, desc.idProduct);
-
- if (!is_adb_interface (desc.idVendor, desc.idProduct,
- ADB_CLASS, ADB_SUBCLASS, ADB_PROTOCOL))
- {
- D("check_device(): Ignored due unknown vendor id\n");
- return;
- }
-
- uh.dev_bus = libusb_get_bus_number(dev);
- uh.dev_addr = libusb_get_device_address(dev);
-
- if (already_registered(&uh)) {
- D("check_device(): Device (bus: %d, address: %d) "
- "is already registered\n", uh.dev_bus, uh.dev_addr);
- return;
- }
-
- D("check_device(): Device bus: %d, address: %d\n",
- uh.dev_bus, uh.dev_addr);
-
- r = libusb_get_active_config_descriptor(dev, &config);
-
- if (r != 0) {
- if (r == LIBUSB_ERROR_NOT_FOUND) {
- D("check_device(): Device %4x:%4x is unconfigured\n",
- desc.idVendor, desc.idProduct);
- return;
- }
-
- D("check_device(): Failed to get configuration for %4x:%4x\n",
- desc.idVendor, desc.idProduct);
- return;
- }
-
- if (config == NULL) {
- D("check_device(): Sanity check failed after "
- "getting active config\n");
- return;
- }
-
- if (config->interface != NULL) {
- found = check_usb_interfaces(config, &desc, &uh);
- }
-
- /* not needed anymore */
- libusb_free_config_descriptor(config);
-
- r = libusb_open(dev, &uh.devh);
- uh.dev = dev;
-
- if (r != 0) {
- switch (r) {
- case LIBUSB_ERROR_NO_MEM:
- D("check_device(): Memory allocation problem\n");
- break;
-
- case LIBUSB_ERROR_ACCESS:
- D("check_device(): Permissions problem, "
- "current user priveleges are messed up?\n");
- break;
-
- case LIBUSB_ERROR_NO_DEVICE:
- D("check_device(): Device disconected, bad cable?\n");
- break;
-
- default:
- D("check_device(): libusb triggered error %d\n", r);
- }
- // skip rest
- found = -1;
- }
-
- if (found >= 0) {
- D("check_device(): Device matches Android interface\n");
- // read the device's serial number
- memset(serial, 0, sizeof(serial));
- uh.interface = found;
-
- r = libusb_claim_interface(uh.devh, uh.interface);
-
- if (r < 0) {
- D("check_device(): Failed to claim interface %d\n",
- uh.interface);
-
- goto fail;
- }
-
- if (desc.iSerialNumber) {
- // reading serial
- uint16_t buffer[128] = {0};
- uint16_t languages[128] = {0};
- int languageCount = 0;
-
- memset(languages, 0, sizeof(languages));
- r = libusb_control_transfer(uh.devh,
- LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
- LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_STRING << 8,
- 0, (uint8_t *)languages, sizeof(languages), 0);
-
- if (r <= 0) {
- D("check_device(): Failed to get languages count\n");
- goto fail;
- }
-
- languageCount = (r - 2) / 2;
-
- for (i = 1; i <= languageCount; ++i) {
- memset(buffer, 0, sizeof(buffer));
-
- r = libusb_control_transfer(uh.devh,
- LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
- LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc.iSerialNumber,
- languages[i], (uint8_t *)buffer, sizeof(buffer), 0);
-
- if (r > 0) { /* converting serial */
- int j = 0;
- r /= 2;
-
- for (j = 1; j < r; ++j)
- serial[j - 1] = buffer[j];
-
- serial[j - 1] = '\0';
- break; /* languagesCount cycle */
- }
- }
-
- if (register_device(&uh, serial) == 0) {
- D("check_device(): Failed to register device\n");
- goto fail_interface;
- }
-
- libusb_ref_device(dev);
- }
- }
-
- return;
-
-fail_interface:
- libusb_release_interface(uh.devh, uh.interface);
-
-fail:
- libusb_close(uh.devh);
- uh.devh = NULL;
-}
-
-int
-check_device_connected(struct usb_handle *uh)
-{
- int r = libusb_kernel_driver_active(uh->devh, uh->interface);
-
- if (r == LIBUSB_ERROR_NO_DEVICE)
- return 0;
-
- if (r < 0)
- return -1;
-
- return 1;
-}
-
-void
-kick_disconnected()
-{
- struct usb_handle *usb= NULL;
-
- adb_mutex_lock(&usb_lock);
-
- for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
-
- if (check_device_connected(usb) == 0) {
- D("kick_disconnected(): Transport %p is not online anymore\n",
- usb);
-
- usb_kick(usb);
- }
- }
-
- adb_mutex_unlock(&usb_lock);
-}
-
-void
-scan_usb_devices()
-{
- D("scan_usb_devices(): started\n");
-
- libusb_device **devs= NULL;
- libusb_device *dev= NULL;
- ssize_t cnt = libusb_get_device_list(ctx, &devs);
-
- if (cnt < 0) {
- D("scan_usb_devices(): Failed to get device list (error: %d)\n",
- cnt);
-
- return;
- }
-
- int i = 0;
-
- while ((dev = devs[i++]) != NULL) {
- check_device(dev);
- }
-
- libusb_free_device_list(devs, 1);
-}
-
-void *
-device_poll_thread(void* unused)
-{
- D("device_poll_thread(): Created USB scan thread\n");
-
- for (;;) {
- sleep(5);
- kick_disconnected();
- scan_usb_devices();
- }
-
- /* never reaching this point */
- return (NULL);
-}
-
-static void
-sigalrm_handler(int signo)
-{
- /* nothing */
-}
-
-void
-usb_init()
-{
- D("usb_init(): started\n");
- adb_thread_t tid;
- struct sigaction actions;
-
- int r = libusb_init(&ctx);
-
- if (r != LIBUSB_SUCCESS) {
- err(EX_IOERR, "Failed to init libusb\n");
- }
-
- memset(&actions, 0, sizeof(actions));
-
- sigemptyset(&actions.sa_mask);
-
- actions.sa_flags = 0;
- actions.sa_handler = sigalrm_handler;
-
- sigaction(SIGALRM, &actions, NULL);
-
- /* initial device scan */
- scan_usb_devices();
-
- /* starting USB event polling thread */
- if (adb_thread_create(&tid, device_poll_thread, NULL)) {
- err(EX_IOERR, "cannot create USB scan thread\n");
- }
-
- D("usb_init(): finished\n");
-}
-
diff --git a/adb/usb_linux.c b/adb/usb_linux.cpp
similarity index 93%
rename from adb/usb_linux.c
rename to adb/usb_linux.cpp
index 8ff753e..c01ec8c 100644
--- a/adb/usb_linux.c
+++ b/adb/usb_linux.cpp
@@ -14,33 +14,30 @@
* limitations under the License.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <errno.h>
#include <ctype.h>
-
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
#include <linux/usbdevice_fs.h>
#include <linux/version.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
#include <linux/usb/ch9.h>
#else
#include <linux/usb_ch9.h>
#endif
-#include <asm/byteorder.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_USB
#include "adb.h"
-
+#include "transport.h"
/* usb scan debugging is waaaay too verbose */
#define DBGX(x...)
@@ -170,7 +167,7 @@
}
// DBGX("[ scanning %s ]\n", devname);
- if((fd = unix_open(devname, O_RDONLY)) < 0) {
+ if((fd = unix_open(devname, O_RDONLY | O_CLOEXEC)) < 0) {
continue;
}
@@ -237,8 +234,20 @@
// looks like ADB...
ep1 = (struct usb_endpoint_descriptor *)bufptr;
bufptr += USB_DT_ENDPOINT_SIZE;
+ // For USB 3.0 SuperSpeed devices, skip potential
+ // USB 3.0 SuperSpeed Endpoint Companion descriptor
+ if (bufptr+2 <= devdesc + desclength &&
+ bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
+ bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
+ bufptr += USB_DT_SS_EP_COMP_SIZE;
+ }
ep2 = (struct usb_endpoint_descriptor *)bufptr;
bufptr += USB_DT_ENDPOINT_SIZE;
+ if (bufptr+2 <= devdesc + desclength &&
+ bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
+ bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
+ bufptr += USB_DT_SS_EP_COMP_SIZE;
+ }
if (bufptr > devdesc + desclength ||
ep1->bLength != USB_DT_ENDPOINT_SIZE ||
@@ -367,6 +376,7 @@
struct usbdevfs_urb *out = NULL;
int res;
+ D("++ usb_bulk_read ++\n");
memset(urb, 0, sizeof(*urb));
urb->type = USBDEVFS_URB_TYPE_BULK;
urb->endpoint = h->ep_in;
@@ -429,6 +439,7 @@
}
fail:
adb_mutex_unlock(&h->lock);
+ D("-- usb_bulk_read --\n");
return res;
}
@@ -439,6 +450,7 @@
int n;
int need_zero = 0;
+ D("++ usb_write ++\n");
if(h->zero_mask) {
/* if we need 0-markers and our transfer
** is an even multiple of the packet size,
@@ -468,6 +480,7 @@
return n;
}
+ D("-- usb_write --\n");
return 0;
}
@@ -542,7 +555,7 @@
int usb_close(usb_handle *h)
{
- D("[ usb close ... ]\n");
+ D("++ usb close ++\n");
adb_mutex_lock(&usb_lock);
h->next->prev = h->prev;
h->prev->next = h->next;
@@ -550,7 +563,7 @@
h->next = 0;
adb_close(h->desc);
- D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
+ D("-- usb closed %p (fd = %d) --\n", h, h->desc);
adb_mutex_unlock(&usb_lock);
free(h);
@@ -561,7 +574,6 @@
unsigned char ep_in, unsigned char ep_out,
int interface, int serial_index, unsigned zero_mask)
{
- usb_handle* usb = 0;
int n = 0;
char serial[256];
@@ -574,8 +586,9 @@
** name, we have no further work to do.
*/
adb_mutex_lock(&usb_lock);
- for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
- if(!strcmp(usb->fname, dev_name)) {
+ for (usb_handle* usb = handle_list.next; usb != &handle_list;
+ usb = usb->next) {
+ if (!strcmp(usb->fname, dev_name)) {
adb_mutex_unlock(&usb_lock);
return;
}
@@ -584,7 +597,8 @@
D("[ usb located new device %s (%d/%d/%d) ]\n",
dev_name, ep_in, ep_out, interface);
- usb = calloc(1, sizeof(usb_handle));
+ usb_handle* usb = reinterpret_cast<usb_handle*>(
+ calloc(1, sizeof(usb_handle)));
strcpy(usb->fname, dev_name);
usb->ep_in = ep_in;
usb->ep_out = ep_out;
@@ -597,10 +611,10 @@
usb->mark = 1;
usb->reaper_thread = 0;
- usb->desc = unix_open(usb->fname, O_RDWR);
+ usb->desc = unix_open(usb->fname, O_RDWR | O_CLOEXEC);
if(usb->desc < 0) {
/* if we fail, see if have read-only access */
- usb->desc = unix_open(usb->fname, O_RDONLY);
+ usb->desc = unix_open(usb->fname, O_RDONLY | O_CLOEXEC);
if(usb->desc < 0) goto fail;
usb->writeable = 0;
D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c
index 8426e0e..c88b258 100644
--- a/adb/usb_linux_client.c
+++ b/adb/usb_linux_client.c
@@ -14,25 +14,26 @@
* limitations under the License.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include <linux/usb/ch9.h>
-#include <linux/usb/functionfs.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/functionfs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_USB
#include "adb.h"
+#include "transport.h"
#define MAX_PACKET_SIZE_FS 64
#define MAX_PACKET_SIZE_HS 512
+#define MAX_PACKET_SIZE_SS 1024
#define cpu_to_le16(x) htole16(x)
#define cpu_to_le32(x) htole32(x)
@@ -55,71 +56,81 @@
int bulk_in; /* "in" from the host's perspective => sink for adbd */
};
-static const struct {
- struct usb_functionfs_descs_head header;
- struct {
- struct usb_interface_descriptor intf;
- struct usb_endpoint_descriptor_no_audio source;
- struct usb_endpoint_descriptor_no_audio sink;
- } __attribute__((packed)) fs_descs, hs_descs;
-} __attribute__((packed)) descriptors = {
- .header = {
- .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
- .length = cpu_to_le32(sizeof(descriptors)),
- .fs_count = 3,
- .hs_count = 3,
+struct func_desc {
+ struct usb_interface_descriptor intf;
+ struct usb_endpoint_descriptor_no_audio source;
+ struct usb_endpoint_descriptor_no_audio sink;
+} __attribute__((packed));
+
+struct desc_v1 {
+ struct usb_functionfs_descs_head_v1 {
+ __le32 magic;
+ __le32 length;
+ __le32 fs_count;
+ __le32 hs_count;
+ } __attribute__((packed)) header;
+ struct func_desc fs_descs, hs_descs;
+} __attribute__((packed));
+
+struct desc_v2 {
+ struct usb_functionfs_descs_head_v2 header;
+ // The rest of the structure depends on the flags in the header.
+ __le32 fs_count;
+ __le32 hs_count;
+ struct func_desc fs_descs, hs_descs;
+} __attribute__((packed));
+
+struct func_desc fs_descriptors = {
+ .intf = {
+ .bLength = sizeof(fs_descriptors.intf),
+ .bDescriptorType = USB_DT_INTERFACE,
+ .bInterfaceNumber = 0,
+ .bNumEndpoints = 2,
+ .bInterfaceClass = ADB_CLASS,
+ .bInterfaceSubClass = ADB_SUBCLASS,
+ .bInterfaceProtocol = ADB_PROTOCOL,
+ .iInterface = 1, /* first string from the provided table */
},
- .fs_descs = {
- .intf = {
- .bLength = sizeof(descriptors.fs_descs.intf),
- .bDescriptorType = USB_DT_INTERFACE,
- .bInterfaceNumber = 0,
- .bNumEndpoints = 2,
- .bInterfaceClass = ADB_CLASS,
- .bInterfaceSubClass = ADB_SUBCLASS,
- .bInterfaceProtocol = ADB_PROTOCOL,
- .iInterface = 1, /* first string from the provided table */
- },
- .source = {
- .bLength = sizeof(descriptors.fs_descs.source),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 1 | USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_FS,
- },
- .sink = {
- .bLength = sizeof(descriptors.fs_descs.sink),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 2 | USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_FS,
- },
+ .source = {
+ .bLength = sizeof(fs_descriptors.source),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = MAX_PACKET_SIZE_FS,
},
- .hs_descs = {
- .intf = {
- .bLength = sizeof(descriptors.hs_descs.intf),
- .bDescriptorType = USB_DT_INTERFACE,
- .bInterfaceNumber = 0,
- .bNumEndpoints = 2,
- .bInterfaceClass = ADB_CLASS,
- .bInterfaceSubClass = ADB_SUBCLASS,
- .bInterfaceProtocol = ADB_PROTOCOL,
- .iInterface = 1, /* first string from the provided table */
- },
- .source = {
- .bLength = sizeof(descriptors.hs_descs.source),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 1 | USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_HS,
- },
- .sink = {
- .bLength = sizeof(descriptors.hs_descs.sink),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 2 | USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_HS,
- },
+ .sink = {
+ .bLength = sizeof(fs_descriptors.sink),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 2 | USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = MAX_PACKET_SIZE_FS,
+ },
+};
+
+struct func_desc hs_descriptors = {
+ .intf = {
+ .bLength = sizeof(hs_descriptors.intf),
+ .bDescriptorType = USB_DT_INTERFACE,
+ .bInterfaceNumber = 0,
+ .bNumEndpoints = 2,
+ .bInterfaceClass = ADB_CLASS,
+ .bInterfaceSubClass = ADB_SUBCLASS,
+ .bInterfaceProtocol = ADB_PROTOCOL,
+ .iInterface = 1, /* first string from the provided table */
+ },
+ .source = {
+ .bLength = sizeof(hs_descriptors.source),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = MAX_PACKET_SIZE_HS,
+ },
+ .sink = {
+ .bLength = sizeof(hs_descriptors.sink),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 2 | USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = MAX_PACKET_SIZE_HS,
},
};
@@ -263,6 +274,16 @@
static void init_functionfs(struct usb_handle *h)
{
ssize_t ret;
+ struct desc_v1 v1_descriptor;
+ struct desc_v2 v2_descriptor;
+
+ v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
+ v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
+ v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
+ v2_descriptor.fs_count = 3;
+ v2_descriptor.hs_count = 3;
+ v2_descriptor.fs_descs = fs_descriptors;
+ v2_descriptor.hs_descs = hs_descriptors;
if (h->control < 0) { // might have already done this before
D("OPENING %s\n", USB_FFS_ADB_EP0);
@@ -272,10 +293,20 @@
goto err;
}
- ret = adb_write(h->control, &descriptors, sizeof(descriptors));
+ ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
if (ret < 0) {
- D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
- goto err;
+ v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
+ v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
+ v1_descriptor.header.fs_count = 3;
+ v1_descriptor.header.hs_count = 3;
+ v1_descriptor.fs_descs = fs_descriptors;
+ v1_descriptor.hs_descs = hs_descriptors;
+ D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
+ ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
+ if (ret < 0) {
+ D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
+ goto err;
+ }
}
ret = adb_write(h->control, &strings, sizeof(strings));
diff --git a/adb/usb_osx.c b/adb/usb_osx.c
index 45ce444..aa7e1ea 100644
--- a/adb/usb_osx.c
+++ b/adb/usb_osx.c
@@ -22,18 +22,18 @@
#include <IOKit/IOMessage.h>
#include <mach/mach_port.h>
-#include "sysdeps.h"
-
#include <stdio.h>
+#include "sysdeps.h"
+
#define TRACE_TAG TRACE_USB
#include "adb.h"
-#include "usb_vendors.h"
+#include "transport.h"
#define DBG D
static IONotificationPortRef notificationPort = 0;
-static io_iterator_t* notificationIterators;
+static io_iterator_t notificationIterator;
struct usb_handle
{
@@ -61,8 +61,6 @@
{
CFMutableDictionaryRef matchingDict;
CFRunLoopSourceRef runLoopSource;
- SInt32 vendor, if_subclass, if_protocol;
- unsigned i;
//* To set up asynchronous notifications, create a notification port and
//* add its run loop event source to the program's run loop
@@ -70,47 +68,33 @@
runLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
- memset(notificationIterators, 0, sizeof(notificationIterators));
+ //* Create our matching dictionary to find the Android device's
+ //* adb interface
+ //* IOServiceAddMatchingNotification consumes the reference, so we do
+ //* not need to release this
+ matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
- //* loop through all supported vendors
- for (i = 0; i < vendorIdCount; i++) {
- //* Create our matching dictionary to find the Android device's
- //* adb interface
- //* IOServiceAddMatchingNotification consumes the reference, so we do
- //* not need to release this
- matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
-
- if (!matchingDict) {
- DBG("ERR: Couldn't create USB matching dictionary.\n");
- return -1;
- }
-
- //* Match based on vendor id, interface subclass and protocol
- vendor = vendorIds[i];
- if_subclass = ADB_SUBCLASS;
- if_protocol = ADB_PROTOCOL;
- CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID),
- CFNumberCreate(kCFAllocatorDefault,
- kCFNumberSInt32Type, &vendor));
- CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceSubClass),
- CFNumberCreate(kCFAllocatorDefault,
- kCFNumberSInt32Type, &if_subclass));
- CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceProtocol),
- CFNumberCreate(kCFAllocatorDefault,
- kCFNumberSInt32Type, &if_protocol));
- IOServiceAddMatchingNotification(
- notificationPort,
- kIOFirstMatchNotification,
- matchingDict,
- AndroidInterfaceAdded,
- NULL,
- ¬ificationIterators[i]);
-
- //* Iterate over set of matching interfaces to access already-present
- //* devices and to arm the notification
- AndroidInterfaceAdded(NULL, notificationIterators[i]);
+ if (!matchingDict) {
+ DBG("ERR: Couldn't create USB matching dictionary.\n");
+ return -1;
}
+ //* We have to get notifications for all potential candidates and test them
+ //* at connection time because the matching rules don't allow for a
+ //* USB interface class of 0xff for class+subclass+protocol matches
+ //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html
+ IOServiceAddMatchingNotification(
+ notificationPort,
+ kIOFirstMatchNotification,
+ matchingDict,
+ AndroidInterfaceAdded,
+ NULL,
+ ¬ificationIterator);
+
+ //* Iterate over set of matching interfaces to access already-present
+ //* devices and to arm the notification
+ AndroidInterfaceAdded(NULL, notificationIterator);
+
return 0;
}
@@ -126,6 +110,7 @@
HRESULT result;
SInt32 score;
UInt32 locationId;
+ UInt8 class, subclass, protocol;
UInt16 vendor;
UInt16 product;
UInt8 serialIndex;
@@ -156,6 +141,16 @@
continue;
}
+ kr = (*iface)->GetInterfaceClass(iface, &class);
+ kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
+ kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
+ if(class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
+ // Ignore non-ADB devices.
+ DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", class, subclass, protocol);
+ (*iface)->Release(iface);
+ continue;
+ }
+
//* this gets us an ioservice, with which we will find the actual
//* device; after getting a plugin, and querying the interface, of
//* course.
@@ -192,12 +187,12 @@
//* Now after all that, we actually have a ref to the device and
//* the interface that matched our criteria
-
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
kr = (*dev)->GetLocationID(dev, &locationId);
if (kr == 0) {
- snprintf(devpathBuf, sizeof(devpathBuf), "usb:%lX", locationId);
+ snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
+ (unsigned int)locationId);
devpath = devpathBuf;
}
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
@@ -384,8 +379,6 @@
void* RunLoopThread(void* unused)
{
- unsigned i;
-
InitUSB();
currentRunLoop = CFRunLoopGetCurrent();
@@ -398,9 +391,7 @@
CFRunLoopRun();
currentRunLoop = 0;
- for (i = 0; i < vendorIdCount; i++) {
- IOObjectRelease(notificationIterators[i]);
- }
+ IOObjectRelease(notificationIterator);
IONotificationPortDestroy(notificationPort);
DBG("RunLoopThread done\n");
@@ -415,9 +406,6 @@
{
adb_thread_t tid;
- notificationIterators = (io_iterator_t*)malloc(
- vendorIdCount * sizeof(io_iterator_t));
-
adb_mutex_init(&start_lock, NULL);
adb_cond_init(&start_cond, NULL);
@@ -442,11 +430,6 @@
close_usb_devices();
if (currentRunLoop)
CFRunLoopStop(currentRunLoop);
-
- if (notificationIterators != NULL) {
- free(notificationIterators);
- notificationIterators = NULL;
- }
}
int usb_write(usb_handle *handle, const void *buf, int len)
@@ -512,14 +495,18 @@
return -1;
}
- result =
- (*handle->interface)->ReadPipe(handle->interface,
- handle->bulkIn, buf, &numBytes);
+ result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
- if (0 == result)
+ if (kIOUSBPipeStalled == result) {
+ DBG(" Pipe stalled, clearing stall.\n");
+ (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
+ result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
+ }
+
+ if (kIOReturnSuccess == result)
return 0;
else {
- DBG("ERR: usb_read failed with status %d\n", result);
+ DBG("ERR: usb_read failed with status %x\n", result);
}
return -1;
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
deleted file mode 100755
index cd5885e..0000000
--- a/adb/usb_vendors.c
+++ /dev/null
@@ -1,346 +0,0 @@
-/*
- * 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 "usb_vendors.h"
-
-#include <stdio.h>
-
-#ifdef _WIN32
-# define WIN32_LEAN_AND_MEAN
-# include "windows.h"
-# include "shlobj.h"
-#else
-# include <unistd.h>
-# include <sys/stat.h>
-#endif
-
-#include "sysdeps.h"
-#include "adb.h"
-
-#define ANDROID_PATH ".android"
-#define ANDROID_ADB_INI "adb_usb.ini"
-
-#define TRACE_TAG TRACE_USB
-
-/* Keep the list below sorted alphabetically by #define name */
-// Acer's USB Vendor ID
-#define VENDOR_ID_ACER 0x0502
-// Allwinner's USB Vendor ID
-#define VENDOR_ID_ALLWINNER 0x1F3A
-// Amlogic's USB Vendor ID
-#define VENDOR_ID_AMLOGIC 0x1b8e
-// AnyDATA's USB Vendor ID
-#define VENDOR_ID_ANYDATA 0x16D5
-// Archos's USB Vendor ID
-#define VENDOR_ID_ARCHOS 0x0E79
-// Asus's USB Vendor ID
-#define VENDOR_ID_ASUS 0x0b05
-// BYD's USB Vendor ID
-#define VENDOR_ID_BYD 0x1D91
-// Compal's USB Vendor ID
-#define VENDOR_ID_COMPAL 0x1219
-// Dell's USB Vendor ID
-#define VENDOR_ID_DELL 0x413c
-// ECS's USB Vendor ID
-#define VENDOR_ID_ECS 0x03fc
-// EMERGING_TECH's USB Vendor ID
-#define VENDOR_ID_EMERGING_TECH 0x297F
-// Emerson's USB Vendor ID
-#define VENDOR_ID_EMERSON 0x2207
-// Foxconn's USB Vendor ID
-#define VENDOR_ID_FOXCONN 0x0489
-// Fujitsu's USB Vendor ID
-#define VENDOR_ID_FUJITSU 0x04C5
-// Funai's USB Vendor ID
-#define VENDOR_ID_FUNAI 0x0F1C
-// Garmin-Asus's USB Vendor ID
-#define VENDOR_ID_GARMIN_ASUS 0x091E
-// Gigabyte's USB Vendor ID
-#define VENDOR_ID_GIGABYTE 0x0414
-// Gigaset's USB Vendor ID
-#define VENDOR_ID_GIGASET 0x1E85
-// Google's USB Vendor ID
-#define VENDOR_ID_GOOGLE 0x18d1
-// Haier's USB Vendor ID
-#define VENDOR_ID_HAIER 0x201E
-// Harris's USB Vendor ID
-#define VENDOR_ID_HARRIS 0x19A5
-// Hisense's USB Vendor ID
-#define VENDOR_ID_HISENSE 0x109b
-// HP's USB Vendor ID
-#define VENDOR_ID_HP 0x03f0
-// HTC's USB Vendor ID
-#define VENDOR_ID_HTC 0x0bb4
-// Huawei's USB Vendor ID
-#define VENDOR_ID_HUAWEI 0x12D1
-// INQ Mobile's USB Vendor ID
-#define VENDOR_ID_INQ_MOBILE 0x2314
-// Intel's USB Vendor ID
-#define VENDOR_ID_INTEL 0x8087
-// IRiver's USB Vendor ID
-#define VENDOR_ID_IRIVER 0x2420
-// K-Touch's USB Vendor ID
-#define VENDOR_ID_K_TOUCH 0x24E3
-// KT Tech's USB Vendor ID
-#define VENDOR_ID_KT_TECH 0x2116
-// Kobo's USB Vendor ID
-#define VENDOR_ID_KOBO 0x2237
-// Kyocera's USB Vendor ID
-#define VENDOR_ID_KYOCERA 0x0482
-// Lab126's USB Vendor ID
-#define VENDOR_ID_LAB126 0x1949
-// Lenovo's USB Vendor ID
-#define VENDOR_ID_LENOVO 0x17EF
-// LenovoMobile's USB Vendor ID
-#define VENDOR_ID_LENOVOMOBILE 0x2006
-// LG's USB Vendor ID
-#define VENDOR_ID_LGE 0x1004
-// Lumigon's USB Vendor ID
-#define VENDOR_ID_LUMIGON 0x25E3
-// Motorola's USB Vendor ID
-#define VENDOR_ID_MOTOROLA 0x22b8
-// MSI's USB Vendor ID
-#define VENDOR_ID_MSI 0x0DB0
-// MTK's USB Vendor ID
-#define VENDOR_ID_MTK 0x0e8d
-// NEC's USB Vendor ID
-#define VENDOR_ID_NEC 0x0409
-// B&N Nook's USB Vendor ID
-#define VENDOR_ID_NOOK 0x2080
-// Nvidia's USB Vendor ID
-#define VENDOR_ID_NVIDIA 0x0955
-// OPPO's USB Vendor ID
-#define VENDOR_ID_OPPO 0x22D9
-// On-The-Go-Video's USB Vendor ID
-#define VENDOR_ID_OTGV 0x2257
-// OUYA's USB Vendor ID
-#define VENDOR_ID_OUYA 0x2836
-// Pantech's USB Vendor ID
-#define VENDOR_ID_PANTECH 0x10A9
-// Pegatron's USB Vendor ID
-#define VENDOR_ID_PEGATRON 0x1D4D
-// Philips's USB Vendor ID
-#define VENDOR_ID_PHILIPS 0x0471
-// Panasonic Mobile Communication's USB Vendor ID
-#define VENDOR_ID_PMC 0x04DA
-// Positivo's USB Vendor ID
-#define VENDOR_ID_POSITIVO 0x1662
-// Qisda's USB Vendor ID
-#define VENDOR_ID_QISDA 0x1D45
-// Qualcomm's USB Vendor ID
-#define VENDOR_ID_QUALCOMM 0x05c6
-// Quanta's USB Vendor ID
-#define VENDOR_ID_QUANTA 0x0408
-// Rockchip's USB Vendor ID
-#define VENDOR_ID_ROCKCHIP 0x2207
-// Samsung's USB Vendor ID
-#define VENDOR_ID_SAMSUNG 0x04e8
-// Sharp's USB Vendor ID
-#define VENDOR_ID_SHARP 0x04dd
-// SK Telesys's USB Vendor ID
-#define VENDOR_ID_SK_TELESYS 0x1F53
-// Sony's USB Vendor ID
-#define VENDOR_ID_SONY 0x054C
-// Sony Ericsson's USB Vendor ID
-#define VENDOR_ID_SONY_ERICSSON 0x0FCE
-// T & A Mobile Phones' USB Vendor ID
-#define VENDOR_ID_T_AND_A 0x1BBB
-// TechFaith's USB Vendor ID
-#define VENDOR_ID_TECHFAITH 0x1d09
-// Teleepoch's USB Vendor ID
-#define VENDOR_ID_TELEEPOCH 0x2340
-// Texas Instruments's USB Vendor ID
-#define VENDOR_ID_TI 0x0451
-// Toshiba's USB Vendor ID
-#define VENDOR_ID_TOSHIBA 0x0930
-// Vizio's USB Vendor ID
-#define VENDOR_ID_VIZIO 0xE040
-// Wacom's USB Vendor ID
-#define VENDOR_ID_WACOM 0x0531
-// Xiaomi's USB Vendor ID
-#define VENDOR_ID_XIAOMI 0x2717
-// YotaDevices's USB Vendor ID
-#define VENDOR_ID_YOTADEVICES 0x2916
-// Yulong Coolpad's USB Vendor ID
-#define VENDOR_ID_YULONG_COOLPAD 0x1EBF
-// ZTE's USB Vendor ID
-#define VENDOR_ID_ZTE 0x19D2
-/* Keep the list above sorted alphabetically by #define name */
-
-/** built-in vendor list */
-/* Keep the list below sorted alphabetically */
-int builtInVendorIds[] = {
- VENDOR_ID_ACER,
- VENDOR_ID_ALLWINNER,
- VENDOR_ID_AMLOGIC,
- VENDOR_ID_ANYDATA,
- VENDOR_ID_ARCHOS,
- VENDOR_ID_ASUS,
- VENDOR_ID_BYD,
- VENDOR_ID_COMPAL,
- VENDOR_ID_DELL,
- VENDOR_ID_ECS,
- VENDOR_ID_EMERGING_TECH,
- VENDOR_ID_EMERSON,
- VENDOR_ID_FOXCONN,
- VENDOR_ID_FUJITSU,
- VENDOR_ID_FUNAI,
- VENDOR_ID_GARMIN_ASUS,
- VENDOR_ID_GIGABYTE,
- VENDOR_ID_GIGASET,
- VENDOR_ID_GOOGLE,
- VENDOR_ID_HAIER,
- VENDOR_ID_HARRIS,
- VENDOR_ID_HISENSE,
- VENDOR_ID_HP,
- VENDOR_ID_HTC,
- VENDOR_ID_HUAWEI,
- VENDOR_ID_INQ_MOBILE,
- VENDOR_ID_INTEL,
- VENDOR_ID_IRIVER,
- VENDOR_ID_KOBO,
- VENDOR_ID_K_TOUCH,
- VENDOR_ID_KT_TECH,
- VENDOR_ID_KYOCERA,
- VENDOR_ID_LAB126,
- VENDOR_ID_LENOVO,
- VENDOR_ID_LENOVOMOBILE,
- VENDOR_ID_LGE,
- VENDOR_ID_LUMIGON,
- VENDOR_ID_MOTOROLA,
- VENDOR_ID_MSI,
- VENDOR_ID_MTK,
- VENDOR_ID_NEC,
- VENDOR_ID_NOOK,
- VENDOR_ID_NVIDIA,
- VENDOR_ID_OPPO,
- VENDOR_ID_OTGV,
- VENDOR_ID_OUYA,
- VENDOR_ID_PANTECH,
- VENDOR_ID_PEGATRON,
- VENDOR_ID_PHILIPS,
- VENDOR_ID_PMC,
- VENDOR_ID_POSITIVO,
- VENDOR_ID_QISDA,
- VENDOR_ID_QUALCOMM,
- VENDOR_ID_QUANTA,
- VENDOR_ID_ROCKCHIP,
- VENDOR_ID_SAMSUNG,
- VENDOR_ID_SHARP,
- VENDOR_ID_SK_TELESYS,
- VENDOR_ID_SONY,
- VENDOR_ID_SONY_ERICSSON,
- VENDOR_ID_T_AND_A,
- VENDOR_ID_TECHFAITH,
- VENDOR_ID_TELEEPOCH,
- VENDOR_ID_TI,
- VENDOR_ID_TOSHIBA,
- VENDOR_ID_VIZIO,
- VENDOR_ID_WACOM,
- VENDOR_ID_XIAOMI,
- VENDOR_ID_YOTADEVICES,
- VENDOR_ID_YULONG_COOLPAD,
- VENDOR_ID_ZTE,
-};
-/* Keep the list above sorted alphabetically */
-
-#define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
-
-/* max number of supported vendor ids (built-in + 3rd party). increase as needed */
-#define VENDOR_COUNT_MAX 128
-
-int vendorIds[VENDOR_COUNT_MAX];
-unsigned vendorIdCount = 0;
-
-int get_adb_usb_ini(char* buff, size_t len);
-
-void usb_vendors_init(void)
-{
- if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
- fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
- exit(2);
- }
-
- /* add the built-in vendors at the beginning of the array */
- memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
-
- /* default array size is the number of built-in vendors */
- vendorIdCount = BUILT_IN_VENDOR_COUNT;
-
- if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
- return;
-
- char temp[PATH_MAX];
- if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
- FILE * f = fopen(temp, "rt");
-
- if (f != NULL) {
- /* The vendor id file is pretty basic. 1 vendor id per line.
- Lines starting with # are comments */
- while (fgets(temp, sizeof(temp), f) != NULL) {
- if (temp[0] == '#')
- continue;
-
- long value = strtol(temp, NULL, 0);
- if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
- fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
- exit(2);
- }
-
- vendorIds[vendorIdCount++] = (int)value;
-
- /* make sure we don't go beyond the array */
- if (vendorIdCount == VENDOR_COUNT_MAX) {
- break;
- }
- }
- fclose(f);
- }
- }
-}
-
-/* Utils methods */
-
-/* builds the path to the adb vendor id file. returns 0 if success */
-int build_path(char* buff, size_t len, const char* format, const char* home)
-{
- if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
- return 1;
- }
-
- return 0;
-}
-
-/* fills buff with the path to the adb vendor id file. returns 0 if success */
-int get_adb_usb_ini(char* buff, size_t len)
-{
-#ifdef _WIN32
- const char* home = getenv("ANDROID_SDK_HOME");
- if (home != NULL) {
- return build_path(buff, len, "%s\\%s\\%s", home);
- } else {
- char path[MAX_PATH];
- SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
- return build_path(buff, len, "%s\\%s\\%s", path);
- }
-#else
- const char* home = getenv("HOME");
- if (home == NULL)
- home = "/tmp";
-
- return build_path(buff, len, "%s/%s/%s", home);
-#endif
-}
diff --git a/adb/usb_windows.c b/adb/usb_windows.cpp
similarity index 98%
rename from adb/usb_windows.c
rename to adb/usb_windows.cpp
index 4936b77..3c5533b 100644
--- a/adb/usb_windows.c
+++ b/adb/usb_windows.cpp
@@ -14,17 +14,20 @@
* limitations under the License.
*/
+#include <winsock2.h> // winsock.h *must* be included before windows.h.
+#include <adb_api.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <usb100.h>
#include <windows.h>
#include <winerror.h>
-#include <errno.h>
-#include <usb100.h>
-#include <adb_api.h>
-#include <stdio.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_USB
#include "adb.h"
+#include "transport.h"
/** Structure usb_handle describes our connection to the usb device via
AdbWinApi.dll. This structure is returned from usb_open() routine and
@@ -310,14 +313,14 @@
int xfer = (len > 4096) ? 4096 : len;
ret = AdbReadEndpointSync(handle->adb_read_pipe,
- (void*)data,
+ data,
(unsigned long)xfer,
&read,
time_out);
int saved_errno = GetLastError();
D("usb_write got: %ld, expected: %d, errno: %d\n", read, xfer, saved_errno);
if (ret) {
- data += read;
+ data = (char *)data + read;
len -= read;
if (len == 0)
diff --git a/adf/libadf/Android.mk b/adf/libadf/Android.mk
index 908aa6c..7df354b 100644
--- a/adf/libadf/Android.mk
+++ b/adf/libadf/Android.mk
@@ -18,6 +18,7 @@
LOCAL_SRC_FILES := adf.c
LOCAL_MODULE := libadf
LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS += -Werror
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_EXPORT_C_INCLUDE_DIRS)
include $(BUILD_STATIC_LIBRARY)
diff --git a/adf/libadf/adf.c b/adf/libadf/adf.c
index 871629e..66c329c 100644
--- a/adf/libadf/adf.c
+++ b/adf/libadf/adf.c
@@ -17,9 +17,11 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
#include <linux/limits.h>
@@ -768,7 +770,7 @@
const __u32 *formats, size_t n_formats,
adf_id_t *interface, adf_id_t *overlay_engine)
{
- adf_id_t *intfs;
+ adf_id_t *intfs = NULL;
ssize_t n_intfs = adf_interfaces(dev, &intfs);
if (n_intfs < 0)
diff --git a/adf/libadf/tests/Android.mk b/adf/libadf/tests/Android.mk
new file mode 100644
index 0000000..68e5817
--- /dev/null
+++ b/adf/libadf/tests/Android.mk
@@ -0,0 +1,23 @@
+#
+# Copyright (C) 2013 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 := $(my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := adf_test.cpp
+LOCAL_MODULE := adf-unit-tests
+LOCAL_STATIC_LIBRARIES := libadf
+LOCAL_CFLAGS += -Werror
+include $(BUILD_NATIVE_TEST)
diff --git a/adf/libadf/tests/adf_test.cpp b/adf/libadf/tests/adf_test.cpp
new file mode 100644
index 0000000..01b2785
--- /dev/null
+++ b/adf/libadf/tests/adf_test.cpp
@@ -0,0 +1,342 @@
+/*
+ * Copyright (C) 2013 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 <errno.h>
+#include <fcntl.h>
+
+#include <adf/adf.h>
+#include <gtest/gtest.h>
+#include <sys/mman.h>
+
+class AdfTest : public testing::Test {
+public:
+ AdfTest() : intf_id(0), intf(-1), eng_id(0), eng(-1) { }
+
+ virtual void SetUp() {
+ int err = adf_device_open(dev_id, O_RDWR, &dev);
+ ASSERT_GE(err, 0) << "opening ADF device " << dev_id <<
+ " failed: " << strerror(-err);
+
+ err = adf_find_simple_post_configuration(&dev, fmt8888, n_fmt8888,
+ &intf_id, &eng_id);
+ ASSERT_GE(err, 0) << "finding ADF configuration failed: " <<
+ strerror(-err);
+
+ intf = adf_interface_open(&dev, intf_id, O_RDWR);
+ ASSERT_GE(intf, 0) << "opening ADF interface " << dev_id << "." <<
+ intf_id << " failed: " << strerror(-intf);
+
+ eng = adf_overlay_engine_open(&dev, eng_id, O_RDWR);
+ ASSERT_GE(eng, 0) << "opening ADF overlay engine " << dev_id << "." <<
+ eng_id << " failed: " << strerror(-eng);
+ }
+
+ virtual void TearDown() {
+ if (eng >= 0)
+ close(eng);
+ if (intf >= 0)
+ close(intf);
+ adf_device_close(&dev);
+ }
+
+ void get8888Format(uint32_t &fmt, char fmt_str[ADF_FORMAT_STR_SIZE]) {
+ adf_overlay_engine_data data;
+ int err = adf_get_overlay_engine_data(eng, &data);
+ ASSERT_GE(err, 0) << "getting ADF overlay engine data failed: " <<
+ strerror(-err);
+
+ for (size_t i = 0; i < data.n_supported_formats; i++) {
+ for (size_t j = 0; j < n_fmt8888; j++) {
+ if (data.supported_formats[i] == fmt8888[j]) {
+ fmt = data.supported_formats[i];
+ adf_format_str(fmt, fmt_str);
+ adf_free_overlay_engine_data(&data);
+ return;
+ }
+ }
+ }
+
+ adf_free_overlay_engine_data(&data);
+ FAIL(); /* this should never happen */
+ }
+
+ void drawCheckerboard(void *buf, uint32_t w, uint32_t h, uint32_t pitch) {
+ uint8_t *buf8 = reinterpret_cast<uint8_t *>(buf);
+ for (uint32_t y = 0; y < h / 2; y++) {
+ uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
+ for (uint32_t x = 0; x < w / 2; x++)
+ scanline[x] = 0xFF0000FF;
+ for (uint32_t x = w / 2; x < w; x++)
+ scanline[x] = 0xFF00FFFF;
+ }
+ for (uint32_t y = h / 2; y < h; y++) {
+ uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
+ for (uint32_t x = 0; x < w / 2; x++)
+ scanline[x] = 0xFFFF00FF;
+ for (uint32_t x = w / 2; x < w; x++)
+ scanline[x] = 0xFFFFFFFF;
+ }
+ }
+
+ /* various helpers to call ADF and die on failure */
+
+ void getInterfaceData(adf_interface_data &data) {
+ int err = adf_get_interface_data(intf, &data);
+ ASSERT_GE(err, 0) << "getting ADF interface data failed: " <<
+ strerror(-err);
+ }
+
+ void getCurrentMode(uint32_t &w, uint32_t &h) {
+ adf_interface_data data;
+ ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+ w = data.current_mode.hdisplay;
+ h = data.current_mode.vdisplay;
+ adf_free_interface_data(&data);
+ }
+
+ void blank(uint8_t mode) {
+ int err = adf_interface_blank(intf, mode);
+ ASSERT_FALSE(err < 0 && err != -EBUSY) <<
+ "unblanking interface failed: " << strerror(-err);
+ }
+
+ void attach() {
+ int err = adf_device_attach(&dev, eng_id, intf_id);
+ ASSERT_FALSE(err < 0 && err != -EALREADY) <<
+ "attaching overlay engine " << eng_id << " to interface " <<
+ intf_id << " failed: " << strerror(-err);
+ }
+
+ void detach() {
+ int err = adf_device_detach(&dev, eng_id, intf_id);
+ ASSERT_FALSE(err < 0 && err != -EINVAL) <<
+ "detaching overlay engine " << eng_id << " from interface " <<
+ intf_id << " failed: " << strerror(-err);
+ }
+
+ void readVsyncTimestamp(uint64_t ×tamp) {
+ adf_event *event;
+ int err = adf_read_event(intf, &event);
+ ASSERT_GE(err, 0) << "reading ADF event failed: " << strerror(-err);
+
+ ASSERT_EQ(ADF_EVENT_VSYNC, event->type);
+ ASSERT_EQ(sizeof(adf_vsync_event), event->length);
+
+ adf_vsync_event *vsync_event =
+ reinterpret_cast<adf_vsync_event *>(event);
+ timestamp = vsync_event->timestamp;
+ free(event);
+ }
+
+protected:
+ adf_device dev;
+ adf_id_t intf_id;
+ int intf;
+ adf_id_t eng_id;
+ int eng;
+
+private:
+ const static adf_id_t dev_id = 0;
+ const static __u32 fmt8888[];
+ const static size_t n_fmt8888;
+};
+
+const __u32 AdfTest::fmt8888[] = {
+ DRM_FORMAT_XRGB8888,
+ DRM_FORMAT_XBGR8888,
+ DRM_FORMAT_RGBX8888,
+ DRM_FORMAT_BGRX8888,
+ DRM_FORMAT_ARGB8888,
+ DRM_FORMAT_ABGR8888,
+ DRM_FORMAT_RGBA8888,
+ DRM_FORMAT_BGRA8888
+};
+const size_t AdfTest::n_fmt8888 = sizeof(fmt8888) / sizeof(fmt8888[0]);
+
+TEST(adf, devices) {
+ adf_id_t *devs;
+ ssize_t n_devs = adf_devices(&devs);
+ free(devs);
+
+ ASSERT_GE(n_devs, 0) << "enumerating ADF devices failed: " <<
+ strerror(-n_devs);
+ ASSERT_TRUE(devs != NULL);
+}
+
+TEST_F(AdfTest, device_data) {
+ adf_device_data data;
+ int err = adf_get_device_data(&dev, &data);
+ ASSERT_GE(err, 0) << "getting ADF device data failed: " << strerror(-err);
+
+ EXPECT_LT(data.n_attachments, ADF_MAX_ATTACHMENTS);
+ EXPECT_GT(data.n_allowed_attachments, 0U);
+ EXPECT_LT(data.n_allowed_attachments, ADF_MAX_ATTACHMENTS);
+ EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
+ adf_free_device_data(&data);
+}
+
+TEST_F(AdfTest, interface_data) {
+ adf_interface_data data;
+ ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+
+ EXPECT_LT(data.type, ADF_INTF_TYPE_MAX);
+ EXPECT_LE(data.dpms_state, DRM_MODE_DPMS_OFF);
+ EXPECT_EQ(1, data.hotplug_detect);
+ EXPECT_GT(data.n_available_modes, 0U);
+ EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
+ adf_free_interface_data(&data);
+}
+
+TEST_F(AdfTest, overlay_engine_data) {
+ adf_overlay_engine_data data;
+ int err = adf_get_overlay_engine_data(eng, &data);
+ ASSERT_GE(err, 0) << "getting ADF overlay engine failed: " <<
+ strerror(-err);
+
+ EXPECT_GT(data.n_supported_formats, 0U);
+ EXPECT_LT(data.n_supported_formats, ADF_MAX_SUPPORTED_FORMATS);
+ EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
+ adf_free_overlay_engine_data(&data);
+}
+
+TEST_F(AdfTest, blank) {
+ int err = adf_interface_blank(intf, (uint8_t)-1);
+ EXPECT_EQ(-EINVAL, err) << "setting bogus DPMS mode should have failed";
+
+ err = adf_interface_blank(eng, DRM_MODE_DPMS_OFF);
+ EXPECT_EQ(-EINVAL, err) << "blanking overlay engine should have failed";
+
+ ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_OFF));
+ err = adf_interface_blank(intf, DRM_MODE_DPMS_OFF);
+ EXPECT_EQ(-EBUSY, err) << "blanking interface twice should have failed";
+
+ ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+ err = adf_interface_blank(intf, DRM_MODE_DPMS_ON);
+ EXPECT_EQ(-EBUSY, err) << "unblanking interface twice should have failed";
+
+ adf_interface_data data;
+ ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+ EXPECT_EQ(DRM_MODE_DPMS_ON, data.dpms_state);
+ adf_free_interface_data(&data);
+}
+
+TEST_F(AdfTest, event) {
+ int err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, true);
+ EXPECT_EQ(-EINVAL, err) << "enabling bogus ADF event should have failed";
+
+ err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, false);
+ EXPECT_EQ(-EINVAL, err) << "disabling bogus ADF event should have failed";
+
+ err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
+ ASSERT_GE(err, 0) << "enabling vsync event failed: " << strerror(-err);
+
+ err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
+ EXPECT_EQ(-EALREADY, err) <<
+ "enabling vsync event twice should have failed";
+
+ ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+
+ uint64_t timestamp1, timestamp2;
+ ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp1));
+ ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp2));
+ EXPECT_GT(timestamp2, timestamp1);
+
+ err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
+ EXPECT_GE(err, 0) << "disabling vsync event failed: " << strerror(-err);
+
+ err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
+ EXPECT_EQ(-EALREADY, err) <<
+ "disabling vsync event twice should have failed";
+}
+
+TEST_F(AdfTest, attach) {
+ ASSERT_NO_FATAL_FAILURE(attach());
+ int err = adf_device_attach(&dev, eng_id, intf_id);
+ EXPECT_EQ(-EALREADY, err) << "attaching overlay engine " << eng_id <<
+ " to interface " << intf_id << " twice should have failed";
+
+ ASSERT_NO_FATAL_FAILURE(detach());
+ err = adf_device_detach(&dev, eng_id, intf_id);
+ EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
+ " from interface " << intf_id << " twice should have failed";
+
+ err = adf_device_attach(&dev, eng_id, ADF_MAX_INTERFACES);
+ EXPECT_EQ(-EINVAL, err) << "attaching overlay engine " << eng_id <<
+ " to bogus interface should have failed";
+
+ err = adf_device_detach(&dev, eng_id, ADF_MAX_INTERFACES);
+ EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
+ " from bogus interface should have failed";
+}
+
+TEST_F(AdfTest, simple_buffer_alloc) {
+ uint32_t w = 0, h = 0;
+ ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
+
+ uint32_t format;
+ char format_str[ADF_FORMAT_STR_SIZE];
+ ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
+
+ uint32_t offset;
+ uint32_t pitch;
+ int buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
+ &pitch);
+ EXPECT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
+ format_str << " buffer failed: " << strerror(-buf_fd);
+ EXPECT_GE(pitch, w * 4);
+ close(buf_fd);
+
+ buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, 0xDEADBEEF, &offset,
+ &pitch);
+ /* n.b.: ADF only allows simple buffers with built-in RGB formats,
+ so this should fail even if a driver supports custom format 0xDEADBEEF */
+ EXPECT_EQ(-EINVAL, buf_fd) <<
+ "allocating buffer with bogus format should have failed";
+}
+
+TEST_F(AdfTest, simple_buffer) {
+ uint32_t w = 0, h = 0;
+ ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
+
+ uint32_t format = 0;
+ char format_str[ADF_FORMAT_STR_SIZE];
+ ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
+
+ uint32_t offset;
+ uint32_t pitch;
+ int buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
+ &pitch);
+ ASSERT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
+ format_str << " buffer failed: " << strerror(-buf_fd);
+ EXPECT_GE(pitch, w * 4);
+
+ void *mapped = mmap(NULL, pitch * h, PROT_WRITE, MAP_SHARED, buf_fd,
+ offset);
+ ASSERT_NE(mapped, MAP_FAILED) << "mapping " << w << "x" << h << " " <<
+ format_str << " buffer failed: " << strerror(-errno);
+ drawCheckerboard(mapped, w, h, pitch);
+ munmap(mapped, pitch * h);
+
+ ASSERT_NO_FATAL_FAILURE(attach());
+ ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+
+ int release_fence = adf_interface_simple_post(intf, eng_id, w, h, format,
+ buf_fd, offset, pitch, -1);
+ close(buf_fd);
+ ASSERT_GE(release_fence, 0) << "posting " << w << "x" << h << " " <<
+ format_str << " buffer failed: " << strerror(-release_fence);
+ close(release_fence);
+}
diff --git a/adf/libadfhwc/Android.mk b/adf/libadfhwc/Android.mk
index acea322..898f9c9 100644
--- a/adf/libadfhwc/Android.mk
+++ b/adf/libadfhwc/Android.mk
@@ -19,7 +19,7 @@
LOCAL_MODULE := libadfhwc
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_LIBRARIES := libadf liblog libutils
-LOCAL_CFLAGS += -DLOG_TAG=\"adfhwc\"
+LOCAL_CFLAGS += -DLOG_TAG=\"adfhwc\" -Werror
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_EXPORT_C_INCLUDE_DIRS)
include $(BUILD_STATIC_LIBRARY)
diff --git a/adf/libadfhwc/adfhwc.cpp b/adf/libadfhwc/adfhwc.cpp
index dee3cae..21f245e 100644
--- a/adf/libadfhwc/adfhwc.cpp
+++ b/adf/libadfhwc/adfhwc.cpp
@@ -15,6 +15,7 @@
*/
#include <fcntl.h>
+#include <malloc.h>
#include <poll.h>
#include <pthread.h>
#include <sys/resource.h>
@@ -257,7 +258,7 @@
ret = adf_set_event(dup_intf_fd, ADF_EVENT_HOTPLUG, 1);
if (ret < 0 && ret != -EINVAL) {
- ALOGE("failed to enable hotplug event on display %u: %s",
+ ALOGE("failed to enable hotplug event on display %zu: %s",
i, strerror(errno));
goto err;
}
diff --git a/base/.clang-format b/base/.clang-format
new file mode 100644
index 0000000..2b83a1f
--- /dev/null
+++ b/base/.clang-format
@@ -0,0 +1,11 @@
+BasedOnStyle: Google
+AllowShortBlocksOnASingleLine: false
+AllowShortFunctionsOnASingleLine: false
+
+CommentPragmas: NOLINT:.*
+DerivePointerAlignment: false
+IndentWidth: 2
+PointerAlignment: Left
+TabWidth: 2
+UseTab: Never
+PenaltyExcessCharacter: 32
diff --git a/base/Android.mk b/base/Android.mk
new file mode 100644
index 0000000..0e1a9b6
--- /dev/null
+++ b/base/Android.mk
@@ -0,0 +1,98 @@
+#
+# Copyright (C) 2015 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)
+
+libbase_src_files := \
+ file.cpp \
+ stringprintf.cpp \
+ strings.cpp \
+
+libbase_test_src_files := \
+ file_test.cpp \
+ stringprintf_test.cpp \
+ strings_test.cpp \
+
+libbase_cppflags := \
+ -Wall \
+ -Wextra \
+ -Werror \
+
+# Device
+# ------------------------------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := $(libbase_src_files)
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CPPFLAGS := $(libbase_cppflags)
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_MULTILIB := both
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase
+LOCAL_CLANG := true
+LOCAL_WHOLE_STATIC_LIBRARIES := libbase
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_MULTILIB := both
+include $(BUILD_SHARED_LIBRARY)
+
+# Host
+# ------------------------------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := $(libbase_src_files)
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CPPFLAGS := $(libbase_cppflags)
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_MULTILIB := both
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase
+LOCAL_CLANG := true
+LOCAL_WHOLE_STATIC_LIBRARIES := libbase
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_MULTILIB := both
+include $(BUILD_HOST_SHARED_LIBRARY)
+
+# Tests
+# ------------------------------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase_test
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := $(libbase_test_src_files)
+LOCAL_CPPFLAGS := $(libbase_cppflags)
+LOCAL_SHARED_LIBRARIES := libbase
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+include $(BUILD_NATIVE_TEST)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libbase_test
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := $(libbase_test_src_files)
+LOCAL_CPPFLAGS := $(libbase_cppflags)
+LOCAL_SHARED_LIBRARIES := libbase
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+include $(BUILD_HOST_NATIVE_TEST)
diff --git a/base/CPPLINT.cfg b/base/CPPLINT.cfg
new file mode 100644
index 0000000..5ee068e
--- /dev/null
+++ b/base/CPPLINT.cfg
@@ -0,0 +1,2 @@
+set noparent
+filter=-build/header_guard
diff --git a/base/file.cpp b/base/file.cpp
new file mode 100644
index 0000000..118071e
--- /dev/null
+++ b/base/file.cpp
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2015 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 "base/file.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <string>
+
+#include "base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
+#define LOG_TAG "base.file"
+#include "cutils/log.h"
+
+namespace android {
+namespace base {
+
+bool ReadFdToString(int fd, std::string* content) {
+ content->clear();
+
+ char buf[BUFSIZ];
+ ssize_t n;
+ while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
+ content->append(buf, n);
+ }
+ return (n == 0) ? true : false;
+}
+
+bool ReadFileToString(const std::string& path, std::string* content) {
+ content->clear();
+
+ int fd =
+ TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
+ if (fd == -1) {
+ return false;
+ }
+ bool result = ReadFdToString(fd, content);
+ TEMP_FAILURE_RETRY(close(fd));
+ return result;
+}
+
+bool WriteStringToFd(const std::string& content, int fd) {
+ const char* p = content.data();
+ size_t left = content.size();
+ while (left > 0) {
+ ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
+ if (n == -1) {
+ return false;
+ }
+ p += n;
+ left -= n;
+ }
+ return true;
+}
+
+static bool CleanUpAfterFailedWrite(const std::string& path) {
+ // Something went wrong. Let's not leave a corrupt file lying around.
+ int saved_errno = errno;
+ unlink(path.c_str());
+ errno = saved_errno;
+ return false;
+}
+
+#if !defined(_WIN32)
+bool WriteStringToFile(const std::string& content, const std::string& path,
+ mode_t mode, uid_t owner, gid_t group) {
+ int fd = TEMP_FAILURE_RETRY(
+ open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
+ mode));
+ if (fd == -1) {
+ ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
+ return false;
+ }
+
+ // We do an explicit fchmod here because we assume that the caller really
+ // meant what they said and doesn't want the umask-influenced mode.
+ if (fchmod(fd, mode) == -1) {
+ ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
+ return CleanUpAfterFailedWrite(path);
+ }
+ if (fchown(fd, owner, group) == -1) {
+ ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno));
+ return CleanUpAfterFailedWrite(path);
+ }
+ if (!WriteStringToFd(content, fd)) {
+ ALOGE("android::WriteStringToFile write failed: %s", strerror(errno));
+ return CleanUpAfterFailedWrite(path);
+ }
+ TEMP_FAILURE_RETRY(close(fd));
+ return true;
+}
+#endif
+
+bool WriteStringToFile(const std::string& content, const std::string& path) {
+ int fd = TEMP_FAILURE_RETRY(
+ open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
+ DEFFILEMODE));
+ if (fd == -1) {
+ return false;
+ }
+
+ bool result = WriteStringToFd(content, fd);
+ TEMP_FAILURE_RETRY(close(fd));
+ return result || CleanUpAfterFailedWrite(path);
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/file_test.cpp b/base/file_test.cpp
new file mode 100644
index 0000000..34b8755
--- /dev/null
+++ b/base/file_test.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2013 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 "base/file.h"
+
+#include <gtest/gtest.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <string>
+
+class TemporaryFile {
+ public:
+ TemporaryFile() {
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
+ }
+ }
+
+ ~TemporaryFile() {
+ close(fd);
+ unlink(filename);
+ }
+
+ int fd;
+ char filename[1024];
+
+ private:
+ void init(const char* tmp_dir) {
+ snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
+ fd = mkstemp(filename);
+ }
+};
+
+TEST(file, ReadFileToString_ENOENT) {
+ std::string s("hello");
+ errno = 0;
+ ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
+ EXPECT_EQ(ENOENT, errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(file, ReadFileToString_success) {
+ std::string s("hello");
+ ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) << errno;
+ EXPECT_GT(s.length(), 6U);
+ EXPECT_EQ('\n', s[s.length() - 1]);
+ s[5] = 0;
+ EXPECT_STREQ("Linux", s.c_str());
+}
+
+TEST(file, WriteStringToFile) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) << errno;
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFile2) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660,
+ getuid(), getgid()))
+ << errno;
+ struct stat sb;
+ ASSERT_EQ(0, stat(tf.filename, &sb));
+ ASSERT_EQ(0660U, (sb.st_mode & ~S_IFMT));
+ ASSERT_EQ(getuid(), sb.st_uid);
+ ASSERT_EQ(getgid(), sb.st_gid);
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFd) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
+
+ ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
+
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << errno;
+ EXPECT_EQ("abc", s);
+}
diff --git a/base/include/base/file.h b/base/include/base/file.h
new file mode 100644
index 0000000..ef97742
--- /dev/null
+++ b/base/include/base/file.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2015 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 BASE_FILE_H
+#define BASE_FILE_H
+
+#include <sys/stat.h>
+#include <string>
+
+namespace android {
+namespace base {
+
+bool ReadFdToString(int fd, std::string* content);
+bool ReadFileToString(const std::string& path, std::string* content);
+
+bool WriteStringToFile(const std::string& content, const std::string& path);
+bool WriteStringToFd(const std::string& content, int fd);
+
+#if !defined(_WIN32)
+bool WriteStringToFile(const std::string& content, const std::string& path,
+ mode_t mode, uid_t owner, gid_t group);
+#endif
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_FILE_H
diff --git a/base/include/base/macros.h b/base/include/base/macros.h
new file mode 100644
index 0000000..b1ce7c6
--- /dev/null
+++ b/base/include/base/macros.h
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2015 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 UTILS_MACROS_H
+#define UTILS_MACROS_H
+
+#include <stddef.h> // for size_t
+#include <unistd.h> // for TEMP_FAILURE_RETRY
+
+// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
+#ifndef TEMP_FAILURE_RETRY
+#define TEMP_FAILURE_RETRY(exp) \
+ ({ \
+ decltype(exp) _rc; \
+ do { \
+ _rc = (exp); \
+ } while (_rc == -1 && errno == EINTR); \
+ _rc; \
+ })
+#endif
+
+// A macro to disallow the copy constructor and operator= functions
+// This must be placed in the private: declarations for a class.
+//
+// For disallowing only assign or copy, delete the relevant operator or
+// constructor, for example:
+// void operator=(const TypeName&) = delete;
+// Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken
+// semantically, one should either use disallow both or neither. Try to
+// avoid these in new code.
+//
+// When building with C++11 toolchains, just use the language support
+// for explicitly deleted methods.
+#if __cplusplus >= 201103L
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
+ TypeName(const TypeName&) = delete; \
+ void operator=(const TypeName&) = delete
+#else
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
+ TypeName(const TypeName&); \
+ void operator=(const TypeName&)
+#endif
+
+// A macro to disallow all the implicit constructors, namely the
+// default constructor, copy constructor and operator= functions.
+//
+// This should be used in the private: declarations for a class
+// that wants to prevent anyone from instantiating it. This is
+// especially useful for classes containing only static methods.
+#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
+ TypeName(); \
+ DISALLOW_COPY_AND_ASSIGN(TypeName)
+
+// The arraysize(arr) macro returns the # of elements in an array arr.
+// The expression is a compile-time constant, and therefore can be
+// used in defining new arrays, for example. If you use arraysize on
+// a pointer by mistake, you will get a compile-time error.
+//
+// One caveat is that arraysize() doesn't accept any array of an
+// anonymous type or a type defined inside a function. In these rare
+// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
+// due to a limitation in C++'s template system. The limitation might
+// eventually be removed, but it hasn't happened yet.
+
+// This template function declaration is used in defining arraysize.
+// Note that the function doesn't need an implementation, as we only
+// use its type.
+template <typename T, size_t N>
+char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting)
+
+#define arraysize(array) (sizeof(ArraySizeHelper(array)))
+
+// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
+// but can be used on anonymous types or types defined inside
+// functions. It's less safe than arraysize as it accepts some
+// (although not all) pointers. Therefore, you should use arraysize
+// whenever possible.
+//
+// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
+// size_t.
+//
+// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
+//
+// "warning: division by zero in ..."
+//
+// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
+// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
+//
+// The following comments are on the implementation details, and can
+// be ignored by the users.
+//
+// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
+// the array) and sizeof(*(arr)) (the # of bytes in one array
+// element). If the former is divisible by the latter, perhaps arr is
+// indeed an array, in which case the division result is the # of
+// elements in the array. Otherwise, arr cannot possibly be an array,
+// and we generate a compiler error to prevent the code from
+// compiling.
+//
+// Since the size of bool is implementation-defined, we need to cast
+// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
+// result has type size_t.
+//
+// This macro is not perfect as it wrongfully accepts certain
+// pointers, namely where the pointer size is divisible by the pointee
+// size. Since all our code has to go through a 32-bit compiler,
+// where a pointer is 4 bytes, this means all pointers to a type whose
+// size is 3 or greater than 4 will be (righteously) rejected.
+#define ARRAYSIZE_UNSAFE(a) \
+ ((sizeof(a) / sizeof(*(a))) / \
+ static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
+
+#define LIKELY(x) __builtin_expect((x), true)
+#define UNLIKELY(x) __builtin_expect((x), false)
+
+#define WARN_UNUSED __attribute__((warn_unused_result))
+
+// A deprecated function to call to create a false use of the parameter, for
+// example:
+// int foo(int x) { UNUSED(x); return 10; }
+// to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
+template <typename... T>
+void UNUSED(const T&...) {
+}
+
+// An attribute to place on a parameter to a function, for example:
+// int foo(int x ATTRIBUTE_UNUSED) { return 10; }
+// to avoid compiler warnings.
+#define ATTRIBUTE_UNUSED __attribute__((__unused__))
+
+// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
+// between switch labels:
+// switch (x) {
+// case 40:
+// case 41:
+// if (truth_is_out_there) {
+// ++x;
+// FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
+// // comments.
+// } else {
+// return x;
+// }
+// case 42:
+// ...
+//
+// As shown in the example above, the FALLTHROUGH_INTENDED macro should be
+// followed by a semicolon. It is designed to mimic control-flow statements
+// like 'break;', so it can be placed in most places where 'break;' can, but
+// only if there are no statements on the execution path between it and the
+// next switch label.
+//
+// When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is
+// expanded to [[clang::fallthrough]] attribute, which is analysed when
+// performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
+// See clang documentation on language extensions for details:
+// http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
+//
+// When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
+// effect on diagnostics.
+//
+// In either case this macro has no effect on runtime behavior and performance
+// of code.
+#if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
+#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
+#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
+#endif
+#endif
+
+#ifndef FALLTHROUGH_INTENDED
+#define FALLTHROUGH_INTENDED \
+ do { \
+ } while (0)
+#endif
+
+#endif // UTILS_MACROS_H
diff --git a/base/include/base/stringprintf.h b/base/include/base/stringprintf.h
new file mode 100644
index 0000000..195c1de
--- /dev/null
+++ b/base/include/base/stringprintf.h
@@ -0,0 +1,40 @@
+/*
+ * 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 BASE_STRINGPRINTF_H
+#define BASE_STRINGPRINTF_H
+
+#include <stdarg.h>
+#include <string>
+
+namespace android {
+namespace base {
+
+// Returns a string corresponding to printf-like formatting of the arguments.
+std::string StringPrintf(const char* fmt, ...)
+ __attribute__((__format__(__printf__, 1, 2)));
+
+// Appends a printf-like formatting of the arguments to 'dst'.
+void StringAppendF(std::string* dst, const char* fmt, ...)
+ __attribute__((__format__(__printf__, 2, 3)));
+
+// Appends a printf-like formatting of the arguments to 'dst'.
+void StringAppendV(std::string* dst, const char* format, va_list ap);
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_STRINGPRINTF_H
diff --git a/base/include/base/strings.h b/base/include/base/strings.h
new file mode 100644
index 0000000..5ddfbbd
--- /dev/null
+++ b/base/include/base/strings.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2015 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 BASE_STRINGS_H
+#define BASE_STRINGS_H
+
+#include <string>
+#include <vector>
+
+namespace android {
+namespace base {
+
+// Splits a string using the given separator character into a vector of strings.
+// Empty strings will be omitted.
+void Split(const std::string& s, char separator,
+ std::vector<std::string>* result);
+
+// Trims whitespace off both ends of the given string.
+std::string Trim(const std::string& s);
+
+// Joins a vector of strings into a single string, using the given separator.
+template <typename StringT>
+std::string Join(const std::vector<StringT>& strings, char separator);
+
+// Tests whether 's' starts with 'prefix'.
+bool StartsWith(const std::string& s, const char* prefix);
+
+// Tests whether 's' ends with 'suffix'.
+bool EndsWith(const std::string& s, const char* suffix);
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_STRINGS_H
diff --git a/base/stringprintf.cpp b/base/stringprintf.cpp
new file mode 100644
index 0000000..d55ff52
--- /dev/null
+++ b/base/stringprintf.cpp
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+
+#include "base/stringprintf.h"
+
+#include <stdio.h>
+
+#include <string>
+
+namespace android {
+namespace base {
+
+void StringAppendV(std::string* dst, const char* format, va_list ap) {
+ // First try with a small fixed size buffer
+ char space[1024];
+
+ // It's possible for methods that use a va_list to invalidate
+ // the data in it upon use. The fix is to make a copy
+ // of the structure before using it and use that copy instead.
+ va_list backup_ap;
+ va_copy(backup_ap, ap);
+ int result = vsnprintf(space, sizeof(space), format, backup_ap);
+ va_end(backup_ap);
+
+ if (result < static_cast<int>(sizeof(space))) {
+ if (result >= 0) {
+ // Normal case -- everything fit.
+ dst->append(space, result);
+ return;
+ }
+
+ if (result < 0) {
+ // Just an error.
+ return;
+ }
+ }
+
+ // Increase the buffer size to the size requested by vsnprintf,
+ // plus one for the closing \0.
+ int length = result + 1;
+ char* buf = new char[length];
+
+ // Restore the va_list before we use it again
+ va_copy(backup_ap, ap);
+ result = vsnprintf(buf, length, format, backup_ap);
+ va_end(backup_ap);
+
+ if (result >= 0 && result < length) {
+ // It fit
+ dst->append(buf, result);
+ }
+ delete[] buf;
+}
+
+std::string StringPrintf(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ std::string result;
+ StringAppendV(&result, fmt, ap);
+ va_end(ap);
+ return result;
+}
+
+void StringAppendF(std::string* dst, const char* format, ...) {
+ va_list ap;
+ va_start(ap, format);
+ StringAppendV(dst, format, ap);
+ va_end(ap);
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/stringprintf_test.cpp b/base/stringprintf_test.cpp
new file mode 100644
index 0000000..5cc2086
--- /dev/null
+++ b/base/stringprintf_test.cpp
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+#include "base/stringprintf.h"
+
+#include <gtest/gtest.h>
+
+#include <string>
+
+TEST(StringPrintfTest, HexSizeT) {
+ size_t size = 0x00107e59;
+ EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
+ EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
+}
+
+TEST(StringPrintfTest, StringAppendF) {
+ std::string s("a");
+ android::base::StringAppendF(&s, "b");
+ EXPECT_EQ("ab", s);
+}
+
+TEST(StringPrintfTest, Errno) {
+ errno = 123;
+ android::base::StringPrintf("hello %s", "world");
+ EXPECT_EQ(123, errno);
+}
+
+void TestN(size_t n) {
+ char* buf = new char[n + 1];
+ memset(buf, 'x', n);
+ buf[n] = '\0';
+ std::string s(android::base::StringPrintf("%s", buf));
+ EXPECT_EQ(buf, s);
+ delete[] buf;
+}
+
+TEST(StringPrintfTest, At1023) {
+ TestN(1023);
+}
+
+TEST(StringPrintfTest, At1024) {
+ TestN(1024);
+}
+
+TEST(StringPrintfTest, At1025) {
+ TestN(1025);
+}
diff --git a/base/strings.cpp b/base/strings.cpp
new file mode 100644
index 0000000..224a46f
--- /dev/null
+++ b/base/strings.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2015 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 "base/strings.h"
+
+#include <string>
+#include <vector>
+
+namespace android {
+namespace base {
+
+void Split(const std::string& s, char separator,
+ std::vector<std::string>* result) {
+ const char* p = s.data();
+ const char* end = p + s.size();
+ while (p != end) {
+ if (*p == separator) {
+ ++p;
+ } else {
+ const char* start = p;
+ while (++p != end && *p != separator) {
+ // Skip to the next occurrence of the separator.
+ }
+ result->push_back(std::string(start, p - start));
+ }
+ }
+}
+
+std::string Trim(const std::string& s) {
+ std::string result;
+
+ if (s.size() == 0) {
+ return result;
+ }
+
+ size_t start_index = 0;
+ size_t end_index = s.size() - 1;
+
+ // Skip initial whitespace.
+ while (start_index < s.size()) {
+ if (!isspace(s[start_index])) {
+ break;
+ }
+ start_index++;
+ }
+
+ // Skip terminating whitespace.
+ while (end_index >= start_index) {
+ if (!isspace(s[end_index])) {
+ break;
+ }
+ end_index--;
+ }
+
+ // All spaces, no beef.
+ if (end_index < start_index) {
+ return "";
+ }
+ // Start_index is the first non-space, end_index is the last one.
+ return s.substr(start_index, end_index - start_index + 1);
+}
+
+template <typename StringT>
+std::string Join(const std::vector<StringT>& strings, char separator) {
+ if (strings.empty()) {
+ return "";
+ }
+
+ std::string result(strings[0]);
+ for (size_t i = 1; i < strings.size(); ++i) {
+ result += separator;
+ result += strings[i];
+ }
+ return result;
+}
+
+// Explicit instantiations.
+template std::string Join<std::string>(const std::vector<std::string>& strings,
+ char separator);
+template std::string Join<const char*>(const std::vector<const char*>& strings,
+ char separator);
+
+bool StartsWith(const std::string& s, const char* prefix) {
+ return s.compare(0, strlen(prefix), prefix) == 0;
+}
+
+bool EndsWith(const std::string& s, const char* suffix) {
+ size_t suffix_length = strlen(suffix);
+ size_t string_length = s.size();
+ if (suffix_length > string_length) {
+ return false;
+ }
+ size_t offset = string_length - suffix_length;
+ return s.compare(offset, suffix_length, suffix) == 0;
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/strings_test.cpp b/base/strings_test.cpp
new file mode 100644
index 0000000..824598d
--- /dev/null
+++ b/base/strings_test.cpp
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2015 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 "base/strings.h"
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <vector>
+
+TEST(strings, split_empty) {
+ std::vector<std::string> parts;
+ android::base::Split("", '\0', &parts);
+ ASSERT_EQ(0U, parts.size());
+}
+
+TEST(strings, split_single) {
+ std::vector<std::string> parts;
+ android::base::Split("foo", ',', &parts);
+ ASSERT_EQ(1U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+}
+
+TEST(strings, split_simple) {
+ std::vector<std::string> parts;
+ android::base::Split("foo,bar,baz", ',', &parts);
+ ASSERT_EQ(3U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+ ASSERT_EQ("baz", parts[2]);
+}
+
+TEST(strings, split_with_empty_part) {
+ std::vector<std::string> parts;
+ android::base::Split("foo,,bar", ',', &parts);
+ ASSERT_EQ(2U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+}
+
+TEST(strings, trim_empty) {
+ ASSERT_EQ("", android::base::Trim(""));
+}
+
+TEST(strings, trim_already_trimmed) {
+ ASSERT_EQ("foo", android::base::Trim("foo"));
+}
+
+TEST(strings, trim_left) {
+ ASSERT_EQ("foo", android::base::Trim(" foo"));
+}
+
+TEST(strings, trim_right) {
+ ASSERT_EQ("foo", android::base::Trim("foo "));
+}
+
+TEST(strings, trim_both) {
+ ASSERT_EQ("foo", android::base::Trim(" foo "));
+}
+
+TEST(strings, trim_no_trim_middle) {
+ ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
+}
+
+TEST(strings, trim_other_whitespace) {
+ ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
+}
+
+TEST(strings, join_nothing) {
+ std::vector<std::string> list = {};
+ ASSERT_EQ("", android::base::Join(list, ','));
+}
+
+TEST(strings, join_single) {
+ std::vector<std::string> list = {"foo"};
+ ASSERT_EQ("foo", android::base::Join(list, ','));
+}
+
+TEST(strings, join_simple) {
+ std::vector<std::string> list = {"foo", "bar", "baz"};
+ ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
+}
+
+TEST(strings, join_separator_in_vector) {
+ std::vector<std::string> list = {",", ","};
+ ASSERT_EQ(",,,", android::base::Join(list, ','));
+}
+
+TEST(strings, startswith_empty) {
+ ASSERT_FALSE(android::base::StartsWith("", "foo"));
+ ASSERT_TRUE(android::base::StartsWith("", ""));
+}
+
+TEST(strings, startswith_simple) {
+ ASSERT_TRUE(android::base::StartsWith("foo", ""));
+ ASSERT_TRUE(android::base::StartsWith("foo", "f"));
+ ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
+ ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
+}
+
+TEST(strings, startswith_prefix_too_long) {
+ ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
+}
+
+TEST(strings, startswith_contains_prefix) {
+ ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
+ ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
+}
+
+TEST(strings, endswith_empty) {
+ ASSERT_FALSE(android::base::EndsWith("", "foo"));
+ ASSERT_TRUE(android::base::EndsWith("", ""));
+}
+
+TEST(strings, endswith_simple) {
+ ASSERT_TRUE(android::base::EndsWith("foo", ""));
+ ASSERT_TRUE(android::base::EndsWith("foo", "o"));
+ ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
+ ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
+}
+
+TEST(strings, endswith_prefix_too_long) {
+ ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
+}
+
+TEST(strings, endswith_contains_prefix) {
+ ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
+ ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
+}
diff --git a/cpio/Android.mk b/cpio/Android.mk
index 5184463..575beb2 100644
--- a/cpio/Android.mk
+++ b/cpio/Android.mk
@@ -8,6 +8,8 @@
LOCAL_MODULE := mkbootfs
+LOCAL_CFLAGS := -Werror
+
include $(BUILD_HOST_EXECUTABLE)
$(call dist-for-goals,dist_files,$(LOCAL_BUILT_MODULE))
diff --git a/cpio/mkbootfs.c b/cpio/mkbootfs.c
index 7d3740c..7175749 100644
--- a/cpio/mkbootfs.c
+++ b/cpio/mkbootfs.c
@@ -78,8 +78,9 @@
s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
} else {
// Use the compiled-in fs_config() function.
-
- fs_config(path, S_ISDIR(s->st_mode), &s->st_uid, &s->st_gid, &s->st_mode, &capabilities);
+ unsigned st_mode = s->st_mode;
+ fs_config(path, S_ISDIR(s->st_mode), &s->st_uid, &s->st_gid, &st_mode, &capabilities);
+ s->st_mode = (typeof(s->st_mode)) st_mode;
}
}
diff --git a/debuggerd/Android.mk b/debuggerd/Android.mk
index e0a2d96..dd53296 100644
--- a/debuggerd/Android.mk
+++ b/debuggerd/Android.mk
@@ -1,71 +1,72 @@
-# Copyright 2005 The Android Open Source Project
-
LOCAL_PATH:= $(call my-dir)
+
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
- backtrace.cpp \
- debuggerd.cpp \
- getevent.cpp \
- tombstone.cpp \
- utility.cpp \
- $(TARGET_ARCH)/machine.cpp \
+ backtrace.cpp \
+ debuggerd.cpp \
+ elf_utils.cpp \
+ getevent.cpp \
+ tombstone.cpp \
+ utility.cpp \
-LOCAL_CONLYFLAGS := -std=gnu99
-LOCAL_CPPFLAGS := -std=gnu++11
-LOCAL_CFLAGS := \
- -Wall \
- -Wno-array-bounds \
- -Werror \
- -Wno-unused-parameter \
+LOCAL_SRC_FILES_arm := arm/machine.cpp
+LOCAL_SRC_FILES_arm64 := arm64/machine.cpp
+LOCAL_SRC_FILES_mips := mips/machine.cpp
+LOCAL_SRC_FILES_mips64 := mips64/machine.cpp
+LOCAL_SRC_FILES_x86 := x86/machine.cpp
+LOCAL_SRC_FILES_x86_64 := x86_64/machine.cpp
+
+LOCAL_CPPFLAGS := \
+ -std=gnu++11 \
+ -W -Wall -Wextra \
+ -Wunused \
+ -Werror \
ifeq ($(TARGET_IS_64_BIT),true)
- LOCAL_MODULE := debuggerd64
-else
- LOCAL_MODULE := debuggerd
+LOCAL_CPPFLAGS += -DTARGET_IS_64_BIT
endif
-ifeq ($(ARCH_ARM_HAVE_VFP),true)
-LOCAL_CFLAGS += -DWITH_VFP
-endif # ARCH_ARM_HAVE_VFP
-ifeq ($(ARCH_ARM_HAVE_VFP_D32),true)
-LOCAL_CFLAGS += -DWITH_VFP_D32
-endif # ARCH_ARM_HAVE_VFP_D32
-
LOCAL_SHARED_LIBRARIES := \
- libbacktrace \
- libc \
- libcutils \
- liblog \
- libselinux \
+ libbacktrace \
+ libbase \
+ libcutils \
+ liblog \
+ libselinux \
-include external/stlport/libstlport.mk
+LOCAL_CLANG := true
+
+LOCAL_MODULE := debuggerd
+LOCAL_MODULE_STEM_32 := debuggerd
+LOCAL_MODULE_STEM_64 := debuggerd64
+LOCAL_MULTILIB := both
include $(BUILD_EXECUTABLE)
+
+
include $(CLEAR_VARS)
LOCAL_SRC_FILES := crasher.c
-LOCAL_SRC_FILES += $(TARGET_ARCH)/crashglue.S
-LOCAL_MODULE := crasher
+LOCAL_SRC_FILES_arm := arm/crashglue.S
+LOCAL_SRC_FILES_arm64 := arm64/crashglue.S
+LOCAL_SRC_FILES_mips := mips/crashglue.S
+LOCAL_SRC_FILES_mips64 := mips64/crashglue.S
+LOCAL_SRC_FILES_x86 := x86/crashglue.S
+LOCAL_SRC_FILES_x86_64 := x86_64/crashglue.S
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS += -fstack-protector-all -Wno-unused-parameter -Wno-free-nonheap-object
+LOCAL_CFLAGS += -fstack-protector-all -Werror -Wno-free-nonheap-object
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_SHARED_LIBRARIES := libcutils liblog libc
-include $(BUILD_EXECUTABLE)
-ifeq ($(ARCH_ARM_HAVE_VFP),true)
-include $(CLEAR_VARS)
-
-LOCAL_CFLAGS += -DWITH_VFP
+# The arm emulator has VFP but not VFPv3-D32.
ifeq ($(ARCH_ARM_HAVE_VFP_D32),true)
-LOCAL_CFLAGS += -DWITH_VFP_D32
-endif # ARCH_ARM_HAVE_VFP_D32
+LOCAL_ASFLAGS_arm += -DHAS_VFP_D32
+endif
-LOCAL_SRC_FILES := vfp-crasher.c $(TARGET_ARCH)/vfp.S
-LOCAL_MODULE := vfp-crasher
-LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
-LOCAL_MODULE_TAGS := optional
-LOCAL_SHARED_LIBRARIES := libcutils liblog libc
+LOCAL_MODULE := crasher
+LOCAL_MODULE_STEM_32 := crasher
+LOCAL_MODULE_STEM_64 := crasher64
+LOCAL_MULTILIB := both
+
include $(BUILD_EXECUTABLE)
-endif # ARCH_ARM_HAVE_VFP == true
diff --git a/debuggerd/arm/crashglue.S b/debuggerd/arm/crashglue.S
index eb9d0e3..4fbfd6e 100644
--- a/debuggerd/arm/crashglue.S
+++ b/debuggerd/arm/crashglue.S
@@ -1,8 +1,5 @@
.globl crash1
.type crash1, %function
-.globl crashnostack
-.type crashnostack, %function
-
crash1:
ldr r0, =0xa5a50000
ldr r1, =0xa5a50001
@@ -18,11 +15,48 @@
ldr r11, =0xa5a50011
ldr r12, =0xa5a50012
+
+ fconstd d0, #0
+ fconstd d1, #1
+ fconstd d2, #2
+ fconstd d3, #3
+ fconstd d4, #4
+ fconstd d5, #5
+ fconstd d6, #6
+ fconstd d7, #7
+ fconstd d8, #8
+ fconstd d9, #9
+ fconstd d10, #10
+ fconstd d11, #11
+ fconstd d12, #12
+ fconstd d13, #13
+ fconstd d14, #14
+ fconstd d15, #15
+#if defined(HAS_VFP_D32)
+ fconstd d16, #16
+ fconstd d17, #17
+ fconstd d18, #18
+ fconstd d19, #19
+ fconstd d20, #20
+ fconstd d21, #21
+ fconstd d22, #22
+ fconstd d23, #23
+ fconstd d24, #24
+ fconstd d25, #25
+ fconstd d26, #26
+ fconstd d27, #27
+ fconstd d28, #28
+ fconstd d29, #29
+ fconstd d30, #30
+ fconstd d31, #31
+#endif
+
mov lr, #0
ldr lr, [lr]
b .
-
+.globl crashnostack
+.type crashnostack, %function
crashnostack:
mov sp, #0
mov r0, #0
diff --git a/debuggerd/arm/machine.cpp b/debuggerd/arm/machine.cpp
index fd2f69b..50e78c5 100644
--- a/debuggerd/arm/machine.cpp
+++ b/debuggerd/arm/machine.cpp
@@ -27,87 +27,68 @@
#include "../utility.h"
#include "../machine.h"
-// enable to dump memory pointed to by every register
-#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
-
-#ifdef WITH_VFP
-#ifdef WITH_VFP_D32
-#define NUM_VFP_REGS 32
-#else
-#define NUM_VFP_REGS 16
-#endif
-#endif
-
-// If configured to do so, dump memory around *all* registers
-// for the crashing thread.
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
- struct pt_regs regs;
+void dump_memory_and_code(log_t* log, pid_t tid) {
+ pt_regs regs;
if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) {
return;
}
- if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
- static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
+ static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
- for (int reg = 0; reg < 14; reg++) {
- // this may not be a valid way to access, but it'll do for now
- uintptr_t addr = regs.uregs[reg];
+ for (int reg = 0; reg < 14; reg++) {
+ // this may not be a valid way to access, but it'll do for now
+ uintptr_t addr = regs.uregs[reg];
- // Don't bother if it looks like a small int or ~= null, or if
- // it's in the kernel area.
- if (addr < 4096 || addr >= 0xc0000000) {
- continue;
- }
-
- _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]);
- dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
+ // Don't bother if it looks like a small int or ~= null, or if
+ // it's in the kernel area.
+ if (addr < 4096 || addr >= 0xc0000000) {
+ continue;
}
+
+ _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]);
+ dump_memory(log, tid, addr);
}
// explicitly allow upload of code dump logging
- _LOG(log, scope_flags, "\ncode around pc:\n");
- dump_memory(log, tid, static_cast<uintptr_t>(regs.ARM_pc), scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
+ dump_memory(log, tid, static_cast<uintptr_t>(regs.ARM_pc));
if (regs.ARM_pc != regs.ARM_lr) {
- _LOG(log, scope_flags, "\ncode around lr:\n");
- dump_memory(log, tid, static_cast<uintptr_t>(regs.ARM_lr), scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around lr:\n");
+ dump_memory(log, tid, static_cast<uintptr_t>(regs.ARM_lr));
}
}
-void dump_registers(log_t* log, pid_t tid, int scope_flags) {
- struct pt_regs r;
+void dump_registers(log_t* log, pid_t tid) {
+ pt_regs r;
if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
- _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
+ _LOG(log, logtype::REGISTERS, "cannot get registers: %s\n", strerror(errno));
return;
}
- _LOG(log, scope_flags, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
+ _LOG(log, logtype::REGISTERS, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
static_cast<uint32_t>(r.ARM_r0), static_cast<uint32_t>(r.ARM_r1),
static_cast<uint32_t>(r.ARM_r2), static_cast<uint32_t>(r.ARM_r3));
- _LOG(log, scope_flags, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
+ _LOG(log, logtype::REGISTERS, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
static_cast<uint32_t>(r.ARM_r4), static_cast<uint32_t>(r.ARM_r5),
static_cast<uint32_t>(r.ARM_r6), static_cast<uint32_t>(r.ARM_r7));
- _LOG(log, scope_flags, " r8 %08x r9 %08x sl %08x fp %08x\n",
+ _LOG(log, logtype::REGISTERS, " r8 %08x r9 %08x sl %08x fp %08x\n",
static_cast<uint32_t>(r.ARM_r8), static_cast<uint32_t>(r.ARM_r9),
static_cast<uint32_t>(r.ARM_r10), static_cast<uint32_t>(r.ARM_fp));
- _LOG(log, scope_flags, " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
+ _LOG(log, logtype::REGISTERS, " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
static_cast<uint32_t>(r.ARM_ip), static_cast<uint32_t>(r.ARM_sp),
static_cast<uint32_t>(r.ARM_lr), static_cast<uint32_t>(r.ARM_pc),
static_cast<uint32_t>(r.ARM_cpsr));
-#ifdef WITH_VFP
- struct user_vfp vfp_regs;
- int i;
-
+ user_vfp vfp_regs;
if (ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) {
- _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
+ _LOG(log, logtype::FP_REGISTERS, "cannot get FP registers: %s\n", strerror(errno));
return;
}
- for (i = 0; i < NUM_VFP_REGS; i += 2) {
- _LOG(log, scope_flags, " d%-2d %016llx d%-2d %016llx\n",
+ for (size_t i = 0; i < 32; i += 2) {
+ _LOG(log, logtype::FP_REGISTERS, " d%-2d %016llx d%-2d %016llx\n",
i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
}
- _LOG(log, scope_flags, " scr %08lx\n", vfp_regs.fpscr);
-#endif
+ _LOG(log, logtype::FP_REGISTERS, " scr %08lx\n", vfp_regs.fpscr);
}
diff --git a/debuggerd/arm/vfp.S b/debuggerd/arm/vfp.S
deleted file mode 100644
index 9744f6f..0000000
--- a/debuggerd/arm/vfp.S
+++ /dev/null
@@ -1,43 +0,0 @@
- .text
- .align 2
- .global crash
- .type crash, %function
-crash:
- fconstd d0, #0
- fconstd d1, #1
- fconstd d2, #2
- fconstd d3, #3
- fconstd d4, #4
- fconstd d5, #5
- fconstd d6, #6
- fconstd d7, #7
- fconstd d8, #8
- fconstd d9, #9
- fconstd d10, #10
- fconstd d11, #11
- fconstd d12, #12
- fconstd d13, #13
- fconstd d14, #14
- fconstd d15, #15
-#ifdef WITH_VFP_D32
- fconstd d16, #16
- fconstd d17, #17
- fconstd d18, #18
- fconstd d19, #19
- fconstd d20, #20
- fconstd d21, #21
- fconstd d22, #22
- fconstd d23, #23
- fconstd d24, #24
- fconstd d25, #25
- fconstd d26, #26
- fconstd d27, #27
- fconstd d28, #28
- fconstd d29, #29
- fconstd d30, #30
- fconstd d31, #31
-#endif
- mov r0, #0
- str r0, [r0]
- bx lr
-
diff --git a/debuggerd/arm64/crashglue.S b/debuggerd/arm64/crashglue.S
index b06b67c..e58b542 100644
--- a/debuggerd/arm64/crashglue.S
+++ b/debuggerd/arm64/crashglue.S
@@ -1,8 +1,5 @@
.globl crash1
.type crash1, %function
-.globl crashnostack
-.type crashnostack, %function
-
crash1:
ldr x0, =0xa5a50000
ldr x1, =0xa5a50001
@@ -35,11 +32,46 @@
ldr x28, =0xa5a50028
ldr x29, =0xa5a50029
+ fmov d0, -1.0 // -1 is more convincing than 0.
+ fmov d1, 1.0
+ fmov d2, 2.0
+ fmov d3, 3.0
+ fmov d4, 4.0
+ fmov d5, 5.0
+ fmov d6, 6.0
+ fmov d7, 7.0
+ fmov d8, 8.0
+ fmov d9, 9.0
+ fmov d10, 10.0
+ fmov d11, 11.0
+ fmov d12, 12.0
+ fmov d13, 13.0
+ fmov d14, 14.0
+ fmov d15, 15.0
+ fmov d16, 16.0
+ fmov d17, 17.0
+ fmov d18, 18.0
+ fmov d19, 19.0
+ fmov d20, 20.0
+ fmov d21, 21.0
+ fmov d22, 22.0
+ fmov d23, 23.0
+ fmov d24, 24.0
+ fmov d25, 25.0
+ fmov d26, 26.0
+ fmov d27, 27.0
+ fmov d28, 28.0
+ fmov d29, 29.0
+ fmov d30, 30.0
+ fmov d31, 31.0
+
mov x30, xzr
ldr x30, [x30]
b .
+.globl crashnostack
+.type crashnostack, %function
crashnostack:
mov x0, xzr
add sp, x0, xzr
diff --git a/debuggerd/arm64/machine.cpp b/debuggerd/arm64/machine.cpp
index 7159228..8b17d53 100644
--- a/debuggerd/arm64/machine.cpp
+++ b/debuggerd/arm64/machine.cpp
@@ -15,109 +15,98 @@
* limitations under the License.
*/
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <stdio.h>
+#include <elf.h>
#include <errno.h>
+#include <inttypes.h>
+#include <string.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/uio.h>
-#include <linux/elf.h>
#include "../utility.h"
#include "../machine.h"
-/* enable to dump memory pointed to by every register */
-#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
-
-/*
- * If configured to do so, dump memory around *all* registers
- * for the crashing thread.
- */
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
+void dump_memory_and_code(log_t* log, pid_t tid) {
struct user_pt_regs regs;
struct iovec io;
io.iov_base = ®s;
io.iov_len = sizeof(regs);
if (ptrace(PTRACE_GETREGSET, tid, (void*)NT_PRSTATUS, &io) == -1) {
- _LOG(log, scope_flags, "%s: ptrace failed to get registers: %s\n",
+ _LOG(log, logtype::ERROR, "%s: ptrace failed to get registers: %s\n",
__func__, strerror(errno));
return;
}
- if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
- for (int reg = 0; reg < 31; reg++) {
- uintptr_t addr = regs.regs[reg];
+ for (int reg = 0; reg < 31; reg++) {
+ uintptr_t addr = regs.regs[reg];
- /*
- * Don't bother if it looks like a small int or ~= null, or if
- * it's in the kernel area.
- */
- if (addr < 4096 || addr >= (1UL<<63)) {
- continue;
- }
-
- _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near x%d:\n", reg);
- dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
+ /*
+ * Don't bother if it looks like a small int or ~= null, or if
+ * it's in the kernel area.
+ */
+ if (addr < 4096 || addr >= (1UL<<63)) {
+ continue;
}
+
+ _LOG(log, logtype::MEMORY, "\nmemory near x%d:\n", reg);
+ dump_memory(log, tid, addr);
}
- _LOG(log, scope_flags, "\ncode around pc:\n");
- dump_memory(log, tid, (uintptr_t)regs.pc, scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
+ dump_memory(log, tid, (uintptr_t)regs.pc);
if (regs.pc != regs.sp) {
- _LOG(log, scope_flags, "\ncode around sp:\n");
- dump_memory(log, tid, (uintptr_t)regs.sp, scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around sp:\n");
+ dump_memory(log, tid, (uintptr_t)regs.sp);
}
}
-void dump_registers(log_t* log, pid_t tid, int scope_flags)
-{
+void dump_registers(log_t* log, pid_t tid) {
struct user_pt_regs r;
struct iovec io;
io.iov_base = &r;
io.iov_len = sizeof(r);
- bool only_in_tombstone = !IS_AT_FAULT(scope_flags);
-
if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRSTATUS, (void*) &io) == -1) {
- _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
+ _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno));
return;
}
for (int i = 0; i < 28; i += 4) {
- _LOG(log, scope_flags, " x%-2d %016lx x%-2d %016lx x%-2d %016lx x%-2d %016lx\n",
- i, (uint64_t)r.regs[i],
- i+1, (uint64_t)r.regs[i+1],
- i+2, (uint64_t)r.regs[i+2],
- i+3, (uint64_t)r.regs[i+3]);
+ _LOG(log, logtype::REGISTERS,
+ " x%-2d %016llx x%-2d %016llx x%-2d %016llx x%-2d %016llx\n",
+ i, r.regs[i],
+ i+1, r.regs[i+1],
+ i+2, r.regs[i+2],
+ i+3, r.regs[i+3]);
}
- _LOG(log, scope_flags, " x28 %016lx x29 %016lx x30 %016lx\n",
- (uint64_t)r.regs[28], (uint64_t)r.regs[29], (uint64_t)r.regs[30]);
+ _LOG(log, logtype::REGISTERS, " x28 %016llx x29 %016llx x30 %016llx\n",
+ r.regs[28], r.regs[29], r.regs[30]);
- _LOG(log, scope_flags, " sp %016lx pc %016lx\n",
- (uint64_t)r.sp, (uint64_t)r.pc);
-
+ _LOG(log, logtype::REGISTERS, " sp %016llx pc %016llx pstate %016llx\n",
+ r.sp, r.pc, r.pstate);
struct user_fpsimd_state f;
io.iov_base = &f;
io.iov_len = sizeof(f);
if (ptrace(PTRACE_GETREGSET, tid, (void*) NT_PRFPREG, (void*) &io) == -1) {
- _LOG(log, scope_flags, "ptrace error: %s\n", strerror(errno));
+ _LOG(log, logtype::ERROR, "ptrace error: %s\n", strerror(errno));
return;
}
- for (int i = 0; i < 32; i += 4) {
- _LOG(log, scope_flags, " v%-2d %016lx v%-2d %016lx v%-2d %016lx v%-2d %016lx\n",
- i, (uint64_t)f.vregs[i],
- i+1, (uint64_t)f.vregs[i+1],
- i+2, (uint64_t)f.vregs[i+2],
- i+3, (uint64_t)f.vregs[i+3]);
+ for (int i = 0; i < 32; i += 2) {
+ _LOG(log, logtype::FP_REGISTERS,
+ " v%-2d %016" PRIx64 "%016" PRIx64 " v%-2d %016" PRIx64 "%016" PRIx64 "\n",
+ i,
+ static_cast<uint64_t>(f.vregs[i] >> 64),
+ static_cast<uint64_t>(f.vregs[i]),
+ i+1,
+ static_cast<uint64_t>(f.vregs[i+1] >> 64),
+ static_cast<uint64_t>(f.vregs[i+1]));
}
+ _LOG(log, logtype::FP_REGISTERS, " fpsr %08x fpcr %08x\n", f.fpsr, f.fpcr);
}
diff --git a/debuggerd/arm64/vfp.S b/debuggerd/arm64/vfp.S
deleted file mode 100644
index bf12c22..0000000
--- a/debuggerd/arm64/vfp.S
+++ /dev/null
@@ -1,42 +0,0 @@
- .text
- .align 2
- .global crash
- .type crash, %function
-crash:
- fmov d0, XZR
- fmov d1, 1.0
- fmov d2, 2.0
- fmov d3, 3.0
- fmov d4, 4.0
- fmov d5, 5.0
- fmov d6, 6.0
- fmov d7, 7.0
- fmov d8, 8.0
- fmov d9, 9.0
- fmov d10, 10.0
- fmov d11, 11.0
- fmov d12, 12.0
- fmov d13, 13.0
- fmov d14, 14.0
- fmov d15, 15.0
- fmov d16, 16.0
- fmov d17, 17.0
- fmov d18, 18.0
- fmov d19, 19.0
- fmov d20, 20.0
- fmov d21, 21.0
- fmov d22, 22.0
- fmov d23, 23.0
- fmov d24, 24.0
- fmov d25, 25.0
- fmov d26, 26.0
- fmov d27, 27.0
- fmov d28, 28.0
- fmov d29, 29.0
- fmov d30, 30.0
- fmov d31, 31.0
-
- mov x0, xzr
- str x0, [x0]
- br x30
-
diff --git a/debuggerd/backtrace.cpp b/debuggerd/backtrace.cpp
index d388348..c2a1dbc 100644
--- a/debuggerd/backtrace.cpp
+++ b/debuggerd/backtrace.cpp
@@ -30,6 +30,7 @@
#include <UniquePtr.h>
#include "backtrace.h"
+
#include "utility.h"
static void dump_process_header(log_t* log, pid_t pid) {
@@ -49,15 +50,16 @@
localtime_r(&t, &tm);
char timestr[64];
strftime(timestr, sizeof(timestr), "%F %T", &tm);
- _LOG(log, SCOPE_AT_FAULT, "\n\n----- pid %d at %s -----\n", pid, timestr);
+ _LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, timestr);
if (procname) {
- _LOG(log, SCOPE_AT_FAULT, "Cmd line: %s\n", procname);
+ _LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", procname);
}
+ _LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
}
static void dump_process_footer(log_t* log, pid_t pid) {
- _LOG(log, SCOPE_AT_FAULT, "\n----- end %d -----\n", pid);
+ _LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
}
static void dump_thread(
@@ -79,22 +81,24 @@
}
}
- _LOG(log, SCOPE_AT_FAULT, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
+ _LOG(log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", threadname ? threadname : "<unknown>", tid);
if (!attached && ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
- _LOG(log, SCOPE_AT_FAULT, "Could not attach to thread: %s\n", strerror(errno));
+ _LOG(log, logtype::BACKTRACE, "Could not attach to thread: %s\n", strerror(errno));
return;
}
- wait_for_stop(tid, total_sleep_time_usec);
+ if (!attached && wait_for_sigstop(tid, total_sleep_time_usec, detach_failed) == -1) {
+ return;
+ }
UniquePtr<Backtrace> backtrace(Backtrace::Create(tid, BACKTRACE_CURRENT_THREAD));
if (backtrace->Unwind(0)) {
- dump_backtrace_to_log(backtrace.get(), log, SCOPE_AT_FAULT, " ");
+ dump_backtrace_to_log(backtrace.get(), log, " ");
}
if (!attached && ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
- LOG("ptrace detach from %d failed: %s\n", tid, strerror(errno));
+ _LOG(log, logtype::ERROR, "ptrace detach from %d failed: %s\n", tid, strerror(errno));
*detach_failed = true;
}
}
@@ -104,7 +108,6 @@
log_t log;
log.tfd = fd;
log.amfd = amfd;
- log.quiet = true;
dump_process_header(&log, pid);
dump_thread(&log, tid, true, detach_failed, total_sleep_time_usec);
@@ -133,9 +136,8 @@
dump_process_footer(&log, pid);
}
-void dump_backtrace_to_log(Backtrace* backtrace, log_t* log,
- int scope_flags, const char* prefix) {
+void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix) {
for (size_t i = 0; i < backtrace->NumFrames(); i++) {
- _LOG(log, scope_flags, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
+ _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, backtrace->FormatFrameData(i).c_str());
}
}
diff --git a/debuggerd/backtrace.h b/debuggerd/backtrace.h
index 2ec8afb..da14cd4 100644
--- a/debuggerd/backtrace.h
+++ b/debuggerd/backtrace.h
@@ -29,7 +29,6 @@
int* total_sleep_time_usec);
/* Dumps the backtrace in the backtrace data structure to the log. */
-void dump_backtrace_to_log(Backtrace* backtrace, log_t* log,
- int scope_flags, const char* prefix);
+void dump_backtrace_to_log(Backtrace* backtrace, log_t* log, const char* prefix);
#endif // _DEBUGGERD_BACKTRACE_H
diff --git a/debuggerd/crasher.c b/debuggerd/crasher.c
index 5a2bc3c..af86fe9 100644
--- a/debuggerd/crasher.c
+++ b/debuggerd/crasher.c
@@ -1,21 +1,24 @@
-
-//#include <cutils/misc.h>
-
-#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sched.h>
-#include <errno.h>
-
-#include <signal.h>
+#include <sys/cdefs.h>
+#include <sys/mman.h>
#include <sys/ptrace.h>
-#include <sys/wait.h>
#include <sys/socket.h>
-
-#include <pthread.h>
+#include <sys/wait.h>
+#include <unistd.h>
#include <cutils/sockets.h>
+#include <log/log.h>
+
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
extern const char* __progname;
@@ -23,22 +26,29 @@
void crashnostack(void);
static int do_action(const char* arg);
-static void maybeabort() {
- if(time(0) != 42) {
+static void maybe_abort() {
+ if (time(0) != 42) {
abort();
}
}
-static int smash_stack(int i) {
+static char* smash_stack_dummy_buf;
+__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
+ smash_stack_dummy_buf[*plen] = 0;
+}
+
+// This must be marked with "__attribute__ ((noinline))", to ensure the
+// compiler generates the proper stack guards around this function.
+// Assign local array address to global variable to force stack guards.
+// Use another noinline function to corrupt the stack.
+__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
printf("crasher: deliberately corrupting stack...\n");
- // Unless there's a "big enough" buffer on the stack, gcc
- // doesn't bother inserting checks.
- char buf[8];
- // If we don't write something relatively unpredictable
- // into the buffer and then do something with it, gcc
- // optimizes everything away and just returns a constant.
- *(int*)(&buf[7]) = (uintptr_t) &buf[0];
- return *(int*)(&buf[0]);
+
+ char buf[128];
+ smash_stack_dummy_buf = buf;
+ // This should corrupt stack guards and make process abort.
+ smash_stack_dummy_function(plen);
+ return 0;
}
static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
@@ -50,18 +60,13 @@
overflow_stack(&buf);
}
-static void test_call1()
-{
- *((int*) 32) = 1;
-}
-
static void *noisy(void *x)
{
char c = (uintptr_t) x;
for(;;) {
usleep(250*1000);
write(2, &c, 1);
- if(c == 'C') *((unsigned*) 0) = 42;
+ if(c == 'C') *((volatile unsigned*) 0) = 42;
}
return NULL;
}
@@ -113,41 +118,82 @@
free((void*) buf); // GCC is smart enough to warn about this, but we're doing it deliberately.
}
+static void sigsegv_non_null() {
+ int* a = (int *)(&do_action);
+ *a = 42;
+}
+
static int do_action(const char* arg)
{
fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
if (!strncmp(arg, "thread-", strlen("thread-"))) {
return do_action_on_thread(arg + strlen("thread-"));
- } else if (!strcmp(arg,"smash-stack")) {
- return smash_stack(42);
- } else if (!strcmp(arg,"stack-overflow")) {
+ } else if (!strcmp(arg, "SIGSEGV-non-null")) {
+ sigsegv_non_null();
+ } else if (!strcmp(arg, "smash-stack")) {
+ volatile int len = 128;
+ return smash_stack(&len);
+ } else if (!strcmp(arg, "stack-overflow")) {
overflow_stack(NULL);
- } else if (!strcmp(arg,"nostack")) {
+ } else if (!strcmp(arg, "nostack")) {
crashnostack();
- } else if (!strcmp(arg,"ctest")) {
+ } else if (!strcmp(arg, "ctest")) {
return ctest();
- } else if (!strcmp(arg,"exit")) {
+ } else if (!strcmp(arg, "exit")) {
exit(1);
- } else if (!strcmp(arg,"crash")) {
+ } else if (!strcmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
return crash(42);
- } else if (!strcmp(arg,"abort")) {
- maybeabort();
+ } else if (!strcmp(arg, "abort")) {
+ maybe_abort();
+ } else if (!strcmp(arg, "assert")) {
+ __assert("some_file.c", 123, "false");
+ } else if (!strcmp(arg, "assert2")) {
+ __assert2("some_file.c", 123, "some_function", "false");
+ } else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
+ LOG_ALWAYS_FATAL("hello %s", "world");
+ } else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
+ LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
+ } else if (!strcmp(arg, "SIGFPE")) {
+ raise(SIGFPE);
+ return EXIT_SUCCESS;
+ } else if (!strcmp(arg, "SIGPIPE")) {
+ int pipe_fds[2];
+ pipe(pipe_fds);
+ close(pipe_fds[0]);
+ write(pipe_fds[1], "oops", 4);
+ return EXIT_SUCCESS;
+ } else if (!strcmp(arg, "SIGTRAP")) {
+ raise(SIGTRAP);
+ return EXIT_SUCCESS;
} else if (!strcmp(arg, "heap-usage")) {
abuse_heap();
+ } else if (!strcmp(arg, "SIGSEGV-unmapped")) {
+ char* map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ munmap(map, sizeof(int));
+ map[0] = '8';
}
fprintf(stderr, "%s OP\n", __progname);
fprintf(stderr, "where OP is:\n");
- fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
- fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
- fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
- fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
- fprintf(stderr, " nostack crash with a NULL stack pointer\n");
- fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
- fprintf(stderr, " exit call exit(1)\n");
- fprintf(stderr, " crash cause a SIGSEGV\n");
- fprintf(stderr, " abort call abort()\n");
+ fprintf(stderr, " smash-stack overwrite a stack-guard canary\n");
+ fprintf(stderr, " stack-overflow recurse until the stack overflows\n");
+ fprintf(stderr, " heap-corruption cause a libc abort by corrupting the heap\n");
+ fprintf(stderr, " heap-usage cause a libc abort by abusing a heap function\n");
+ fprintf(stderr, " nostack crash with a NULL stack pointer\n");
+ fprintf(stderr, " ctest (obsoleted by thread-crash?)\n");
+ fprintf(stderr, " exit call exit(1)\n");
+ fprintf(stderr, " abort call abort()\n");
+ fprintf(stderr, " assert call assert() without a function\n");
+ fprintf(stderr, " assert2 call assert() with a function\n");
+ fprintf(stderr, " LOG_ALWAYS_FATAL call LOG_ALWAYS_FATAL\n");
+ fprintf(stderr, " LOG_ALWAYS_FATAL_IF call LOG_ALWAYS_FATAL\n");
+ fprintf(stderr, " SIGFPE cause a SIGFPE\n");
+ fprintf(stderr, " SIGPIPE cause a SIGPIPE\n");
+ fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
+ fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
+ fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
+ fprintf(stderr, " SIGTRAP cause a SIGTRAP\n");
fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
fprintf(stderr, "on the process' main thread.\n");
return EXIT_SUCCESS;
diff --git a/debuggerd/debuggerd.cpp b/debuggerd/debuggerd.cpp
index a2b164e..039b8ec 100644
--- a/debuggerd/debuggerd.cpp
+++ b/debuggerd/debuggerd.cpp
@@ -30,7 +30,8 @@
#include <sys/stat.h>
#include <sys/poll.h>
-#include <log/logd.h>
+#include <selinux/android.h>
+
#include <log/logger.h>
#include <cutils/sockets.h>
@@ -46,105 +47,63 @@
#include "tombstone.h"
#include "utility.h"
+// If the 32 bit executable is compiled on a 64 bit system,
+// use the 32 bit socket name.
+#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
+#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
+#else
+#define SOCKET_NAME DEBUGGER_SOCKET_NAME
+#endif
+
struct debugger_request_t {
debugger_action_t action;
pid_t pid, tid;
uid_t uid, gid;
uintptr_t abort_msg_address;
+ int32_t original_si_code;
};
-static int write_string(const char* file, const char* string) {
- int len;
- int fd;
- ssize_t amt;
- fd = open(file, O_RDWR);
- len = strlen(string);
- if (fd < 0)
- return -errno;
- amt = write(fd, string, len);
- close(fd);
- return amt >= 0 ? 0 : -errno;
-}
+static void wait_for_user_action(const debugger_request_t &request) {
+ // Find out the name of the process that crashed.
+ char path[64];
+ snprintf(path, sizeof(path), "/proc/%d/exe", request.pid);
-static void init_debug_led() {
- // trout leds
- write_string("/sys/class/leds/red/brightness", "0");
- write_string("/sys/class/leds/green/brightness", "0");
- write_string("/sys/class/leds/blue/brightness", "0");
- write_string("/sys/class/leds/red/device/blink", "0");
- // sardine leds
- write_string("/sys/class/leds/left/cadence", "0,0");
-}
+ char exe[PATH_MAX];
+ int count;
+ if ((count = readlink(path, exe, sizeof(exe) - 1)) == -1) {
+ ALOGE("readlink('%s') failed: %s", path, strerror(errno));
+ strlcpy(exe, "unknown", sizeof(exe));
+ } else {
+ exe[count] = '\0';
+ }
-static void enable_debug_led() {
- // trout leds
- write_string("/sys/class/leds/red/brightness", "255");
- // sardine leds
- write_string("/sys/class/leds/left/cadence", "1,0");
-}
+ // Explain how to attach the debugger.
+ ALOGI("********************************************************\n"
+ "* Process %d has been suspended while crashing.\n"
+ "* To attach gdbserver for a gdb connection on port 5039\n"
+ "* and start gdbclient:\n"
+ "*\n"
+ "* gdbclient %s :5039 %d\n"
+ "*\n"
+ "* Wait for gdb to start, then press the VOLUME DOWN key\n"
+ "* to let the process continue crashing.\n"
+ "********************************************************",
+ request.pid, exe, request.tid);
-static void disable_debug_led() {
- // trout leds
- write_string("/sys/class/leds/red/brightness", "0");
- // sardine leds
- write_string("/sys/class/leds/left/cadence", "0,0");
-}
-
-static void wait_for_user_action(pid_t pid) {
- // First log a helpful message
- LOG( "********************************************************\n"
- "* Process %d has been suspended while crashing. To\n"
- "* attach gdbserver for a gdb connection on port 5039\n"
- "* and start gdbclient:\n"
- "*\n"
- "* gdbclient app_process :5039 %d\n"
- "*\n"
- "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
- "* to let the process continue crashing.\n"
- "********************************************************\n",
- pid, pid);
-
- // wait for HOME or VOLUME DOWN key
+ // Wait for VOLUME DOWN.
if (init_getevent() == 0) {
- int ms = 1200 / 10;
- int dit = 1;
- int dah = 3*dit;
- int _ = -dit;
- int ___ = 3*_;
- int _______ = 7*_;
- const int codes[] = {
- dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
- };
- size_t s = 0;
- input_event e;
- bool done = false;
- init_debug_led();
- enable_debug_led();
- do {
- int timeout = abs(codes[s]) * ms;
- int res = get_event(&e, timeout);
- if (res == 0) {
- if (e.type == EV_KEY
- && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
- && e.value == 0) {
- done = true;
- }
- } else if (res == 1) {
- if (++s >= sizeof(codes)/sizeof(*codes))
- s = 0;
- if (codes[s] > 0) {
- enable_debug_led();
- } else {
- disable_debug_led();
+ while (true) {
+ input_event e;
+ if (get_event(&e, -1) == 0) {
+ if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
+ break;
}
}
- } while (!done);
+ }
uninit_getevent();
}
- // don't forget to turn debug led off
- disable_debug_led();
- LOG("debuggerd resuming process %d", pid);
+ ALOGI("debuggerd resuming process %d", request.pid);
}
static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
@@ -175,16 +134,63 @@
return fields == 7 ? 0 : -1;
}
+static int selinux_enabled;
+
+/*
+ * Corresponds with debugger_action_t enum type in
+ * include/cutils/debugger.h.
+ */
+static const char *debuggerd_perms[] = {
+ NULL, /* crash is only used on self, no check applied */
+ "dump_tombstone",
+ "dump_backtrace"
+};
+
+static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action)
+{
+ char *scon = NULL, *tcon = NULL;
+ const char *tclass = "debuggerd";
+ const char *perm;
+ bool allowed = false;
+
+ if (selinux_enabled <= 0)
+ return true;
+
+ if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
+ ALOGE("SELinux: No permission defined for debugger action %d", action);
+ return false;
+ }
+
+ perm = debuggerd_perms[action];
+
+ if (getpeercon(s, &scon) < 0) {
+ ALOGE("Cannot get peer context from socket\n");
+ goto out;
+ }
+
+ if (getpidcon(tid, &tcon) < 0) {
+ ALOGE("Cannot get context for tid %d\n", tid);
+ goto out;
+ }
+
+ allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0);
+
+out:
+ freecon(scon);
+ freecon(tcon);
+ return allowed;
+}
+
static int read_request(int fd, debugger_request_t* out_request) {
ucred cr;
socklen_t len = sizeof(cr);
int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
if (status != 0) {
- LOG("cannot get credentials\n");
+ ALOGE("cannot get credentials");
return -1;
}
- XLOG("reading tid\n");
+ ALOGV("reading tid");
fcntl(fd, F_SETFL, O_NONBLOCK);
pollfd pollfds[1];
@@ -193,7 +199,7 @@
pollfds[0].revents = 0;
status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
if (status != 1) {
- LOG("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
+ ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
return -1;
}
@@ -201,23 +207,21 @@
memset(&msg, 0, sizeof(msg));
status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
if (status < 0) {
- LOG("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
+ ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
return -1;
}
- if (status == sizeof(debugger_msg_t)) {
- XLOG("crash request of size %d abort_msg_address=0x%" PRIPTR "\n",
- status, msg.abort_msg_address);
- } else {
- LOG("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
+ if (status != sizeof(debugger_msg_t)) {
+ ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
return -1;
}
- out_request->action = msg.action;
+ out_request->action = static_cast<debugger_action_t>(msg.action);
out_request->tid = msg.tid;
out_request->pid = cr.pid;
out_request->uid = cr.uid;
out_request->gid = cr.gid;
out_request->abort_msg_address = msg.abort_msg_address;
+ out_request->original_si_code = msg.original_si_code;
if (msg.action == DEBUGGER_ACTION_CRASH) {
// Ensure that the tid reported by the crashing process is valid.
@@ -225,7 +229,7 @@
struct stat s;
snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
if (stat(buf, &s)) {
- LOG("tid %d does not exist in pid %d. ignoring debug request\n",
+ ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
out_request->tid, out_request->pid);
return -1;
}
@@ -236,9 +240,12 @@
status = get_process_info(out_request->tid, &out_request->pid,
&out_request->uid, &out_request->gid);
if (status < 0) {
- LOG("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
+ ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
return -1;
}
+
+ if (!selinux_action_allowed(fd, out_request->tid, out_request->action))
+ return -1;
} else {
// No one else is allowed to dump arbitrary processes.
return -1;
@@ -256,16 +263,113 @@
return false;
}
+#if defined(__LP64__)
+static bool is32bit(pid_t tid) {
+ char* exeline;
+ if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
+ return false;
+ }
+ int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
+ int saved_errno = errno;
+ free(exeline);
+ if (fd == -1) {
+ ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
+ return false;
+ }
+
+ char ehdr[EI_NIDENT];
+ ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
+ TEMP_FAILURE_RETRY(close(fd));
+ if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
+ return false;
+ }
+ if (ehdr[EI_CLASS] == ELFCLASS32) {
+ return true;
+ }
+ return false;
+}
+
+static void redirect_to_32(int fd, debugger_request_t* request) {
+ debugger_msg_t msg;
+ memset(&msg, 0, sizeof(msg));
+ msg.tid = request->tid;
+ msg.action = request->action;
+
+ int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
+ SOCK_STREAM | SOCK_CLOEXEC);
+ if (sock_fd < 0) {
+ ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
+ return;
+ }
+
+ if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
+ ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
+ TEMP_FAILURE_RETRY(close(sock_fd));
+ return;
+ }
+
+ char ack;
+ if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
+ ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
+ TEMP_FAILURE_RETRY(close(sock_fd));
+ return;
+ }
+
+ char buffer[1024];
+ ssize_t bytes_read;
+ while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
+ ssize_t bytes_to_send = bytes_read;
+ ssize_t bytes_written;
+ do {
+ bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
+ bytes_to_send));
+ if (bytes_written == -1) {
+ if (errno == EAGAIN) {
+ // Retry the write.
+ continue;
+ }
+ ALOGE("Error while writing data to fd: %s", strerror(errno));
+ break;
+ }
+ bytes_to_send -= bytes_written;
+ } while (bytes_written != 0 && bytes_to_send > 0);
+ if (bytes_to_send != 0) {
+ ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
+ break;
+ }
+ }
+ TEMP_FAILURE_RETRY(close(sock_fd));
+}
+#endif
+
static void handle_request(int fd) {
- XLOG("handle_request(%d)\n", fd);
+ ALOGV("handle_request(%d)\n", fd);
debugger_request_t request;
memset(&request, 0, sizeof(request));
int status = read_request(fd, &request);
if (!status) {
- XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
+ ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
request.pid, request.uid, request.gid, request.tid);
+#if defined(__LP64__)
+ // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
+ // to the 64 bit debuggerd. If the process is a 32 bit executable,
+ // redirect the request to the 32 bit debuggerd.
+ if (is32bit(request.tid)) {
+ // Only dump backtrace and dump tombstone requests can be redirected.
+ if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE
+ || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
+ redirect_to_32(fd, &request);
+ } else {
+ ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n",
+ request.action);
+ }
+ TEMP_FAILURE_RETRY(close(fd));
+ return;
+ }
+#endif
+
// At this point, the thread that made the request is blocked in
// a read() call. If the thread has crashed, then this gives us
// time to PTRACE_ATTACH to it before it has a chance to really fault.
@@ -277,12 +381,13 @@
// See details in bionic/libc/linker/debugger.c, in function
// debugger_signal_handler().
if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
- LOG("ptrace attach failed: %s\n", strerror(errno));
+ ALOGE("ptrace attach failed: %s\n", strerror(errno));
} else {
bool detach_failed = false;
+ bool tid_unresponsive = false;
bool attach_gdb = should_attach_gdb(&request);
if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
- LOG("failed responding to client: %s\n", strerror(errno));
+ ALOGE("failed responding to client: %s\n", strerror(errno));
} else {
char* tombstone_path = NULL;
@@ -293,42 +398,45 @@
int total_sleep_time_usec = 0;
for (;;) {
- int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
- if (signal < 0) {
+ int signal = wait_for_sigstop(request.tid, &total_sleep_time_usec, &detach_failed);
+ if (signal == -1) {
+ tid_unresponsive = true;
break;
}
switch (signal) {
case SIGSTOP:
if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
- XLOG("stopped -- dumping to tombstone\n");
- tombstone_path = engrave_tombstone(
- request.pid, request.tid, signal, request.abort_msg_address, true, true,
- &detach_failed, &total_sleep_time_usec);
+ ALOGV("stopped -- dumping to tombstone\n");
+ tombstone_path = engrave_tombstone(request.pid, request.tid,
+ signal, request.original_si_code,
+ request.abort_msg_address, true,
+ &detach_failed, &total_sleep_time_usec);
} else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
- XLOG("stopped -- dumping to fd\n");
+ ALOGV("stopped -- dumping to fd\n");
dump_backtrace(fd, -1, request.pid, request.tid, &detach_failed,
&total_sleep_time_usec);
} else {
- XLOG("stopped -- continuing\n");
+ ALOGV("stopped -- continuing\n");
status = ptrace(PTRACE_CONT, request.tid, 0, 0);
if (status) {
- LOG("ptrace continue failed: %s\n", strerror(errno));
+ ALOGE("ptrace continue failed: %s\n", strerror(errno));
}
continue; // loop again
}
break;
- case SIGILL:
case SIGABRT:
case SIGBUS:
case SIGFPE:
- case SIGSEGV:
+ case SIGILL:
case SIGPIPE:
+ case SIGSEGV:
#ifdef SIGSTKFLT
case SIGSTKFLT:
#endif
- XLOG("stopped -- fatal signal\n");
+ case SIGTRAP:
+ ALOGV("stopped -- fatal signal\n");
// Send a SIGSTOP to the process to make all of
// the non-signaled threads stop moving. Without
// this we get a lot of "ptrace detach failed:
@@ -336,14 +444,14 @@
kill(request.pid, SIGSTOP);
// don't dump sibling threads when attaching to GDB because it
// makes the process less reliable, apparently...
- tombstone_path = engrave_tombstone(
- request.pid, request.tid, signal, request.abort_msg_address, !attach_gdb,
- false, &detach_failed, &total_sleep_time_usec);
+ tombstone_path = engrave_tombstone(request.pid, request.tid,
+ signal, request.original_si_code,
+ request.abort_msg_address, !attach_gdb,
+ &detach_failed, &total_sleep_time_usec);
break;
default:
- XLOG("stopped -- unexpected signal\n");
- LOG("process stopped due to unexpected signal %d\n", signal);
+ ALOGE("process stopped due to unexpected signal %d\n", signal);
break;
}
break;
@@ -359,27 +467,21 @@
free(tombstone_path);
}
- XLOG("detaching\n");
- if (attach_gdb) {
- // stop the process so we can debug
- kill(request.pid, SIGSTOP);
-
- // detach so we can attach gdbserver
- if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
- LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
- detach_failed = true;
+ if (!tid_unresponsive) {
+ ALOGV("detaching");
+ if (attach_gdb) {
+ // stop the process so we can debug
+ kill(request.pid, SIGSTOP);
}
-
- // if debug.db.uid is set, its value indicates if we should wait
- // for user action for the crashing process.
- // in this case, we log a message and turn the debug LED on
- // waiting for a gdb connection (for instance)
- wait_for_user_action(request.pid);
- } else {
- // just detach
if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
- LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
+ ALOGE("ptrace detach from %d failed: %s", request.tid, strerror(errno));
detach_failed = true;
+ } else if (attach_gdb) {
+ // if debug.db.uid is set, its value indicates if we should wait
+ // for user action for the crashing process.
+ // in this case, we log a message and turn the debug LED on
+ // waiting for a gdb connection (for instance)
+ wait_for_user_action(request);
}
}
@@ -390,7 +492,7 @@
// actual parent won't receive a death notification via wait(2). At this point
// there's not much we can do about that.
if (detach_failed) {
- LOG("debuggerd committing suicide to free the zombie!\n");
+ ALOGE("debuggerd committing suicide to free the zombie!\n");
kill(getpid(), SIGKILL);
}
}
@@ -402,52 +504,50 @@
}
static int do_server() {
- int s;
- struct sigaction act;
- int logsocket = -1;
-
- // debuggerd crashes can't be reported to debuggerd. Reset all of the
- // crash handlers.
- signal(SIGILL, SIG_DFL);
+ // debuggerd crashes can't be reported to debuggerd.
+ // Reset all of the crash handlers.
signal(SIGABRT, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGFPE, SIG_DFL);
+ signal(SIGILL, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
#ifdef SIGSTKFLT
signal(SIGSTKFLT, SIG_DFL);
#endif
+ signal(SIGTRAP, SIG_DFL);
// Ignore failed writes to closed sockets
signal(SIGPIPE, SIG_IGN);
- logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
+ int logsocket = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
if (logsocket < 0) {
logsocket = -1;
} else {
fcntl(logsocket, F_SETFD, FD_CLOEXEC);
}
+ struct sigaction act;
act.sa_handler = SIG_DFL;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask,SIGCHLD);
act.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &act, 0);
- s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+ int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
if (s < 0)
return 1;
fcntl(s, F_SETFD, FD_CLOEXEC);
- LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
+ ALOGI("debuggerd: " __DATE__ " " __TIME__ "\n");
for (;;) {
sockaddr addr;
socklen_t alen = sizeof(addr);
- XLOG("waiting for connection\n");
+ ALOGV("waiting for connection\n");
int fd = accept(s, &addr, &alen);
if (fd < 0) {
- XLOG("accept failed: %s\n", strerror(errno));
+ ALOGV("accept failed: %s\n", strerror(errno));
continue;
}
@@ -487,7 +587,11 @@
}
int main(int argc, char** argv) {
+ union selinux_callback cb;
if (argc == 1) {
+ selinux_enabled = is_selinux_enabled();
+ cb.func_log = selinux_log_callback;
+ selinux_set_callback(SELINUX_CB_LOG, cb);
return do_server();
}
diff --git a/debuggerd/elf_utils.cpp b/debuggerd/elf_utils.cpp
new file mode 100644
index 0000000..764b9db
--- /dev/null
+++ b/debuggerd/elf_utils.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#define LOG_TAG "DEBUG"
+
+#include <elf.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <string>
+
+#include <backtrace/Backtrace.h>
+#include <base/stringprintf.h>
+#include <log/log.h>
+
+#include "elf_utils.h"
+
+template <typename HdrType, typename PhdrType, typename NhdrType>
+static bool get_build_id(
+ Backtrace* backtrace, uintptr_t base_addr, uint8_t* e_ident, std::string* build_id) {
+ HdrType hdr;
+
+ memcpy(&hdr.e_ident[0], e_ident, EI_NIDENT);
+
+ // First read the rest of the header.
+ if (backtrace->Read(base_addr + EI_NIDENT, reinterpret_cast<uint8_t*>(&hdr) + EI_NIDENT,
+ sizeof(HdrType) - EI_NIDENT) != sizeof(HdrType) - EI_NIDENT) {
+ return false;
+ }
+
+ for (size_t i = 0; i < hdr.e_phnum; i++) {
+ PhdrType phdr;
+ if (backtrace->Read(base_addr + hdr.e_phoff + i * hdr.e_phentsize,
+ reinterpret_cast<uint8_t*>(&phdr), sizeof(phdr)) != sizeof(phdr)) {
+ return false;
+ }
+ // Looking for the .note.gnu.build-id note.
+ if (phdr.p_type == PT_NOTE) {
+ size_t hdr_size = phdr.p_filesz;
+ uintptr_t addr = base_addr + phdr.p_offset;
+ while (hdr_size >= sizeof(NhdrType)) {
+ NhdrType nhdr;
+ if (backtrace->Read(addr, reinterpret_cast<uint8_t*>(&nhdr), sizeof(nhdr)) != sizeof(nhdr)) {
+ return false;
+ }
+ addr += sizeof(nhdr);
+ if (nhdr.n_type == NT_GNU_BUILD_ID) {
+ // Skip the name (which is the owner and should be "GNU").
+ addr += nhdr.n_namesz;
+ uint8_t build_id_data[128];
+ if (nhdr.n_namesz > sizeof(build_id_data)) {
+ ALOGE("Possible corrupted note, name size value is too large: %u",
+ nhdr.n_namesz);
+ return false;
+ }
+ if (backtrace->Read(addr, build_id_data, nhdr.n_descsz) != nhdr.n_descsz) {
+ return false;
+ }
+
+ build_id->clear();
+ for (size_t bytes = 0; bytes < nhdr.n_descsz; bytes++) {
+ *build_id += android::base::StringPrintf("%02x", build_id_data[bytes]);
+ }
+
+ return true;
+ } else {
+ // Move past the extra note data.
+ hdr_size -= sizeof(nhdr);
+ size_t skip_bytes = nhdr.n_namesz + nhdr.n_descsz;
+ addr += skip_bytes;
+ if (hdr_size < skip_bytes) {
+ break;
+ }
+ hdr_size -= skip_bytes;
+ }
+ }
+ }
+ }
+ return false;
+}
+
+bool elf_get_build_id(Backtrace* backtrace, uintptr_t addr, std::string* build_id) {
+ // Read and verify the elf magic number first.
+ uint8_t e_ident[EI_NIDENT];
+ if (backtrace->Read(addr, e_ident, SELFMAG) != SELFMAG) {
+ return false;
+ }
+
+ if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
+ return false;
+ }
+
+ // Read the rest of EI_NIDENT.
+ if (backtrace->Read(addr + SELFMAG, e_ident + SELFMAG, EI_NIDENT - SELFMAG) != EI_NIDENT - SELFMAG) {
+ return false;
+ }
+
+ if (e_ident[EI_CLASS] == ELFCLASS32) {
+ return get_build_id<Elf32_Ehdr, Elf32_Phdr, Elf32_Nhdr>(backtrace, addr, e_ident, build_id);
+ } else if (e_ident[EI_CLASS] == ELFCLASS64) {
+ return get_build_id<Elf64_Ehdr, Elf64_Phdr, Elf64_Nhdr>(backtrace, addr, e_ident, build_id);
+ }
+
+ return false;
+}
diff --git a/adb/usb_vendors.h b/debuggerd/elf_utils.h
similarity index 67%
copy from adb/usb_vendors.h
copy to debuggerd/elf_utils.h
index cee23a1..11d0a43 100644
--- a/adb/usb_vendors.h
+++ b/debuggerd/elf_utils.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2015 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.
@@ -14,12 +14,14 @@
* limitations under the License.
*/
-#ifndef __USB_VENDORS_H
-#define __USB_VENDORS_H
+#ifndef _DEBUGGERD_ELF_UTILS_H
+#define _DEBUGGERD_ELF_UTILS_H
-extern int vendorIds[];
-extern unsigned vendorIdCount;
+#include <stdint.h>
+#include <string>
-void usb_vendors_init(void);
+class Backtrace;
-#endif
+bool elf_get_build_id(Backtrace*, uintptr_t, std::string*);
+
+#endif // _DEBUGGERD_ELF_UTILS_H
diff --git a/debuggerd/machine.h b/debuggerd/machine.h
index 2f1e201..fca9fbe 100644
--- a/debuggerd/machine.h
+++ b/debuggerd/machine.h
@@ -21,7 +21,7 @@
#include "utility.h"
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags);
-void dump_registers(log_t* log, pid_t tid, int scope_flags);
+void dump_memory_and_code(log_t* log, pid_t tid);
+void dump_registers(log_t* log, pid_t tid);
#endif // _DEBUGGERD_MACHINE_H
diff --git a/debuggerd/mips/machine.cpp b/debuggerd/mips/machine.cpp
index 7b4e29e..1145963 100644
--- a/debuggerd/mips/machine.cpp
+++ b/debuggerd/mips/machine.cpp
@@ -22,86 +22,79 @@
#include <sys/types.h>
#include <sys/ptrace.h>
-#include <corkscrew/ptrace.h>
-
#include <sys/user.h>
#include "../utility.h"
#include "../machine.h"
-// enable to dump memory pointed to by every register
-#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
-
#define R(x) (static_cast<unsigned int>(x))
// If configured to do so, dump memory around *all* registers
// for the crashing thread.
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
- pt_regs_mips_t r;
+void dump_memory_and_code(log_t* log, pid_t tid) {
+ pt_regs r;
if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
return;
}
- if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
- static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
+ static const char REG_NAMES[] = "$0atv0v1a0a1a2a3t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
- for (int reg = 0; reg < 32; reg++) {
- // skip uninteresting registers
- if (reg == 0 // $0
- || reg == 26 // $k0
- || reg == 27 // $k1
- || reg == 31 // $ra (done below)
- )
- continue;
+ for (int reg = 0; reg < 32; reg++) {
+ // skip uninteresting registers
+ if (reg == 0 // $0
+ || reg == 26 // $k0
+ || reg == 27 // $k1
+ || reg == 31 // $ra (done below)
+ )
+ continue;
- uintptr_t addr = R(r.regs[reg]);
+ uintptr_t addr = R(r.regs[reg]);
- // Don't bother if it looks like a small int or ~= null, or if
- // it's in the kernel area.
- if (addr < 4096 || addr >= 0x80000000) {
- continue;
- }
-
- _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]);
- dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
+ // Don't bother if it looks like a small int or ~= null, or if
+ // it's in the kernel area.
+ if (addr < 4096 || addr >= 0x80000000) {
+ continue;
}
+
+ _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]);
+ dump_memory(log, tid, addr);
}
unsigned int pc = R(r.cp0_epc);
unsigned int ra = R(r.regs[31]);
- _LOG(log, scope_flags, "\ncode around pc:\n");
- dump_memory(log, tid, (uintptr_t)pc, scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
+ dump_memory(log, tid, (uintptr_t)pc);
if (pc != ra) {
- _LOG(log, scope_flags, "\ncode around ra:\n");
- dump_memory(log, tid, (uintptr_t)ra, scope_flags);
+ _LOG(log, logtype::MEMORY, "\ncode around ra:\n");
+ dump_memory(log, tid, (uintptr_t)ra);
}
}
-void dump_registers(log_t* log, pid_t tid, int scope_flags) {
- pt_regs_mips_t r;
+void dump_registers(log_t* log, pid_t tid) {
+ pt_regs r;
if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
- _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
+ _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
return;
}
- _LOG(log, scope_flags, " zr %08x at %08x v0 %08x v1 %08x\n",
+ _LOG(log, logtype::REGISTERS, " zr %08x at %08x v0 %08x v1 %08x\n",
R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
- _LOG(log, scope_flags, " a0 %08x a1 %08x a2 %08x a3 %08x\n",
+ _LOG(log, logtype::REGISTERS, " a0 %08x a1 %08x a2 %08x a3 %08x\n",
R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
- _LOG(log, scope_flags, " t0 %08x t1 %08x t2 %08x t3 %08x\n",
+ _LOG(log, logtype::REGISTERS, " t0 %08x t1 %08x t2 %08x t3 %08x\n",
R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
- _LOG(log, scope_flags, " t4 %08x t5 %08x t6 %08x t7 %08x\n",
+ _LOG(log, logtype::REGISTERS, " t4 %08x t5 %08x t6 %08x t7 %08x\n",
R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
- _LOG(log, scope_flags, " s0 %08x s1 %08x s2 %08x s3 %08x\n",
+ _LOG(log, logtype::REGISTERS, " s0 %08x s1 %08x s2 %08x s3 %08x\n",
R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
- _LOG(log, scope_flags, " s4 %08x s5 %08x s6 %08x s7 %08x\n",
+ _LOG(log, logtype::REGISTERS, " s4 %08x s5 %08x s6 %08x s7 %08x\n",
R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
- _LOG(log, scope_flags, " t8 %08x t9 %08x k0 %08x k1 %08x\n",
+ _LOG(log, logtype::REGISTERS, " t8 %08x t9 %08x k0 %08x k1 %08x\n",
R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
- _LOG(log, scope_flags, " gp %08x sp %08x s8 %08x ra %08x\n",
+ _LOG(log, logtype::REGISTERS, " gp %08x sp %08x s8 %08x ra %08x\n",
R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
- _LOG(log, scope_flags, " hi %08x lo %08x bva %08x epc %08x\n",
+ _LOG(log, logtype::REGISTERS, " hi %08x lo %08x bva %08x epc %08x\n",
R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
}
diff --git a/debuggerd/mips64/crashglue.S b/debuggerd/mips64/crashglue.S
new file mode 100644
index 0000000..70a6641
--- /dev/null
+++ b/debuggerd/mips64/crashglue.S
@@ -0,0 +1,48 @@
+ .set noat
+
+ .globl crash1
+ .globl crashnostack
+
+crash1:
+ li $0,0xdead0000+0
+ li $1,0xdead0000+1
+ li $2,0xdead0000+2
+ li $3,0xdead0000+3
+ li $4,0xdead0000+4
+ li $5,0xdead0000+5
+ li $6,0xdead0000+6
+ li $7,0xdead0000+7
+ li $8,0xdead0000+8
+ li $9,0xdead0000+9
+ li $10,0xdead0000+10
+ li $11,0xdead0000+11
+ li $12,0xdead0000+12
+ li $13,0xdead0000+13
+ li $14,0xdead0000+14
+ li $15,0xdead0000+15
+ li $16,0xdead0000+16
+ li $17,0xdead0000+17
+ li $18,0xdead0000+18
+ li $19,0xdead0000+19
+ li $20,0xdead0000+20
+ li $21,0xdead0000+21
+ li $22,0xdead0000+22
+ li $23,0xdead0000+23
+ li $24,0xdead0000+24
+ li $25,0xdead0000+25
+ li $26,0xdead0000+26
+ li $27,0xdead0000+27
+ li $28,0xdead0000+28
+ # don't trash the stack otherwise the signal handler won't run
+ #li $29,0xdead0000+29
+ li $30,0xdead0000+30
+ li $31,0xdead0000+31
+
+ lw $zero,($0)
+ b .
+
+
+crashnostack:
+ li $sp, 0
+ lw $zero,($0)
+ b .
diff --git a/debuggerd/mips64/machine.cpp b/debuggerd/mips64/machine.cpp
new file mode 100644
index 0000000..ef9092f
--- /dev/null
+++ b/debuggerd/mips64/machine.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2014, 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 <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+
+#include <sys/user.h>
+
+#include "../utility.h"
+#include "../machine.h"
+
+#define R(x) (static_cast<unsigned long>(x))
+
+// If configured to do so, dump memory around *all* registers
+// for the crashing thread.
+void dump_memory_and_code(log_t* log, pid_t tid) {
+ pt_regs r;
+ if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
+ return;
+ }
+
+ static const char REG_NAMES[] = "$0atv0v1a0a1a2a3a4a5a6a7t0t1t2t3s0s1s2s3s4s5s6s7t8t9k0k1gpsps8ra";
+
+ for (int reg = 0; reg < 32; reg++) {
+ // skip uninteresting registers
+ if (reg == 0 // $0
+ || reg == 26 // $k0
+ || reg == 27 // $k1
+ || reg == 31 // $ra (done below)
+ )
+ continue;
+
+ uintptr_t addr = R(r.regs[reg]);
+
+ // Don't bother if it looks like a small int or ~= null, or if
+ // it's in the kernel area.
+ if (addr < 4096 || addr >= 0x4000000000000000) {
+ continue;
+ }
+
+ _LOG(log, logtype::MEMORY, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]);
+ dump_memory(log, tid, addr);
+ }
+
+ unsigned long pc = R(r.cp0_epc);
+ unsigned long ra = R(r.regs[31]);
+
+ _LOG(log, logtype::MEMORY, "\ncode around pc:\n");
+ dump_memory(log, tid, (uintptr_t)pc);
+
+ if (pc != ra) {
+ _LOG(log, logtype::MEMORY, "\ncode around ra:\n");
+ dump_memory(log, tid, (uintptr_t)ra);
+ }
+}
+
+void dump_registers(log_t* log, pid_t tid) {
+ pt_regs r;
+ if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
+ _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
+ return;
+ }
+
+ _LOG(log, logtype::REGISTERS, " zr %016lx at %016lx v0 %016lx v1 %016lx\n",
+ R(r.regs[0]), R(r.regs[1]), R(r.regs[2]), R(r.regs[3]));
+ _LOG(log, logtype::REGISTERS, " a0 %016lx a1 %016lx a2 %016lx a3 %016lx\n",
+ R(r.regs[4]), R(r.regs[5]), R(r.regs[6]), R(r.regs[7]));
+ _LOG(log, logtype::REGISTERS, " a4 %016lx a5 %016lx a6 %016lx a7 %016lx\n",
+ R(r.regs[8]), R(r.regs[9]), R(r.regs[10]), R(r.regs[11]));
+ _LOG(log, logtype::REGISTERS, " t0 %016lx t1 %016lx t2 %016lx t3 %016lx\n",
+ R(r.regs[12]), R(r.regs[13]), R(r.regs[14]), R(r.regs[15]));
+ _LOG(log, logtype::REGISTERS, " s0 %016lx s1 %016lx s2 %016lx s3 %016lx\n",
+ R(r.regs[16]), R(r.regs[17]), R(r.regs[18]), R(r.regs[19]));
+ _LOG(log, logtype::REGISTERS, " s4 %016lx s5 %016lx s6 %016lx s7 %016lx\n",
+ R(r.regs[20]), R(r.regs[21]), R(r.regs[22]), R(r.regs[23]));
+ _LOG(log, logtype::REGISTERS, " t8 %016lx t9 %016lx k0 %016lx k1 %016lx\n",
+ R(r.regs[24]), R(r.regs[25]), R(r.regs[26]), R(r.regs[27]));
+ _LOG(log, logtype::REGISTERS, " gp %016lx sp %016lx s8 %016lx ra %016lx\n",
+ R(r.regs[28]), R(r.regs[29]), R(r.regs[30]), R(r.regs[31]));
+ _LOG(log, logtype::REGISTERS, " hi %016lx lo %016lx bva %016lx epc %016lx\n",
+ R(r.hi), R(r.lo), R(r.cp0_badvaddr), R(r.cp0_epc));
+}
diff --git a/debuggerd/tombstone.cpp b/debuggerd/tombstone.cpp
old mode 100755
new mode 100644
index 2319f37..094ab48
--- a/debuggerd/tombstone.cpp
+++ b/debuggerd/tombstone.cpp
@@ -14,6 +14,9 @@
* limitations under the License.
*/
+#define LOG_TAG "DEBUG"
+
+#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@@ -31,9 +34,11 @@
#include <private/android_filesystem_config.h>
+#include <base/stringprintf.h>
+#include <cutils/properties.h>
#include <log/log.h>
#include <log/logger.h>
-#include <cutils/properties.h>
+#include <log/logprint.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
@@ -42,9 +47,12 @@
#include <UniquePtr.h>
+#include <string>
+
+#include "backtrace.h"
+#include "elf_utils.h"
#include "machine.h"
#include "tombstone.h"
-#include "backtrace.h"
#define STACK_WORDS 16
@@ -55,12 +63,13 @@
// Must match the path defined in NativeCrashListener.java
#define NCRASH_SOCKET_PATH "/data/system/ndebugsocket"
-static bool signal_has_address(int sig) {
+static bool signal_has_si_addr(int sig) {
switch (sig) {
- case SIGILL:
- case SIGFPE:
- case SIGSEGV:
case SIGBUS:
+ case SIGFPE:
+ case SIGILL:
+ case SIGSEGV:
+ case SIGTRAP:
return true;
default:
return false;
@@ -69,16 +78,17 @@
static const char* get_signame(int sig) {
switch(sig) {
- case SIGILL: return "SIGILL";
case SIGABRT: return "SIGABRT";
case SIGBUS: return "SIGBUS";
case SIGFPE: return "SIGFPE";
- case SIGSEGV: return "SIGSEGV";
+ case SIGILL: return "SIGILL";
case SIGPIPE: return "SIGPIPE";
-#ifdef SIGSTKFLT
+ case SIGSEGV: return "SIGSEGV";
+#if defined(SIGSTKFLT)
case SIGSTKFLT: return "SIGSTKFLT";
#endif
case SIGSTOP: return "SIGSTOP";
+ case SIGTRAP: return "SIGTRAP";
default: return "?";
}
}
@@ -97,13 +107,17 @@
case ILL_COPROC: return "ILL_COPROC";
case ILL_BADSTK: return "ILL_BADSTK";
}
+ static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
break;
case SIGBUS:
switch (code) {
case BUS_ADRALN: return "BUS_ADRALN";
case BUS_ADRERR: return "BUS_ADRERR";
case BUS_OBJERR: return "BUS_OBJERR";
+ case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
+ case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
}
+ static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
break;
case SIGFPE:
switch (code) {
@@ -116,74 +130,76 @@
case FPE_FLTINV: return "FPE_FLTINV";
case FPE_FLTSUB: return "FPE_FLTSUB";
}
+ static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
break;
case SIGSEGV:
switch (code) {
case SEGV_MAPERR: return "SEGV_MAPERR";
case SEGV_ACCERR: return "SEGV_ACCERR";
}
+ static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
break;
case SIGTRAP:
switch (code) {
case TRAP_BRKPT: return "TRAP_BRKPT";
case TRAP_TRACE: return "TRAP_TRACE";
+ case TRAP_BRANCH: return "TRAP_BRANCH";
+ case TRAP_HWBKPT: return "TRAP_HWBKPT";
}
+ static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code");
break;
}
// Then the other codes...
switch (code) {
case SI_USER: return "SI_USER";
-#if defined(SI_KERNEL)
case SI_KERNEL: return "SI_KERNEL";
-#endif
case SI_QUEUE: return "SI_QUEUE";
case SI_TIMER: return "SI_TIMER";
case SI_MESGQ: return "SI_MESGQ";
case SI_ASYNCIO: return "SI_ASYNCIO";
-#if defined(SI_SIGIO)
case SI_SIGIO: return "SI_SIGIO";
-#endif
-#if defined(SI_TKILL)
case SI_TKILL: return "SI_TKILL";
-#endif
+ case SI_DETHREAD: return "SI_DETHREAD";
}
// Then give up...
return "?";
}
-static void dump_revision_info(log_t* log) {
+static void dump_header_info(log_t* log) {
+ char fingerprint[PROPERTY_VALUE_MAX];
char revision[PROPERTY_VALUE_MAX];
+ property_get("ro.build.fingerprint", fingerprint, "unknown");
property_get("ro.revision", revision, "unknown");
- _LOG(log, SCOPE_AT_FAULT, "Revision: '%s'\n", revision);
+ _LOG(log, logtype::HEADER, "Build fingerprint: '%s'\n", fingerprint);
+ _LOG(log, logtype::HEADER, "Revision: '%s'\n", revision);
+ _LOG(log, logtype::HEADER, "ABI: '%s'\n", ABI_STRING);
}
-static void dump_build_info(log_t* log) {
- char fingerprint[PROPERTY_VALUE_MAX];
-
- property_get("ro.build.fingerprint", fingerprint, "unknown");
-
- _LOG(log, SCOPE_AT_FAULT, "Build fingerprint: '%s'\n", fingerprint);
-}
-
-static void dump_fault_addr(log_t* log, pid_t tid, int sig) {
+static void dump_signal_info(log_t* log, pid_t tid, int signal, int si_code) {
siginfo_t si;
-
memset(&si, 0, sizeof(si));
- if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
- _LOG(log, SCOPE_AT_FAULT, "cannot get siginfo: %s\n", strerror(errno));
- } else if (signal_has_address(sig)) {
- _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr %" PRIPTR "\n",
- sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code),
- reinterpret_cast<uintptr_t>(si.si_addr));
- } else {
- _LOG(log, SCOPE_AT_FAULT, "signal %d (%s), code %d (%s), fault addr --------\n",
- sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
+ if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == -1) {
+ _LOG(log, logtype::HEADER, "cannot get siginfo: %s\n", strerror(errno));
+ return;
}
+
+ // bionic has to re-raise some signals, which overwrites the si_code with SI_TKILL.
+ si.si_code = si_code;
+
+ char addr_desc[32]; // ", fault addr 0x1234"
+ if (signal_has_si_addr(signal)) {
+ snprintf(addr_desc, sizeof(addr_desc), "%p", si.si_addr);
+ } else {
+ snprintf(addr_desc, sizeof(addr_desc), "--------");
+ }
+
+ _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s), fault addr %s\n",
+ signal, get_signame(signal), si.si_code, get_sigcode(signal, si.si_code), addr_desc);
}
-static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, int scope_flags) {
+static void dump_thread_info(log_t* log, pid_t pid, pid_t tid) {
char path[64];
char threadnamebuf[1024];
char* threadname = NULL;
@@ -200,74 +216,64 @@
}
}
}
-
- if (IS_AT_FAULT(scope_flags)) {
- char procnamebuf[1024];
- char* procname = NULL;
-
- snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
- if ((fp = fopen(path, "r"))) {
- procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
- fclose(fp);
- }
-
- _LOG(log, SCOPE_AT_FAULT, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
- threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
- } else {
- _LOG(log, 0, "pid: %d, tid: %d, name: %s\n", pid, tid, threadname ? threadname : "UNKNOWN");
+ // Blacklist logd, logd.reader, logd.writer, logd.auditd, logd.control ...
+ static const char logd[] = "logd";
+ if (!strncmp(threadname, logd, sizeof(logd) - 1)
+ && (!threadname[sizeof(logd) - 1] || (threadname[sizeof(logd) - 1] == '.'))) {
+ log->should_retrieve_logcat = false;
}
+
+ char procnamebuf[1024];
+ char* procname = NULL;
+
+ snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
+ if ((fp = fopen(path, "r"))) {
+ procname = fgets(procnamebuf, sizeof(procnamebuf), fp);
+ fclose(fp);
+ }
+
+ _LOG(log, logtype::HEADER, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid,
+ threadname ? threadname : "UNKNOWN", procname ? procname : "UNKNOWN");
}
static void dump_stack_segment(
- Backtrace* backtrace, log_t* log, int scope_flags, uintptr_t* sp, size_t words, int label) {
+ Backtrace* backtrace, log_t* log, uintptr_t* sp, size_t words, int label) {
+ // Read the data all at once.
+ word_t stack_data[words];
+ size_t bytes_read = backtrace->Read(*sp, reinterpret_cast<uint8_t*>(&stack_data[0]), sizeof(word_t) * words);
+ words = bytes_read / sizeof(word_t);
+ std::string line;
for (size_t i = 0; i < words; i++) {
- word_t stack_content;
- if (!backtrace->ReadWord(*sp, &stack_content)) {
- break;
+ line = " ";
+ if (i == 0 && label >= 0) {
+ // Print the label once.
+ line += android::base::StringPrintf("#%02d ", label);
+ } else {
+ line += " ";
}
+ line += android::base::StringPrintf("%" PRIPTR " %" PRIPTR, *sp, stack_data[i]);
- const backtrace_map_t* map = backtrace->FindMap(stack_content);
- const char* map_name;
- if (!map) {
- map_name = "";
- } else {
- map_name = map->name.c_str();
- }
- uintptr_t offset = 0;
- std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
- if (!func_name.empty()) {
- if (!i && label >= 0) {
+ backtrace_map_t map;
+ backtrace->FillInMap(stack_data[i], &map);
+ if (BacktraceMap::IsValid(map) && !map.name.empty()) {
+ line += " " + map.name;
+ uintptr_t offset = 0;
+ std::string func_name(backtrace->GetFunctionName(stack_data[i], &offset));
+ if (!func_name.empty()) {
+ line += " (" + func_name;
if (offset) {
- _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
- label, *sp, stack_content, map_name, func_name.c_str(), offset);
- } else {
- _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s (%s)\n",
- label, *sp, stack_content, map_name, func_name.c_str());
+ line += android::base::StringPrintf("+%" PRIuPTR, offset);
}
- } else {
- if (offset) {
- _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s+%" PRIuPTR ")\n",
- *sp, stack_content, map_name, func_name.c_str(), offset);
- } else {
- _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s (%s)\n",
- *sp, stack_content, map_name, func_name.c_str());
- }
- }
- } else {
- if (!i && label >= 0) {
- _LOG(log, scope_flags, " #%02d %" PRIPTR " %" PRIPTR " %s\n",
- label, *sp, stack_content, map_name);
- } else {
- _LOG(log, scope_flags, " %" PRIPTR " %" PRIPTR " %s\n",
- *sp, stack_content, map_name);
+ line += ')';
}
}
+ _LOG(log, logtype::STACK, "%s\n", line.c_str());
*sp += sizeof(word_t);
}
}
-static void dump_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
+static void dump_stack(Backtrace* backtrace, log_t* log) {
size_t first = 0, last;
for (size_t i = 0; i < backtrace->NumFrames(); i++) {
const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
@@ -283,27 +289,22 @@
}
first--;
- scope_flags |= SCOPE_SENSITIVE;
-
// Dump a few words before the first frame.
word_t sp = backtrace->GetFrame(first)->sp - STACK_WORDS * sizeof(word_t);
- dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, -1);
+ dump_stack_segment(backtrace, log, &sp, STACK_WORDS, -1);
// Dump a few words from all successive frames.
// Only log the first 3 frames, put the rest in the tombstone.
for (size_t i = first; i <= last; i++) {
const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
if (sp != frame->sp) {
- _LOG(log, scope_flags, " ........ ........\n");
+ _LOG(log, logtype::STACK, " ........ ........\n");
sp = frame->sp;
}
- if (i - first == 3) {
- scope_flags &= (~SCOPE_AT_FAULT);
- }
if (i == last) {
- dump_stack_segment(backtrace, log, scope_flags, &sp, STACK_WORDS, i);
+ dump_stack_segment(backtrace, log, &sp, STACK_WORDS, i);
if (sp < frame->sp + frame->stack_size) {
- _LOG(log, scope_flags, " ........ ........\n");
+ _LOG(log, logtype::STACK, " ........ ........\n");
}
} else {
size_t words = frame->stack_size / sizeof(word_t);
@@ -312,85 +313,87 @@
} else if (words > STACK_WORDS) {
words = STACK_WORDS;
}
- dump_stack_segment(backtrace, log, scope_flags, &sp, words, i);
+ dump_stack_segment(backtrace, log, &sp, words, i);
}
}
}
-static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log, int scope_flags) {
- if (backtrace->NumFrames()) {
- _LOG(log, scope_flags, "\nbacktrace:\n");
- dump_backtrace_to_log(backtrace, log, scope_flags, " ");
-
- _LOG(log, scope_flags, "\nstack:\n");
- dump_stack(backtrace, log, scope_flags);
- }
-}
-
-static void dump_map(log_t* log, const backtrace_map_t* map, const char* what, int scope_flags) {
- if (map != NULL) {
- _LOG(log, scope_flags, " %" PRIPTR "-%" PRIPTR " %c%c%c %s\n", map->start, map->end,
- (map->flags & PROT_READ) ? 'r' : '-', (map->flags & PROT_WRITE) ? 'w' : '-',
- (map->flags & PROT_EXEC) ? 'x' : '-', map->name.c_str());
- } else {
- _LOG(log, scope_flags, " (no %s)\n", what);
- }
-}
-
-static void dump_nearby_maps(BacktraceMap* map, log_t* log, pid_t tid, int scope_flags) {
- scope_flags |= SCOPE_SENSITIVE;
+static void dump_all_maps(Backtrace* backtrace, BacktraceMap* map, log_t* log, pid_t tid) {
+ bool print_fault_address_marker = false;
+ uintptr_t addr = 0;
siginfo_t si;
memset(&si, 0, sizeof(si));
if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
- _LOG(log, scope_flags, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
- return;
- }
- if (!signal_has_address(si.si_signo)) {
- return;
+ _LOG(log, logtype::ERROR, "cannot get siginfo for %d: %s\n", tid, strerror(errno));
+ } else {
+ print_fault_address_marker = signal_has_si_addr(si.si_signo);
+ addr = reinterpret_cast<uintptr_t>(si.si_addr);
}
- uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
- addr &= ~0xfff; // round to 4K page boundary
- if (addr == 0) { // null-pointer deref
- return;
- }
-
- _LOG(log, scope_flags, "\nmemory map around fault addr %" PRIPTR ":\n",
- reinterpret_cast<uintptr_t>(si.si_addr));
-
- // Search for a match, or for a hole where the match would be. The list
- // is backward from the file content, so it starts at high addresses.
- const backtrace_map_t* cur_map = NULL;
- const backtrace_map_t* next_map = NULL;
- const backtrace_map_t* prev_map = NULL;
- for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
- if (addr >= it->start && addr < it->end) {
- cur_map = &*it;
- if (it != map->begin()) {
- prev_map = &*(it-1);
- }
- if (++it != map->end()) {
- next_map = &*it;
- }
- break;
+ _LOG(log, logtype::MAPS, "\n");
+ if (!print_fault_address_marker) {
+ _LOG(log, logtype::MAPS, "memory map:\n");
+ } else {
+ _LOG(log, logtype::MAPS, "memory map: (fault address prefixed with --->)\n");
+ if (map->begin() != map->end() && addr < map->begin()->start) {
+ _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " before any mapped regions\n",
+ addr);
+ print_fault_address_marker = false;
}
}
- // Show the map address in ascending order (like /proc/pid/maps).
- dump_map(log, prev_map, "map below", scope_flags);
- dump_map(log, cur_map, "map for address", scope_flags);
- dump_map(log, next_map, "map above", scope_flags);
+ std::string line;
+ for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
+ line = " ";
+ if (print_fault_address_marker) {
+ if (addr < it->start) {
+ _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " between mapped regions\n",
+ addr);
+ print_fault_address_marker = false;
+ } else if (addr >= it->start && addr < it->end) {
+ line = "--->";
+ print_fault_address_marker = false;
+ }
+ }
+ line += android::base::StringPrintf("%" PRIPTR "-%" PRIPTR " ", it->start, it->end - 1);
+ if (it->flags & PROT_READ) {
+ line += 'r';
+ } else {
+ line += '-';
+ }
+ if (it->flags & PROT_WRITE) {
+ line += 'w';
+ } else {
+ line += '-';
+ }
+ if (it->flags & PROT_EXEC) {
+ line += 'x';
+ } else {
+ line += '-';
+ }
+ line += android::base::StringPrintf(" %8" PRIxPTR, it->end - it->start);
+ if (it->name.length() > 0) {
+ line += " " + it->name;
+ std::string build_id;
+ if ((it->flags & PROT_READ) && elf_get_build_id(backtrace, it->start, &build_id)) {
+ line += " (BuildId: " + build_id + ")";
+ }
+ }
+ _LOG(log, logtype::MAPS, "%s\n", line.c_str());
+ }
+ if (print_fault_address_marker) {
+ _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " after any mapped regions\n",
+ addr);
+ }
}
-static void dump_thread(
- Backtrace* backtrace, log_t* log, int scope_flags, int* total_sleep_time_usec) {
- wait_for_stop(backtrace->Tid(), total_sleep_time_usec);
+static void dump_backtrace_and_stack(Backtrace* backtrace, log_t* log) {
+ if (backtrace->NumFrames()) {
+ _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
+ dump_backtrace_to_log(backtrace, log, " ");
- dump_registers(log, backtrace->Tid(), scope_flags);
- dump_backtrace_and_stack(backtrace, log, scope_flags);
- if (IS_AT_FAULT(scope_flags)) {
- dump_memory_and_code(log, backtrace->Tid(), scope_flags);
- dump_nearby_maps(backtrace->GetMap(), log, backtrace->Tid(), scope_flags);
+ _LOG(log, logtype::STACK, "\nstack:\n");
+ dump_stack(backtrace, log);
}
}
@@ -398,12 +401,13 @@
static bool dump_sibling_thread_report(
log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec, BacktraceMap* map) {
char task_path[64];
+
snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
DIR* d = opendir(task_path);
// Bail early if the task directory cannot be opened
if (d == NULL) {
- XLOG("Cannot open /proc/%d/task\n", pid);
+ ALOGE("Cannot open /proc/%d/task\n", pid);
return false;
}
@@ -424,19 +428,28 @@
// Skip this thread if cannot ptrace it
if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
+ _LOG(log, logtype::ERROR, "ptrace attach to %d failed: %s\n", new_tid, strerror(errno));
continue;
}
- _LOG(log, 0, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
- dump_thread_info(log, pid, new_tid, 0);
-
- UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
- if (backtrace->Unwind(0)) {
- dump_thread(backtrace.get(), log, 0, total_sleep_time_usec);
+ if (wait_for_sigstop(new_tid, total_sleep_time_usec, &detach_failed) == -1) {
+ continue;
}
+ log->current_tid = new_tid;
+ _LOG(log, logtype::THREAD, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
+ dump_thread_info(log, pid, new_tid);
+
+ dump_registers(log, new_tid);
+ UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, new_tid, map));
+ if (backtrace->Unwind(0)) {
+ dump_backtrace_and_stack(backtrace.get(), log);
+ }
+
+ log->current_tid = log->crashed_tid;
+
if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
- LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
+ _LOG(log, logtype::ERROR, "ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
detach_failed = true;
}
}
@@ -449,16 +462,22 @@
// that don't match the specified pid, and writes them to the tombstone file.
//
// If "tail" is non-zero, log the last "tail" number of lines.
+static EventTagMap* g_eventTagMap = NULL;
+
static void dump_log_file(
log_t* log, pid_t pid, const char* filename, unsigned int tail) {
bool first = true;
struct logger_list* logger_list;
+ if (!log->should_retrieve_logcat) {
+ return;
+ }
+
logger_list = android_logger_list_open(
- android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid);
+ android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid);
if (!logger_list) {
- XLOG("Unable to open %s: %s\n", filename, strerror(errno));
+ ALOGE("Unable to open %s: %s\n", filename, strerror(errno));
return;
}
@@ -476,16 +495,17 @@
// non-blocking EOF; we're done
break;
} else {
- _LOG(log, 0, "Error while reading log: %s\n", strerror(-actual));
+ _LOG(log, logtype::ERROR, "Error while reading log: %s\n",
+ strerror(-actual));
break;
}
} else if (actual == 0) {
- _LOG(log, 0, "Got zero bytes while reading log: %s\n",
+ _LOG(log, logtype::ERROR, "Got zero bytes while reading log: %s\n",
strerror(errno));
break;
}
- // NOTE: if you XLOG something here, this will spin forever,
+ // NOTE: if you ALOGV something here, this will spin forever,
// because you will be writing as fast as you're reading. Any
// high-frequency debug diagnostics should just be written to
// the tombstone file.
@@ -493,7 +513,8 @@
entry = &log_entry.entry_v1;
if (first) {
- _LOG(log, 0, "--------- %slog %s\n", tail ? "tail end of " : "", filename);
+ _LOG(log, logtype::LOGS, "--------- %slog %s\n",
+ tail ? "tail end of " : "", filename);
first = false;
}
@@ -507,6 +528,27 @@
hdr_size = sizeof(log_entry.entry_v1);
}
char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
+
+ char timeBuf[32];
+ time_t sec = static_cast<time_t>(entry->sec);
+ struct tm tmBuf;
+ struct tm* ptm;
+ ptm = localtime_r(&sec, &tmBuf);
+ strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
+
+ if (log_entry.id() == LOG_ID_EVENTS) {
+ if (!g_eventTagMap) {
+ g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
+ }
+ AndroidLogEntry e;
+ char buf[512];
+ android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
+ _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
+ timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
+ 'I', e.tag, e.message);
+ continue;
+ }
+
unsigned char prio = msg[0];
char* tag = msg + 1;
msg = tag + strlen(tag) + 1;
@@ -519,13 +561,6 @@
char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
- char timeBuf[32];
- time_t sec = static_cast<time_t>(entry->sec);
- struct tm tmBuf;
- struct tm* ptm;
- ptm = localtime_r(&sec, &tmBuf);
- strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
-
// Look for line breaks ('\n') and display each text line
// on a separate line, prefixed with the header, like logcat does.
do {
@@ -535,7 +570,7 @@
++nl;
}
- _LOG(log, 0, "%s.%03d %5d %5d %c %-8s: %s\n",
+ _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
prioChar, tag, msg);
} while ((msg = nl));
@@ -574,12 +609,13 @@
}
msg[sizeof(msg) - 1] = '\0';
- _LOG(log, SCOPE_AT_FAULT, "Abort message: '%s'\n", msg);
+ _LOG(log, logtype::HEADER, "Abort message: '%s'\n", msg);
}
// Dumps all information about the specified pid to the tombstone.
-static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
- bool dump_sibling_threads, int* total_sleep_time_usec) {
+static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, int si_code,
+ uintptr_t abort_msg_address, bool dump_sibling_threads,
+ int* total_sleep_time_usec) {
// don't copy log messages to tombstone unless this is a dev device
char value[PROPERTY_VALUE_MAX];
property_get("ro.debuggable", value, "0");
@@ -595,21 +631,24 @@
TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) );
}
- _LOG(log, SCOPE_AT_FAULT,
+ _LOG(log, logtype::HEADER,
"*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
- dump_build_info(log);
- dump_revision_info(log);
- dump_thread_info(log, pid, tid, SCOPE_AT_FAULT);
+ dump_header_info(log);
+ dump_thread_info(log, pid, tid);
+
if (signal) {
- dump_fault_addr(log, tid, signal);
+ dump_signal_info(log, tid, signal, si_code);
}
UniquePtr<BacktraceMap> map(BacktraceMap::Create(pid));
UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
+ dump_abort_message(backtrace.get(), log, abort_msg_address);
+ dump_registers(log, tid);
if (backtrace->Unwind(0)) {
- dump_abort_message(backtrace.get(), log, abort_msg_address);
- dump_thread(backtrace.get(), log, SCOPE_AT_FAULT, total_sleep_time_usec);
+ dump_backtrace_and_stack(backtrace.get(), log);
}
+ dump_memory_and_code(log, tid);
+ dump_all_maps(backtrace.get(), map.get(), log, tid);
if (want_logs) {
dump_logs(log, pid, 5);
@@ -661,7 +700,7 @@
if (errno != ENOENT)
continue;
- *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
+ *fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0600);
if (*fd < 0)
continue; // raced ?
@@ -670,15 +709,15 @@
}
if (oldest < 0) {
- LOG("Failed to find a valid tombstone, default to using tombstone 0.\n");
+ ALOGE("Failed to find a valid tombstone, default to using tombstone 0.\n");
oldest = 0;
}
// we didn't find an available file, so we clobber the oldest one
snprintf(path, sizeof(path), TOMBSTONE_TEMPLATE, oldest);
- *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
+ *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0600);
if (*fd < 0) {
- LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
+ ALOGE("failed to open tombstone file '%s': %s\n", path, strerror(errno));
return NULL;
}
fchown(*fd, AID_SYSTEM, AID_SYSTEM);
@@ -715,37 +754,49 @@
return amfd;
}
-char* engrave_tombstone(
- pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address, bool dump_sibling_threads,
- bool quiet, bool* detach_failed, int* total_sleep_time_usec) {
+char* engrave_tombstone(pid_t pid, pid_t tid, int signal, int original_si_code,
+ uintptr_t abort_msg_address, bool dump_sibling_threads,
+ bool* detach_failed, int* total_sleep_time_usec) {
+
+ log_t log;
+ log.current_tid = tid;
+ log.crashed_tid = tid;
+
if ((mkdir(TOMBSTONE_DIR, 0755) == -1) && (errno != EEXIST)) {
- LOG("failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
+ _LOG(&log, logtype::ERROR, "failed to create %s: %s\n", TOMBSTONE_DIR, strerror(errno));
}
if (chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM) == -1) {
- LOG("failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
+ _LOG(&log, logtype::ERROR, "failed to change ownership of %s: %s\n", TOMBSTONE_DIR, strerror(errno));
}
- if (selinux_android_restorecon(TOMBSTONE_DIR, 0) == -1) {
+ int fd = -1;
+ char* path = NULL;
+ if (selinux_android_restorecon(TOMBSTONE_DIR, 0) == 0) {
+ path = find_and_open_tombstone(&fd);
+ } else {
+ _LOG(&log, logtype::ERROR, "Failed to restore security context, not writing tombstone.\n");
+ }
+
+ if (fd < 0) {
+ _LOG(&log, logtype::ERROR, "Skipping tombstone write, nothing to do.\n");
*detach_failed = false;
return NULL;
}
- int fd;
- char* path = find_and_open_tombstone(&fd);
- if (!path) {
- *detach_failed = false;
- return NULL;
- }
-
- log_t log;
log.tfd = fd;
- log.amfd = activity_manager_connect();
- log.quiet = quiet;
- *detach_failed = dump_crash(
- &log, pid, tid, signal, abort_msg_address, dump_sibling_threads, total_sleep_time_usec);
+ // Preserve amfd since it can be modified through the calls below without
+ // being closed.
+ int amfd = activity_manager_connect();
+ log.amfd = amfd;
+ *detach_failed = dump_crash(&log, pid, tid, signal, original_si_code, abort_msg_address,
+ dump_sibling_threads, total_sleep_time_usec);
- close(log.amfd);
+ ALOGI("\nTombstone written to: %s\n", path);
+
+ // Either of these file descriptors can be -1, any error is ignored.
+ close(amfd);
close(fd);
+
return path;
}
diff --git a/debuggerd/tombstone.h b/debuggerd/tombstone.h
index e9878bf..7e2b2fe 100644
--- a/debuggerd/tombstone.h
+++ b/debuggerd/tombstone.h
@@ -23,7 +23,9 @@
/* Creates a tombstone file and writes the crash dump to it.
* Returns the path of the tombstone, which must be freed using free(). */
-char* engrave_tombstone(pid_t pid, pid_t tid, int signal, uintptr_t abort_msg_address,
- bool dump_sibling_threads, bool quiet, bool* detach_failed, int* total_sleep_time_usec);
+char* engrave_tombstone(pid_t pid, pid_t tid, int signal, int original_si_code,
+ uintptr_t abort_msg_address,
+ bool dump_sibling_threads, bool* detach_failed,
+ int* total_sleep_time_usec);
#endif // _DEBUGGERD_TOMBSTONE_H
diff --git a/debuggerd/utility.cpp b/debuggerd/utility.cpp
index 9b20914..e10feff 100644
--- a/debuggerd/utility.cpp
+++ b/debuggerd/utility.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#define LOG_TAG "DEBUG"
+
#include "utility.h"
#include <errno.h>
@@ -24,10 +26,10 @@
#include <sys/wait.h>
#include <backtrace/Backtrace.h>
-#include <log/logd.h>
+#include <log/log.h>
-const int sleep_time_usec = 50000; // 0.05 seconds
-const int max_total_sleep_usec = 10000000; // 10 seconds
+const int SLEEP_TIME_USEC = 50000; // 0.05 seconds
+const int MAX_TOTAL_SLEEP_USEC = 10000000; // 10 seconds
static int write_to_am(int fd, const char* buf, int len) {
int to_write = len;
@@ -35,7 +37,7 @@
int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write));
if (written < 0) {
// hard failure
- LOG("AM write failure (%d / %s)\n", errno, strerror(errno));
+ ALOGE("AM write failure (%d / %s)\n", errno, strerror(errno));
return -1;
}
to_write -= written;
@@ -43,10 +45,24 @@
return len;
}
-void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) {
- bool want_tfd_write = log && log->tfd >= 0;
- bool want_log_write = IS_AT_FAULT(scopeFlags) && (!log || !log->quiet);
- bool want_amfd_write = IS_AT_FAULT(scopeFlags) && !IS_SENSITIVE(scopeFlags) && log && log->amfd >= 0;
+// Whitelist output desired in the logcat output.
+bool is_allowed_in_logcat(enum logtype ltype) {
+ if ((ltype == ERROR)
+ || (ltype == HEADER)
+ || (ltype == REGISTERS)
+ || (ltype == BACKTRACE)) {
+ return true;
+ }
+ return false;
+}
+
+void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
+ bool write_to_tombstone = (log->tfd != -1);
+ bool write_to_logcat = is_allowed_in_logcat(ltype)
+ && log->crashed_tid != -1
+ && log->current_tid != -1
+ && (log->crashed_tid == log->current_tid);
+ bool write_to_activitymanager = (log->amfd != -1);
char buf[512];
va_list ap;
@@ -59,13 +75,13 @@
return;
}
- if (want_tfd_write) {
+ if (write_to_tombstone) {
TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
}
- if (want_log_write) {
- __android_log_write(ANDROID_LOG_INFO, "DEBUG", buf);
- if (want_amfd_write) {
+ if (write_to_logcat) {
+ __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, LOG_TAG, buf);
+ if (write_to_activitymanager) {
int written = write_to_am(log->amfd, buf, len);
if (written <= 0) {
// timeout or other failure on write; stop informing the activity manager
@@ -75,57 +91,47 @@
}
}
-int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
+int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) {
+ bool allow_dead_tid = false;
for (;;) {
int status;
- pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
- if (n < 0) {
- if (errno == EAGAIN)
- continue;
- LOG("waitpid failed: %s\n", strerror(errno));
- return -1;
- } else if (n > 0) {
- XLOG("waitpid: n=%d status=%08x\n", n, status);
+ pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
+ if (n == -1) {
+ ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno));
+ break;
+ } else if (n == tid) {
if (WIFSTOPPED(status)) {
return WSTOPSIG(status);
} else {
- LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
- return -1;
+ ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
+ // This is the only circumstance under which we can allow a detach
+ // to fail with ESRCH, which indicates the tid has exited.
+ allow_dead_tid = true;
+ break;
}
}
- if (*total_sleep_time_usec > max_total_sleep_usec) {
- LOG("timed out waiting for tid=%d to die\n", tid);
- return -1;
- }
-
- // not ready yet
- XLOG("not ready yet\n");
- usleep(sleep_time_usec);
- *total_sleep_time_usec += sleep_time_usec;
- }
-}
-
-void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
- siginfo_t si;
- while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
- if (*total_sleep_time_usec > max_total_sleep_usec) {
- LOG("timed out waiting for tid=%d to stop\n", tid);
+ if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) {
+ ALOGE("timed out waiting for stop signal: tid=%d", tid);
break;
}
- usleep(sleep_time_usec);
- *total_sleep_time_usec += sleep_time_usec;
+ usleep(SLEEP_TIME_USEC);
+ *total_sleep_time_usec += SLEEP_TIME_USEC;
}
+
+ if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
+ if (allow_dead_tid && errno == ESRCH) {
+ ALOGE("tid exited before attach completed: tid %d", tid);
+ } else {
+ *detach_failed = true;
+ ALOGE("detach failed: tid %d, %s", tid, strerror(errno));
+ }
+ }
+ return -1;
}
-#if defined (__mips__)
-#define DUMP_MEMORY_AS_ASCII 1
-#else
-#define DUMP_MEMORY_AS_ASCII 0
-#endif
-
-void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags) {
+void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
char code_buffer[64];
char ascii_buffer[32];
uintptr_t p, end;
@@ -171,7 +177,6 @@
static_cast<uintptr_t>(data));
}
-#if DUMP_MEMORY_AS_ASCII
for (size_t j = 0; j < sizeof(long); j++) {
/*
* Our isprint() allows high-ASCII characters that display
@@ -185,10 +190,9 @@
*asc_out++ = '.';
}
}
-#endif
p += sizeof(long);
}
*asc_out = '\0';
- _LOG(log, scope_flags, " %s %s\n", code_buffer, ascii_buffer);
+ _LOG(log, logtype::MEMORY, " %s %s\n", code_buffer, ascii_buffer);
}
}
diff --git a/debuggerd/utility.h b/debuggerd/utility.h
index 0f88605..49b46e8 100644
--- a/debuggerd/utility.h
+++ b/debuggerd/utility.h
@@ -2,16 +2,16 @@
**
** Copyright 2008, 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
+** 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
+** 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
+** 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.
*/
@@ -21,48 +21,60 @@
#include <stdbool.h>
#include <sys/types.h>
-typedef struct {
+// Figure out the abi based on defined macros.
+#if defined(__arm__)
+#define ABI_STRING "arm"
+#elif defined(__aarch64__)
+#define ABI_STRING "arm64"
+#elif defined(__mips__) && !defined(__LP64__)
+#define ABI_STRING "mips"
+#elif defined(__mips__) && defined(__LP64__)
+#define ABI_STRING "mips64"
+#elif defined(__i386__)
+#define ABI_STRING "x86"
+#elif defined(__x86_64__)
+#define ABI_STRING "x86_64"
+#else
+#error "Unsupported ABI"
+#endif
+
+
+struct log_t{
/* tombstone file descriptor */
int tfd;
/* Activity Manager socket file descriptor */
int amfd;
- /* if true, does not log anything to the Android logcat or Activity Manager */
- bool quiet;
-} log_t;
+ // The tid of the thread that crashed.
+ pid_t crashed_tid;
+ // The tid of the thread we are currently working with.
+ pid_t current_tid;
+ // logd daemon crash, can block asking for logcat data, allow suppression.
+ bool should_retrieve_logcat;
-/* Log information onto the tombstone. scopeFlags is a bitmask of the flags defined
- * here. */
-void _LOG(log_t* log, int scopeFlags, const char *fmt, ...)
+ log_t()
+ : tfd(-1), amfd(-1), crashed_tid(-1), current_tid(-1), should_retrieve_logcat(true) {}
+};
+
+// List of types of logs to simplify the logging decision in _LOG
+enum logtype {
+ ERROR,
+ HEADER,
+ THREAD,
+ REGISTERS,
+ FP_REGISTERS,
+ BACKTRACE,
+ MAPS,
+ MEMORY,
+ STACK,
+ LOGS
+};
+
+// Log information onto the tombstone.
+void _LOG(log_t* log, logtype ltype, const char *fmt, ...)
__attribute__ ((format(printf, 3, 4)));
-/* The message pertains specifically to the faulting thread / process */
-#define SCOPE_AT_FAULT (1 << 0)
-/* The message contains sensitive information such as RAM contents */
-#define SCOPE_SENSITIVE (1 << 1)
+int wait_for_sigstop(pid_t, int*, bool*);
-#define IS_AT_FAULT(x) (((x) & SCOPE_AT_FAULT) != 0)
-#define IS_SENSITIVE(x) (((x) & SCOPE_SENSITIVE) != 0)
-
-/* Further helpful macros */
-#define LOG(fmt...) _LOG(NULL, SCOPE_AT_FAULT, fmt)
-
-/* Set to 1 for normal debug traces */
-#if 0
-#define XLOG(fmt...) _LOG(NULL, SCOPE_AT_FAULT, fmt)
-#else
-#define XLOG(fmt...) do {} while(0)
-#endif
-
-/* Set to 1 for chatty debug traces. Includes all resolved dynamic symbols */
-#if 0
-#define XLOG2(fmt...) _LOG(NULL, SCOPE_AT_FAULT, fmt)
-#else
-#define XLOG2(fmt...) do {} while(0)
-#endif
-
-int wait_for_signal(pid_t tid, int* total_sleep_time_usec);
-void wait_for_stop(pid_t tid, int* total_sleep_time_usec);
-
-void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags);
+void dump_memory(log_t* log, pid_t tid, uintptr_t addr);
#endif // _DEBUGGERD_UTILITY_H
diff --git a/debuggerd/vfp-crasher.c b/debuggerd/vfp-crasher.c
deleted file mode 100644
index 7a19cdd..0000000
--- a/debuggerd/vfp-crasher.c
+++ /dev/null
@@ -1,7 +0,0 @@
-int main()
-{
- extern void crash(void);
-
- crash();
- return 0;
-}
diff --git a/debuggerd/x86/machine.cpp b/debuggerd/x86/machine.cpp
index 141f19a..57330c1 100644
--- a/debuggerd/x86/machine.cpp
+++ b/debuggerd/x86/machine.cpp
@@ -25,21 +25,21 @@
#include "../utility.h"
#include "../machine.h"
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
+void dump_memory_and_code(log_t*, pid_t) {
}
-void dump_registers(log_t* log, pid_t tid, int scope_flags) {
+void dump_registers(log_t* log, pid_t tid) {
struct pt_regs r;
if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
- _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
+ _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
return;
}
- _LOG(log, scope_flags, " eax %08lx ebx %08lx ecx %08lx edx %08lx\n",
+ _LOG(log, logtype::REGISTERS, " eax %08lx ebx %08lx ecx %08lx edx %08lx\n",
r.eax, r.ebx, r.ecx, r.edx);
- _LOG(log, scope_flags, " esi %08lx edi %08lx\n",
+ _LOG(log, logtype::REGISTERS, " esi %08lx edi %08lx\n",
r.esi, r.edi);
- _LOG(log, scope_flags, " xcs %08x xds %08x xes %08x xfs %08x xss %08x\n",
+ _LOG(log, logtype::REGISTERS, " xcs %08x xds %08x xes %08x xfs %08x xss %08x\n",
r.xcs, r.xds, r.xes, r.xfs, r.xss);
- _LOG(log, scope_flags, " eip %08lx ebp %08lx esp %08lx flags %08lx\n",
+ _LOG(log, logtype::REGISTERS, " eip %08lx ebp %08lx esp %08lx flags %08lx\n",
r.eip, r.ebp, r.esp, r.eflags);
}
diff --git a/debuggerd/x86_64/machine.cpp b/debuggerd/x86_64/machine.cpp
index 406851a..af4f35a 100755
--- a/debuggerd/x86_64/machine.cpp
+++ b/debuggerd/x86_64/machine.cpp
@@ -27,25 +27,25 @@
#include "../utility.h"
#include "../machine.h"
-void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
+void dump_memory_and_code(log_t*, pid_t) {
}
-void dump_registers(log_t* log, pid_t tid, int scope_flags) {
+void dump_registers(log_t* log, pid_t tid) {
struct user_regs_struct r;
if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
- _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
+ _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
return;
}
- _LOG(log, scope_flags, " rax %016lx rbx %016lx rcx %016lx rdx %016lx\n",
+ _LOG(log, logtype::REGISTERS, " rax %016lx rbx %016lx rcx %016lx rdx %016lx\n",
r.rax, r.rbx, r.rcx, r.rdx);
- _LOG(log, scope_flags, " rsi %016lx rdi %016lx\n",
+ _LOG(log, logtype::REGISTERS, " rsi %016lx rdi %016lx\n",
r.rsi, r.rdi);
- _LOG(log, scope_flags, " r8 %016lx r9 %016lx r10 %016lx r11 %016lx\n",
+ _LOG(log, logtype::REGISTERS, " r8 %016lx r9 %016lx r10 %016lx r11 %016lx\n",
r.r8, r.r9, r.r10, r.r11);
- _LOG(log, scope_flags, " r12 %016lx r13 %016lx r14 %016lx r15 %016lx\n",
+ _LOG(log, logtype::REGISTERS, " r12 %016lx r13 %016lx r14 %016lx r15 %016lx\n",
r.r12, r.r13, r.r14, r.r15);
- _LOG(log, scope_flags, " cs %016lx ss %016lx\n",
+ _LOG(log, logtype::REGISTERS, " cs %016lx ss %016lx\n",
r.cs, r.ss);
- _LOG(log, scope_flags, " rip %016lx rbp %016lx rsp %016lx eflags %016lx\n",
+ _LOG(log, logtype::REGISTERS, " rip %016lx rbp %016lx rsp %016lx eflags %016lx\n",
r.rip, r.rbp, r.rsp, r.eflags);
}
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index b9b3c92..b9e957f 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -17,11 +17,13 @@
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \
- $(LOCAL_PATH)/../../extras/ext4_utils
-LOCAL_SRC_FILES := protocol.c engine.c bootimg.c fastboot.c util.c
+ $(LOCAL_PATH)/../../extras/ext4_utils \
+ $(LOCAL_PATH)/../../extras/f2fs_utils
+LOCAL_SRC_FILES := protocol.c engine.c bootimg_utils.cpp fastboot.cpp util.c fs.c
LOCAL_MODULE := fastboot
LOCAL_MODULE_TAGS := debug
-LOCAL_CFLAGS += -std=gnu99
+LOCAL_CONLYFLAGS += -std=gnu99
+LOCAL_CFLAGS += -Wall -Wextra -Werror
ifeq ($(HOST_OS),linux)
LOCAL_SRC_FILES += usb_linux.c util_linux.c
@@ -31,6 +33,7 @@
LOCAL_SRC_FILES += usb_osx.c util_osx.c
LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit \
-framework Carbon
+ LOCAL_CFLAGS += -Wno-unused-parameter
endif
ifeq ($(HOST_OS),windows)
@@ -39,39 +42,53 @@
ifneq ($(strip $(USE_CYGWIN)),)
# Pure cygwin case
LOCAL_LDLIBS += -lpthread
- LOCAL_C_INCLUDES += /usr/include/w32api/ddk
endif
ifneq ($(strip $(USE_MINGW)),)
# MinGW under Linux case
LOCAL_LDLIBS += -lws2_32
USE_SYSDEPS_WIN32 := 1
- LOCAL_C_INCLUDES += /usr/i586-mingw32msvc/include/ddk
endif
LOCAL_C_INCLUDES += development/host/windows/usb/api
endif
LOCAL_STATIC_LIBRARIES := \
$(EXTRA_STATIC_LIBS) \
- libzipfile \
- libunz \
+ libziparchive-host \
libext4_utils_host \
libsparse_host \
+ libutils \
+ liblog \
libz
ifneq ($(HOST_OS),windows)
LOCAL_STATIC_LIBRARIES += libselinux
endif # HOST_OS != windows
+ifeq ($(HOST_OS),linux)
+# libf2fs_dlutils_host will dlopen("libf2fs_fmt_host_dyn")
+LOCAL_CFLAGS += -DUSE_F2FS
+LOCAL_LDFLAGS += -ldl -rdynamic -Wl,-rpath,.
+LOCAL_REQUIRED_MODULES := libf2fs_fmt_host_dyn
+# The following libf2fs_* are from system/extras/f2fs_utils,
+# and do not use code in external/f2fs-tools.
+LOCAL_STATIC_LIBRARIES += libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host
+endif
+
include $(BUILD_HOST_EXECUTABLE)
-
-$(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
+my_dist_files := $(LOCAL_BUILT_MODULE)
+ifeq ($(HOST_OS),linux)
+my_dist_files += $(HOST_LIBRARY_PATH)/libf2fs_fmt_host_dyn$(HOST_SHLIB_SUFFIX)
+endif
+$(call dist-for-goals,dist_files sdk,$(my_dist_files))
+my_dist_files :=
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := usbtest.c usb_linux.c util.c
LOCAL_MODULE := usbtest
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
endif
diff --git a/fastboot/bootimg.c b/fastboot/bootimg_utils.cpp
similarity index 95%
rename from fastboot/bootimg.c
rename to fastboot/bootimg_utils.cpp
index 240784f..d8905a6 100644
--- a/fastboot/bootimg.c
+++ b/fastboot/bootimg_utils.cpp
@@ -26,12 +26,12 @@
* SUCH DAMAGE.
*/
+#include "bootimg_utils.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <bootimg.h>
-
void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline)
{
strcpy((char*) h->cmdline, cmdline);
@@ -47,7 +47,6 @@
unsigned ramdisk_actual;
unsigned second_actual;
unsigned page_mask;
- boot_img_hdr *hdr;
page_mask = page_size - 1;
@@ -57,9 +56,8 @@
*bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual;
- hdr = calloc(*bootimg_size, 1);
-
- if(hdr == 0) {
+ boot_img_hdr* hdr = reinterpret_cast<boot_img_hdr*>(calloc(*bootimg_size, 1));
+ if (hdr == 0) {
return hdr;
}
diff --git a/fastbootd/trigger.h b/fastboot/bootimg_utils.h
similarity index 66%
rename from fastbootd/trigger.h
rename to fastboot/bootimg_utils.h
index 404acb4..b1a86cd 100644
--- a/fastbootd/trigger.h
+++ b/fastboot/bootimg_utils.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009-2013, Google Inc.
+ * Copyright (C) 2015 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -11,9 +11,6 @@
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -29,17 +26,24 @@
* SUCH DAMAGE.
*/
-#ifndef __FASTBOOTD_TRIGGER_H_
-#define __FASTBOOTD_TRIGGER_H_
+#ifndef _FASTBOOT_BOOTIMG_UTILS_H_
+#define _FASTBOOT_BOOTIMG_UTILS_H_
-#include "commands/partitions.h"
-#include "vendor_trigger.h"
+#include <bootimg.h>
-int load_trigger();
+#if defined(__cplusplus)
+extern "C" {
+#endif
-/* same as in struct triggers */
+void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
+boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
+ void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
+ void *second, unsigned second_size, unsigned second_offset,
+ unsigned page_size, unsigned base, unsigned tags_offset,
+ unsigned *bootimg_size);
-int trigger_gpt_layout(struct GPT_content *table);
-int trigger_oem_cmd(const char *arg, const char **response);
+#if defined(__cplusplus)
+}
+#endif
#endif
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 972c4ed..2f90e41 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -27,13 +27,13 @@
*/
#include "fastboot.h"
-#include "make_ext4fs.h"
+#include "fs.h"
#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -45,15 +45,18 @@
#include <sys/mman.h>
#endif
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define OP_DOWNLOAD 1
#define OP_COMMAND 2
#define OP_QUERY 3
#define OP_NOTICE 4
-#define OP_FORMAT 5
-#define OP_DOWNLOAD_SPARSE 6
-#define OP_WAIT_FOR_DISCONNECT 7
+#define OP_DOWNLOAD_SPARSE 5
+#define OP_WAIT_FOR_DISCONNECT 6
typedef struct Action Action;
@@ -79,14 +82,7 @@
static Action *action_last = 0;
-struct image_data {
- long long partition_size;
- long long image_size; // real size of image file
- void *buffer;
-};
-void generate_ext4_image(struct image_data *image);
-void cleanup_image(struct image_data *image);
int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
{
@@ -102,24 +98,6 @@
return fb_command_response(usb, cmd, response);
}
-struct generator {
- char *fs_type;
-
- /* generate image and return it as image->buffer.
- * size of the buffer returned as image->image_size.
- *
- * image->partition_size specifies what is the size of the
- * file partition we generate image for.
- */
- void (*generate)(struct image_data *image);
-
- /* it cleans the buffer allocated during image creation.
- * this function probably does free() or munmap().
- */
- void (*cleanup)(struct image_data *image);
-} generators[] = {
- { "ext4", generate_ext4_image, cleanup_image }
-};
/* Return true if this partition is supported by the fastboot format command.
* It is also used to determine if we should first erase a partition before
@@ -128,30 +106,19 @@
* Not all devices report the filesystem type, so don't report any errors,
* just return false.
*/
-int fb_format_supported(usb_handle *usb, const char *partition)
+int fb_format_supported(usb_handle *usb, const char *partition, const char *type_override)
{
- char response[FB_RESPONSE_SZ+1];
- struct generator *generator = NULL;
+ char fs_type[FB_RESPONSE_SZ + 1] = {0,};
int status;
- unsigned int i;
- status = fb_getvar(usb, response, "partition-type:%s", partition);
+ if (type_override) {
+ return !!fs_get_generator(type_override);
+ }
+ status = fb_getvar(usb, fs_type, "partition-type:%s", partition);
if (status) {
return 0;
}
-
- for (i = 0; i < ARRAY_SIZE(generators); i++) {
- if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
- generator = &generators[i];
- break;
- }
- }
-
- if (generator) {
- return 1;
- }
-
- return 0;
+ return !!fs_get_generator(fs_type);
}
static int cb_default(Action *a, int status, char *resp)
@@ -205,163 +172,6 @@
a->msg = mkmsg("erasing '%s'", ptn);
}
-/* Loads file content into buffer. Returns NULL on error. */
-static void *load_buffer(int fd, off_t size)
-{
- void *buffer;
-
-#ifdef USE_MINGW
- ssize_t count = 0;
-
- // mmap is more efficient but mingw does not support it.
- // In this case we read whole image into memory buffer.
- buffer = malloc(size);
- if (!buffer) {
- perror("malloc");
- return NULL;
- }
-
- lseek(fd, 0, SEEK_SET);
- while(count < size) {
- ssize_t actually_read = read(fd, (char*)buffer+count, size-count);
-
- if (actually_read == 0) {
- break;
- }
- if (actually_read < 0) {
- if (errno == EINTR) {
- continue;
- }
- perror("read");
- free(buffer);
- return NULL;
- }
-
- count += actually_read;
- }
-#else
- buffer = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (buffer == MAP_FAILED) {
- perror("mmap");
- return NULL;
- }
-#endif
-
- return buffer;
-}
-
-void cleanup_image(struct image_data *image)
-{
-#ifdef USE_MINGW
- free(image->buffer);
-#else
- munmap(image->buffer, image->image_size);
-#endif
-}
-
-void generate_ext4_image(struct image_data *image)
-{
- int fd;
- struct stat st;
-
- fd = fileno(tmpfile());
- make_ext4fs_sparse_fd(fd, image->partition_size, NULL, NULL);
-
- fstat(fd, &st);
- image->image_size = st.st_size;
- image->buffer = load_buffer(fd, st.st_size);
-
- close(fd);
-}
-
-int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
-{
- const char *partition = a->cmd;
- char response[FB_RESPONSE_SZ+1];
- int status = 0;
- struct image_data image;
- struct generator *generator = NULL;
- int fd;
- unsigned i;
- char cmd[CMD_SIZE];
-
- status = fb_getvar(usb, response, "partition-type:%s", partition);
- if (status) {
- if (skip_if_not_supported) {
- fprintf(stderr,
- "Erase successful, but not automatically formatting.\n");
- fprintf(stderr,
- "Can't determine partition type.\n");
- return 0;
- }
- fprintf(stderr,"FAILED (%s)\n", fb_get_error());
- return status;
- }
-
- for (i = 0; i < ARRAY_SIZE(generators); i++) {
- if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
- generator = &generators[i];
- break;
- }
- }
- if (!generator) {
- if (skip_if_not_supported) {
- fprintf(stderr,
- "Erase successful, but not automatically formatting.\n");
- fprintf(stderr,
- "File system type %s not supported.\n", response);
- return 0;
- }
- fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
- response);
- return -1;
- }
-
- status = fb_getvar(usb, response, "partition-size:%s", partition);
- if (status) {
- if (skip_if_not_supported) {
- fprintf(stderr,
- "Erase successful, but not automatically formatting.\n");
- fprintf(stderr, "Unable to get partition size\n.");
- return 0;
- }
- fprintf(stderr,"FAILED (%s)\n", fb_get_error());
- return status;
- }
- image.partition_size = strtoll(response, (char **)NULL, 16);
-
- generator->generate(&image);
- if (!image.buffer) {
- fprintf(stderr,"Cannot generate image.\n");
- return -1;
- }
-
- // Following piece of code is similar to fb_queue_flash() but executes
- // actions directly without queuing
- fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024);
- status = fb_download_data(usb, image.buffer, image.image_size);
- if (status) goto cleanup;
-
- fprintf(stderr, "writing '%s'...\n", partition);
- snprintf(cmd, sizeof(cmd), "flash:%s", partition);
- status = fb_command(usb, cmd);
- if (status) goto cleanup;
-
-cleanup:
- generator->cleanup(&image);
-
- return status;
-}
-
-void fb_queue_format(const char *partition, int skip_if_not_supported)
-{
- Action *a;
-
- a = queue_action(OP_FORMAT, partition);
- a->data = (void*)skip_if_not_supported;
- a->msg = mkmsg("formatting '%s' partition", partition);
-}
-
void fb_queue_flash(const char *ptn, void *data, unsigned sz)
{
Action *a;
@@ -390,9 +200,7 @@
static int match(char *str, const char **value, unsigned count)
{
- const char *val;
unsigned n;
- int len;
for (n = 0; n < count; n++) {
const char *val = value[n];
@@ -518,7 +326,7 @@
a->func = cb_save;
}
-static int cb_do_nothing(Action *a, int status, char *resp)
+static int cb_do_nothing(Action *a __unused, int status __unused, char *resp __unused)
{
fprintf(stderr,"\n");
return 0;
@@ -589,10 +397,6 @@
if (status) break;
} else if (a->op == OP_NOTICE) {
fprintf(stderr,"%s\n",(char*)a->data);
- } else if (a->op == OP_FORMAT) {
- status = fb_format(a, usb, (int)a->data);
- status = a->func(a, status, status ? fb_get_error() : "");
- if (status) break;
} else if (a->op == OP_DOWNLOAD_SPARSE) {
status = fb_download_data_sparse(usb, a->data);
status = a->func(a, status, status ? fb_get_error() : "");
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.cpp
similarity index 75%
rename from fastboot/fastboot.c
rename to fastboot/fastboot.cpp
index 7d26c6f..a25d316 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.cpp
@@ -28,27 +28,28 @@
#define _LARGEFILE64_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <string.h>
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <unistd.h>
-#include <limits.h>
-#include <ctype.h>
#include <getopt.h>
-
+#include <inttypes.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
-#include <sys/stat.h>
+#include <unistd.h>
-#include <bootimg.h>
#include <sparse/sparse.h>
-#include <zipfile/zipfile.h>
+#include <ziparchive/zip_archive.h>
+#include "bootimg_utils.h"
#include "fastboot.h"
+#include "fs.h"
#ifndef O_BINARY
#define O_BINARY 0
@@ -58,19 +59,10 @@
char cur_product[FB_RESPONSE_SZ + 1];
-void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
-
-boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
- void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
- void *second, unsigned second_size, unsigned second_offset,
- unsigned page_size, unsigned base, unsigned tags_offset,
- unsigned *bootimg_size);
-
static usb_handle *usb = 0;
static const char *serial = 0;
static const char *product = 0;
static const char *cmdline = 0;
-static int wipe_data = 0;
static unsigned short vendor_id = 0;
static int long_listing = 0;
static int64_t sparse_limit = -1;
@@ -99,18 +91,17 @@
char sig_name[13];
char part_name[9];
bool is_optional;
-} images[3] = {
+} images[] = {
{"boot.img", "boot.sig", "boot", false},
{"recovery.img", "recovery.sig", "recovery", true},
{"system.img", "system.sig", "system", false},
+ {"vendor.img", "vendor.sig", "vendor", true},
};
-void get_my_path(char *path);
-
char *find_item(const char *item, const char *product)
{
char *dir;
- char *fn;
+ const char *fn;
char path[PATH_MAX + 128];
if(!strcmp(item,"boot")) {
@@ -119,6 +110,8 @@
fn = "recovery.img";
} else if(!strcmp(item,"system")) {
fn = "system.img";
+ } else if(!strcmp(item,"vendor")) {
+ fn = "vendor.img";
} else if(!strcmp(item,"userdata")) {
fn = "userdata.img";
} else if(!strcmp(item,"cache")) {
@@ -231,7 +224,7 @@
int list_devices_callback(usb_ifc_info *info)
{
if (match_fastboot_with_serial(info, NULL) == 0) {
- char* serial = info->serial_number;
+ const char* serial = info->serial_number;
if (!info->writable) {
serial = "no permissions"; // like "adb devices"
}
@@ -241,7 +234,7 @@
// output compatible with "adb devices"
if (!long_listing) {
printf("%s\tfastboot\n", serial);
- } else if (!info->device_path) {
+ } else if (strcmp("", info->device_path) == 0) {
printf("%-22s fastboot\n", serial);
} else {
printf("%-22s fastboot %s\n", serial, info->device_path);
@@ -265,7 +258,7 @@
announce = 0;
fprintf(stderr,"< waiting for device >\n");
}
- sleep(1);
+ usleep(1000);
}
}
@@ -284,16 +277,19 @@
"\n"
"commands:\n"
" update <filename> reflash device from update.zip\n"
- " flashall flash boot + recovery + system\n"
+ " flashall flash boot, system, vendor and if found,\n"
+ " recovery\n"
" flash <partition> [ <filename> ] write a file to a flash partition\n"
" erase <partition> erase a flash partition\n"
- " format <partition> format a flash partition \n"
+ " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
+ " Can override the fs type and/or\n"
+ " size the bootloader reports.\n"
" getvar <variable> display a bootloader variable\n"
" boot <kernel> [ <ramdisk> ] download and boot kernel\n"
" flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
" devices list all connected devices\n"
" continue continue with autoboot\n"
- " reboot reboot device normally\n"
+ " reboot [bootloader] reboot device, optionally into bootloader\n"
" reboot-bootloader reboot device into bootloader\n"
" help show this help message\n"
"\n"
@@ -308,10 +304,12 @@
" -p <product> specify product name\n"
" -c <cmdline> override kernel commandline\n"
" -i <vendor id> specify a custom USB vendor id\n"
- " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
- " -n <page size> specify the nand page size. default: 2048\n"
- " -S <size>[K|M|G] automatically sparse files greater than\n"
- " size. 0 to disable\n"
+ " -b <base_addr> specify a custom kernel base address.\n"
+ " default: 0x10000000\n"
+ " -n <page size> specify the nand page size.\n"
+ " default: 2048\n"
+ " -S <size>[K|M|G] automatically sparse files greater\n"
+ " than size. 0 to disable\n"
);
}
@@ -371,30 +369,26 @@
return bdata;
}
-void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
+static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
{
- void *data;
- zipentry_t entry;
- unsigned datasz;
-
- entry = lookup_zipentry(zip, name);
- if (entry == NULL) {
- fprintf(stderr, "archive does not contain '%s'\n", name);
+ ZipEntryName zip_entry_name(entry_name);
+ ZipEntry zip_entry;
+ if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
+ fprintf(stderr, "archive does not contain '%s'\n", entry_name);
return 0;
}
- *sz = get_zipentry_size(entry);
+ *sz = zip_entry.uncompressed_length;
- datasz = *sz * 1.001;
- data = malloc(datasz);
-
- if(data == 0) {
- fprintf(stderr, "failed to allocate %d bytes\n", *sz);
+ uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
+ if (data == NULL) {
+ fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
return 0;
}
- if (decompress_zipentry(entry, data, datasz)) {
- fprintf(stderr, "failed to unzip '%s' from archive\n", name);
+ int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
+ if (error != 0) {
+ fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
free(data);
return 0;
}
@@ -402,27 +396,28 @@
return data;
}
-static int unzip_to_file(zipfile_t zip, char *name)
-{
- int fd;
- char *data;
- unsigned sz;
-
- fd = fileno(tmpfile());
- if (fd < 0) {
+static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
+ FILE* fp = tmpfile();
+ if (fp == NULL) {
+ fprintf(stderr, "failed to create temporary file for '%s': %s\n",
+ entry_name, strerror(errno));
return -1;
}
- data = unzip_file(zip, name, &sz);
- if (data == 0) {
+ ZipEntryName zip_entry_name(entry_name);
+ ZipEntry zip_entry;
+ if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
+ fprintf(stderr, "archive does not contain '%s'\n", entry_name);
return -1;
}
- if (write(fd, data, sz) != sz) {
- fd = -1;
+ int fd = fileno(fp);
+ int error = ExtractEntryToFile(zip, &zip_entry, fd);
+ if (error != 0) {
+ fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
+ return -1;
}
- free(data);
lseek(fd, 0, SEEK_SET);
return fd;
}
@@ -443,7 +438,6 @@
static int setup_requirement_line(char *name)
{
char *val[MAX_OPTIONS];
- const char **out;
char *prod = NULL;
unsigned n, count;
char *x;
@@ -483,10 +477,11 @@
name = strip(name);
if (name == 0) return -1;
- /* work around an unfortunate name mismatch */
- if (!strcmp(name,"board")) name = "product";
+ const char* var = name;
+ // Work around an unfortunate name mismatch.
+ if (!strcmp(name,"board")) var = "product";
- out = malloc(sizeof(char*) * count);
+ const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
if (out == 0) return -1;
for(n = 0; n < count; n++) {
@@ -500,7 +495,7 @@
}
}
- fb_queue_require(prod, name, invert, n, out);
+ fb_queue_require(prod, var, invert, n, out);
return 0;
}
@@ -533,21 +528,17 @@
static struct sparse_file **load_sparse_files(int fd, int max_size)
{
- struct sparse_file *s;
- int files;
- struct sparse_file **out_s;
-
- s = sparse_file_import_auto(fd, false);
+ struct sparse_file* s = sparse_file_import_auto(fd, false);
if (!s) {
die("cannot sparse read file\n");
}
- files = sparse_file_resparse(s, max_size, NULL, 0);
+ int files = sparse_file_resparse(s, max_size, NULL, 0);
if (files < 0) {
die("Failed to resparse\n");
}
- out_s = calloc(sizeof(struct sparse_file *), files + 1);
+ sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
if (!out_s) {
die("Failed to allocate sparse file array\n");
}
@@ -569,7 +560,7 @@
if (!status) {
limit = strtoul(response, NULL, 0);
if (limit > 0) {
- fprintf(stderr, "target reported max download size of %lld bytes\n",
+ fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
limit);
}
}
@@ -612,7 +603,7 @@
/* The function fb_format_supported() currently returns the value
* we want, so just call it.
*/
- return fb_format_supported(usb, part);
+ return fb_format_supported(usb, part, NULL);
}
static int load_buf_fd(usb_handle *usb, int fd,
@@ -622,10 +613,13 @@
void *data;
int64_t limit;
+
sz64 = file_size(fd);
if (sz64 < 0) {
return -1;
}
+
+ lseek(fd, 0, SEEK_SET);
limit = get_sparse_limit(usb, sz64);
if (limit) {
struct sparse_file **s = load_sparse_files(fd, limit);
@@ -653,7 +647,7 @@
fd = open(fname, O_RDONLY | O_BINARY);
if (fd < 0) {
- die("cannot open '%s'\n", fname);
+ return -1;
}
return load_buf_fd(usb, fd, buf);
@@ -661,11 +655,11 @@
static void flash_buf(const char *pname, struct fastboot_buffer *buf)
{
- struct sparse_file **s;
+ sparse_file** s;
switch (buf->type) {
case FB_BUFFER_SPARSE:
- s = buf->data;
+ s = reinterpret_cast<sparse_file**>(buf->data);
while (*s) {
int64_t sz64 = sparse_file_len(*s, true, false);
fb_queue_flash_sparse(pname, *s++, sz64);
@@ -689,63 +683,44 @@
flash_buf(pname, &buf);
}
-void do_update_signature(zipfile_t zip, char *fn)
+void do_update_signature(ZipArchiveHandle zip, char *fn)
{
- void *data;
unsigned sz;
- data = unzip_file(zip, fn, &sz);
+ void* data = unzip_file(zip, fn, &sz);
if (data == 0) return;
fb_queue_download("signature", data, sz);
fb_queue_command("signature", "installing signature");
}
-void do_update(usb_handle *usb, char *fn, int erase_first)
+void do_update(usb_handle *usb, const char *filename, int erase_first)
{
- void *zdata;
- unsigned zsize;
- void *data;
- unsigned sz;
- zipfile_t zip;
- int fd;
- int rc;
- struct fastboot_buffer buf;
- int i;
-
queue_info_dump();
fb_queue_query_save("product", cur_product, sizeof(cur_product));
- zdata = load_file(fn, &zsize);
- if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
-
- zip = init_zipfile(zdata, zsize);
- if(zip == 0) die("failed to access zipdata in '%s'");
-
- data = unzip_file(zip, "android-info.txt", &sz);
- if (data == 0) {
- char *tmp;
- /* fallback for older zipfiles */
- data = unzip_file(zip, "android-product.txt", &sz);
- if ((data == 0) || (sz < 1)) {
- die("update package has no android-info.txt or android-product.txt");
- }
- tmp = malloc(sz + 128);
- if (tmp == 0) die("out of memory");
- sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
- data = tmp;
- sz = strlen(tmp);
+ ZipArchiveHandle zip;
+ int error = OpenArchive(filename, &zip);
+ if (error != 0) {
+ die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
}
- setup_requirements(data, sz);
+ unsigned sz;
+ void* data = unzip_file(zip, "android-info.txt", &sz);
+ if (data == 0) {
+ die("update package '%s' has no android-info.txt", filename);
+ }
- for (i = 0; i < ARRAY_SIZE(images); i++) {
- fd = unzip_to_file(zip, images[i].img_name);
+ setup_requirements(reinterpret_cast<char*>(data), sz);
+
+ for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
+ int fd = unzip_to_file(zip, images[i].img_name);
if (fd < 0) {
if (images[i].is_optional)
continue;
die("update package missing %s", images[i].img_name);
}
- rc = load_buf_fd(usb, fd, &buf);
+ fastboot_buffer buf;
+ int rc = load_buf_fd(usb, fd, &buf);
if (rc) die("cannot load %s from flash", images[i].img_name);
do_update_signature(zip, images[i].sig_name);
if (erase_first && needs_erase(images[i].part_name)) {
@@ -757,6 +732,8 @@
* program exits.
*/
}
+
+ CloseArchive(zip);
}
void do_send_signature(char *fn)
@@ -779,24 +756,22 @@
void do_flashall(usb_handle *usb, int erase_first)
{
- char *fname;
- void *data;
- unsigned sz;
- struct fastboot_buffer buf;
- int i;
-
queue_info_dump();
fb_queue_query_save("product", cur_product, sizeof(cur_product));
- fname = find_item("info", product);
+ char* fname = find_item("info", product);
if (fname == 0) die("cannot find android-info.txt");
- data = load_file(fname, &sz);
- if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
- setup_requirements(data, sz);
- for (i = 0; i < ARRAY_SIZE(images); i++) {
+ unsigned sz;
+ void* data = load_file(fname, &sz);
+ if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
+
+ setup_requirements(reinterpret_cast<char*>(data), sz);
+
+ for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
fname = find_item(images[i].part_name, product);
+ fastboot_buffer buf;
if (load_buf(usb, fname, &buf)) {
if (images[i].is_optional)
continue;
@@ -815,7 +790,6 @@
int do_oem_command(int argc, char **argv)
{
- int i;
char command[256];
if (argc <= 1) return 0;
@@ -872,6 +846,92 @@
return num;
}
+void fb_perform_format(const char *partition, int skip_if_not_supported,
+ const char *type_override, const char *size_override)
+{
+ char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
+ char *pType = pTypeBuff;
+ char *pSize = pSizeBuff;
+ unsigned int limit = INT_MAX;
+ struct fastboot_buffer buf;
+ const char *errMsg = NULL;
+ const struct fs_generator *gen;
+ uint64_t pSz;
+ int status;
+ int fd;
+
+ if (target_sparse_limit > 0 && target_sparse_limit < limit)
+ limit = target_sparse_limit;
+ if (sparse_limit > 0 && sparse_limit < limit)
+ limit = sparse_limit;
+
+ status = fb_getvar(usb, pType, "partition-type:%s", partition);
+ if (status) {
+ errMsg = "Can't determine partition type.\n";
+ goto failed;
+ }
+ if (type_override) {
+ if (strcmp(type_override, pType)) {
+ fprintf(stderr,
+ "Warning: %s type is %s, but %s was requested for formating.\n",
+ partition, pType, type_override);
+ }
+ pType = (char *)type_override;
+ }
+
+ status = fb_getvar(usb, pSize, "partition-size:%s", partition);
+ if (status) {
+ errMsg = "Unable to get partition size\n";
+ goto failed;
+ }
+ if (size_override) {
+ if (strcmp(size_override, pSize)) {
+ fprintf(stderr,
+ "Warning: %s size is %s, but %s was requested for formating.\n",
+ partition, pSize, size_override);
+ }
+ pSize = (char *)size_override;
+ }
+
+ gen = fs_get_generator(pType);
+ if (!gen) {
+ if (skip_if_not_supported) {
+ fprintf(stderr, "Erase successful, but not automatically formatting.\n");
+ fprintf(stderr, "File system type %s not supported.\n", pType);
+ return;
+ }
+ fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
+ return;
+ }
+
+ pSz = strtoll(pSize, (char **)NULL, 16);
+
+ fd = fileno(tmpfile());
+ if (fs_generator_generate(gen, fd, pSz)) {
+ close(fd);
+ fprintf(stderr, "Cannot generate image.\n");
+ return;
+ }
+
+ if (load_buf_fd(usb, fd, &buf)) {
+ fprintf(stderr, "Cannot read image.\n");
+ close(fd);
+ return;
+ }
+ flash_buf(partition, &buf);
+
+ return;
+
+
+failed:
+ if (skip_if_not_supported) {
+ fprintf(stderr, "Erase successful, but not automatically formatting.\n");
+ if (errMsg)
+ fprintf(stderr, "%s", errMsg);
+ }
+ fprintf(stderr,"FAILED (%s)\n", fb_get_error());
+}
+
int main(int argc, char **argv)
{
int wants_wipe = 0;
@@ -882,7 +942,7 @@
unsigned sz;
int status;
int c;
- int r;
+ int longindex;
const struct option longopts[] = {
{"base", required_argument, 0, 'b'},
@@ -891,14 +951,14 @@
{"ramdisk_offset", required_argument, 0, 'r'},
{"tags_offset", required_argument, 0, 't'},
{"help", 0, 0, 'h'},
+ {"unbuffered", 0, 0, 0},
{0, 0, 0, 0}
};
serial = getenv("ANDROID_SERIAL");
while (1) {
- int option_index = 0;
- c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL);
+ c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
if (c < 0) {
break;
}
@@ -959,6 +1019,12 @@
break;
case '?':
return 1;
+ case 0:
+ if (strcmp("unbuffered", longopts[longindex].name) == 0) {
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ }
+ break;
default:
abort();
}
@@ -993,18 +1059,42 @@
} else if(!strcmp(*argv, "erase")) {
require(2);
- if (fb_format_supported(usb, argv[1])) {
+ if (fb_format_supported(usb, argv[1], NULL)) {
fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
}
fb_queue_erase(argv[1]);
skip(2);
- } else if(!strcmp(*argv, "format")) {
+ } else if(!strncmp(*argv, "format", strlen("format"))) {
+ char *overrides;
+ char *type_override = NULL;
+ char *size_override = NULL;
require(2);
+ /*
+ * Parsing for: "format[:[type][:[size]]]"
+ * Some valid things:
+ * - select ontly the size, and leave default fs type:
+ * format::0x4000000 userdata
+ * - default fs type and size:
+ * format userdata
+ * format:: userdata
+ */
+ overrides = strchr(*argv, ':');
+ if (overrides) {
+ overrides++;
+ size_override = strchr(overrides, ':');
+ if (size_override) {
+ size_override[0] = '\0';
+ size_override++;
+ }
+ type_override = overrides;
+ }
+ if (type_override && !type_override[0]) type_override = NULL;
+ if (size_override && !size_override[0]) size_override = NULL;
if (erase_first && needs_erase(argv[1])) {
fb_queue_erase(argv[1]);
}
- fb_queue_format(argv[1], 0);
+ fb_perform_format(argv[1], 0, type_override, size_override);
skip(2);
} else if(!strcmp(*argv, "signature")) {
require(2);
@@ -1017,6 +1107,14 @@
} else if(!strcmp(*argv, "reboot")) {
wants_reboot = 1;
skip(1);
+ if (argc > 0) {
+ if (!strcmp(*argv, "bootloader")) {
+ wants_reboot = 0;
+ wants_reboot_bootloader = 1;
+ skip(1);
+ }
+ }
+ require(0);
} else if(!strcmp(*argv, "reboot-bootloader")) {
wants_reboot_bootloader = 1;
skip(1);
@@ -1092,12 +1190,13 @@
if (wants_wipe) {
fb_queue_erase("userdata");
- fb_queue_format("userdata", 1);
+ fb_perform_format("userdata", 1, NULL, NULL);
fb_queue_erase("cache");
- fb_queue_format("cache", 1);
+ fb_perform_format("cache", 1, NULL, NULL);
}
if (wants_reboot) {
fb_queue_reboot();
+ fb_queue_wait_for_disconnect();
} else if (wants_reboot_bootloader) {
fb_queue_command("reboot-bootloader", "rebooting into bootloader");
fb_queue_wait_for_disconnect();
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index 976c397..1786e49 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -31,6 +31,10 @@
#include "usb.h"
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
struct sparse_file;
/* protocol.c - fastboot protocol */
@@ -45,11 +49,11 @@
/* engine.c - high level command queue engine */
int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...);
-int fb_format_supported(usb_handle *usb, const char *partition);
+int fb_format_supported(usb_handle *usb, const char *partition, const char *type_override);
void fb_queue_flash(const char *ptn, void *data, unsigned sz);
void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz);
void fb_queue_erase(const char *ptn);
-void fb_queue_format(const char *ptn, int skip_if_not_supported);
+void fb_queue_format(const char *ptn, int skip_if_not_supported, unsigned int max_chunk_sz);
void fb_queue_require(const char *prod, const char *var, int invert,
unsigned nvalues, const char **value);
void fb_queue_display(const char *var, const char *prettyname);
@@ -67,7 +71,13 @@
char *mkmsg(const char *fmt, ...);
void die(const char *fmt, ...);
+void get_my_path(char *path);
+
/* Current product */
extern char cur_product[FB_RESPONSE_SZ + 1];
+#if defined(__cplusplus)
+}
+#endif
+
#endif
diff --git a/fastboot/fastboot_protocol.txt b/fastboot/fastboot_protocol.txt
index 2248992..37b1959 100644
--- a/fastboot/fastboot_protocol.txt
+++ b/fastboot/fastboot_protocol.txt
@@ -12,8 +12,8 @@
------------------
* Two bulk endpoints (in, out) are required
-* Max packet size must be 64 bytes for full-speed and 512 bytes for
- high-speed USB
+* Max packet size must be 64 bytes for full-speed, 512 bytes for
+ high-speed and 1024 bytes for Super Speed USB.
* The protocol is entirely host-driven and synchronous (unlike the
multi-channel, bi-directional, asynchronous ADB protocol)
diff --git a/fastboot/fs.c b/fastboot/fs.c
new file mode 100644
index 0000000..8a15e6f
--- /dev/null
+++ b/fastboot/fs.c
@@ -0,0 +1,65 @@
+#include "fastboot.h"
+#include "make_ext4fs.h"
+#include "make_f2fs.h"
+#include "fs.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sparse/sparse.h>
+#include <unistd.h>
+
+#ifdef USE_MINGW
+#include <fcntl.h>
+#else
+#include <sys/mman.h>
+#endif
+
+
+
+static int generate_ext4_image(int fd, long long partSize)
+{
+ make_ext4fs_sparse_fd(fd, partSize, NULL, NULL);
+
+ return 0;
+}
+
+#ifdef USE_F2FS
+static int generate_f2fs_image(int fd, long long partSize)
+{
+ return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
+}
+#endif
+
+static const struct fs_generator {
+
+ char *fs_type; //must match what fastboot reports for partition type
+ int (*generate)(int fd, long long partSize); //returns 0 or error value
+
+} generators[] = {
+ { "ext4", generate_ext4_image},
+#ifdef USE_F2FS
+ { "f2fs", generate_f2fs_image},
+#endif
+};
+
+const struct fs_generator* fs_get_generator(const char *fs_type)
+{
+ unsigned i;
+
+ for (i = 0; i < sizeof(generators) / sizeof(*generators); i++)
+ if (!strcmp(generators[i].fs_type, fs_type))
+ return generators + i;
+
+ return NULL;
+}
+
+int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize)
+{
+ return gen->generate(tmpFileNo, partSize);
+}
diff --git a/fastboot/fs.h b/fastboot/fs.h
new file mode 100644
index 0000000..307772b
--- /dev/null
+++ b/fastboot/fs.h
@@ -0,0 +1,20 @@
+#ifndef _FS_H_
+#define _FS_H_
+
+#include <stdint.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+struct fs_generator;
+
+const struct fs_generator* fs_get_generator(const char *fs_type);
+int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
diff --git a/fastboot/protocol.c b/fastboot/protocol.c
index a0e0fd4..5b97600 100644
--- a/fastboot/protocol.c
+++ b/fastboot/protocol.c
@@ -110,7 +110,6 @@
char *response)
{
int cmdsize = strlen(cmd);
- int r;
if(response) {
response[0] = 0;
@@ -189,8 +188,6 @@
static int _command_send_no_data(usb_handle *usb, const char *cmd,
char *response)
{
- int r;
-
return _command_start(usb, cmd, 0, response);
}
@@ -219,7 +216,7 @@
}
}
-#define USB_BUF_SIZE 512
+#define USB_BUF_SIZE 1024
static char usb_buf[USB_BUF_SIZE];
static int usb_buf_len;
@@ -308,7 +305,10 @@
return -1;
}
- fb_download_data_sparse_flush(usb);
+ r = fb_download_data_sparse_flush(usb);
+ if (r < 0) {
+ return -1;
+ }
return _command_end(usb);
}
diff --git a/fastboot/usb.h b/fastboot/usb.h
index 17cf0a9..c7b748e 100644
--- a/fastboot/usb.h
+++ b/fastboot/usb.h
@@ -29,6 +29,10 @@
#ifndef _USB_H_
#define _USB_H_
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
typedef struct usb_handle usb_handle;
typedef struct usb_ifc_info usb_ifc_info;
@@ -64,4 +68,8 @@
int usb_write(usb_handle *h, const void *_data, int len);
int usb_wait_for_disconnect(usb_handle *h);
+#if defined(__cplusplus)
+}
+#endif
+
#endif
diff --git a/fastboot/usb_linux.c b/fastboot/usb_linux.c
index f2ce226..022f364 100644
--- a/fastboot/usb_linux.c
+++ b/fastboot/usb_linux.c
@@ -100,12 +100,12 @@
static int check(void *_desc, int len, unsigned type, int size)
{
- unsigned char *desc = _desc;
+ struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)_desc;
if(len < size) return -1;
- if(desc[0] < size) return -1;
- if(desc[0] > len) return -1;
- if(desc[1] != type) return -1;
+ if(hdr->bLength < size) return -1;
+ if(hdr->bLength > len) return -1;
+ if(hdr->bDescriptorType != type) return -1;
return 0;
}
@@ -125,18 +125,15 @@
unsigned i;
unsigned e;
- struct stat st;
- int result;
-
- if(check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
+ if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
return -1;
- dev = (void*) ptr;
+ dev = (struct usb_device_descriptor *)ptr;
len -= dev->bLength;
ptr += dev->bLength;
- if(check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
+ if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE))
return -1;
- cfg = (void*) ptr;
+ cfg = (struct usb_config_descriptor *)ptr;
len -= cfg->bLength;
ptr += cfg->bLength;
@@ -180,9 +177,19 @@
}
for(i = 0; i < cfg->bNumInterfaces; i++) {
- if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE))
+
+ while (len > 0) {
+ struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
+ if (check(hdr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE) == 0)
+ break;
+ len -= hdr->bLength;
+ ptr += hdr->bLength;
+ }
+
+ if (len <= 0)
return -1;
- ifc = (void*) ptr;
+
+ ifc = (struct usb_interface_descriptor *)ptr;
len -= ifc->bLength;
ptr += ifc->bLength;
@@ -193,20 +200,36 @@
info.ifc_protocol = ifc->bInterfaceProtocol;
for(e = 0; e < ifc->bNumEndpoints; e++) {
- if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE))
- return -1;
- ept = (void*) ptr;
+ while (len > 0) {
+ struct usb_descriptor_header *hdr = (struct usb_descriptor_header *)ptr;
+ if (check(hdr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE) == 0)
+ break;
+ len -= hdr->bLength;
+ ptr += hdr->bLength;
+ }
+ if (len < 0) {
+ break;
+ }
+
+ ept = (struct usb_endpoint_descriptor *)ptr;
len -= ept->bLength;
ptr += ept->bLength;
- if((ept->bmAttributes & 0x03) != 0x02)
+ if((ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
continue;
- if(ept->bEndpointAddress & 0x80) {
+ if(ept->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
in = ept->bEndpointAddress;
} else {
out = ept->bEndpointAddress;
}
+
+ // For USB 3.0 devices skip the SS Endpoint Companion descriptor
+ if (check((struct usb_descriptor_hdr *)ptr, len,
+ USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) {
+ len -= USB_DT_SS_EP_COMP_SIZE;
+ ptr += USB_DT_SS_EP_COMP_SIZE;
+ }
}
info.has_bulk_in = (in != -1);
diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c
index 0f55e0d..0b6c515 100644
--- a/fastboot/usb_osx.c
+++ b/fastboot/usb_osx.c
@@ -328,7 +328,8 @@
ERR("GetLocationId");
goto error;
}
- snprintf(handle->info.device_path, sizeof(handle->info.device_path), "usb:%lX", locationId);
+ snprintf(handle->info.device_path, sizeof(handle->info.device_path),
+ "usb:%" PRIu32 "X", (unsigned int)locationId);
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
diff --git a/fastboot/usb_windows.c b/fastboot/usb_windows.c
index 07f7be2..a09610f 100644
--- a/fastboot/usb_windows.c
+++ b/fastboot/usb_windows.c
@@ -32,6 +32,7 @@
#include <usb100.h>
#include <adb_api.h>
#include <stdio.h>
+#include <stdlib.h>
#include "usb.h"
@@ -152,7 +153,7 @@
}
int usb_write(usb_handle* handle, const void* data, int len) {
- unsigned long time_out = 500 + len * 8;
+ unsigned long time_out = 5000;
unsigned long written = 0;
unsigned count = 0;
int ret;
@@ -178,7 +179,7 @@
count += written;
len -= written;
- data += written;
+ data = (const char *)data + written;
if (len == 0)
return count;
@@ -194,7 +195,7 @@
}
int usb_read(usb_handle *handle, void* data, int len) {
- unsigned long time_out = 500 + len * 8;
+ unsigned long time_out = 0;
unsigned long read = 0;
int ret;
@@ -212,7 +213,7 @@
DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno);
if (ret) {
return read;
- } else if (errno != ERROR_SEM_TIMEOUT) {
+ } else {
// assume ERROR_INVALID_HANDLE indicates we are disconnected
if (errno == ERROR_INVALID_HANDLE)
usb_kick(handle);
diff --git a/fastboot/usbtest.c b/fastboot/usbtest.c
index b8fb9e2..e6e2b37 100644
--- a/fastboot/usbtest.c
+++ b/fastboot/usbtest.c
@@ -88,14 +88,14 @@
int test_null(usb_handle *usb)
{
- int i;
+ unsigned i;
unsigned char buf[4096];
memset(buf, 0xee, 4096);
long long t0, t1;
t0 = NOW();
for(i = 0; i < arg_count; i++) {
- if(usb_write(usb, buf, arg_size) != arg_size) {
+ if(usb_write(usb, buf, arg_size) != (int)arg_size) {
fprintf(stderr,"write failed (%s)\n", strerror(errno));
return -1;
}
@@ -107,13 +107,13 @@
int test_zero(usb_handle *usb)
{
- int i;
+ unsigned i;
unsigned char buf[4096];
long long t0, t1;
t0 = NOW();
for(i = 0; i < arg_count; i++) {
- if(usb_read(usb, buf, arg_size) != arg_size) {
+ if(usb_read(usb, buf, arg_size) != (int)arg_size) {
fprintf(stderr,"read failed (%s)\n", strerror(errno));
return -1;
}
@@ -130,11 +130,11 @@
int (*test)(usb_handle *usb);
const char *help;
} tests[] = {
- { "list", printifc, 0, "list interfaces" },
+ { "list", printifc, NULL, "list interfaces" },
{ "send", match_null, test_null, "send to null interface" },
{ "recv", match_zero, test_zero, "recv from zero interface" },
- { "loop", match_loop, 0, "exercise loopback interface" },
- {},
+ { "loop", match_loop, NULL, "exercise loopback interface" },
+ { NULL, NULL, NULL, NULL },
};
int usage(void)
diff --git a/fastboot/util_osx.c b/fastboot/util_osx.c
index 26b832a..e718562 100644
--- a/fastboot/util_osx.c
+++ b/fastboot/util_osx.c
@@ -31,14 +31,15 @@
void get_my_path(char s[PATH_MAX])
{
- char *x;
- ProcessSerialNumber psn;
- GetCurrentProcess(&psn);
- CFDictionaryRef dict;
- dict = ProcessInformationCopyDictionary(&psn, 0xffffffff);
- CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict,
- CFSTR("CFBundleExecutable"));
- CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
+ CFBundleRef mainBundle = CFBundleGetMainBundle();
+ CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
+ CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
+ CFRelease(executableURL);
+
+ CFStringGetFileSystemRepresentation(executablePathString, s, PATH_MAX-1);
+ CFRelease(executablePathString);
+
+ char *x;
x = strrchr(s, '/');
if(x) x[1] = 0;
}
diff --git a/fastbootd/Android.mk b/fastbootd/Android.mk
deleted file mode 100644
index 0f32dbf..0000000
--- a/fastbootd/Android.mk
+++ /dev/null
@@ -1,104 +0,0 @@
-# Copyright (C) 2013 Google Inc.
-#
-# 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_C_INCLUDES := \
- external/openssl/include \
- external/mdnsresponder/mDNSShared \
- $(LOCAL_PATH)/include \
- external/zlib/ \
-
-LOCAL_SRC_FILES := \
- config.c \
- commands.c \
- commands/boot.c \
- commands/flash.c \
- commands/partitions.c \
- commands/virtual_partitions.c \
- fastbootd.c \
- protocol.c \
- network_discovery.c \
- socket_client.c \
- secure.c \
- transport.c \
- transport_socket.c \
- trigger.c \
- usb_linux_client.c \
- utils.c \
-
-LOCAL_MODULE := fastbootd
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter -DFLASH_CERT
-LOCAL_LDFLAGS := -ldl
-
-LOCAL_SHARED_LIBRARIES := \
- libhardware \
- libcrypto \
- libhardware_legacy \
- libmdnssd
-
-LOCAL_STATIC_LIBRARIES := \
- libsparse_static \
- libc \
- libcutils \
- libz
-
-#LOCAL_FORCE_STATIC_EXECUTABLE := true
-
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
-LOCAL_C_INCLUDES := \
- external/zlib/
-
-LOCAL_SRC_FILES := \
- commands/partitions.c \
- other/gptedit.c \
- utils.c
-
-LOCAL_MODULE := gptedit
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
-
-LOCAL_STATIC_LIBRARIES := \
- libsparse_static \
- libc \
- libcutils \
- libz
-
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_C_INCLUDES := \
- $(LOCAL_PATH)/include \
-
-LOCAL_STATIC_LIBRARIES := \
- $(EXTRA_STATIC_LIBS) \
- libcutils
-
-LOCAL_SRC_FILES := \
- other/vendor_trigger.c
-
-LOCAL_MODULE := libvendortrigger.default
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
-
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/fastbootd/bootimg.h b/fastbootd/bootimg.h
deleted file mode 100644
index 44fde92..0000000
--- a/fastbootd/bootimg.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _BOOT_IMAGE_H_
-#define _BOOT_IMAGE_H_
-
-typedef struct boot_img_hdr boot_img_hdr;
-
-#define BOOT_MAGIC "ANDROID!"
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-
-struct boot_img_hdr
-{
- unsigned char magic[BOOT_MAGIC_SIZE];
-
- unsigned kernel_size; /* size in bytes */
- unsigned kernel_addr; /* physical load addr */
-
- unsigned ramdisk_size; /* size in bytes */
- unsigned ramdisk_addr; /* physical load addr */
-
- unsigned second_size; /* size in bytes */
- unsigned second_addr; /* physical load addr */
-
- unsigned tags_addr; /* physical addr for kernel tags */
- unsigned page_size; /* flash page size we assume */
- unsigned unused[2]; /* future expansion: should be 0 */
-
- unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
-
- unsigned char cmdline[BOOT_ARGS_SIZE];
-
- unsigned id[8]; /* timestamp / checksum / sha1 / etc */
-};
-
-/*
-** +-----------------+
-** | boot header | 1 page
-** +-----------------+
-** | kernel | n pages
-** +-----------------+
-** | ramdisk | m pages
-** +-----------------+
-** | second stage | o pages
-** +-----------------+
-**
-** n = (kernel_size + page_size - 1) / page_size
-** m = (ramdisk_size + page_size - 1) / page_size
-** o = (second_size + page_size - 1) / page_size
-**
-** 0. all entities are page_size aligned in flash
-** 1. kernel and ramdisk are required (size != 0)
-** 2. second is optional (second_size == 0 -> no second)
-** 3. load each element (kernel, ramdisk, second) at
-** the specified physical address (kernel_addr, etc)
-** 4. prepare tags at tag_addr. kernel_args[] is
-** appended to the kernel commandline in the tags.
-** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
-** 6. if second_size != 0: jump to second_addr
-** else: jump to kernel_addr
-*/
-
-boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
- void *ramdisk, unsigned ramdisk_size,
- void *second, unsigned second_size,
- unsigned page_size,
- unsigned *bootimg_size);
-
-void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);
-#endif
diff --git a/fastbootd/commands.c b/fastbootd/commands.c
deleted file mode 100644
index 98b7866..0000000
--- a/fastbootd/commands.c
+++ /dev/null
@@ -1,409 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <inttypes.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/reboot.h>
-#include <fcntl.h>
-
-#include "bootimg.h"
-#include "commands/boot.h"
-#include "commands/flash.h"
-#include "commands/partitions.h"
-#include "commands/virtual_partitions.h"
-#include "debug.h"
-#include "protocol.h"
-#include "trigger.h"
-#include "utils.h"
-
-#define ATAGS_LOCATION "/proc/atags"
-
-static void cmd_boot(struct protocol_handle *phandle, const char *arg)
-{
- int sz, atags_sz, new_atags_sz;
- int rv;
- unsigned kernel_actual;
- unsigned ramdisk_actual;
- unsigned second_actual;
- void *kernel_ptr;
- void *ramdisk_ptr;
- void *second_ptr;
- struct boot_img_hdr *hdr;
- char *ptr = NULL;
- char *atags_ptr = NULL;
- char *new_atags = NULL;
- int data_fd = 0;
-
- D(DEBUG, "cmd_boot %s\n", arg);
-
- if (phandle->download_fd < 0) {
- fastboot_fail(phandle, "no kernel file");
- return;
- }
-
- atags_ptr = read_atags(ATAGS_LOCATION, &atags_sz);
- if (atags_ptr == NULL) {
- fastboot_fail(phandle, "atags read error");
- goto error;
- }
-
- // TODO: With cms we can also verify partition name included as
- // cms signed attribute
- if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) {
- fastboot_fail(phandle, "Access forbiden you need the certificate");
- return;
- }
-
- sz = get_file_size(data_fd);
-
- ptr = (char *) mmap(NULL, sz, PROT_READ,
- MAP_POPULATE | MAP_PRIVATE, data_fd, 0);
-
- hdr = (struct boot_img_hdr *) ptr;
-
- if (ptr == MAP_FAILED) {
- fastboot_fail(phandle, "internal fastbootd error");
- goto error;
- }
-
- if ((size_t) sz < sizeof(*hdr)) {
- fastboot_fail(phandle, "invalid bootimage header");
- goto error;
- }
-
- kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, hdr->page_size);
- ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, hdr->page_size);
- second_actual = ROUND_TO_PAGE(hdr->second_size, hdr->page_size);
-
- new_atags = (char *) create_atags((unsigned *) atags_ptr, atags_sz, hdr, &new_atags_sz);
-
- if (new_atags == NULL) {
- fastboot_fail(phandle, "atags generate error");
- goto error;
- }
- if (new_atags_sz > 0x4000) {
- fastboot_fail(phandle, "atags file to large");
- goto error;
- }
-
- if ((int) (hdr->page_size + kernel_actual + ramdisk_actual) < sz) {
- fastboot_fail(phandle, "incomplete bootimage");
- goto error;
- }
-
- kernel_ptr = (void *)((uintptr_t) ptr + hdr->page_size);
- ramdisk_ptr = (void *)((uintptr_t) kernel_ptr + kernel_actual);
- second_ptr = (void *)((uintptr_t) ramdisk_ptr + ramdisk_actual);
-
- D(INFO, "preparing to boot");
- // Prepares boot physical address. Addresses from header are ignored
- rv = prepare_boot_linux(hdr->kernel_addr, kernel_ptr, kernel_actual,
- hdr->ramdisk_addr, ramdisk_ptr, ramdisk_actual,
- hdr->second_addr, second_ptr, second_actual,
- hdr->tags_addr, new_atags, ROUND_TO_PAGE(new_atags_sz, hdr->page_size));
- if (rv < 0) {
- fastboot_fail(phandle, "kexec prepare failed");
- goto error;
- }
-
- fastboot_okay(phandle, "");
-
- free(atags_ptr);
- munmap(ptr, sz);
- free(new_atags);
- close(data_fd);
-
- D(INFO, "Kexec going to reboot");
- reboot(LINUX_REBOOT_CMD_KEXEC);
-
- fastboot_fail(phandle, "reboot error");
-
- return;
-
-error:
-
- if (atags_ptr != NULL)
- free(atags_ptr);
- if (ptr != NULL)
- munmap(ptr, sz);
-
-}
-
-static void cmd_erase(struct protocol_handle *phandle, const char *arg)
-{
- int partition_fd;
- char path[PATH_MAX];
- D(DEBUG, "cmd_erase %s\n", arg);
-
- if (flash_find_entry(arg, path, PATH_MAX)) {
- fastboot_fail(phandle, "partition table doesn't exist");
- return;
- }
-
- if (path == NULL) {
- fastboot_fail(phandle, "Couldn't find partition");
- return;
- }
-
- partition_fd = flash_get_partiton(path);
- if (partition_fd < 0) {
- fastboot_fail(phandle, "partiton file does not exists");
- }
-
- if (flash_erase(partition_fd)) {
- fastboot_fail(phandle, "failed to erase partition");
- flash_close(partition_fd);
- return;
- }
-
- if (flash_close(partition_fd) < 0) {
- D(ERR, "could not close device %s", strerror(errno));
- fastboot_fail(phandle, "failed to erase partition");
- return;
- }
- fastboot_okay(phandle, "");
-}
-
-static int GPT_header_location() {
- const char *location_str = fastboot_getvar("gpt_sector");
- char *str;
- int location;
-
- if (!strcmp("", location_str)) {
- D(INFO, "GPT location not specified using second sector");
- return 1;
- }
- else {
- location = strtoul(location_str, &str, 10);
- D(INFO, "GPT location specified as %d", location);
-
- if (*str != '\0')
- return -1;
-
- return location - 1;
- }
-}
-
-static void cmd_gpt_layout(struct protocol_handle *phandle, const char *arg) {
- struct GPT_entry_table *oldtable;
- int location;
- struct GPT_content content;
- const char *device;
- device = fastboot_getvar("blockdev");
-
- if (!strcmp(device, "")) {
- fastboot_fail(phandle, "blockdev not defined in config file");
- return;
- }
-
- //TODO: add same verification as in cmd_flash
- if (phandle->download_fd < 0) {
- fastboot_fail(phandle, "no layout file");
- return;
- }
-
- location = GPT_header_location();
- oldtable = GPT_get_device(device, location);
-
- GPT_default_content(&content, oldtable);
- if (oldtable == NULL)
- D(WARN, "Could not get old gpt table");
- else
- GPT_release_device(oldtable);
-
- if (!GPT_parse_file(phandle->download_fd, &content)) {
- fastboot_fail(phandle, "Could not parse partition config file");
- return;
- }
-
- if (trigger_gpt_layout(&content)) {
- fastboot_fail(phandle, "Vendor forbids this opperation");
- GPT_release_content(&content);
- return;
- }
-
- if (!GPT_write_content(device, &content)) {
- fastboot_fail(phandle, "Unable to write gpt file");
- GPT_release_content(&content);
- return;
- }
-
- GPT_release_content(&content);
- fastboot_okay(phandle, "");
-}
-
-static void cmd_flash(struct protocol_handle *phandle, const char *arg)
-{
- int partition;
- uint64_t sz;
- char data[BOOT_MAGIC_SIZE];
- char path[PATH_MAX];
- ssize_t header_sz = 0;
- int data_fd = 0;
-
- D(DEBUG, "cmd_flash %s\n", arg);
-
- if (try_handle_virtual_partition(phandle, arg)) {
- return;
- }
-
- if (phandle->download_fd < 0) {
- fastboot_fail(phandle, "no kernel file");
- return;
- }
-
- if (flash_find_entry(arg, path, PATH_MAX)) {
- fastboot_fail(phandle, "partition table doesn't exist");
- return;
- }
-
- if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) {
- fastboot_fail(phandle, "Access forbiden you need certificate");
- return;
- }
-
- // TODO: Maybe its goot idea to check whether the partition is bootable
- if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
- if (read_data_once(data_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) {
- fastboot_fail(phandle, "incoming data read error, cannot read boot header");
- return;
- }
- if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- fastboot_fail(phandle, "image is not a boot image");
- return;
- }
- }
-
- partition = flash_get_partiton(path);
-
- sz = get_file_size64(data_fd);
-
- sz -= header_sz;
-
- if (sz > get_file_size64(partition)) {
- flash_close(partition);
- D(WARN, "size of file too large");
- fastboot_fail(phandle, "size of file too large");
- return;
- }
-
- D(INFO, "writing %"PRId64" bytes to '%s'\n", sz, arg);
-
- if (flash_write(partition, phandle->download_fd, sz, header_sz)) {
- fastboot_fail(phandle, "flash write failure");
- return;
- }
- D(INFO, "partition '%s' updated\n", arg);
-
- flash_close(partition);
- close(data_fd);
-
- fastboot_okay(phandle, "");
-}
-
-static void cmd_continue(struct protocol_handle *phandle, const char *arg)
-{
- fastboot_okay(phandle, "");
-#if 0
- udc_stop();
-
- boot_linux_from_flash();
-#endif
-}
-
-static void cmd_getvar(struct protocol_handle *phandle, const char *arg)
-{
- const char *value;
- D(DEBUG, "cmd_getvar %s\n", arg);
-
- value = fastboot_getvar(arg);
-
- fastboot_okay(phandle, value);
-}
-
-static void cmd_download(struct protocol_handle *phandle, const char *arg)
-{
- unsigned len = strtoul(arg, NULL, 16);
- int old_fd;
-
- if (len > 256 * 1024 * 1024) {
- fastboot_fail(phandle, "data too large");
- return;
- }
-
- fastboot_data(phandle, len);
-
- old_fd = protocol_get_download(phandle);
- if (old_fd >= 0) {
- off_t len = lseek(old_fd, 0, SEEK_END);
- D(INFO, "disposing of unused fd %d, size %ld", old_fd, len);
- close(old_fd);
- }
-
- phandle->download_fd = protocol_handle_download(phandle, len);
- if (phandle->download_fd < 0) {
- fastboot_fail(phandle, "download failed");
- return;
- }
-
- fastboot_okay(phandle, "");
-}
-
-static void cmd_oem(struct protocol_handle *phandle, const char *arg) {
- const char *response = "";
-
- //TODO: Maybe it should get download descriptor also
- if (trigger_oem_cmd(arg, &response))
- fastboot_fail(phandle, response);
- else
- fastboot_okay(phandle, response);
-}
-
-void commands_init()
-{
- virtual_partition_register("partition-table", cmd_gpt_layout);
-
- fastboot_register("boot", cmd_boot);
- fastboot_register("erase:", cmd_erase);
- fastboot_register("flash:", cmd_flash);
- fastboot_register("continue", cmd_continue);
- fastboot_register("getvar:", cmd_getvar);
- fastboot_register("download:", cmd_download);
- fastboot_register("oem", cmd_oem);
- //fastboot_publish("version", "0.5");
- //fastboot_publish("product", "swordfish");
- //fastboot_publish("kernel", "lk");
-}
diff --git a/fastbootd/commands/boot.c b/fastbootd/commands/boot.c
deleted file mode 100644
index 922914b..0000000
--- a/fastbootd/commands/boot.c
+++ /dev/null
@@ -1,254 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/syscall.h>
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "boot.h"
-#include "debug.h"
-#include "utils.h"
-#include "bootimg.h"
-
-
-#define KEXEC_ARM_ATAGS_OFFSET 0x1000
-#define KEXEC_ARM_ZIMAGE_OFFSET 0x8000
-
-#define MEMORY_SIZE 0x0800000
-#define START_ADDRESS 0x44000000
-#define KERNEL_START (START_ADDRESS + KEXEC_ARM_ZIMAGE_OFFSET)
-
-#define ATAG_NONE_TYPE 0x00000000
-#define ATAG_CORE_TYPE 0x54410001
-#define ATAG_RAMDISK_TYPE 0x54410004
-#define ATAG_INITRD2_TYPE 0x54420005
-#define ATAG_CMDLINE_TYPE 0x54410009
-
-#define MAX_ATAG_SIZE 0x4000
-
-struct atag_info {
- unsigned size;
- unsigned type;
-};
-
-struct atag_initrd2 {
- unsigned start;
- unsigned size;
-};
-
-struct atag_cmdline {
- char cmdline[0];
-};
-
-struct atag {
- struct atag_info info;
- union {
- struct atag_initrd2 initrd2;
- struct atag_cmdline cmdline;
- } data;
-};
-
-
-long kexec_load(unsigned int entry, unsigned long nr_segments,
- struct kexec_segment *segment, unsigned long flags) {
- return syscall(__NR_kexec_load, entry, nr_segments, segment, flags);
-}
-
-/*
- * Prepares arguments for kexec
- * Kernel address is not set into kernel_phys
- * Ramdisk is set to position relative to kernel
- */
-int prepare_boot_linux(uintptr_t kernel_phys, void *kernel_addr, int kernel_size,
- uintptr_t ramdisk_phys, void *ramdisk_addr, int ramdisk_size,
- uintptr_t second_phys, void *second_addr, int second_size,
- uintptr_t atags_phys, void *atags_addr, int atags_size) {
- struct kexec_segment segment[4];
- int segment_count = 2;
- unsigned entry = START_ADDRESS + KEXEC_ARM_ZIMAGE_OFFSET;
- int rv;
- int page_size = getpagesize();
-
- segment[0].buf = kernel_addr;
- segment[0].bufsz = kernel_size;
- segment[0].mem = (void *) KERNEL_START;
- segment[0].memsz = ROUND_TO_PAGE(kernel_size, page_size);
-
- if (kernel_size > MEMORY_SIZE - KEXEC_ARM_ZIMAGE_OFFSET) {
- D(INFO, "Kernel image too big");
- return -1;
- }
-
- segment[1].buf = atags_addr;
- segment[1].bufsz = atags_size;
- segment[1].mem = (void *) (START_ADDRESS + KEXEC_ARM_ATAGS_OFFSET);
- segment[1].memsz = ROUND_TO_PAGE(atags_size, page_size);
-
- D(INFO, "Ramdisk size is %d", ramdisk_size);
-
- if (ramdisk_size != 0) {
- segment[segment_count].buf = ramdisk_addr;
- segment[segment_count].bufsz = ramdisk_size;
- segment[segment_count].mem = (void *) (KERNEL_START + ramdisk_phys - kernel_phys);
- segment[segment_count].memsz = ROUND_TO_PAGE(ramdisk_phys, page_size);
- ++segment_count;
- }
-
- D(INFO, "Ramdisk size is %d", ramdisk_size);
- if (second_size != 0) {
- segment[segment_count].buf = second_addr;
- segment[segment_count].bufsz = second_size;
- segment[segment_count].mem = (void *) (KERNEL_START + second_phys - kernel_phys);
- segment[segment_count].memsz = ROUND_TO_PAGE(second_size, page_size);
- entry = second_phys;
- ++segment_count;
- }
-
- rv = kexec_load(entry, segment_count, segment, KEXEC_ARCH_DEFAULT);
-
- if (rv != 0) {
- D(INFO, "Kexec_load returned non-zero exit code: %s\n", strerror(errno));
- return -1;
- }
-
- return 1;
-
-}
-
-unsigned *create_atags(unsigned *atags_position, int atag_size, const struct boot_img_hdr *hdr, int *size) {
- struct atag *current_tag = (struct atag *) atags_position;
- unsigned *current_tag_raw = atags_position;
- unsigned *new_atags = malloc(ROUND_TO_PAGE(atag_size + BOOT_ARGS_SIZE * sizeof(char),
- hdr->page_size));
- //This pointer will point into the beggining of buffer free space
- unsigned *natags_raw_buff = new_atags;
- int new_atags_size = 0;
- int current_size;
- int cmdl_length;
-
- // copy tags from current atag file
- while (current_tag->info.type != ATAG_NONE_TYPE) {
- switch (current_tag->info.type) {
- case ATAG_CMDLINE_TYPE:
- case ATAG_RAMDISK_TYPE:
- case ATAG_INITRD2_TYPE: break;
- default:
- memcpy((void *)natags_raw_buff, (void *)current_tag_raw, current_tag->info.size * sizeof(unsigned));
- natags_raw_buff += current_tag->info.size;
- new_atags_size += current_tag->info.size;
- }
-
- current_tag_raw += current_tag->info.size;
- current_tag = (struct atag *) current_tag_raw;
-
- if (current_tag_raw >= atags_position + atag_size) {
- D(ERR, "Critical error in atags");
- return NULL;
- }
- }
-
- // set INITRD2 tag
- if (hdr->ramdisk_size > 0) {
- current_size = (sizeof(struct atag_info) + sizeof(struct atag_initrd2)) / sizeof(unsigned);
- *((struct atag *) natags_raw_buff) = (struct atag) {
- .info = {
- .size = current_size,
- .type = ATAG_INITRD2_TYPE
- },
- .data = {
- .initrd2 = (struct atag_initrd2) {
- .start = hdr->ramdisk_addr,
- .size = hdr->ramdisk_size
- }
- }
- };
-
- new_atags_size += current_size;
- natags_raw_buff += current_size;
- }
-
- // set ATAG_CMDLINE
- cmdl_length = strnlen((char *) hdr->cmdline, BOOT_ARGS_SIZE - 1);
- current_size = sizeof(struct atag_info) + (1 + cmdl_length);
- current_size = (current_size + sizeof(unsigned) - 1) / sizeof(unsigned);
- *((struct atag *) natags_raw_buff) = (struct atag) {
- .info = {
- .size = current_size,
- .type = ATAG_CMDLINE_TYPE
- },
- };
-
- //copy cmdline and ensure that there is null character
- memcpy(((struct atag *) natags_raw_buff)->data.cmdline.cmdline,
- (char *) hdr->cmdline, cmdl_length);
- ((struct atag *) natags_raw_buff)->data.cmdline.cmdline[cmdl_length] = '\0';
-
- new_atags_size += current_size;
- natags_raw_buff += current_size;
-
- // set ATAG_NONE
- *((struct atag *) natags_raw_buff) = (struct atag) {
- .info = {
- .size = 0,
- .type = ATAG_NONE_TYPE
- },
- };
- new_atags_size += sizeof(struct atag_info) / sizeof(unsigned);
- natags_raw_buff += sizeof(struct atag_info) / sizeof(unsigned);
-
- *size = new_atags_size * sizeof(unsigned);
- return new_atags;
-}
-
-char *read_atags(const char * path, int *atags_sz) {
- int afd = -1;
- char *atags_ptr = NULL;
-
- afd = open(path, O_RDONLY);
- if (afd < 0) {
- D(ERR, "wrong atags file");
- return 0;
- }
-
- atags_ptr = (char *) malloc(MAX_ATAG_SIZE);
- if (atags_ptr == NULL) {
- D(ERR, "insufficient memory");
- return 0;
- }
-
- *atags_sz = read(afd, atags_ptr, MAX_ATAG_SIZE);
-
- close(afd);
- return atags_ptr;
-}
-
diff --git a/fastbootd/commands/boot.h b/fastbootd/commands/boot.h
deleted file mode 100644
index a5efd01..0000000
--- a/fastbootd/commands/boot.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef __FASTBOOT_BOOT_H
-#define __FASTBOOT_BOOT_H
-
-#include <sys/cdefs.h>
-#include <linux/kexec.h>
-
-#include "bootimg.h"
-
-#define KEXEC_TYPE_DEFAULT 0
-#define KEXEC_TYPE_CRASH 1
-
-int prepare_boot_linux(uintptr_t, void *, int, uintptr_t, void *, int,
- uintptr_t, void *, int, uintptr_t, void *, int);
-unsigned *create_atags(unsigned *, int, const struct boot_img_hdr *, int *);
-long kexec_load(unsigned int, unsigned long, struct kexec_segment *, unsigned long);
-char *read_atags(const char *, int *);
-
-#endif /* _SYS_KEXEC_H */
-
diff --git a/fastbootd/commands/flash.c b/fastbootd/commands/flash.c
deleted file mode 100644
index 1eb4d1b..0000000
--- a/fastbootd/commands/flash.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <sys/mman.h>
-
-#include "flash.h"
-#include "protocol.h"
-#include "debug.h"
-#include "utils.h"
-#include "commands/partitions.h"
-
-#ifdef FLASH_CERT
-#include "secure.h"
-#endif
-
-#define ALLOWED_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-."
-#define BUFFER_SIZE 1024 * 1024
-#define MIN(a, b) (a > b ? b : a)
-
-
-int flash_find_entry(const char *name, char *out, size_t outlen)
-{
-//TODO: Assumption: All the partitions has they unique name
-
- const char *path = fastboot_getvar("device-directory");
- size_t length;
- if (strcmp(path, "") == 0) {
- D(ERR, "device-directory: not defined in config file");
- return -1;
- }
-
- length = strspn(name, ALLOWED_CHARS);
- if (length != strlen(name)) {
- D(ERR, "Not allowed char in name: %c", name[length]);
- return -1;
- }
-
- if (snprintf(out, outlen, "%s%s", path, name) >= (int) outlen) {
- D(ERR, "Too long path to partition file");
- return -1;
- }
-
- if (access(out, F_OK ) == -1) {
- D(ERR, "could not find partition file %s", name);
- return -1;
- }
-
- return 0;
-}
-
-int flash_erase(int fd)
-{
- int64_t size;
- size = get_block_device_size(fd);
- D(DEBUG, "erase %"PRId64" data from %d\n", size, fd);
-
- return wipe_block_device(fd, size);
-}
-
-int flash_write(int partition_fd, int data_fd, ssize_t size, ssize_t skip)
-{
- ssize_t written = 0;
- struct GPT_mapping input;
- struct GPT_mapping output;
-
- while (written < size) {
- int current_size = MIN(size - written, BUFFER_SIZE);
-
- if (gpt_mmap(&input, written + skip, current_size, data_fd)) {
- D(ERR, "Error in writing data, unable to map data file %zd at %zd size %d", size, skip, current_size);
- return -1;
- }
- if (gpt_mmap(&output, written, current_size, partition_fd)) {
- D(ERR, "Error in writing data, unable to map output partition");
- return -1;
- }
-
- memcpy(output.ptr, input.ptr, current_size);
-
- gpt_unmap(&input);
- gpt_unmap(&output);
-
- written += current_size;
- }
-
- return 0;
-}
-
-#ifdef FLASH_CERT
-
-int flash_validate_certificate(int signed_fd, int *data_fd) {
- int ret = 0;
- const char *cert_path;
- X509_STORE *store = NULL;
- CMS_ContentInfo *content_info;
- BIO *content;
-
- cert_path = fastboot_getvar("certificate-path");
- if (!strcmp(cert_path, "")) {
- D(ERR, "could not find cert-key value in config file");
- goto finish;
- }
-
- store = cert_store_from_path(cert_path);
- if (store == NULL) {
- D(ERR, "unable to create certification store");
- goto finish;
- }
-
- if (cert_read(signed_fd, &content_info, &content)) {
- D(ERR, "reading data failed");
- goto finish;
- }
-
- ret = cert_verify(content, content_info, store, data_fd);
- cert_release(content, content_info);
-
- return ret;
-
-finish:
- if (store != NULL)
- cert_release_store(store);
-
- return ret;
-}
-
-#else
-int flash_validate_certificate(int signed_fd, int *data_fd) {
- return 1;
-}
-#endif
diff --git a/fastbootd/commands/flash.h b/fastbootd/commands/flash.h
deleted file mode 100644
index 5a64cab..0000000
--- a/fastbootd/commands/flash.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOTD_ERASE_H
-#define _FASTBOOTD_ERASE_H
-
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include "debug.h"
-
-int flash_find_entry(const char *, char *, size_t);
-int flash_erase(int fd);
-
-static inline int flash_get_partiton(const char *path) {
- return open(path, O_RDWR);
-}
-
-static inline int flash_close(int fd) {
- return close(fd);
-}
-
-int flash_write(int partition, int data, ssize_t size, ssize_t skip);
-
-static inline ssize_t read_data_once(int fd, char *buffer, ssize_t size) {
- ssize_t readcount = 0;
- ssize_t len;
-
- while ((len = TEMP_FAILURE_RETRY(read(fd, (void *) &buffer[readcount], size - readcount))) > 0) {
- readcount += len;
- }
- if (len < 0) {
- D(ERR, "Read error:%s", strerror(errno));
- return len;
- }
-
- return readcount;
-}
-
-int flash_validate_certificate(int signed_fd, int *data_fd);
-
-#endif
-
diff --git a/fastbootd/commands/partitions.c b/fastbootd/commands/partitions.c
deleted file mode 100644
index 74232e6..0000000
--- a/fastbootd/commands/partitions.c
+++ /dev/null
@@ -1,772 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <endian.h>
-#include <zlib.h>
-#include <linux/hdreg.h>
-#include <sys/ioctl.h>
-#include <stdlib.h>
-#include <cutils/config_utils.h>
-#include <inttypes.h>
-
-#include "partitions.h"
-#include "debug.h"
-#include "utils.h"
-#include "protocol.h"
-
-#define BLKRRPART _IO(0x12,95) /* re-read partition table */
-#define BLKSSZGET _IO(0x12,104)
-
-#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
-#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
-#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))
-
-
-const uint8_t partition_type_uuid[16] = {
- 0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
- 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7,
-};
-
-//TODO: There is assumption that we are using little endian
-
-static void GPT_entry_clear(struct GPT_entry_raw *entry)
-{
- memset(entry, 0, sizeof(*entry));
-}
-
-/*
- * returns mapped location to choosen area
- * mapped_ptr is pointer to whole area mapped (it can be bigger then requested)
- */
-int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd)
-{
- unsigned int location_diff = location & ~PAGE_MASK;
-
- mapping->size = ALIGN(size + location_diff, PAGE_SIZE);
-
- uint64_t sz = get_file_size64(fd);
- if (sz < size + location) {
- D(ERR, "the location of mapping area is outside of the device size %" PRId64, sz);
- return 1;
- }
- location = ALIGN_DOWN(location, PAGE_SIZE);
-
- mapping->map_ptr = mmap64(NULL, mapping->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, location);
-
- if (mapping->map_ptr == MAP_FAILED) {
- mapping->ptr = MAP_FAILED;
- D(ERR, "map failed: %s", strerror(errno));
- return 1;
- }
-
- mapping->ptr = (void *)((char *) mapping->map_ptr + location_diff);
- return 0;
-}
-
-void gpt_unmap(struct GPT_mapping *mapping) {
- munmap(mapping->map_ptr, mapping->size);
-}
-
-
-#define LBA_ADDR(table, value) ((uint64_t) (table)->sector_size * (value))
-
-int GPT_map_from_content(struct GPT_entry_table *table, const struct GPT_content *content)
-{
-
- // Mapping header
- if (gpt_mmap(&table->header_map, LBA_ADDR(table, content->header.current_lba),
- table->sector_size, table->fd)) {
- D(ERR, "unable to map header:%s\n", strerror(errno));
- goto error_header;
- }
-
- table->header = (struct GPT_header *) table->header_map.ptr;
-
- table->partition_table_size = ROUND_UP(content->header.entries_count * sizeof(*table->entries),
- table->sector_size);
-
- // Mapping entry table
- if (gpt_mmap(&table->entries_map, LBA_ADDR(table, content->header.entries_lba),
- table->partition_table_size, table->fd)) {
- D(ERR, "unable to map entries");
- goto error_signature;
- }
-
- table->entries = (struct GPT_entry_raw *) table->entries_map.ptr;
-
- // Mapping secondary header
- if (gpt_mmap(&table->sec_header_map, LBA_ADDR(table, content->header.backup_lba),
- table->sector_size, table->fd)) {
- D(ERR, "unable to map backup gpt header");
- goto error_sec_header;
- }
-
- // Mapping secondary entries table
- if (gpt_mmap(&table->sec_entries_map,
- LBA_ADDR(table, content->header.backup_lba) - table->partition_table_size,
- table->partition_table_size, table->fd)) {
- D(ERR, "unable to map secondary gpt table");
- goto error_sec_entries;
- }
-
- table->second_header = (struct GPT_header *) table->sec_header_map.ptr;
- table->second_entries = (struct GPT_entry_raw *) table->sec_entries_map.ptr;
- table->second_valid = strcmp("EFI PART", (char *) table->second_header->signature) == 0;
-
- return 0;
-
-error_sec_entries:
- gpt_unmap(&table->sec_header_map);
-error_sec_header:
- gpt_unmap(&table->entries_map);
-error_signature:
- gpt_unmap(&table->header_map);
-error_header:
- return 1;
-}
-
-int GPT_map(struct GPT_entry_table *table, unsigned header_lba)
-{
- struct GPT_content content;
- struct GPT_mapping mapping;
- struct GPT_header *header;
-
- if (gpt_mmap(&mapping, LBA_ADDR(table, header_lba), table->sector_size, table->fd)) {
- D(ERR, "unable to map header: %s", strerror(errno));
- goto error_header;
- }
-
- header = (struct GPT_header *) mapping.ptr;
-
- if (strcmp("EFI PART", (char *) header->signature)) {
- D(ERR, "GPT entry not valid");
- goto error_signature;
- }
-
- content.header = *header;
-
- gpt_unmap(&mapping);
-
- return GPT_map_from_content(table, &content);
-
-error_signature:
- gpt_unmap(&table->header_map);
-error_header:
- return 1;
-}
-
-struct GPT_entry_table* GPT_get_device(const char *path, unsigned header_lba)
-{
- struct GPT_entry_table *table;
- size_t sector_bytes;
-
- table = (struct GPT_entry_table *) malloc(sizeof(*table));
- table->fd = open(path, O_RDWR);
-
- if (table->fd < 0) {
- D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
- return NULL;
- }
-
- if (!ioctl(table->fd, BLKSSZGET, §or_bytes)) {
- table->sector_size = (unsigned) sector_bytes;
- D(INFO, "Got sector size %d", table->sector_size);
- } else {
- D(WARN, "unable to get sector size, assuming 512");
- table->sector_size = 512;
- }
-
- if (GPT_map(table, header_lba)) {
- D(ERR, "Could not map gpt");
- return NULL;
- }
-
- return table;
-}
-
-static struct GPT_entry_table* GPT_get_from_content(const char *path, const struct GPT_content *content)
-{
- struct GPT_entry_table *table;
- size_t sector_bytes;
-
- table = (struct GPT_entry_table *) malloc(sizeof(*table));
- table->fd = open(path, O_RDWR);
-
- if (table->fd < 0) {
- D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
- return NULL;
- }
-
- if (!ioctl(table->fd, BLKSSZGET, §or_bytes)) {
- table->sector_size = (unsigned) sector_bytes;
- D(INFO, "Got sector size %d", table->sector_size);
- } else {
- D(WARN, "unable to get sector size %s, assuming 512", strerror(errno));
- table->sector_size = 512;
- }
-
- if (GPT_map_from_content(table, content)) {
- D(ERR, "Could not map gpt");
- return NULL;
- }
-
- return table;
-}
-
-
-void GPT_release_device(struct GPT_entry_table *table)
-{
- gpt_unmap(&table->header_map);
- gpt_unmap(&table->entries_map);
- gpt_unmap(&table->sec_header_map);
- gpt_unmap(&table->sec_entries_map);
- close(table->fd);
- free(table);
-}
-
-static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-static int GPT_check_overlap_except(struct GPT_entry_table *table,
- struct GPT_entry_raw *entry,
- struct GPT_entry_raw *exclude);
-
-void GPT_edit_entry(struct GPT_entry_table *table,
- struct GPT_entry_raw *old_entry,
- struct GPT_entry_raw *new_entry)
-{
- struct GPT_entry_raw *current_entry = GPT_get_pointer(table, old_entry);
-
- if (GPT_check_overlap_except(table, new_entry, current_entry)) {
- D(ERR, "Couldn't add overlaping partition");
- return;
- }
-
- if (current_entry == NULL) {
- D(ERR, "Couldn't find entry");
- return;
- }
-
- *current_entry = *new_entry;
-}
-
-int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
- struct GPT_entry_raw *raw = GPT_get_pointer(table, entry);
-
- if (raw == NULL) {
- D(ERR, "could not find entry");
- return 1;
- }
- D(DEBUG, "Deleting gpt entry '%s'\n", raw->partition_guid);
-
- // Entry in the middle of table may become empty
- GPT_entry_clear(raw);
-
- return 0;
-}
-
-void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
- unsigned i;
- int inserted = 0;
- if (GPT_check_overlap(table, entry)) {
- D(ERR, "Couldn't add overlaping partition");
- return;
- }
-
- if (GPT_get_pointer(table, entry) != NULL) {
- D(WARN, "Add entry fault, this entry already exists");
- return;
- }
-
- struct GPT_entry_raw *entries = table->entries;
-
- for (i = 0; i < table->header->entries_count; ++i) {
- if (!entries[i].type_guid[0]) {
- inserted = 1;
- D(DEBUG, "inserting");
- memcpy(&entries[i], entry, sizeof(entries[i]));
- break;
- }
- }
-
- if (!inserted) {
- D(ERR, "Unable to find empty partion entry");
- }
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name);
-
-struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
- if (entry->partition_guid[0] != 0)
- return GPT_get_pointer_by_guid(table, (const char *) entry->partition_guid);
- else if (entry->name[0] != 0)
- return GPT_get_pointer_by_UTFname(table, entry->name);
-
- D(WARN, "Name or guid needed to find entry");
- return NULL;
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *table, const char *name)
-{
- int current = (int) table->header->entries_count;
-
- for (current = current - 1; current >= 0; --current) {
- if (strncmp((char *) name,
- (char *) table->entries[current].partition_guid, 16) == 0) {
- return &table->entries[current];
- }
- }
-
- return NULL;
-}
-
-int strncmp_UTF16_char(const uint16_t *s1, const char *s2, size_t n)
-{
- if (n == 0)
- return (0);
- do {
- if (((*s1) & 127) != *s2++)
- return (((unsigned char) ((*s1) & 127)) - *(unsigned char *)--s2);
- if (*s1++ == 0)
- break;
- } while (--n != 0);
- return (0);
-}
-
-int strncmp_UTF16(const uint16_t *s1, const uint16_t *s2, size_t n)
-{
- if (n == 0)
- return (0);
- do {
- if ((*s1) != *s2++)
- return (*s1 - *--s2);
- if (*s1++ == 0)
- break;
- } while (--n != 0);
- return (0);
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *table, const char *name)
-{
- int count = (int) table->header->entries_count;
- int current;
-
- for (current = 0; current < count; ++current) {
- if (strncmp_UTF16_char(table->entries[current].name,
- (char *) name, 16) == 0) {
- return &table->entries[current];
- }
- }
-
- return NULL;
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name)
-{
- int count = (int) table->header->entries_count;
- int current;
-
- for (current = 0; current < count; ++current) {
- if (strncmp_UTF16(table->entries[current].name,
- name, GPT_NAMELEN) == 0) {
- return &table->entries[current];
- }
- }
-
- return NULL;
-}
-
-void GPT_sync(struct GPT_entry_table *table)
-{
- uint32_t crc;
-
- //calculate crc32
- crc = crc32(0, Z_NULL, 0);
- crc = crc32(crc, (void*) table->entries, table->header->entries_count * sizeof(*table->entries));
- table->header->partition_array_checksum = crc;
-
- table->header->header_checksum = 0;
- crc = crc32(0, Z_NULL, 0);
- crc = crc32(crc, (void*) table->header, table->header->header_size);
- table->header->header_checksum = crc;
-
- //sync secondary partion
- if (table->second_valid) {
- memcpy((void *)table->second_entries, (void *) table->entries, table->partition_table_size);
- memcpy((void *)table->second_header, (void *)table->header, sizeof(*table->header));
- }
-
- if(!ioctl(table->fd, BLKRRPART, NULL)) {
- D(WARN, "Unable to force kernel to refresh partition table");
- }
-}
-
-void GPT_to_UTF16(uint16_t *to, const char *from, int n)
-{
- int i;
- for (i = 0; i < (n - 1) && (to[i] = from[i]) != '\0'; ++i);
- to[i] = '\0';
-}
-
-void GPT_from_UTF16(char *to, const uint16_t *from, int n)
-{
- int i;
- for (i = 0; i < (n - 1) && (to[i] = from[i] & 127) != '\0'; ++i);
- to[i] = '\0';
-}
-
-static int GPT_check_overlap_except(struct GPT_entry_table *table,
- struct GPT_entry_raw *entry,
- struct GPT_entry_raw *exclude) {
- int current = (int) table->header->entries_count;
- int dontcheck;
- struct GPT_entry_raw *current_entry;
- if (entry->last_lba < entry->first_lba) {
- D(WARN, "Start address have to be less than end address");
- return 1;
- }
-
- for (current = current - 1; current >= 0; --current) {
- current_entry = &table->entries[current];
- dontcheck = strncmp((char *) entry->partition_guid,
- (char *) current_entry->partition_guid , 16) == 0;
- dontcheck |= current_entry->type_guid[0] == 0;
- dontcheck |= current_entry == exclude;
-
- if (!dontcheck && ((entry->last_lba >= current_entry->first_lba &&
- entry->first_lba < current_entry->last_lba ))) {
- return 1;
- }
- }
-
- return 0;
-}
-
-static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
- return GPT_check_overlap_except(table, entry, NULL);
-}
-
-static char *get_key_value(char *ptr, char **key, char **value)
-{
- *key = ptr;
- ptr = strchr(ptr, '=');
-
- if (ptr == NULL)
- return NULL;
-
- *ptr++ = '\0';
- *value = ptr;
- ptr = strchr(ptr, ';');
-
- if (ptr == NULL)
- ptr = *value + strlen(*value);
- else
- *ptr = '\0';
-
- *key = strip(*key);
- *value = strip(*value);
-
- return ptr;
-}
-
-//TODO: little endian?
-static int add_key_value(const char *key, const char *value, struct GPT_entry_raw *entry)
-{
- char *endptr;
- if (!strcmp(key, "type")) {
- strncpy((char *) entry->type_guid, value, 16);
- entry->type_guid[15] = 0;
- }
- else if (!strcmp(key, "guid")) {
- strncpy((char *) entry->partition_guid, value, 16);
- entry->type_guid[15] = 0;
- }
- else if (!strcmp(key, "firstlba")) {
- entry->first_lba = strtoul(value, &endptr, 10);
- if (*endptr != '\0') goto error;
- }
- else if (!strcmp(key, "lastlba")) {
- entry->last_lba = strtoul(value, &endptr, 10);
- if (*endptr != '\0') goto error;
- }
- else if (!strcmp(key, "flags")) {
- entry->flags = strtoul(value, &endptr, 16);
- if (*endptr != '\0') goto error;
- }
- else if (!strcmp(key, "name")) {
- GPT_to_UTF16(entry->name, value, GPT_NAMELEN);
- }
- else {
- goto error;
- }
-
- return 0;
-
-error:
- D(ERR, "Could not find key or parse value: %s,%s", key, value);
- return 1;
-}
-
-int GPT_parse_entry(char *string, struct GPT_entry_raw *entry)
-{
- char *ptr = string;
- char *key, *value;
-
- while ((ptr = get_key_value(ptr, &key, &value)) != NULL) {
- if (add_key_value(key, value, entry)) {
- D(WARN, "key or value not valid: %s %s", key, value);
- return 1;
- }
- }
-
- return 0;
-}
-
-void entry_set_guid(int n, uint8_t *guid)
-{
- int fd;
- fd = open("/dev/urandom", O_RDONLY);
- read(fd, guid, 16);
- close(fd);
-
- //rfc4122
- guid[8] = (guid[8] & 0x3F) | 0x80;
- guid[7] = (guid[7] & 0x0F) | 0x40;
-}
-
-void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table)
-{
- if (table != NULL) {
- memcpy(&content->header, table->header, sizeof(content->header));
- content->header.header_size = sizeof(content->header);
- content->header.entry_size = sizeof(struct GPT_entry_raw);
- }
- else {
- D(WARN, "Could not locate old gpt table, using default values");
- memset(&content->header, 0, sizeof(content->header) / sizeof(int));
- content->header = (struct GPT_header) {
- .revision = 0x10000,
- .header_size = sizeof(content->header),
- .header_checksum = 0,
- .reserved_zeros = 0,
- .current_lba = 1,
- .backup_lba = 1,
- .entry_size = sizeof(struct GPT_entry_raw),
- .partition_array_checksum = 0
- };
- strncpy((char *)content->header.signature, "EFI PART", 8);
- strncpy((char *)content->header.disk_guid, "ANDROID MMC DISK", 16);
- }
-}
-
-static int get_config_uint64(cnode *node, uint64_t *ptr, const char *name)
-{
- const char *tmp;
- uint64_t val;
- char *endptr;
- if ((tmp = config_str(node, name, NULL))) {
- val = strtoull(tmp, &endptr, 10);
- if (*endptr != '\0') {
- D(WARN, "Value for %s is not a number: %s", name, tmp);
- return 1;
- }
- *ptr = val;
- return 0;
- }
- return 1;
-}
-
-static int get_config_string(cnode *node, char *ptr, int max_len, const char *name)
-{
- size_t begin, end;
- const char *value = config_str(node, name, NULL);
- if (!value)
- return -1;
-
- begin = strcspn(value, "\"") + 1;
- end = strcspn(&value[begin], "\"");
-
- if ((int) end > max_len) {
- D(WARN, "Identifier \"%s\" too long", value);
- return -1;
- }
-
- strncpy(ptr, &value[begin], end);
- if((int) end < max_len)
- ptr[end] = 0;
- return 0;
-}
-
-static void GPT_parse_header(cnode *node, struct GPT_content *content)
-{
- get_config_uint64(node, &content->header.current_lba, "header_lba");
- get_config_uint64(node, &content->header.backup_lba, "backup_lba");
- get_config_uint64(node, &content->header.first_usable_lba, "first_lba");
- get_config_uint64(node, &content->header.last_usable_lba, "last_lba");
- get_config_uint64(node, &content->header.entries_lba, "entries_lba");
- get_config_string(node, (char *) content->header.disk_guid, 16, "guid");
-}
-
-static int GPT_parse_partitions(cnode *node, struct GPT_content *content)
-{
- cnode *current;
- int i;
- uint64_t partition_size;
- struct GPT_entry_raw *entry;
- for (i = 0, current = node->first_child; current; current = current->next, ++i) {
- entry = &content->entries[i];
- entry_set_guid(i, content->entries[i].partition_guid);
- memcpy(&content->entries[i].type_guid, partition_type_uuid, 16);
- if (get_config_uint64(current, &entry->first_lba, "first_lba")) {
- D(ERR, "first_lba not specified");
- return 1;
- }
- if (get_config_uint64(current, &partition_size, "partition_size")) {
- D(ERR, "partition_size not specified");
- return 1;
- }
- if (config_str(current, "system", NULL)) {
- entry->flags |= GPT_FLAG_SYSTEM;
- }
- if (config_str(current, "bootable", NULL)) {
- entry->flags |= GPT_FLAG_BOOTABLE;
- }
- if (config_str(current, "readonly", NULL)) {
- entry->flags |= GPT_FLAG_READONLY;
- }
- if (config_str(current, "automount", NULL)) {
- entry->flags |= GPT_FLAG_DOAUTOMOUNT;
- }
-
- get_config_uint64(current, &content->entries[i].flags, "flags");
- content->entries[i].last_lba = content->entries[i].first_lba + partition_size - 1;
- GPT_to_UTF16(content->entries[i].name, current->name, 16);
- }
- return 0;
-}
-
-static inline int cnode_count(cnode *node)
-{
- int i;
- cnode *current;
- for (i = 0, current = node->first_child; current; current = current->next, ++i)
- ;
- return i;
-}
-
-
-static int GPT_parse_cnode(cnode *root, struct GPT_content *content)
-{
- cnode *partnode;
-
- if (!(partnode = config_find(root, "partitions"))) {
- D(ERR, "Could not find partition table");
- return 0;
- }
-
- GPT_parse_header(root, content);
-
- content->header.entries_count = cnode_count(partnode);
- content->entries = malloc(content->header.entries_count * sizeof(struct GPT_entry_raw));
-
- if (GPT_parse_partitions(partnode, content)) {
- D(ERR, "Could not parse partitions");
- return 0;
- }
-
- return 1;
-}
-
-int GPT_parse_file(int fd, struct GPT_content *content)
-{
- char *data;
- int size;
- int ret;
- cnode *root = config_node("", "");
-
- size = get_file_size(fd);
- data = (char *) mmap(NULL, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-
- if (data == NULL) {
- if (size == 0)
- D(ERR, "config file empty");
- else
- D(ERR, "Out of memory");
- return 0;
- }
-
- data[size - 1] = 0;
- config_load(root, data);
-
- if (root->first_child == NULL) {
- D(ERR, "Could not read config file");
- return 0;
- }
-
- ret = GPT_parse_cnode(root, content);
- munmap(data, size);
- return ret;
-}
-
-void GPT_release_content(struct GPT_content *content)
-{
- free(content->entries);
-}
-
-int GPT_write_content(const char *device, struct GPT_content *content)
-{
- struct GPT_entry_table *maptable;
-
- maptable = GPT_get_from_content(device, content);
- if (maptable == NULL) {
- D(ERR, "could not map device");
- return 0;
- }
-
- memcpy(maptable->header, &content->header, sizeof(*maptable->header));
- memcpy(maptable->entries, content->entries,
- content->header.entries_count * sizeof(*maptable->entries));
-
- GPT_sync(maptable);
- GPT_release_device(maptable);
-
- return 1;
-}
-
diff --git a/fastbootd/commands/partitions.h b/fastbootd/commands/partitions.h
deleted file mode 100644
index 9a6a88d..0000000
--- a/fastbootd/commands/partitions.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-
-#ifndef __FASTBOOTD_PATITIONS_
-#define __FASTBOOTD_PATITIONS_
-
-#include <stdint.h>
-
-#define GPT_ENTRIES 128
-#define GPT_NAMELEN 36
-
-#define GPT_FLAG_SYSTEM (1ULL << 0)
-#define GPT_FLAG_BOOTABLE (1ULL << 2)
-#define GPT_FLAG_READONLY (1ULL << 60)
-#define GPT_FLAG_DOAUTOMOUNT (1ULL << 63)
-
-// it should be passed in little endian order
-struct GPT_entry_raw {
- uint8_t type_guid[16];
- uint8_t partition_guid[16];
- uint64_t first_lba; // little endian
- uint64_t last_lba;
- uint64_t flags;
- uint16_t name[GPT_NAMELEN]; // UTF-16 LE
-};
-
-struct GPT_mapping {
- void *map_ptr;
- void *ptr;
- unsigned size;
-};
-
-struct GPT_entry_table {
- int fd;
-
- struct GPT_mapping header_map;
- struct GPT_mapping entries_map;
- struct GPT_mapping sec_header_map;
- struct GPT_mapping sec_entries_map;
-
- struct GPT_header *header;
- struct GPT_entry_raw *entries;
- struct GPT_header *second_header;
- struct GPT_entry_raw *second_entries;
-
- unsigned sector_size;
- unsigned partition_table_size;
- int second_valid;
-};
-
-struct GPT_header {
- uint8_t signature[8];
- uint32_t revision;
- uint32_t header_size;
- uint32_t header_checksum;
- uint32_t reserved_zeros;
- uint64_t current_lba;
- uint64_t backup_lba;
- uint64_t first_usable_lba;
- uint64_t last_usable_lba;
- uint8_t disk_guid[16];
- uint64_t entries_lba;
- uint32_t entries_count;
- uint32_t entry_size;
- uint32_t partition_array_checksum;
- // the rest should be filled with zeros
-} __attribute__((packed));
-
-struct GPT_content {
- struct GPT_header header;
- struct GPT_entry_raw *entries;
-};
-
-
-struct GPT_entry_table* GPT_get_device(const char *, unsigned lba);
-
-void GPT_release_device(struct GPT_entry_table *);
-
-void GPT_edit_entry(struct GPT_entry_table *table,
- struct GPT_entry_raw *old_entry,
- struct GPT_entry_raw *new_entry);
-
-int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-
-void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-
-struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *, const char *);
-struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *, const char *);
-
-//Use after every edit operation
-void GPT_sync();
-
-void GPT_to_UTF16(uint16_t *, const char *, int );
-void GPT_from_UTF16(char *, const uint16_t *, int);
-
-int GPT_parse_entry(char *string, struct GPT_entry_raw *entry);
-
-void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table);
-
-void GPT_release_content(struct GPT_content *content);
-
-int GPT_parse_file(int fd, struct GPT_content *content);
-
-int GPT_write_content(const char *device, struct GPT_content *content);
-
-int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd);
-
-void gpt_unmap(struct GPT_mapping *mapping);
-
-#endif
diff --git a/fastbootd/commands/virtual_partitions.c b/fastbootd/commands/virtual_partitions.c
deleted file mode 100644
index 813f485..0000000
--- a/fastbootd/commands/virtual_partitions.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include "commands/virtual_partitions.h"
-#include "debug.h"
-
-static struct virtual_partition *partitions = NULL;
-
-int try_handle_virtual_partition(struct protocol_handle *handle, const char *arg)
-{
- struct virtual_partition *current;
-
- for (current = partitions; current != NULL; current = current->next) {
- if (!strcmp(current->name, arg)) {
- current->handler(handle, arg);
- }
- }
-
- return 0;
-}
-
-void virtual_partition_register(
- const char * name,
- void (*handler)(struct protocol_handle *phandle, const char *arg))
-{
- struct virtual_partition *new;
- new = malloc(sizeof(*new));
- if (new) {
- new->name = name;
- new->handler = handler;
- new->next = partitions;
- partitions = new;
- }
- else {
- D(ERR, "Out of memory");
- }
-}
diff --git a/fastbootd/commands/virtual_partitions.h b/fastbootd/commands/virtual_partitions.h
deleted file mode 100644
index 88df71e..0000000
--- a/fastbootd/commands/virtual_partitions.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef FASTBOOTD_VIRTUAL_PARTITIONS_H
-#define FASTBOOTD_VIRTUAL_PARTITIONS_H
-
-#include "protocol.h"
-
-struct virtual_partition {
- struct virtual_partition *next;
- const char *name;
- void (*handler)(struct protocol_handle *phandle, const char *arg);
-};
-
-int try_handle_virtual_partition(struct protocol_handle *handle, const char *arg);
-
-void virtual_partition_register(
- const char * name,
- void (*handler)(struct protocol_handle *phandle, const char *arg));
-
-#endif
diff --git a/fastbootd/config.c b/fastbootd/config.c
deleted file mode 100644
index fe6da69..0000000
--- a/fastbootd/config.c
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Copyright (c) 2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-
-#include "protocol.h"
-#include "utils.h"
-
-#include "debug.h"
-
-// TODO: change config path
-#define CONFIG_PATH "/data/fastboot.cfg"
-
-static int config_parse_line(char *line)
-{
- char *c;
- char *key;
- char *value;
-
- c = strchr(line, '#');
- if (c)
- *c = '\0';
-
- if (strspn(line, " \t") == strlen(line))
- return 0;
-
- c = strchr(line, '=');
- if (c == NULL)
- return -1;
-
- key = line;
- *c = '\0';
- value = c + 1;
-
- key = strip(key);
- value = strip(value);
-
- key = strdup(key);
- value = strdup(value);
-
- fastboot_publish(key, value);
-
- return 0;
-}
-
-static void config_parse(char *buffer)
-{
- char *saveptr;
- char *str = buffer;
- char *line = buffer;
- int c;
- int ret;
-
- for (c = 1; line != NULL; c++) {
- line = strtok_r(str, "\r\n", &saveptr);
- if (line != NULL) {
- D(VERBOSE, "'%s'", line);
- ret = config_parse_line(line);
- if (ret < 0) {
- D(WARN, "error parsing " CONFIG_PATH " line %d", c);
- }
- }
- str = NULL;
- }
-}
-
-void config_init()
-{
- int fd;
- off_t len;
- ssize_t ret;
- size_t count = 0;
- char *buffer;
-
- fd = open(CONFIG_PATH, O_RDONLY);
- if (fd < 0) {
- D(ERR, "failed to open " CONFIG_PATH);
- return;
- }
-
- len = lseek(fd, 0, SEEK_END);
- if (len < 0) {
- D(ERR, "failed to seek to end of " CONFIG_PATH);
- return;
- }
-
- lseek(fd, 0, SEEK_SET);
-
- buffer = malloc(len + 1);
- if (buffer == NULL) {
- D(ERR, "failed to allocate %ld bytes", len);
- return;
- }
-
- while (count < (size_t)len) {
- ret = read(fd, buffer + count, len - count);
- if (ret < 0 && errno != EINTR) {
- D(ERR, "failed to read " CONFIG_PATH ": %d %s", errno, strerror(errno));
- return;
- }
- if (ret == 0) {
- D(ERR, "early EOF reading " CONFIG_PATH);
- return;
- }
-
- count += ret;
- }
-
- buffer[len] = '\0';
-
- config_parse(buffer);
-
- free(buffer);
-}
diff --git a/fastbootd/debug.h b/fastbootd/debug.h
deleted file mode 100644
index 74620b8..0000000
--- a/fastbootd/debug.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2013 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 _FASTBOOTD_DEBUG_H_
-#define _FASTBOOTD_DEBUG_H_
-
-#include <stdio.h>
-
-#include <cutils/klog.h>
-
-#define ERR 0
-#define WARN 1
-#define INFO 2
-#define VERBOSE 3
-#define DEBUG 4
-
-extern unsigned int debug_level;
-
-//#define DLOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
-#define DLOG(fmt, ...) KLOG_INFO("fastbootd", fmt, ##__VA_ARGS__)
-
-#define D(level, fmt, ...) \
- do { \
- if (debug_level == level || debug_level > level) { \
- DLOG("%s:%d " fmt "\n", __BASE_FILE__, __LINE__, ##__VA_ARGS__); \
- } \
- } while (0)
-
-#endif
diff --git a/fastbootd/fastbootd.c b/fastbootd/fastbootd.c
deleted file mode 100644
index 2b51b33..0000000
--- a/fastbootd/fastbootd.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2013 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 <unistd.h>
-#include <cutils/klog.h>
-#include <getopt.h>
-#include <stdlib.h>
-
-#include "debug.h"
-#include "trigger.h"
-#include "socket_client.h"
-#include "secure.h"
-
-unsigned int debug_level = DEBUG;
-
-void commands_init();
-void usb_init();
-void config_init();
-int transport_socket_init();
-int network_discovery_init();
-void ssh_server_start();
-
-int main(int argc, char **argv)
-{
- int socket_client = 0;
- int c;
- int network = 1;
-
- klog_init();
- klog_set_level(6);
-
- const struct option longopts[] = {
- {"socket", no_argument, 0, 'S'},
- {"nonetwork", no_argument, 0, 'n'},
- {0, 0, 0, 0}
- };
-
- while (1) {
- c = getopt_long(argc, argv, "Sn", longopts, NULL);
- /* Alphabetical cases */
- if (c < 0)
- break;
- switch (c) {
- case 'S':
- socket_client = 1;
- break;
- case 'n':
- network = 0;
- break;
- case '?':
- return 1;
- default:
- return 0;
- }
- }
-
- (void)argc;
- (void)argv;
-
- klog_init();
- klog_set_level(6);
-
- if (socket_client) {
- //TODO: Shouldn't we change current tty into raw mode?
- run_socket_client();
- }
- else {
- cert_init_crypto();
- config_init();
- load_trigger();
- commands_init();
- usb_init();
-
- if (network) {
- if (!transport_socket_init())
- exit(1);
- ssh_server_start();
- network_discovery_init();
- }
-
- while (1) {
- sleep(1);
- }
- }
- return 0;
-}
diff --git a/fastbootd/include/vendor_trigger.h b/fastbootd/include/vendor_trigger.h
deleted file mode 100644
index 51204fa..0000000
--- a/fastbootd/include/vendor_trigger.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef __VENDOR_TRIGGER_H_
-#define __VENDOR_TRIGGER_H_
-
-#define TRIGGER_MODULE_ID "fastbootd"
-#include <hardware/hardware.h>
-
-__BEGIN_DECLS
-
-struct GPT_entry_raw;
-struct GPT_content;
-
-/*
- * Structer with function pointers may become longer in the future
- */
-
-struct vendor_trigger_t {
- struct hw_device_t common;
-
- /*
- * This function runs at the beggining and shoud never be changed
- *
- * version is number parameter indicating version on the fastbootd side
- * libversion is version indicateing version of the library version
- *
- * returns 0 if it can cooperate with the current version and 1 in opposite
- */
- int (*check_version)(const int version, int *libversion);
-
-
- /*
- * Return value -1 forbid the action from the vendor site and sets errno
- */
- int (* gpt_layout)(struct GPT_content *);
- int (* oem_cmd)(const char *arg, const char **response);
-};
-
-__END_DECLS
-
-#endif
diff --git a/fastbootd/network_discovery.c b/fastbootd/network_discovery.c
deleted file mode 100644
index 1cd3e48..0000000
--- a/fastbootd/network_discovery.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <dns_sd.h>
-#include <cutils/properties.h>
-#include <unistd.h>
-
-#include "debug.h"
-#include "network_discovery.h"
-#include "utils.h"
-
-#define MDNS_SERVICE_NAME "mdnsd"
-#define MDNS_SERVICE_STATUS "init.svc.mdnsd"
-#define FASTBOOTD_TYPE "_fastbootd._tcp"
-#define FASTBOOTD_DOMAIN "local."
-#define FASTBOOTD_NAME "fastbootd"
-
-
-static void reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
- const char *name, const char *regtype, const char *domain, void *context)
-{
- (void)sdref; // Unused
- (void)flags; // Unused
- (void)context; // Unused
- if (errorCode == kDNSServiceErr_ServiceNotRunning) {
- fprintf(stderr, "Error code %d\n", errorCode);
- }
-
-
- printf("Got a reply for service %s.%s%s: ", name, regtype, domain);
-
- if (errorCode == kDNSServiceErr_NoError)
- {
- if (flags & kDNSServiceFlagsAdd)
- printf("Name now registered and active\n");
- else
- printf("Name registration removed\n");
- if (errorCode == kDNSServiceErr_NameConflict)
- printf("Name in use, please choose another\n");
- else
- printf("Error %d\n", errorCode);
-
- if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
- }
-}
-
-static int register_service() {
- DNSServiceRef sdref = NULL;
- const char *domain = FASTBOOTD_DOMAIN;
- const char *type = FASTBOOTD_TYPE;
- const char *host = NULL;
- char name[PROP_VALUE_MAX];
- uint16_t port = 22;
- int flags = 0;
- DNSServiceErrorType result;
- property_get("ro.serialno", name, "");
- if (!strcmp(name, "")) {
- D(ERR, "No property serialno");
- return -1;
- }
-
- result = DNSServiceRegister(&sdref, flags, kDNSServiceInterfaceIndexAny,
- name, type, domain, host, port,
- 0, NULL, reg_reply, NULL);
- if (result != kDNSServiceErr_NoError) {
- D(ERR, "Unable to register service");
- return -1;
- }
- return 0;
-}
-
-
-int network_discovery_init()
-{
- D(INFO, "Starting discovery");
- if (service_start(MDNS_SERVICE_NAME)) {
- D(ERR, "Unable to start discovery");
- return -1;
- }
-
- if (register_service()) {
- D(ERR, "Unable to register service");
- return -1;
- }
-
- return 0;
-}
-
diff --git a/fastbootd/network_discovery.h b/fastbootd/network_discovery.h
deleted file mode 100644
index 75fda63..0000000
--- a/fastbootd/network_discovery.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOTD_NETWORK_DISCOVERY_H
-#define _FASTBOOTD_NETWORK_DISCOVERY_H
-
-int network_discovery_init();
-
-#endif
diff --git a/fastbootd/other/gptedit.c b/fastbootd/other/gptedit.c
deleted file mode 100644
index d423529..0000000
--- a/fastbootd/other/gptedit.c
+++ /dev/null
@@ -1,303 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <getopt.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include <cutils/klog.h>
-
-#include "commands/partitions.h"
-#include "debug.h"
-
-unsigned int debug_level = DEBUG;
-//TODO: add tool to generate config file
-
-void usage() {
- fprintf(stderr,
- "usage: test_gpt [ <option> ] <file>\n"
- "\n"
- "options:\n"
- " -p print partitions\n"
- " -c print config file\n"
- " -a adds new partition\n"
- " -d deletes partition (-o needed)\n"
- "\n"
- " -n name@startlba,endlba new partition detail\n"
- " -o old partition name\n"
- " -t type guid\n"
- " -g partition guid\n"
- " -l gpt_location specyfies gpt secto\n"
- );
-
-}
-
-void printGPT(struct GPT_entry_table *table);
-void addGPT(struct GPT_entry_table *table, const char *arg, const char *guid, const char *tguid);
-void deleteGPT(struct GPT_entry_table *table, const char *name);
-void configPrintGPT(struct GPT_entry_table *table);
-
-int main(int argc, char *argv[]) {
- int print_cmd = 0;
- int config_cmd = 0;
- int add_cmd = 0;
- int del_cmd = 0;
- int sync_cmd = 0;
- int c;
- const char *new_partition = NULL;
- const char *old_partition = NULL;
- const char *type_guid = NULL;
- const char *partition_guid = NULL;
- unsigned gpt_location = 1;
-
- klog_init();
- klog_set_level(6);
-
- const struct option longopts[] = {
- {"print", no_argument, 0, 'p'},
- {"config-print", no_argument, 0, 'c'},
- {"add", no_argument, 0, 'a'},
- {"del", no_argument, 0, 'd'},
- {"new", required_argument, 0, 'n'},
- {"old", required_argument, 0, 'o'},
- {"type", required_argument, 0, 't'},
- {"sync", required_argument, 0, 's'},
- {"guid", required_argument, 0, 'g'},
- {"location", required_argument, 0, 'l'},
- {0, 0, 0, 0}
- };
-
- while (1) {
- c = getopt_long(argc, argv, "pcadt:g:n:o:sl:", longopts, NULL);
- /* Alphabetical cases */
- if (c < 0)
- break;
- switch (c) {
- case 'p':
- print_cmd = 1;
- break;
- case 'c':
- config_cmd = 1;
- break;
- case 'a':
- add_cmd = 1;
- break;
- case 'd':
- del_cmd = 1;
- break;
- case 'n':
- new_partition = optarg;
- break;
- case 'o':
- old_partition = optarg;
- break;
- case 't':
- type_guid = optarg;
- case 'g':
- partition_guid = optarg;
- break;
- case 's':
- sync_cmd = 1;
- break;
- case 'l':
- gpt_location = strtoul(optarg, NULL, 10);
- fprintf(stderr, "Got offset as %d", gpt_location);
- break;
- case '?':
- return 1;
- default:
- abort();
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if (argc < 1) {
- usage();
- return 1;
- }
-
- const char *path = argv[0];
- struct GPT_entry_table *table = GPT_get_device(path, gpt_location);
- if (table == NULL) {
- fprintf(stderr, "unable to get GPT table from %s\n", path);
- return 1;
- }
-
- if (add_cmd)
- addGPT(table, new_partition, partition_guid, type_guid);
- if (del_cmd)
- deleteGPT(table, old_partition);
- if (print_cmd)
- printGPT(table);
- if (config_cmd)
- configPrintGPT(table);
- if (sync_cmd)
- GPT_sync(table);
-
- GPT_release_device(table);
-
- return 0;
-}
-
-void printGPT(struct GPT_entry_table *table) {
- struct GPT_entry_raw *entry = table->entries;
- unsigned n, m;
- char name[GPT_NAMELEN + 1];
-
- printf("ptn start block end block name\n");
- printf("---- ------------- -------------\n");
-
- for (n = 0; n < table->header->entries_count; n++, entry++) {
- if (entry->type_guid[0] == 0)
- continue;
- for (m = 0; m < GPT_NAMELEN; m++) {
- name[m] = entry->name[m] & 127;
- }
- name[m] = 0;
- printf("#%03d %13"PRId64" %13"PRId64" %s\n",
- n + 1, entry->first_lba, entry->last_lba, name);
- }
-}
-
-void configPrintGPT(struct GPT_entry_table *table) {
- struct GPT_entry_raw *entry = table->entries;
- unsigned n, m;
- char name[GPT_NAMELEN + 1];
- char temp_guid[17];
- temp_guid[16] = 0;
-
- printf("header_lba %"PRId64"\n", table->header->current_lba);
- printf("backup_lba %"PRId64"\n", table->header->backup_lba);
- printf("first_lba %"PRId64"\n", table->header->first_usable_lba);
- printf("last_lba %"PRId64"\n", table->header->last_usable_lba);
- printf("entries_lba %"PRId64"\n", table->header->entries_lba);
- snprintf(temp_guid, 17, "%s", table->header->disk_guid);
- printf("guid \"%s\"", temp_guid);
-
- printf("\npartitions {\n");
-
- for (n = 0; n < table->header->entries_count; n++, entry++) {
- uint64_t size = entry->last_lba - entry->first_lba + 1;
-
- if (entry->type_guid[0] == 0)
- continue;
- for (m = 0; m < GPT_NAMELEN; m++) {
- name[m] = entry->name[m] & 127;
- }
- name[m] = 0;
-
- printf(" %s {\n", name);
- snprintf(temp_guid, 17, "%s", entry->partition_guid);
- printf(" guid \"%s\"\n", temp_guid);
- printf(" first_lba %"PRId64"\n", entry->first_lba);
- printf(" partition_size %"PRId64"\n", size);
- if (entry->flags & GPT_FLAG_SYSTEM)
- printf(" system\n");
- if (entry->flags & GPT_FLAG_BOOTABLE)
- printf(" bootable\n");
- if (entry->flags & GPT_FLAG_READONLY)
- printf(" readonly\n");
- if (entry->flags & GPT_FLAG_DOAUTOMOUNT)
- printf(" automount\n");
- printf(" }\n\n");
- }
- printf("}\n");
-}
-
-void addGPT(struct GPT_entry_table *table, const char *str , const char *guid, const char *tguid) {
- char *c, *c2;
- char *arg = malloc(strlen(str));
- char *name = arg;
- unsigned start, end;
- strcpy(arg, str);
- if (guid == NULL || tguid == NULL) {
- fprintf(stderr, "Type guid and partion guid needed");
- free(arg);
- return;
- }
-
- c = strchr(arg, '@');
-
- if (c == NULL) {
- fprintf(stderr, "Wrong entry format");
- free(arg);
- return;
- }
-
- *c++ = '\0';
-
- c2 = strchr(c, ',');
-
- if (c2 == NULL) {
- fprintf(stderr, "Wrong entry format");
- free(arg);
- return;
- }
-
- start = strtoul(c, NULL, 10);
- *c2++ = '\0';
- end = strtoul(c2, NULL, 10);
-
- struct GPT_entry_raw data;
- strncpy((char *)data.partition_guid, guid, 15);
- data.partition_guid[15] = '\0';
- strncpy((char *)data.type_guid, tguid, 15);
- data.type_guid[15] = '\0';
- GPT_to_UTF16(data.name, name, GPT_NAMELEN);
- data.first_lba = start;
- data.last_lba = end;
-
- fprintf(stderr, "Adding (%d,%d) %s as, [%s, %s]", start, end, name, (char *) data.type_guid, (char *) data.partition_guid);
- GPT_add_entry(table, &data);
- free(arg);
-}
-
-void deleteGPT(struct GPT_entry_table *table, const char *name) {
- struct GPT_entry_raw *entry;
-
- if (name == NULL) {
- fprintf(stderr, "Need partition name");
- return;
- }
-
- entry = GPT_get_pointer_by_name(table, name);
-
- if (!entry) {
- fprintf(stderr, "Unable to find partition: %s", name);
- return;
- }
- GPT_delete_entry(table, entry);
-}
-
diff --git a/fastbootd/other/partitions.sample.cfg b/fastbootd/other/partitions.sample.cfg
deleted file mode 100644
index 49562cf..0000000
--- a/fastbootd/other/partitions.sample.cfg
+++ /dev/null
@@ -1,60 +0,0 @@
-
-header_lba 1
-backup_lba 101
-first_lba 43
-last_lba 100
-entries_lba 2
-
-partitions {
- #You can generate this as output from gptedit -c
- SOS {
- first_lba 28672
- partition_size 16384
- }
-
- DTB {
- first_lba 45056
- partition_size 8192
- }
-
- LNX {
- first_lba 53248
- partition_size 16384
- }
-
- APP {
- first_lba 69632
- partition_size 1048576
- }
-
- CAC {
- first_lba 1118208
- partition_size 1572864
- }
-
- MSC {
- first_lba 2691072
- partition_size 4096
- }
-
- USP {
- first_lba 2695168
- partition_size 65536
- }
-
- MDA {
- first_lba 2760704
- partition_size 4096
- }
-
- FCT {
- first_lba 2764800
- partition_size 32768
- }
-
- UDA {
- first_lba 2797568
- partition_size 27975680
- }
-}
-
diff --git a/fastbootd/other/sign/src/SignImg.java b/fastbootd/other/sign/src/SignImg.java
deleted file mode 100644
index 338d427..0000000
--- a/fastbootd/other/sign/src/SignImg.java
+++ /dev/null
@@ -1,181 +0,0 @@
-package signtool;
-
-import java.io.*;
-import java.util.Properties;
-import java.util.ArrayList;
-
-import javax.mail.internet.*;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.activation.MailcapCommandMap;
-import javax.activation.CommandMap;
-
-import java.security.PrivateKey;
-import java.security.Security;
-import java.security.KeyFactory;
-import java.security.KeyStore;
-import java.security.NoSuchAlgorithmException;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.security.spec.InvalidKeySpecException;
-import java.security.cert.X509Certificate;
-import java.security.cert.CertificateFactory;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateEncodingException;
-
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
-import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
-import org.bouncycastle.operator.ContentSigner;
-import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
-import org.bouncycastle.cms.CMSProcessableByteArray;
-import org.bouncycastle.cms.CMSSignedGenerator;
-import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.cms.CMSSignedGenerator;
-import org.bouncycastle.cms.CMSProcessable;
-import org.bouncycastle.cms.CMSSignedData;
-import org.bouncycastle.cms.CMSTypedData;
-import org.bouncycastle.cert.jcajce.JcaCertStore;
-import org.bouncycastle.util.Store;
-import org.bouncycastle.asn1.ASN1InputStream;
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.ASN1Object;
-
-
-public class SignImg {
-
- /* It reads private key in pkcs#8 formate
- * Conversion:
- * openssl pkcs8 -topk8 -nocrypt -outform DER < inkey.pem > outkey.pk8
- */
- private static PrivateKey getPrivateKey(String path) throws IOException, FileNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
- File file = new File(path);
- FileInputStream fis = new FileInputStream(file);
- byte[] data = new byte[(int)file.length()];
- fis.read(data);
- fis.close();
-
- PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(data);
- KeyFactory kf = KeyFactory.getInstance("RSA");
- PrivateKey privateKey = kf.generatePrivate(kspec);
-
- return privateKey;
- }
-
- private static MimeBodyPart getContent(String path) throws IOException, FileNotFoundException, MessagingException {
- MimeBodyPart body = new MimeBodyPart();
-
- File file = new File(path);
- FileInputStream fis = new FileInputStream(file);
- byte[] data = new byte[(int)file.length()];
- fis.read(data);
- fis.close();
-
- body.setContent(data, "application/octet-stream");
-
- return body;
- }
-
- private static CMSProcessableByteArray getCMSContent(String path) throws IOException, FileNotFoundException, MessagingException {
- File file = new File(path);
- FileInputStream fis = new FileInputStream(file);
- byte[] data = new byte[(int)file.length()];
- fis.read(data);
- fis.close();
- CMSProcessableByteArray cms = new CMSProcessableByteArray(data);
-
- return cms;
- }
-
- private static X509Certificate readCert(String path) throws IOException, FileNotFoundException, CertificateException {
- File file = new File(path);
- FileInputStream is = new FileInputStream(file);
-
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- Certificate cert = cf.generateCertificate(is);
- is.close();
-
- return (X509Certificate) cert;
- }
-
- private static void save(MimeBodyPart content, String path) throws IOException, FileNotFoundException, MessagingException {
- File file = new File(path);
- FileOutputStream os = new FileOutputStream(file);
-
- content.writeTo(os);
-
- os.close();
- }
-
- private static Store certToStore(X509Certificate certificate) throws CertificateEncodingException {
- ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
- certList.add(certificate);
- return new JcaCertStore(certList);
- }
-
- public static void setDefaultMailcap()
- {
- MailcapCommandMap _mailcap =
- (MailcapCommandMap)CommandMap.getDefaultCommandMap();
-
- _mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
- _mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
- _mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
- _mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
- _mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
-
- CommandMap.setDefaultCommandMap(_mailcap);
- }
-
- public static void main(String[] args) {
- try {
- if (args.length < 4) {
- System.out.println("Usage: signimg data private_key certificate output");
- return;
- }
- System.out.println("Signing the image");
- setDefaultMailcap();
-
- Security.addProvider(new BouncyCastleProvider());
-
- PrivateKey key = getPrivateKey(args[1]);
- System.out.println("File read sucessfully");
-
- CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
-
- CMSTypedData body = getCMSContent(args[0]);
- System.out.println("Content read sucessfully");
-
- X509Certificate cert = (X509Certificate) readCert(args[2]);
- System.out.println("Certificate read sucessfully");
-
- ContentSigner sha256Signer = new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(key);
-
- Store certs = certToStore(cert);
-
- generator.addCertificates(certs);
- generator.addSignerInfoGenerator(
- new JcaSignerInfoGeneratorBuilder(
- new JcaDigestCalculatorProviderBuilder().setProvider("BC").build())
- .build(sha256Signer, cert));
-
- CMSSignedData signed = generator.generate(body, true);
- System.out.println("Signed");
-
- Properties props = System.getProperties();
- Session session = Session.getDefaultInstance(props, null);
-
- File file = new File(args[3]);
- FileOutputStream os = new FileOutputStream(file);
-
- ASN1InputStream asn1 = new ASN1InputStream(signed.getEncoded());
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- DEROutputStream dOut = new DEROutputStream(os);
- dOut.writeObject(ASN1Object.fromByteArray(signed.getEncoded()));
-
- }
- catch (Exception ex) {
- System.out.println("Exception during programm execution: " + ex.getMessage());
- }
- }
-}
diff --git a/fastbootd/other/vendor_trigger.c b/fastbootd/other/vendor_trigger.c
deleted file mode 100644
index 101959b..0000000
--- a/fastbootd/other/vendor_trigger.c
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdlib.h>
-
-#include "vendor_trigger.h"
-#include "debug.h"
-
-unsigned int debug_level = DEBUG;
-
-static const int version = 1;
-
-int check_version(const int fastboot_version, int *libversion) {
- *libversion = version;
- return !(fastboot_version == version);
-}
-
-int gpt_layout(struct GPT_content *table) {
- D(DEBUG, "message from libvendor");
- return 0;
-}
-
-int oem_cmd(const char *arg, const char **response) {
- D(DEBUG, "message from libvendor, oem catched request %s", arg);
- return 0;
-}
-
-static int close_triggers(struct vendor_trigger_t *dev)
-{
- if (dev)
- free(dev);
-
- return 0;
-}
-
-static int open_triggers(const struct hw_module_t *module, char const *name,
- struct hw_device_t **device) {
- struct vendor_trigger_t *dev = malloc(sizeof(struct vendor_trigger_t));
- klog_init();
- klog_set_level(6);
-
- memset(dev, 0, sizeof(*dev));
- dev->common.module = (struct hw_module_t *) module;
- dev->common.close = (int (*)(struct hw_device_t *)) close_triggers;
-
- dev->gpt_layout = gpt_layout;
- dev->oem_cmd = oem_cmd;
-
- *device = (struct hw_device_t *) dev;
-
- return 0;
-}
-
-
-static struct hw_module_methods_t trigger_module_methods = {
- .open = open_triggers,
-};
-
-struct hw_module_t HAL_MODULE_INFO_SYM = {
- .tag = HARDWARE_MODULE_TAG,
- .version_major = 1,
- .version_minor = 0,
- .id = TRIGGER_MODULE_ID,
- .name = "vendor trigger library for fastbootd",
- .author = "Google, Inc.",
- .methods = &trigger_module_methods,
-};
-
diff --git a/fastbootd/protocol.c b/fastbootd/protocol.c
deleted file mode 100644
index 3908020..0000000
--- a/fastbootd/protocol.c
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "debug.h"
-#include "protocol.h"
-#include "transport.h"
-
-#define STATE_OFFLINE 0
-#define STATE_COMMAND 1
-#define STATE_COMPLETE 2
-#define STATE_ERROR 3
-
-struct fastboot_cmd {
- struct fastboot_cmd *next;
- const char *prefix;
- unsigned prefix_len;
- void (*execute)(struct protocol_handle *phandle, const char *arg);
-};
-
-struct fastboot_var {
- struct fastboot_var *next;
- const char *name;
- const char *value;
-};
-
-static struct fastboot_cmd *cmdlist;
-
-void fastboot_register(const char *prefix,
- void (*phandle)(struct protocol_handle *phandle, const char *arg))
-{
- struct fastboot_cmd *cmd;
- cmd = malloc(sizeof(*cmd));
- if (cmd) {
- cmd->prefix = prefix;
- cmd->prefix_len = strlen(prefix);
- cmd->execute = phandle;
- cmd->next = cmdlist;
- cmdlist = cmd;
- }
-}
-
-static struct fastboot_var *varlist;
-
-void fastboot_publish(const char *name, const char *value)
-{
- struct fastboot_var *var;
- var = malloc(sizeof(*var));
- if (var) {
- var->name = name;
- var->value = value;
- var->next = varlist;
- varlist = var;
- }
-}
-
-const char *fastboot_getvar(const char *name)
-{
- struct fastboot_var *var;
-
- for (var = varlist; var; var = var->next) {
- if (!strcmp(var->name, name)) {
- return var->value;
- }
- }
-
- return "";
-}
-
-int protocol_handle_download(struct protocol_handle *phandle, size_t len)
-{
- return transport_handle_download(phandle->transport_handle, len);
-}
-
-static ssize_t protocol_handle_write(struct protocol_handle *phandle,
- char *buffer, size_t len)
-{
- return transport_handle_write(phandle->transport_handle, buffer, len);
-}
-
-static void fastboot_ack(struct protocol_handle *phandle, const char *code,
- const char *reason)
-{
- char response[64];
-
- if (phandle->state != STATE_COMMAND)
- return;
-
- if (reason == 0)
- reason = "";
-
- snprintf(response, 64, "%s%s", code, reason);
- phandle->state = STATE_COMPLETE;
-
- protocol_handle_write(phandle, response, strlen(response));
-}
-
-void fastboot_fail(struct protocol_handle *phandle, const char *reason)
-{
- fastboot_ack(phandle, "FAIL", reason);
-}
-
-void fastboot_okay(struct protocol_handle *phandle, const char *info)
-{
- fastboot_ack(phandle, "OKAY", info);
-}
-
-void fastboot_data(struct protocol_handle *phandle, size_t len)
-{
- char response[64];
- ssize_t ret;
-
- snprintf(response, 64, "DATA%08zx", len);
- ret = protocol_handle_write(phandle, response, strlen(response));
- if (ret < 0)
- return;
-}
-
-void protocol_handle_command(struct protocol_handle *phandle, char *buffer)
-{
- D(INFO,"fastboot: %s\n", buffer);
-
- struct fastboot_cmd *cmd;
-
- for (cmd = cmdlist; cmd; cmd = cmd->next) {
- if (memcmp(buffer, cmd->prefix, cmd->prefix_len))
- continue;
- phandle->state = STATE_COMMAND;
- cmd->execute(phandle, buffer + cmd->prefix_len);
- if (phandle->state == STATE_COMMAND)
- fastboot_fail(phandle, "unknown reason");
- return;
- }
-
- fastboot_fail(phandle, "unknown command");
-}
-
-struct protocol_handle *create_protocol_handle(struct transport_handle *thandle)
-{
- struct protocol_handle *phandle;
-
- phandle = calloc(sizeof(struct protocol_handle), 1);
-
- phandle->transport_handle = thandle;
- phandle->state = STATE_OFFLINE;
- phandle->download_fd = -1;
-
- pthread_mutex_init(&phandle->lock, NULL);
-
- return phandle;
-}
-
-int protocol_get_download(struct protocol_handle *phandle)
-{
- int fd;
-
- pthread_mutex_lock(&phandle->lock);
- fd = phandle->download_fd;
- phandle->download_fd = -1;
- pthread_mutex_unlock(&phandle->lock);
-
- return fd;
-}
diff --git a/fastbootd/protocol.h b/fastbootd/protocol.h
deleted file mode 100644
index ea2a8df..0000000
--- a/fastbootd/protocol.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOTD_PROTOCOL_H_
-#define _FASTBOOTD_PROTOCOL_H_
-
-#include <pthread.h>
-#include <stddef.h>
-
-struct protocol_handle {
- struct transport_handle *transport_handle;
- unsigned int state;
- int download_fd;
-
- pthread_mutex_t lock;
-};
-
-void fastboot_register(const char *prefix,
- void (*handle)(struct protocol_handle *handle, const char *arg));
-
-void fastboot_publish(const char *name, const char *value);
-const char *fastboot_getvar(const char *name);
-
-struct protocol_handle *create_protocol_handle(struct transport_handle *t);
-void protocol_handle_command(struct protocol_handle *handle, char *buffer);
-int protocol_handle_download(struct protocol_handle *phandle, size_t len);
-int protocol_get_download(struct protocol_handle *phandle);
-
-void fastboot_fail(struct protocol_handle *handle, const char *reason);
-void fastboot_okay(struct protocol_handle *handle, const char *reason);
-void fastboot_data(struct protocol_handle *handle, size_t len);
-
-#endif
diff --git a/fastbootd/secure.c b/fastbootd/secure.c
deleted file mode 100644
index 186e026..0000000
--- a/fastbootd/secure.c
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-
-#include <openssl/pem.h>
-#include <openssl/engine.h>
-#include <openssl/x509.h>
-#include <openssl/x509v3.h>
-#include <openssl/pem.h>
-#include <openssl/cms.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "secure.h"
-#include "debug.h"
-#include "utils.h"
-
-
-void cert_init_crypto() {
- CRYPTO_malloc_init();
- ERR_load_crypto_strings();
- OpenSSL_add_all_algorithms();
- ENGINE_load_builtin_engines();
-}
-
-X509_STORE *cert_store_from_path(const char *path) {
-
- X509_STORE *store;
- struct stat st;
- X509_LOOKUP *lookup;
-
- if (stat(path, &st)) {
- D(ERR, "Unable to stat cert path");
- goto error;
- }
-
- if (!(store = X509_STORE_new())) {
- goto error;
- }
-
- if (S_ISDIR(st.st_mode)) {
- lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
- if (lookup == NULL)
- goto error;
- if (!X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM)) {
- D(ERR, "Error loading cert directory %s", path);
- goto error;
- }
- }
- else if(S_ISREG(st.st_mode)) {
- lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
- if (lookup == NULL)
- goto error;
- if (!X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM)) {
- D(ERR, "Error loading cert directory %s", path);
- goto error;
- }
- }
- else {
- D(ERR, "cert path is not directory or regular file");
- goto error;
- }
-
- return store;
-
-error:
- return NULL;
-}
-
-
-int cert_read(int fd, CMS_ContentInfo **content, BIO **output) {
- BIO *input;
- *output = NULL;
-
-
- input = BIO_new_fd(fd, BIO_NOCLOSE);
- if (input == NULL) {
- D(ERR, "Unable to open input");
- goto error;
- }
-
- //TODO:
- // read with d2i_CMS_bio to support DER
- // with java or just encode data with base64
- *content = SMIME_read_CMS(input, output);
- if (*content == NULL) {
- unsigned long err = ERR_peek_last_error();
- D(ERR, "Unable to parse input file: %s", ERR_lib_error_string(err));
- goto error_read;
- }
-
- BIO_free(input);
-
- return 0;
-
-error_read:
- BIO_free(input);
-error:
- return 1;
-}
-
-int cert_verify(BIO *content, CMS_ContentInfo *content_info, X509_STORE *store, int *out_fd) {
- BIO *output_temp;
- int ret;
-
- *out_fd = create_temp_file();
- if (*out_fd < 0) {
- D(ERR, "unable to create temporary file");
- return -1;
- }
-
- output_temp = BIO_new_fd(*out_fd, BIO_NOCLOSE);
- if (output_temp == NULL) {
- D(ERR, "unable to create temporary bio");
- close(*out_fd);
- return -1;
- }
-
- ret = CMS_verify(content_info, NULL ,store, content, output_temp, 0);
-
- if (ret == 0) {
- char buf[256];
- unsigned long err = ERR_peek_last_error();
- D(ERR, "Verification failed with reason: %s, %s", ERR_lib_error_string(err), ERR_error_string(err, buf));
- D(ERR, "Data used: content %p", content);
- }
-
- ERR_clear_error();
- ERR_remove_state(0);
-
- BIO_free(output_temp);
-
- return ret;
-}
-
-void cert_release(BIO *content, CMS_ContentInfo *info) {
- BIO_free(content);
- CMS_ContentInfo_free(info);
-}
-
diff --git a/fastbootd/secure.h b/fastbootd/secure.h
deleted file mode 100644
index 878a643..0000000
--- a/fastbootd/secure.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOTD_SECURE_H
-#define _FASTBOOTD_SECURE_H
-
-#include <openssl/ssl.h>
-#include <openssl/x509.h>
-#include <openssl/x509v3.h>
-#include <openssl/pem.h>
-#include <openssl/cms.h>
-
-void cert_init_crypto();
-
-X509_STORE *cert_store_from_path(const char*stream);
-
-static inline void cert_release_store(X509_STORE *store) {
- X509_STORE_free(store);
-}
-
-int cert_read(int fd, CMS_ContentInfo **content, BIO **output);
-int cert_verify(BIO *content, CMS_ContentInfo *content_info, X509_STORE *store, int *out_fd);
-void cert_release(BIO *content, CMS_ContentInfo *info);
-
-#endif
diff --git a/fastbootd/socket_client.c b/fastbootd/socket_client.c
deleted file mode 100644
index da636db..0000000
--- a/fastbootd/socket_client.c
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdio.h>
-#include <cutils/sockets.h>
-#include <poll.h>
-#include <unistd.h>
-
-#include "utils.h"
-
-#define BUFFER_SIZE 256
-
-#define STDIN_FD 0
-#define STDOUT_FD 1
-#define STDERR_FD 2
-
-void run_socket_client() {
- int fd;
- char buffer[BUFFER_SIZE];
- int n;
- struct pollfd fds[2];
-
- fd = socket_local_client("fastbootd",
- ANDROID_SOCKET_NAMESPACE_RESERVED,
- SOCK_STREAM);
-
- if (fd < 0) {
- fprintf(stderr, "ERROR: Unable to open fastbootd socket\n");
- return;
- }
-
- fds[0].fd = STDIN_FD;
- fds[0].events = POLLIN;
- fds[1].fd = fd;
- fds[1].events = POLLIN;
-
- while(true) {
- if (poll(fds, 2, -1) <= 0) {
- fprintf(stderr, "ERROR: socket error");
- return;
- }
-
- if (fds[0].revents & POLLIN) {
- if ((n = read(STDIN_FD, buffer, BUFFER_SIZE)) < 0) {
- goto error;
- }
-
- if (bulk_write(fd, buffer, n) < 0) {
- goto error;
- }
- }
-
- if (fds[1].revents & POLLIN) {
- if ((n = read(fd, buffer, BUFFER_SIZE)) < 0) {
- goto error;
- }
-
- if (bulk_write(STDOUT_FD, buffer, n) < 0) {
- goto error;
- }
- }
- }
-
-error:
- fprintf(stderr, "Transport error\n");
-}
diff --git a/fastbootd/socket_client.h b/fastbootd/socket_client.h
deleted file mode 100644
index 4481ff2..0000000
--- a/fastbootd/socket_client.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOTD_SOCKET_CLIENT_H
-#define _FASTBOOTD_SOCKET_CLIENT_H
-
-void run_socket_client();
-
-#endif
diff --git a/fastbootd/transport.c b/fastbootd/transport.c
deleted file mode 100644
index ce8f9d0..0000000
--- a/fastbootd/transport.c
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * Copyright (C) 2013 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 <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/mman.h>
-
-#include "debug.h"
-#include "protocol.h"
-#include "transport.h"
-
-#define COMMAND_BUF_SIZE 64
-
-ssize_t transport_handle_write(struct transport_handle *thandle, char *buffer, size_t len)
-{
- return thandle->transport->write(thandle, buffer, len);
-}
-
-void transport_handle_close(struct transport_handle *thandle)
-{
- thandle->transport->close(thandle);
-}
-
-int transport_handle_download(struct transport_handle *thandle, size_t len)
-{
- ssize_t ret;
- size_t n = 0;
- int fd;
- // TODO: move out of /dev
- char tempname[] = "/dev/fastboot_download_XXXXXX";
- char *buffer;
-
- fd = mkstemp(tempname);
- if (fd < 0)
- return -1;
-
- unlink(tempname);
-
- ftruncate(fd, len);
-
- buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (buffer == NULL) {
- D(ERR, "mmap(%zu) failed: %d %s", len, errno, strerror(errno));
- goto err;
- }
-
- while (n < len) {
- ret = thandle->transport->read(thandle, buffer + n, len - n);
- if (ret <= 0) {
- D(WARN, "transport read failed, ret=%zd %s", ret, strerror(-ret));
- break;
- }
- n += ret;
- }
-
- munmap(buffer, len);
-
- if (n != len)
- goto err;
-
- return fd;
-
-err:
- close(fd);
- transport_handle_close(thandle);
- return -1;
-}
-
-static void *transport_data_thread(void *arg)
-{
- struct transport_handle *thandle = arg;
- struct protocol_handle *phandle = create_protocol_handle(thandle);
-
- while (!thandle->stopped) {
- int ret;
- char buffer[COMMAND_BUF_SIZE + 1];
- D(VERBOSE, "transport_data_thread\n");
-
- ret = thandle->transport->read(thandle, buffer, COMMAND_BUF_SIZE);
- if (ret <= 0) {
- D(DEBUG, "ret = %d\n", ret);
- break;
- }
- if (ret > 0) {
- buffer[ret] = 0;
- //TODO: multiple threads
- protocol_handle_command(phandle, buffer);
- }
- }
-
- transport_handle_close(thandle);
- free(thandle);
-
- return NULL;
-}
-
-static void *transport_connect_thread(void *arg)
-{
- struct transport *transport = arg;
- while (!transport->stopped) {
- struct transport_handle *thandle;
- pthread_t thread;
- pthread_attr_t attr;
-
- D(VERBOSE, "transport_connect_thread\n");
- thandle = transport->connect(transport);
- if (thandle == NULL) {
- D(ERR, "transport connect failed\n");
- sleep(1);
- continue;
- }
- thandle->transport = transport;
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
- pthread_create(&thread, &attr, transport_data_thread, thandle);
-
- sleep(1);
- }
-
- return NULL;
-}
-
-void transport_register(struct transport *transport)
-{
- pthread_t thread;
- pthread_attr_t attr;
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
- pthread_create(&thread, &attr, transport_connect_thread, transport);
-}
diff --git a/fastbootd/transport.h b/fastbootd/transport.h
deleted file mode 100644
index 209340d..0000000
--- a/fastbootd/transport.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2013 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 _FASTBOOTD_TRANSPORT_H_
-#define _FASTBOOTD_TRANSPORT_H_
-
-#include <stdbool.h>
-
-struct transport_handle {
- struct transport *transport;
-
- bool stopped;
-};
-
-struct transport {
- void (*init)();
- void (*close)(struct transport_handle *thandle);
- ssize_t (*read)(struct transport_handle *thandle, void *data, size_t len);
- ssize_t (*write)(struct transport_handle *thandle, const void *data, size_t len);
- struct transport_handle *(*connect)(struct transport *transport);
- bool stopped;
-};
-
-void transport_register(struct transport *transport);
-ssize_t transport_handle_write(struct transport_handle *handle, char *buffer, size_t len);
-int transport_handle_download(struct transport_handle *handle, size_t len);
-
-#endif
diff --git a/fastbootd/transport_socket.c b/fastbootd/transport_socket.c
deleted file mode 100644
index 664d473..0000000
--- a/fastbootd/transport_socket.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#include <cutils/sockets.h>
-
-#include "debug.h"
-#include "transport.h"
-#include "utils.h"
-
-
-#define container_of(ptr, type, member) \
- ((type*)((char*)(ptr) - offsetof(type, member)))
-
-#define SOCKET_WORKING 0
-#define SOCKET_STOPPED -1
-
-
-struct socket_transport {
- struct transport transport;
-
- int fd;
-};
-
-struct socket_handle {
- struct transport_handle handle;
-
- int fd;
-};
-
-void socket_close(struct transport_handle *thandle)
-{
- struct socket_handle * handle = container_of(thandle, struct socket_handle, handle);
- close(handle->fd);
-}
-
-struct transport_handle *socket_connect(struct transport *transport)
-{
- struct socket_handle *handle = calloc(sizeof(struct socket_handle), 1);
- struct socket_transport *socket_transport = container_of(transport, struct socket_transport, transport);
- struct sockaddr addr;
- socklen_t alen = sizeof(addr);
-
- handle->fd = accept(socket_transport->fd, &addr, &alen);
-
- if (handle->fd < 0) {
- D(WARN, "socket connect error");
- return NULL;
- }
-
- D(DEBUG, "[ socket_thread - registering device ]");
- return &handle->handle;
-}
-
-ssize_t socket_write(struct transport_handle *thandle, const void *data, size_t len)
-{
- ssize_t ret;
- struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
-
- D(DEBUG, "about to write (fd=%d, len=%zu)", handle->fd, len);
- ret = bulk_write(handle->fd, data, len);
- if (ret < 0) {
- D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
- return -1;
- }
- D(DEBUG, "[ socket_write done fd=%d ]", handle->fd);
- return ret;
-}
-
-ssize_t socket_read(struct transport_handle *thandle, void *data, size_t len)
-{
- ssize_t ret;
- struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
-
- D(DEBUG, "about to read (fd=%d, len=%zu)", handle->fd, len);
- ret = bulk_read(handle->fd, data, len);
- if (ret < 0) {
- D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
- return -1;
- }
- D(DEBUG, "[ socket_read done fd=%d ret=%zd]", handle->fd, ret);
- return ret;
-}
-
-static int listen_socket_init(struct socket_transport *socket_transport)
-{
- int s = android_get_control_socket("fastbootd");
-
- if (s < 0) {
- D(WARN, "android_get_control_socket(fastbootd): %s\n", strerror(errno));
- return 0;
- }
-
- if (listen(s, 4) < 0) {
- D(WARN, "listen(control socket): %s\n", strerror(errno));
- return 0;
- }
-
- socket_transport->fd = s;
-
- return 1;
-}
-
-
-int transport_socket_init()
-{
- struct socket_transport *socket_transport = malloc(sizeof(struct socket_transport));
-
- socket_transport->transport.connect = socket_connect;
- socket_transport->transport.close = socket_close;
- socket_transport->transport.read = socket_read;
- socket_transport->transport.write = socket_write;
-
- if (!listen_socket_init(socket_transport)) {
- D(ERR, "socket transport init failed");
- free(socket_transport);
- return 0;
- }
-
- transport_register(&socket_transport->transport);
- return 1;
-}
-
diff --git a/fastbootd/trigger.c b/fastbootd/trigger.c
deleted file mode 100644
index e63e64d..0000000
--- a/fastbootd/trigger.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <dlfcn.h>
-
-#include <hardware/hardware.h>
-#include "debug.h"
-#include "trigger.h"
-#include "protocol.h"
-#include "vendor_trigger.h"
-
-static const int version = 1;
-
-static struct vendor_trigger_t *triggers = NULL;
-
-int load_trigger() {
- int err;
- hw_module_t* module;
- hw_device_t* device;
- int libversion;
-
- err = hw_get_module(TRIGGER_MODULE_ID, (hw_module_t const**)&module);
-
- if (err == 0) {
- err = module->methods->open(module, NULL, &device);
-
- if (err == 0) {
- triggers = (struct vendor_trigger_t *) device;
- } else {
- D(WARN, "Libvendor load error");
- return 1;
- }
- }
- else {
- D(WARN, "Libvendor not load: %s", strerror(-err));
- return 0;
- }
-
- if (triggers->check_version != NULL &&
- triggers->check_version(version, &libversion)) {
-
- triggers = NULL;
- D(ERR, "Library report incompability");
- return 1;
- }
- D(INFO, "libvendortrigger loaded");
-
- return 0;
-}
-
-int trigger_oem_cmd(const char *arg, const char **response) {
- if (triggers != NULL && triggers->oem_cmd != NULL)
- return triggers->oem_cmd(arg, response);
- return 0;
-}
-
-int trigger_gpt_layout(struct GPT_content *table) {
- if (triggers != NULL && triggers->gpt_layout != NULL)
- return triggers->gpt_layout(table);
- return 0;
-}
-
diff --git a/fastbootd/usb_linux_client.c b/fastbootd/usb_linux_client.c
deleted file mode 100644
index 64420e9..0000000
--- a/fastbootd/usb_linux_client.c
+++ /dev/null
@@ -1,308 +0,0 @@
-/*
- * 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.
- */
-
-#include <endian.h>
-#include <fcntl.h>
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sys/ioctl.h>
-#include <sys/types.h>
-
-#include <linux/usb/ch9.h>
-#include <linux/usb/functionfs.h>
-
-#include "debug.h"
-#include "transport.h"
-#include "utils.h"
-
-#define TRACE_TAG TRACE_USB
-
-#define MAX_PACKET_SIZE_FS 64
-#define MAX_PACKET_SIZE_HS 512
-
-#define cpu_to_le16(x) htole16(x)
-#define cpu_to_le32(x) htole32(x)
-
-#define FASTBOOT_CLASS 0xff
-#define FASTBOOT_SUBCLASS 0x42
-#define FASTBOOT_PROTOCOL 0x3
-
-#define USB_FFS_FASTBOOT_PATH "/dev/usb-ffs/adb/"
-#define USB_FFS_FASTBOOT_EP(x) USB_FFS_FASTBOOT_PATH#x
-
-#define USB_FFS_FASTBOOT_EP0 USB_FFS_FASTBOOT_EP(ep0)
-#define USB_FFS_FASTBOOT_OUT USB_FFS_FASTBOOT_EP(ep1)
-#define USB_FFS_FASTBOOT_IN USB_FFS_FASTBOOT_EP(ep2)
-
-#define container_of(ptr, type, member) \
- ((type*)((char*)(ptr) - offsetof(type, member)))
-
-struct usb_transport {
- struct transport transport;
-
- pthread_cond_t notify;
- pthread_mutex_t lock;
-
- int control;
- int bulk_out; /* "out" from the host's perspective => source for fastbootd */
- int bulk_in; /* "in" from the host's perspective => sink for fastbootd */
-};
-
-struct usb_handle {
- struct transport_handle handle;
-};
-
-static const struct {
- struct usb_functionfs_descs_head header;
- struct {
- struct usb_interface_descriptor intf;
- struct usb_endpoint_descriptor_no_audio source;
- struct usb_endpoint_descriptor_no_audio sink;
- } __attribute__((packed)) fs_descs, hs_descs;
-} __attribute__((packed)) descriptors = {
- .header = {
- .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
- .length = cpu_to_le32(sizeof(descriptors)),
- .fs_count = 3,
- .hs_count = 3,
- },
- .fs_descs = {
- .intf = {
- .bLength = sizeof(descriptors.fs_descs.intf),
- .bDescriptorType = USB_DT_INTERFACE,
- .bInterfaceNumber = 0,
- .bNumEndpoints = 2,
- .bInterfaceClass = FASTBOOT_CLASS,
- .bInterfaceSubClass = FASTBOOT_SUBCLASS,
- .bInterfaceProtocol = FASTBOOT_PROTOCOL,
- .iInterface = 1, /* first string from the provided table */
- },
- .source = {
- .bLength = sizeof(descriptors.fs_descs.source),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 1 | USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_FS,
- },
- .sink = {
- .bLength = sizeof(descriptors.fs_descs.sink),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 2 | USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_FS,
- },
- },
- .hs_descs = {
- .intf = {
- .bLength = sizeof(descriptors.hs_descs.intf),
- .bDescriptorType = USB_DT_INTERFACE,
- .bInterfaceNumber = 0,
- .bNumEndpoints = 2,
- .bInterfaceClass = FASTBOOT_CLASS,
- .bInterfaceSubClass = FASTBOOT_SUBCLASS,
- .bInterfaceProtocol = FASTBOOT_PROTOCOL,
- .iInterface = 1, /* first string from the provided table */
- },
- .source = {
- .bLength = sizeof(descriptors.hs_descs.source),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 1 | USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_HS,
- },
- .sink = {
- .bLength = sizeof(descriptors.hs_descs.sink),
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = 2 | USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = MAX_PACKET_SIZE_HS,
- },
- },
-};
-
-#define STR_INTERFACE_ "Fastboot Interface"
-
-static const struct {
- struct usb_functionfs_strings_head header;
- struct {
- __le16 code;
- const char str1[sizeof(STR_INTERFACE_)];
- } __attribute__((packed)) lang0;
-} __attribute__((packed)) strings = {
- .header = {
- .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
- .length = cpu_to_le32(sizeof(strings)),
- .str_count = cpu_to_le32(1),
- .lang_count = cpu_to_le32(1),
- },
- .lang0 = {
- cpu_to_le16(0x0409), /* en-us */
- STR_INTERFACE_,
- },
-};
-
-static int init_functionfs(struct usb_transport *usb_transport)
-{
- ssize_t ret;
-
- D(VERBOSE, "OPENING %s", USB_FFS_FASTBOOT_EP0);
- usb_transport->control = open(USB_FFS_FASTBOOT_EP0, O_RDWR);
- if (usb_transport->control < 0) {
- D(ERR, "[ %s: cannot open control endpoint: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
- goto err;
- }
-
- ret = write(usb_transport->control, &descriptors, sizeof(descriptors));
- if (ret < 0) {
- D(ERR, "[ %s: write descriptors failed: errno=%d ]", USB_FFS_FASTBOOT_EP0, errno);
- goto err;
- }
-
- ret = write(usb_transport->control, &strings, sizeof(strings));
- if (ret < 0) {
- D(ERR, "[ %s: writing strings failed: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
- goto err;
- }
-
- usb_transport->bulk_out = open(USB_FFS_FASTBOOT_OUT, O_RDWR);
- if (usb_transport->bulk_out < 0) {
- D(ERR, "[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_FASTBOOT_OUT, errno);
- goto err;
- }
-
- usb_transport->bulk_in = open(USB_FFS_FASTBOOT_IN, O_RDWR);
- if (usb_transport->bulk_in < 0) {
- D(ERR, "[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_FASTBOOT_IN, errno);
- goto err;
- }
-
- return 0;
-
-err:
- if (usb_transport->bulk_in > 0) {
- close(usb_transport->bulk_in);
- usb_transport->bulk_in = -1;
- }
- if (usb_transport->bulk_out > 0) {
- close(usb_transport->bulk_out);
- usb_transport->bulk_out = -1;
- }
- if (usb_transport->control > 0) {
- close(usb_transport->control);
- usb_transport->control = -1;
- }
- return -1;
-}
-
-static ssize_t usb_write(struct transport_handle *thandle, const void *data, size_t len)
-{
- ssize_t ret;
- struct transport *t = thandle->transport;
- struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
- D(DEBUG, "about to write (fd=%d, len=%zu)", usb_transport->bulk_in, len);
- ret = bulk_write(usb_transport->bulk_in, data, len);
- if (ret < 0) {
- D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_in, ret);
- return -1;
- }
- D(DEBUG, "[ usb_write done fd=%d ]", usb_transport->bulk_in);
- return ret;
-}
-
-ssize_t usb_read(struct transport_handle *thandle, void *data, size_t len)
-{
- ssize_t ret;
- struct transport *t = thandle->transport;
- struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
- D(DEBUG, "about to read (fd=%d, len=%zu)", usb_transport->bulk_out, len);
- ret = bulk_read(usb_transport->bulk_out, data, len);
- if (ret < 0) {
- D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_out, ret);
- return -1;
- }
- D(DEBUG, "[ usb_read done fd=%d ret=%zd]", usb_transport->bulk_out, ret);
- return ret;
-}
-
-void usb_close(struct transport_handle *thandle)
-{
- int err;
- struct transport *t = thandle->transport;
- struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
- err = ioctl(usb_transport->bulk_in, FUNCTIONFS_CLEAR_HALT);
- if (err < 0)
- D(WARN, "[ kick: source (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_in, errno);
-
- err = ioctl(usb_transport->bulk_out, FUNCTIONFS_CLEAR_HALT);
- if (err < 0)
- D(WARN, "[ kick: sink (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_out, errno);
-
- pthread_mutex_lock(&usb_transport->lock);
- close(usb_transport->control);
- close(usb_transport->bulk_out);
- close(usb_transport->bulk_in);
- usb_transport->control = usb_transport->bulk_out = usb_transport->bulk_in = -1;
-
- pthread_cond_signal(&usb_transport->notify);
- pthread_mutex_unlock(&usb_transport->lock);
-}
-
-struct transport_handle *usb_connect(struct transport *transport)
-{
- int ret;
- struct usb_handle *usb_handle = calloc(sizeof(struct usb_handle), 1);
- struct usb_transport *usb_transport = container_of(transport, struct usb_transport, transport);
-
- pthread_mutex_lock(&usb_transport->lock);
- while (usb_transport->control != -1)
- pthread_cond_wait(&usb_transport->notify, &usb_transport->lock);
- pthread_mutex_unlock(&usb_transport->lock);
-
- ret = init_functionfs(usb_transport);
- if (ret < 0) {
- D(ERR, "usb connect: failed to initialize usb transport");
- return NULL;
- }
-
- D(DEBUG, "[ usb_thread - registering device ]");
- return &usb_handle->handle;
-}
-
-void usb_init()
-{
- struct usb_transport *usb_transport = calloc(1, sizeof(struct usb_transport));
-
- usb_transport->transport.connect = usb_connect;
- usb_transport->transport.close = usb_close;
- usb_transport->transport.read = usb_read;
- usb_transport->transport.write = usb_write;
- usb_transport->control = -1;
- usb_transport->bulk_out = -1;
- usb_transport->bulk_out = -1;
-
- pthread_cond_init(&usb_transport->notify, NULL);
- pthread_mutex_init(&usb_transport->lock, NULL);
-
- transport_register(&usb_transport->transport);
-}
-
diff --git a/fastbootd/utils.c b/fastbootd/utils.c
deleted file mode 100644
index bef2463..0000000
--- a/fastbootd/utils.c
+++ /dev/null
@@ -1,260 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <sys/ioctl.h>
-#include <linux/fs.h>
-#include <stdlib.h>
-#include <cutils/properties.h>
-
-#include "utils.h"
-#include "debug.h"
-
-#ifndef BLKDISCARD
-#define BLKDISCARD _IO(0x12,119)
-#endif
-
-#ifndef BLKSECDISCARD
-#define BLKSECDISCARD _IO(0x12,125)
-#endif
-
-#define READ_BUF_SIZE (16*1024)
-
-int get_stream_size(FILE *stream) {
- int size;
- fseek(stream, 0, SEEK_END);
- size = ftell(stream);
- fseek(stream, 0, SEEK_SET);
- return size;
-}
-
-uint64_t get_block_device_size(int fd)
-{
- uint64_t size = 0;
- int ret;
-
- ret = ioctl(fd, BLKGETSIZE64, &size);
-
- if (ret)
- return 0;
-
- return size;
-}
-
-uint64_t get_file_size(int fd)
-{
- struct stat buf;
- int ret;
- int64_t computed_size;
-
- ret = fstat(fd, &buf);
- if (ret)
- return 0;
-
- if (S_ISREG(buf.st_mode))
- computed_size = buf.st_size;
- else if (S_ISBLK(buf.st_mode))
- computed_size = get_block_device_size(fd);
- else
- computed_size = 0;
-
- return computed_size;
-}
-
-uint64_t get_file_size64(int fd)
-{
- struct stat64 buf;
- int ret;
- uint64_t computed_size;
-
- ret = fstat64(fd, &buf);
- if (ret)
- return 0;
-
- if (S_ISREG(buf.st_mode))
- computed_size = buf.st_size;
- else if (S_ISBLK(buf.st_mode))
- computed_size = get_block_device_size(fd);
- else
- computed_size = 0;
-
- return computed_size;
-}
-
-
-char *strip(char *str)
-{
- int n;
-
- n = strspn(str, " \t");
- str += n;
- n = strcspn(str, " \t");
- str[n] = '\0';
-
- return str;
-}
-
-int wipe_block_device(int fd, int64_t len)
-{
- uint64_t range[2];
- int ret;
-
- range[0] = 0;
- range[1] = len;
- ret = ioctl(fd, BLKSECDISCARD, &range);
- if (ret < 0) {
- range[0] = 0;
- range[1] = len;
- ret = ioctl(fd, BLKDISCARD, &range);
- if (ret < 0) {
- D(WARN, "Discard failed\n");
- return 1;
- } else {
- D(WARN, "Wipe via secure discard failed, used discard instead\n");
- return 0;
- }
- }
-
- return 0;
-}
-
-int create_temp_file() {
- char tempname[] = "/dev/fastboot_data_XXXXXX";
- int fd;
-
- fd = mkstemp(tempname);
- if (fd < 0)
- return -1;
-
- unlink(tempname);
-
- return fd;
-}
-
-ssize_t bulk_write(int bulk_in, const char *buf, size_t length)
-{
- size_t count = 0;
- ssize_t ret;
-
- do {
- ret = TEMP_FAILURE_RETRY(write(bulk_in, buf + count, length - count));
- if (ret < 0) {
- D(WARN, "[ bulk_write failed fd=%d length=%zu errno=%d %s ]",
- bulk_in, length, errno, strerror(errno));
- return -1;
- } else {
- count += ret;
- }
- } while (count < length);
-
- D(VERBOSE, "[ bulk_write done fd=%d ]", bulk_in);
- return count;
-}
-
-ssize_t bulk_read(int bulk_out, char *buf, size_t length)
-{
- ssize_t ret;
- size_t n = 0;
-
- while (n < length) {
- size_t to_read = (length - n > READ_BUF_SIZE) ? READ_BUF_SIZE : length - n;
- ret = TEMP_FAILURE_RETRY(read(bulk_out, buf + n, to_read));
- if (ret < 0) {
- D(WARN, "[ bulk_read failed fd=%d length=%zu errno=%d %s ]",
- bulk_out, length, errno, strerror(errno));
- return ret;
- }
- n += ret;
- if (ret < (ssize_t)to_read) {
- D(VERBOSE, "bulk_read short read, ret=%zd to_read=%zu n=%zu length=%zu",
- ret, to_read, n, length);
- break;
- }
- }
-
- return n;
-}
-
-#define NAP_TIME 200 // 200 ms between polls
-static int wait_for_property(const char *name, const char *desired_value, int maxwait)
-{
- char value[PROPERTY_VALUE_MAX] = {'\0'};
- int maxnaps = (maxwait * 1000) / NAP_TIME;
-
- if (maxnaps < 1) {
- maxnaps = 1;
- }
-
- while (maxnaps-- > 0) {
- usleep(NAP_TIME * 1000);
- if (property_get(name, value, NULL)) {
- if (desired_value == NULL || strcmp(value, desired_value) == 0) {
- return 0;
- }
- }
- }
- return -1; /* failure */
-}
-
-int service_start(const char *service_name)
-{
- int result = 0;
- char property_value[PROPERTY_VALUE_MAX];
-
- property_get(service_name, property_value, "");
- if (strcmp("running", property_value) != 0) {
- D(INFO, "Starting %s", service_name);
- property_set("ctl.start", service_name);
- if (wait_for_property(service_name, "running", 5))
- result = -1;
- }
-
- return result;
-}
-
-int service_stop(const char *service_name)
-{
- int result = 0;
-
- D(INFO, "Stopping MDNSD");
- property_set("ctl.stop", service_name);
- if (wait_for_property(service_name, "stopped", 5))
- result = -1;
-
- return result;
-}
-
-int ssh_server_start()
-{
- return service_start("sshd");
-}
diff --git a/fastbootd/utils.h b/fastbootd/utils.h
deleted file mode 100644
index 3d98699..0000000
--- a/fastbootd/utils.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOT_UTLIS_H
-#define _FASTBOOT_UTILS_H
-
-#include <stdio.h>
-
-int get_stream_size(FILE *);
-
-char *strip(char *str);
-
-uint64_t get_file_size64(int fd);
-uint64_t get_file_size(int fd);
-uint64_t get_block_device_size(int fd);
-int wipe_block_device(int fd, int64_t len);
-int create_temp_file();
-ssize_t bulk_read(int bulk_out, char *buf, size_t length);
-ssize_t bulk_write(int bulk_in, const char *buf, size_t length);
-int service_start(const char *service_name);
-int service_stop(const char *service_name);
-int ssh_server_start();
-
-#define ROUND_TO_PAGE(address,pagesize) ((address + pagesize - 1) & (~(pagesize - 1)))
-
-#define ROUND_UP(number,size) (((number + size - 1) / size) * size)
-
-#endif
diff --git a/fs_mgr/Android.mk b/fs_mgr/Android.mk
index 165ebd4..61bf1ee 100644
--- a/fs_mgr/Android.mk
+++ b/fs_mgr/Android.mk
@@ -11,6 +11,11 @@
LOCAL_STATIC_LIBRARIES := liblogwrap libmincrypt libext4_utils_static
LOCAL_C_INCLUDES += system/extras/ext4_utils
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
+
+ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT)))
+LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
+endif
include $(BUILD_STATIC_LIBRARY)
@@ -31,5 +36,7 @@
LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils liblog libc libmincrypt libext4_utils_static
+LOCAL_CFLAGS := -Werror
+
include $(BUILD_EXECUTABLE)
diff --git a/fs_mgr/fs_mgr.c b/fs_mgr/fs_mgr.c
index 4c2f247..d4daed6 100644
--- a/fs_mgr/fs_mgr.c
+++ b/fs_mgr/fs_mgr.c
@@ -28,9 +28,13 @@
#include <libgen.h>
#include <time.h>
#include <sys/swap.h>
+#include <dirent.h>
+#include <ext4.h>
+#include <ext4_sb.h>
#include <linux/loop.h>
#include <private/android_filesystem_config.h>
+#include <cutils/android_reboot.h>
#include <cutils/partition_utils.h>
#include <cutils/properties.h>
#include <logwrap/logwrap.h>
@@ -46,6 +50,7 @@
#define KEY_IN_FOOTER "footer"
#define E2FSCK_BIN "/system/bin/e2fsck"
+#define F2FS_FSCK_BIN "/system/bin/fsck.f2fs"
#define MKSWAP_BIN "/system/bin/mkswap"
#define FSCK_LOG_FILE "/dev/fscklogs/log"
@@ -112,19 +117,54 @@
* fix the filesystem.
*/
ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
+ INFO("%s(): mount(%s,%s,%s)=%d\n", __func__, blk_device, target, fs_type, ret);
if (!ret) {
- umount(target);
+ int i;
+ for (i = 0; i < 5; i++) {
+ // Try to umount 5 times before continuing on.
+ // Should we try rebooting if all attempts fail?
+ int result = umount(target);
+ if (result == 0) {
+ break;
+ }
+ ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
+ sleep(1);
+ }
}
- INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
+ /*
+ * Some system images do not have e2fsck for licensing reasons
+ * (e.g. recent SDK system images). Detect these and skip the check.
+ */
+ if (access(E2FSCK_BIN, X_OK)) {
+ INFO("Not running %s on %s (executable not in system image)\n",
+ E2FSCK_BIN, blk_device);
+ } else {
+ INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
- ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
+ ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
+ &status, true, LOG_KLOG | LOG_FILE,
+ true, FSCK_LOG_FILE);
+
+ if (ret < 0) {
+ /* No need to check for error in fork, we can't really handle it now */
+ ERROR("Failed trying to run %s\n", E2FSCK_BIN);
+ }
+ }
+ } else if (!strcmp(fs_type, "f2fs")) {
+ char *f2fs_fsck_argv[] = {
+ F2FS_FSCK_BIN,
+ "-f",
+ blk_device
+ };
+ INFO("Running %s -f %s\n", F2FS_FSCK_BIN, blk_device);
+
+ ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv,
&status, true, LOG_KLOG | LOG_FILE,
true, FSCK_LOG_FILE);
-
if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */
- ERROR("Failed trying to run %s\n", E2FSCK_BIN);
+ ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN);
}
}
@@ -146,19 +186,22 @@
* Mark the given block device as read-only, using the BLKROSET ioctl.
* Return 0 on success, and -1 on error.
*/
-static void fs_set_blk_ro(const char *blockdev)
+int fs_mgr_set_blk_ro(const char *blockdev)
{
int fd;
+ int rc = -1;
int ON = 1;
- fd = open(blockdev, O_RDONLY);
+ fd = TEMP_FAILURE_RETRY(open(blockdev, O_RDONLY | O_CLOEXEC));
if (fd < 0) {
// should never happen
- return;
+ return rc;
}
- ioctl(fd, BLKROSET, &ON);
- close(fd);
+ rc = ioctl(fd, BLKROSET, &ON);
+ TEMP_FAILURE_RETRY(close(fd));
+
+ return rc;
}
/*
@@ -166,16 +209,27 @@
* sets the underlying block device to read-only if the mount is read-only.
* See "man 2 mount" for return values.
*/
-static int __mount(const char *source, const char *target,
- const char *filesystemtype, unsigned long mountflags,
- const void *data)
+static int __mount(const char *source, const char *target, const struct fstab_rec *rec)
{
- int ret = mount(source, target, filesystemtype, mountflags, data);
+ unsigned long mountflags = rec->flags;
+ int ret;
+ int save_errno;
+ /* We need this because sometimes we have legacy symlinks
+ * that are lingering around and need cleaning up.
+ */
+ struct stat info;
+ if (!lstat(target, &info))
+ if ((info.st_mode & S_IFMT) == S_IFLNK)
+ unlink(target);
+ mkdir(target, 0755);
+ ret = mount(source, target, rec->fs_type, mountflags, rec->fs_options);
+ save_errno = errno;
+ INFO("%s(source=%s,target=%s,type=%s)=%d\n", __func__, source, target, rec->fs_type, ret);
if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
- fs_set_blk_ro(source);
+ fs_mgr_set_blk_ro(source);
}
-
+ errno = save_errno;
return ret;
}
@@ -199,16 +253,197 @@
return ret;
}
+static int device_is_debuggable() {
+ int ret = -1;
+ char value[PROP_VALUE_MAX];
+ ret = __system_property_get("ro.debuggable", value);
+ if (ret < 0)
+ return ret;
+ return strcmp(value, "1") ? 0 : 1;
+}
+
+static int device_is_secure() {
+ int ret = -1;
+ char value[PROP_VALUE_MAX];
+ ret = __system_property_get("ro.secure", value);
+ /* If error, we want to fail secure */
+ if (ret < 0)
+ return 1;
+ return strcmp(value, "0") ? 1 : 0;
+}
+
+static int device_is_force_encrypted() {
+ int ret = -1;
+ char value[PROP_VALUE_MAX];
+ ret = __system_property_get("ro.vold.forceencryption", value);
+ if (ret < 0)
+ return 0;
+ return strcmp(value, "1") ? 0 : 1;
+}
+
+/*
+ * Tries to mount any of the consecutive fstab entries that match
+ * the mountpoint of the one given by fstab->recs[start_idx].
+ *
+ * end_idx: On return, will be the last rec that was looked at.
+ * attempted_idx: On return, will indicate which fstab rec
+ * succeeded. In case of failure, it will be the start_idx.
+ * Returns
+ * -1 on failure with errno set to match the 1st mount failure.
+ * 0 on success.
+ */
+static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx)
+{
+ int i;
+ int mount_errno = 0;
+ int mounted = 0;
+
+ if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) {
+ errno = EINVAL;
+ if (end_idx) *end_idx = start_idx;
+ if (attempted_idx) *end_idx = start_idx;
+ return -1;
+ }
+
+ /* Hunt down an fstab entry for the same mount point that might succeed */
+ for (i = start_idx;
+ /* We required that fstab entries for the same mountpoint be consecutive */
+ i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point);
+ i++) {
+ /*
+ * Don't try to mount/encrypt the same mount point again.
+ * Deal with alternate entries for the same point which are required to be all following
+ * each other.
+ */
+ if (mounted) {
+ ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__,
+ fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
+ continue;
+ }
+
+ if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
+ check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
+ fstab->recs[i].mount_point);
+ }
+ if (!__mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, &fstab->recs[i])) {
+ *attempted_idx = i;
+ mounted = 1;
+ if (i != start_idx) {
+ ERROR("%s(): Mounted %s on %s with fs_type=%s instead of %s\n", __func__,
+ fstab->recs[i].blk_device, fstab->recs[i].mount_point, fstab->recs[i].fs_type,
+ fstab->recs[start_idx].fs_type);
+ }
+ } else {
+ /* back up errno for crypto decisions */
+ mount_errno = errno;
+ }
+ }
+
+ /* Adjust i for the case where it was still withing the recs[] */
+ if (i < fstab->num_entries) --i;
+
+ *end_idx = i;
+ if (!mounted) {
+ *attempted_idx = start_idx;
+ errno = mount_errno;
+ return -1;
+ }
+ return 0;
+}
+
+static int translate_ext_labels(struct fstab_rec *rec)
+{
+ DIR *blockdir = NULL;
+ struct dirent *ent;
+ char *label;
+ size_t label_len;
+ int ret = -1;
+
+ if (strncmp(rec->blk_device, "LABEL=", 6))
+ return 0;
+
+ label = rec->blk_device + 6;
+ label_len = strlen(label);
+
+ if (label_len > 16) {
+ ERROR("FS label is longer than allowed by filesystem\n");
+ goto out;
+ }
+
+
+ blockdir = opendir("/dev/block");
+ if (!blockdir) {
+ ERROR("couldn't open /dev/block\n");
+ goto out;
+ }
+
+ while ((ent = readdir(blockdir))) {
+ int fd;
+ char super_buf[1024];
+ struct ext4_super_block *sb;
+
+ if (ent->d_type != DT_BLK)
+ continue;
+
+ fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY);
+ if (fd < 0) {
+ ERROR("Cannot open block device /dev/block/%s\n", ent->d_name);
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
+ TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) {
+ /* Probably a loopback device or something else without a readable
+ * superblock.
+ */
+ close(fd);
+ continue;
+ }
+
+ sb = (struct ext4_super_block *)super_buf;
+ if (sb->s_magic != EXT4_SUPER_MAGIC) {
+ INFO("/dev/block/%s not ext{234}\n", ent->d_name);
+ continue;
+ }
+
+ if (!strncmp(label, sb->s_volume_name, label_len)) {
+ char *new_blk_device;
+
+ if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) {
+ ERROR("Could not allocate block device string\n");
+ goto out;
+ }
+
+ INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device);
+
+ free(rec->blk_device);
+ rec->blk_device = new_blk_device;
+ ret = 0;
+ break;
+ }
+ }
+
+out:
+ closedir(blockdir);
+ return ret;
+}
+
+/* When multiple fstab records share the same mount_point, it will
+ * try to mount each one in turn, and ignore any duplicates after a
+ * first successful mount.
+ * Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
+ */
int fs_mgr_mount_all(struct fstab *fstab)
{
int i = 0;
- int encrypted = 0;
- int ret = -1;
- int mret;
- int mount_errno;
+ int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
+ int error_count = 0;
+ int mret = -1;
+ int mount_errno = 0;
+ int attempted_idx = -1;
if (!fstab) {
- return ret;
+ return -1;
}
for (i = 0; i < fstab->num_entries; i++) {
@@ -224,74 +459,111 @@
continue;
}
- if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
- wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
- }
-
- if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
- check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
- fstab->recs[i].mount_point);
- }
-
- if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) {
- if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) {
- ERROR("Could not set up verified partition, skipping!");
+ /* Translate LABEL= file system labels into block devices */
+ if (!strcmp(fstab->recs[i].fs_type, "ext2") ||
+ !strcmp(fstab->recs[i].fs_type, "ext3") ||
+ !strcmp(fstab->recs[i].fs_type, "ext4")) {
+ int tret = translate_ext_labels(&fstab->recs[i]);
+ if (tret < 0) {
+ ERROR("Could not translate label to block device\n");
continue;
}
}
- mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point,
- fstab->recs[i].fs_type, fstab->recs[i].flags,
- fstab->recs[i].fs_options);
+ if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
+ wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
+ }
+ if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
+ int rc = fs_mgr_setup_verity(&fstab->recs[i]);
+ if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
+ INFO("Verity disabled");
+ } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
+ ERROR("Could not set up verified partition, skipping!\n");
+ continue;
+ }
+ }
+ int last_idx_inspected;
+ mret = mount_with_alternatives(fstab, i, &last_idx_inspected, &attempted_idx);
+ i = last_idx_inspected;
+ mount_errno = errno;
+
+ /* Deal with encryptability. */
if (!mret) {
+ /* If this is encryptable, need to trigger encryption */
+ if ( (fstab->recs[attempted_idx].fs_mgr_flags & MF_FORCECRYPT)
+ || (device_is_force_encrypted()
+ && fs_mgr_is_encryptable(&fstab->recs[attempted_idx]))) {
+ if (umount(fstab->recs[attempted_idx].mount_point) == 0) {
+ if (encryptable == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
+ ERROR("Will try to encrypt %s %s\n", fstab->recs[attempted_idx].mount_point,
+ fstab->recs[attempted_idx].fs_type);
+ encryptable = FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION;
+ } else {
+ ERROR("Only one encryptable/encrypted partition supported\n");
+ encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
+ }
+ } else {
+ WARNING("Could not umount %s (%s) - allow continue unencrypted\n",
+ fstab->recs[attempted_idx].mount_point, strerror(errno));
+ continue;
+ }
+ }
/* Success! Go get the next one */
continue;
}
- /* back up errno as partition_wipe clobbers the value */
- mount_errno = errno;
-
- /* mount(2) returned an error, check if it's encrypted and deal with it */
- if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) &&
- !partition_wiped(fstab->recs[i].blk_device)) {
- /* Need to mount a tmpfs at this mountpoint for now, and set
- * properties that vold will query later for decrypting
- */
- if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs",
- MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
- ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s error: %s\n",
- fstab->recs[i].mount_point, strerror(errno));
- goto out;
+ /* mount(2) returned an error, check if it's encryptable and deal with it */
+ if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
+ fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) {
+ if(partition_wiped(fstab->recs[attempted_idx].blk_device)) {
+ ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__,
+ fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
+ fstab->recs[attempted_idx].fs_type);
+ encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
+ continue;
+ } else {
+ /* Need to mount a tmpfs at this mountpoint for now, and set
+ * properties that vold will query later for decrypting
+ */
+ ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__,
+ fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
+ fstab->recs[attempted_idx].fs_type);
+ if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) {
+ ++error_count;
+ continue;
+ }
}
- encrypted = 1;
+ encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
} else {
ERROR("Failed to mount an un-encryptable or wiped partition on"
- "%s at %s options: %s error: %s\n",
- fstab->recs[i].blk_device, fstab->recs[i].mount_point,
- fstab->recs[i].fs_options, strerror(mount_errno));
- goto out;
+ "%s at %s options: %s error: %s\n",
+ fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
+ fstab->recs[attempted_idx].fs_options, strerror(mount_errno));
+ ++error_count;
+ continue;
}
}
- if (encrypted) {
- ret = 1;
+ if (error_count) {
+ return -1;
} else {
- ret = 0;
+ return encryptable;
}
-
-out:
- return ret;
}
/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
* tmp mount we do to check the user password
+ * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one
+ * in turn, and stop on 1st success, or no more match.
*/
int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
char *tmp_mount_point)
{
int i = 0;
- int ret = -1;
+ int ret = FS_MGR_DOMNT_FAILED;
+ int mount_errors = 0;
+ int first_mount_errno = 0;
char *m;
if (!fstab) {
@@ -323,9 +595,12 @@
fstab->recs[i].mount_point);
}
- if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) {
- if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) {
- ERROR("Could not set up verified partition, skipping!");
+ if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
+ int rc = fs_mgr_setup_verity(&fstab->recs[i]);
+ if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
+ INFO("Verity disabled");
+ } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
+ ERROR("Could not set up verified partition, skipping!\n");
continue;
}
}
@@ -336,19 +611,27 @@
} else {
m = fstab->recs[i].mount_point;
}
- if (__mount(n_blk_device, m, fstab->recs[i].fs_type,
- fstab->recs[i].flags, fstab->recs[i].fs_options)) {
- ERROR("Cannot mount filesystem on %s at %s options: %s error: %s\n",
- n_blk_device, m, fstab->recs[i].fs_options, strerror(errno));
- goto out;
+ if (__mount(n_blk_device, m, &fstab->recs[i])) {
+ if (!first_mount_errno) first_mount_errno = errno;
+ mount_errors++;
+ continue;
} else {
ret = 0;
goto out;
}
}
-
- /* We didn't find a match, say so and return an error */
- ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
+ if (mount_errors) {
+ ERROR("Cannot mount filesystem on %s at %s. error: %s\n",
+ n_blk_device, m, strerror(first_mount_errno));
+ if (first_mount_errno == EBUSY) {
+ ret = FS_MGR_DOMNT_BUSY;
+ } else {
+ ret = FS_MGR_DOMNT_FAILED;
+ }
+ } else {
+ /* We didn't find a match, say so and return an error */
+ ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
+ }
out:
return ret;
@@ -428,7 +711,7 @@
zram_fp = fopen(ZRAM_CONF_DEV, "r+");
if (zram_fp == NULL) {
- ERROR("Unable to open zram conf device " ZRAM_CONF_DEV);
+ ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV);
ret = -1;
continue;
}
@@ -495,7 +778,7 @@
if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) {
continue;
}
- if (!(fstab->recs[i].fs_mgr_flags & MF_CRYPT)) {
+ if (!(fstab->recs[i].fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT))) {
continue;
}
diff --git a/fs_mgr/fs_mgr_fstab.c b/fs_mgr/fs_mgr_fstab.c
index 6145771..64f7edc 100644
--- a/fs_mgr/fs_mgr_fstab.c
+++ b/fs_mgr/fs_mgr_fstab.c
@@ -59,6 +59,7 @@
{ "wait", MF_WAIT },
{ "check", MF_CHECK },
{ "encryptable=",MF_CRYPT },
+ { "forceencrypt=",MF_FORCECRYPT },
{ "nonremovable",MF_NONREMOVABLE },
{ "voldmanaged=",MF_VOLDMANAGED},
{ "length=", MF_LENGTH },
@@ -67,6 +68,7 @@
{ "zramsize=", MF_ZRAMSIZE },
{ "verify", MF_VERIFY },
{ "noemulatedsd", MF_NOEMULATEDSD },
+ { "notrim", MF_NOTRIM },
{ "defaults", 0 },
{ 0, 0 },
};
@@ -106,6 +108,11 @@
* location of the keys. Get it and return it.
*/
flag_vals->key_loc = strdup(strchr(p, '=') + 1);
+ } else if ((fl[i].flag == MF_FORCECRYPT) && flag_vals) {
+ /* The forceencrypt flag is followed by an = and the
+ * location of the keys. Get it and return it.
+ */
+ flag_vals->key_loc = strdup(strchr(p, '=') + 1);
} else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
/* The length flag is followed by an = and the
* size of the partition. Get it and return it.
@@ -162,7 +169,6 @@
p = strtok_r(NULL, ",", &savep);
}
-out:
if (fs_options && fs_options[0]) {
/* remove the last trailing comma from the list of options */
fs_options[strlen(fs_options) - 1] = '\0';
@@ -181,7 +187,6 @@
const char *delim = " \t";
char *save_ptr, *p;
struct fstab *fstab = NULL;
- struct fstab_rec *recs;
struct fs_mgr_flag_values flag_vals;
#define FS_OPTIONS_LEN 1024
char tmp_fs_options[FS_OPTIONS_LEN];
@@ -337,7 +342,7 @@
/* Add an entry to the fstab, and return 0 on success or -1 on error */
int fs_mgr_add_entry(struct fstab *fstab,
const char *mount_point, const char *fs_type,
- const char *blk_device, long long length)
+ const char *blk_device)
{
struct fstab_rec *new_fstab_recs;
int n = fstab->num_entries;
@@ -363,25 +368,47 @@
return 0;
}
-struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path)
+/*
+ * Returns the 1st matching fstab_rec that follows the start_rec.
+ * start_rec is the result of a previous search or NULL.
+ */
+struct fstab_rec *fs_mgr_get_entry_for_mount_point_after(struct fstab_rec *start_rec, struct fstab *fstab, const char *path)
{
int i;
-
if (!fstab) {
return NULL;
}
- for (i = 0; i < fstab->num_entries; i++) {
+ if (start_rec) {
+ for (i = 0; i < fstab->num_entries; i++) {
+ if (&fstab->recs[i] == start_rec) {
+ i++;
+ break;
+ }
+ }
+ } else {
+ i = 0;
+ }
+ for (; i < fstab->num_entries; i++) {
int len = strlen(fstab->recs[i].mount_point);
if (strncmp(path, fstab->recs[i].mount_point, len) == 0 &&
(path[len] == '\0' || path[len] == '/')) {
return &fstab->recs[i];
}
}
-
return NULL;
}
+/*
+ * Returns the 1st matching mount point.
+ * There might be more. To look for others, use fs_mgr_get_entry_for_mount_point_after()
+ * and give the fstab_rec from the previous search.
+ */
+struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path)
+{
+ return fs_mgr_get_entry_for_mount_point_after(NULL, fstab, path);
+}
+
int fs_mgr_is_voldmanaged(struct fstab_rec *fstab)
{
return fstab->fs_mgr_flags & MF_VOLDMANAGED;
@@ -392,12 +419,22 @@
return fstab->fs_mgr_flags & MF_NONREMOVABLE;
}
+int fs_mgr_is_verified(struct fstab_rec *fstab)
+{
+ return fstab->fs_mgr_flags & MF_VERIFY;
+}
+
int fs_mgr_is_encryptable(struct fstab_rec *fstab)
{
- return fstab->fs_mgr_flags & MF_CRYPT;
+ return fstab->fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT);
}
int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab)
{
return fstab->fs_mgr_flags & MF_NOEMULATEDSD;
}
+
+int fs_mgr_is_notrim(struct fstab_rec *fstab)
+{
+ return fstab->fs_mgr_flags & MF_NOTRIM;
+}
diff --git a/fs_mgr/fs_mgr_main.c b/fs_mgr/fs_mgr_main.c
index 4bde4a1..e5a00d5 100644
--- a/fs_mgr/fs_mgr_main.c
+++ b/fs_mgr/fs_mgr_main.c
@@ -80,10 +80,10 @@
int a_flag=0;
int u_flag=0;
int n_flag=0;
- char *n_name;
- char *n_blk_dev;
- char *fstab_file;
- struct fstab *fstab;
+ char *n_name=NULL;
+ char *n_blk_dev=NULL;
+ char *fstab_file=NULL;
+ struct fstab *fstab=NULL;
klog_init();
klog_set_level(6);
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index 59ffd78..efc7ca5 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -21,9 +21,10 @@
#include <fs_mgr.h>
#define INFO(x...) KLOG_INFO("fs_mgr", x)
+#define WARNING(x...) KLOG_WARNING("fs_mgr", x)
#define ERROR(x...) KLOG_ERROR("fs_mgr", x)
-#define CRYPTO_TMPFS_OPTIONS "size=128m,mode=0771,uid=1000,gid=1000"
+#define CRYPTO_TMPFS_OPTIONS "size=256m,mode=0771,uid=1000,gid=1000"
#define WAIT_TIMEOUT 20
@@ -72,14 +73,14 @@
#define MF_SWAPPRIO 0x80
#define MF_ZRAMSIZE 0x100
#define MF_VERIFY 0x200
-/*
- * There is no emulated sdcard daemon running on /data/media on this device,
- * so treat the physical SD card as the only external storage device,
- * a la the Nexus One.
- */
-#define MF_NOEMULATEDSD 0x400
+#define MF_FORCECRYPT 0x400
+#define MF_NOEMULATEDSD 0x800 /* no emulated sdcard daemon, sd card is the only
+ external storage */
+#define MF_NOTRIM 0x1000
#define DM_BUF_SIZE 4096
+int fs_mgr_set_blk_ro(const char *blockdev);
+
#endif /* __CORE_FS_MGR_PRIV_H */
diff --git a/fs_mgr/fs_mgr_priv_verity.h b/fs_mgr/fs_mgr_priv_verity.h
index 6193784..f90e596 100644
--- a/fs_mgr/fs_mgr_priv_verity.h
+++ b/fs_mgr/fs_mgr_priv_verity.h
@@ -14,4 +14,7 @@
* limitations under the License.
*/
-int fs_mgr_setup_verity(struct fstab_rec *fstab);
\ No newline at end of file
+#define FS_MGR_SETUP_VERITY_DISABLED -2
+#define FS_MGR_SETUP_VERITY_FAIL -1
+#define FS_MGR_SETUP_VERITY_SUCCESS 0
+int fs_mgr_setup_verity(struct fstab_rec *fstab);
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index 40bc2ec..feb3c19 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -30,6 +30,7 @@
#include <time.h>
#include <private/android_filesystem_config.h>
+#include <cutils/properties.h>
#include <logwrap/logwrap.h>
#include "mincrypt/rsa.h"
@@ -41,10 +42,24 @@
#include "fs_mgr_priv.h"
#include "fs_mgr_priv_verity.h"
+#define FSTAB_PREFIX "/fstab."
+
#define VERITY_METADATA_SIZE 32768
-#define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
#define VERITY_TABLE_RSA_KEY "/verity_key"
+#define VERITY_STATE_HEADER 0x83c0ae9d
+#define VERITY_STATE_VERSION 1
+
+#define VERITY_KMSG_RESTART "dm-verity device corrupted"
+#define VERITY_KMSG_BUFSIZE 1024
+
+struct verity_state {
+ uint32_t header;
+ uint32_t version;
+ uint32_t size;
+ int32_t mode;
+};
+
extern struct fs_info info;
static RSAPublicKey *load_key(char *path)
@@ -85,13 +100,12 @@
static int verify_table(char *signature, char *table, int table_length)
{
- int fd;
RSAPublicKey *key;
- uint8_t hash_buf[SHA_DIGEST_SIZE];
+ uint8_t hash_buf[SHA256_DIGEST_SIZE];
int retval = -1;
// Hash the table
- SHA_hash((uint8_t*)table, table_length, hash_buf);
+ SHA256_hash((uint8_t*)table, table_length, hash_buf);
// Now get the public key from the keyfile
key = load_key(VERITY_TABLE_RSA_KEY);
@@ -105,7 +119,7 @@
(uint8_t*) signature,
RSANUMBYTES,
(uint8_t*) hash_buf,
- SHA_DIGEST_SIZE)) {
+ SHA256_DIGEST_SIZE)) {
ERROR("Couldn't verify table.");
goto out;
}
@@ -121,30 +135,32 @@
{
int data_device;
struct ext4_super_block sb;
- struct fs_info info = {0};
+ struct fs_info info;
- data_device = open(blk_device, O_RDONLY);
- if (data_device < 0) {
+ info.len = 0; /* Only len is set to 0 to ask the device for real size. */
+
+ data_device = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC));
+ if (data_device == -1) {
ERROR("Error opening block device (%s)", strerror(errno));
return -1;
}
- if (lseek64(data_device, 1024, SEEK_SET) < 0) {
+ if (TEMP_FAILURE_RETRY(lseek64(data_device, 1024, SEEK_SET)) < 0) {
ERROR("Error seeking to superblock");
- close(data_device);
+ TEMP_FAILURE_RETRY(close(data_device));
return -1;
}
- if (read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
+ if (TEMP_FAILURE_RETRY(read(data_device, &sb, sizeof(sb))) != sizeof(sb)) {
ERROR("Error reading superblock");
- close(data_device);
+ TEMP_FAILURE_RETRY(close(data_device));
return -1;
}
ext4_parse_sb(&sb, &info);
*device_size = info.len;
- close(data_device);
+ TEMP_FAILURE_RETRY(close(data_device));
return 0;
}
@@ -154,11 +170,13 @@
unsigned table_length;
uint64_t device_length;
int protocol_version;
- FILE *device;
- int retval = -1;
+ int device;
+ int retval = FS_MGR_SETUP_VERITY_FAIL;
+ *signature = 0;
+ *table = 0;
- device = fopen(block_device, "r");
- if (!device) {
+ device = TEMP_FAILURE_RETRY(open(block_device, O_RDONLY | O_CLOEXEC));
+ if (device == -1) {
ERROR("Could not open block device %s (%s).\n", block_device, strerror(errno));
goto out;
}
@@ -168,23 +186,35 @@
ERROR("Could not get target device size.\n");
goto out;
}
- if (fseek(device, device_length, SEEK_SET) < 0) {
+ if (TEMP_FAILURE_RETRY(lseek64(device, device_length, SEEK_SET)) < 0) {
ERROR("Could not seek to start of verity metadata block.\n");
goto out;
}
// check the magic number
- if (!fread(&magic_number, sizeof(int), 1, device)) {
+ if (TEMP_FAILURE_RETRY(read(device, &magic_number, sizeof(magic_number))) !=
+ sizeof(magic_number)) {
ERROR("Couldn't read magic number!\n");
goto out;
}
+
+#ifdef ALLOW_ADBD_DISABLE_VERITY
+ if (magic_number == VERITY_METADATA_MAGIC_DISABLE) {
+ retval = FS_MGR_SETUP_VERITY_DISABLED;
+ INFO("Attempt to cleanly disable verity - only works in USERDEBUG");
+ goto out;
+ }
+#endif
+
if (magic_number != VERITY_METADATA_MAGIC_NUMBER) {
- ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n", device_length);
+ ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n",
+ device_length);
goto out;
}
// check the protocol version
- if (!fread(&protocol_version, sizeof(int), 1, device)) {
+ if (TEMP_FAILURE_RETRY(read(device, &protocol_version,
+ sizeof(protocol_version))) != sizeof(protocol_version)) {
ERROR("Couldn't read verity metadata protocol version!\n");
goto out;
}
@@ -194,43 +224,49 @@
}
// get the signature
- *signature = (char*) malloc(RSANUMBYTES * sizeof(char));
+ *signature = (char*) malloc(RSANUMBYTES);
if (!*signature) {
ERROR("Couldn't allocate memory for signature!\n");
goto out;
}
- if (!fread(*signature, RSANUMBYTES, 1, device)) {
+ if (TEMP_FAILURE_RETRY(read(device, *signature, RSANUMBYTES)) != RSANUMBYTES) {
ERROR("Couldn't read signature from verity metadata!\n");
- free(*signature);
goto out;
}
// get the size of the table
- if (!fread(&table_length, sizeof(int), 1, device)) {
+ if (TEMP_FAILURE_RETRY(read(device, &table_length, sizeof(table_length))) !=
+ sizeof(table_length)) {
ERROR("Couldn't get the size of the verity table from metadata!\n");
- free(*signature);
goto out;
}
// get the table + null terminator
- table_length += 1;
- *table = malloc(table_length);
- if(!*table) {
+ *table = malloc(table_length + 1);
+ if (!*table) {
ERROR("Couldn't allocate memory for verity table!\n");
goto out;
}
- if (!fgets(*table, table_length, device)) {
+ if (TEMP_FAILURE_RETRY(read(device, *table, table_length)) !=
+ (ssize_t)table_length) {
ERROR("Couldn't read the verity table from metadata!\n");
- free(*table);
- free(*signature);
goto out;
}
- retval = 0;
+ (*table)[table_length] = 0;
+ retval = FS_MGR_SETUP_VERITY_SUCCESS;
out:
- if (device)
- fclose(device);
+ if (device != -1)
+ TEMP_FAILURE_RETRY(close(device));
+
+ if (retval != FS_MGR_SETUP_VERITY_SUCCESS) {
+ free(*table);
+ free(*signature);
+ *table = 0;
+ *signature = 0;
+ }
+
return retval;
}
@@ -273,10 +309,12 @@
return 0;
}
-static int load_verity_table(struct dm_ioctl *io, char *name, char *blockdev, int fd, char *table)
+static int load_verity_table(struct dm_ioctl *io, char *name, char *blockdev, int fd, char *table,
+ int mode)
{
char *verity_params;
char *buffer = (char*) io;
+ size_t bufsize;
uint64_t device_size = 0;
if (get_target_device_size(blockdev, &device_size) < 0) {
@@ -296,7 +334,15 @@
// build the verity params here
verity_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
- if (sprintf(verity_params, "%s", table) < 0) {
+ bufsize = DM_BUF_SIZE - (verity_params - buffer);
+
+ if (mode == VERITY_MODE_EIO) {
+ // allow operation with older dm-verity drivers that are unaware
+ // of the mode parameter by omitting it; this also means that we
+ // cannot use logging mode with these drivers, they always cause
+ // an I/O error for corrupted blocks
+ strcpy(verity_params, table);
+ } else if (snprintf(verity_params, bufsize, "%s %d", table, mode) < 0) {
return -1;
}
@@ -335,15 +381,300 @@
return -1;
}
+static int check_verity_restart(const char *fname)
+{
+ char buffer[VERITY_KMSG_BUFSIZE + 1];
+ int fd;
+ int rc = 0;
+ ssize_t size;
+ struct stat s;
+
+ fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
+
+ if (fd == -1) {
+ if (errno != ENOENT) {
+ ERROR("Failed to open %s (%s)\n", fname, strerror(errno));
+ }
+ goto out;
+ }
+
+ if (fstat(fd, &s) == -1) {
+ ERROR("Failed to fstat %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ size = VERITY_KMSG_BUFSIZE;
+
+ if (size > s.st_size) {
+ size = s.st_size;
+ }
+
+ if (lseek(fd, s.st_size - size, SEEK_SET) == -1) {
+ ERROR("Failed to lseek %jd %s (%s)\n", (intmax_t)(s.st_size - size), fname,
+ strerror(errno));
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(read(fd, buffer, size)) != size) {
+ ERROR("Failed to read %zd bytes from %s (%s)\n", size, fname,
+ strerror(errno));
+ goto out;
+ }
+
+ buffer[size] = '\0';
+
+ if (strstr(buffer, VERITY_KMSG_RESTART) != NULL) {
+ rc = 1;
+ }
+
+out:
+ if (fd != -1) {
+ TEMP_FAILURE_RETRY(close(fd));
+ }
+
+ return rc;
+}
+
+static int was_verity_restart()
+{
+ static const char *files[] = {
+ "/sys/fs/pstore/console-ramoops",
+ "/proc/last_kmsg",
+ NULL
+ };
+ int i;
+
+ for (i = 0; files[i]; ++i) {
+ if (check_verity_restart(files[i])) {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int write_verity_state(const char *fname, off64_t offset, int32_t mode)
+{
+ int fd;
+ int rc = -1;
+
+ struct verity_state s = {
+ VERITY_STATE_HEADER,
+ VERITY_STATE_VERSION,
+ sizeof(struct verity_state),
+ mode
+ };
+
+ fd = TEMP_FAILURE_RETRY(open(fname, O_WRONLY | O_SYNC | O_CLOEXEC));
+
+ if (fd == -1) {
+ ERROR("Failed to open %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(lseek64(fd, offset, SEEK_SET)) < 0) {
+ ERROR("Failed to seek %s to %" PRIu64 " (%s)\n", fname, offset, strerror(errno));
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(write(fd, &s, sizeof(s))) != sizeof(s)) {
+ ERROR("Failed to write %zu bytes to %s (%s)\n", sizeof(s), fname, strerror(errno));
+ goto out;
+ }
+
+ rc = 0;
+
+out:
+ if (fd != -1) {
+ TEMP_FAILURE_RETRY(close(fd));
+ }
+
+ return rc;
+}
+
+static int get_verity_state_location(char *location, off64_t *offset)
+{
+ char state_off[PROPERTY_VALUE_MAX];
+
+ if (property_get("ro.verity.state.location", location, NULL) <= 0) {
+ return -1;
+ }
+
+ if (*location != '/' || access(location, R_OK | W_OK) == -1) {
+ ERROR("Failed to access verity state %s (%s)\n", location, strerror(errno));
+ return -1;
+ }
+
+ *offset = 0;
+
+ if (property_get("ro.verity.state.offset", state_off, NULL) > 0) {
+ *offset = strtoll(state_off, NULL, 0);
+
+ if (errno == ERANGE || errno == EINVAL) {
+ ERROR("Invalid value in ro.verity.state.offset (%s)\n", state_off);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+int fs_mgr_load_verity_state(int *mode)
+{
+ char fname[PROPERTY_VALUE_MAX];
+ int fd = -1;
+ int rc = -1;
+ off64_t offset = 0;
+ struct verity_state s;
+
+ if (get_verity_state_location(fname, &offset) < 0) {
+ /* location for dm-verity state is not specified, fall back to
+ * default behavior: return -EIO for corrupted blocks */
+ *mode = VERITY_MODE_EIO;
+ rc = 0;
+ goto out;
+ }
+
+ if (was_verity_restart()) {
+ /* device was restarted after dm-verity detected a corrupted
+ * block, so switch to logging mode */
+ *mode = VERITY_MODE_LOGGING;
+ rc = write_verity_state(fname, offset, *mode);
+ goto out;
+ }
+
+ fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
+
+ if (fd == -1) {
+ ERROR("Failed to open %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(lseek64(fd, offset, SEEK_SET)) < 0) {
+ ERROR("Failed to seek %s to %" PRIu64 " (%s)\n", fname, offset, strerror(errno));
+ goto out;
+ }
+
+ if (TEMP_FAILURE_RETRY(read(fd, &s, sizeof(s))) != sizeof(s)) {
+ ERROR("Failed to read %zu bytes from %s (%s)\n", sizeof(s), fname, strerror(errno));
+ goto out;
+ }
+
+ if (s.header != VERITY_STATE_HEADER) {
+ goto out;
+ }
+
+ if (s.version != VERITY_STATE_VERSION) {
+ ERROR("Unsupported verity state version (%u)\n", s.version);
+ goto out;
+ }
+
+ if (s.size != sizeof(s)) {
+ ERROR("Unexpected verity state size (%u)\n", s.size);
+ goto out;
+ }
+
+ if (s.mode < VERITY_MODE_EIO ||
+ s.mode > VERITY_MODE_LAST) {
+ ERROR("Unsupported verity mode (%u)\n", s.mode);
+ goto out;
+ }
+
+ *mode = s.mode;
+ rc = 0;
+
+out:
+ if (fd != -1) {
+ TEMP_FAILURE_RETRY(close(fd));
+ }
+
+ return rc;
+}
+
+int fs_mgr_update_verity_state()
+{
+ _Alignas(struct dm_ioctl) char buffer[DM_BUF_SIZE];
+ char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+ char *mount_point;
+ char propbuf[PROPERTY_VALUE_MAX];
+ char state_loc[PROPERTY_VALUE_MAX];
+ char *status;
+ int fd = -1;
+ int i;
+ int rc = -1;
+ off64_t offset = 0;
+ struct dm_ioctl *io = (struct dm_ioctl *) buffer;
+ struct fstab *fstab = NULL;
+
+ if (get_verity_state_location(state_loc, &offset) < 0) {
+ goto out;
+ }
+
+ fd = TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC));
+
+ if (fd == -1) {
+ ERROR("Error opening device mapper (%s)\n", strerror(errno));
+ goto out;
+ }
+
+ property_get("ro.hardware", propbuf, "");
+ snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
+
+ fstab = fs_mgr_read_fstab(fstab_filename);
+
+ if (!fstab) {
+ ERROR("Failed to read %s\n", fstab_filename);
+ goto out;
+ }
+
+ for (i = 0; i < fstab->num_entries; i++) {
+ if (!fs_mgr_is_verified(&fstab->recs[i])) {
+ continue;
+ }
+
+ mount_point = basename(fstab->recs[i].mount_point);
+ verity_ioctl_init(io, mount_point, 0);
+
+ if (ioctl(fd, DM_TABLE_STATUS, io)) {
+ ERROR("Failed to query DM_TABLE_STATUS for %s (%s)\n", mount_point,
+ strerror(errno));
+ goto out;
+ }
+
+ status = &buffer[io->data_start + sizeof(struct dm_target_spec)];
+
+ if (*status == 'C') {
+ rc = write_verity_state(state_loc, offset, VERITY_MODE_LOGGING);
+ goto out;
+ }
+ }
+
+ /* Don't overwrite possible previous state if there's no corruption. */
+ rc = 0;
+
+out:
+ if (fstab) {
+ fs_mgr_free_fstab(fstab);
+ }
+
+ if (fd) {
+ TEMP_FAILURE_RETRY(close(fd));
+ }
+
+ return rc;
+}
+
int fs_mgr_setup_verity(struct fstab_rec *fstab) {
- int retval = -1;
+ int retval = FS_MGR_SETUP_VERITY_FAIL;
+ int fd = -1;
+ int mode;
- char *verity_blk_name;
- char *verity_table;
- char *verity_table_signature;
+ char *verity_blk_name = 0;
+ char *verity_table = 0;
+ char *verity_table_signature = 0;
- char buffer[DM_BUF_SIZE];
+ _Alignas(struct dm_ioctl) char buffer[DM_BUF_SIZE];
struct dm_ioctl *io = (struct dm_ioctl *) buffer;
char *mount_point = basename(fstab->mount_point);
@@ -351,11 +682,28 @@
io->flags |= 1;
io->target_count = 1;
+ // check to ensure that the verity device is ext4
+ // TODO: support non-ext4 filesystems
+ if (strcmp(fstab->fs_type, "ext4")) {
+ ERROR("Cannot verify non-ext4 device (%s)", fstab->fs_type);
+ return retval;
+ }
+
+ // read the verity block at the end of the block device
+ // send error code up the chain so we can detect attempts to disable verity
+ retval = read_verity_metadata(fstab->blk_device,
+ &verity_table_signature,
+ &verity_table);
+ if (retval < 0) {
+ goto out;
+ }
+
+ retval = FS_MGR_SETUP_VERITY_FAIL;
+
// get the device mapper fd
- int fd;
if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
ERROR("Error opening device mapper (%s)", strerror(errno));
- return retval;
+ goto out;
}
// create the device
@@ -370,13 +718,6 @@
goto out;
}
- // read the verity block at the end of the block device
- if (read_verity_metadata(fstab->blk_device,
- &verity_table_signature,
- &verity_table) < 0) {
- goto out;
- }
-
// verify the signature on the table
if (verify_table(verity_table_signature,
verity_table,
@@ -384,8 +725,13 @@
goto out;
}
+ if (fs_mgr_load_verity_state(&mode) < 0) {
+ mode = VERITY_MODE_RESTART; /* default dm-verity mode */
+ }
+
// load the verity mapping table
- if (load_verity_table(io, mount_point, fstab->blk_device, fd, verity_table) < 0) {
+ if (load_verity_table(io, mount_point, fstab->blk_device, fd, verity_table,
+ mode) < 0) {
goto out;
}
@@ -394,18 +740,29 @@
goto out;
}
+ // mark the underlying block device as read-only
+ fs_mgr_set_blk_ro(fstab->blk_device);
+
// assign the new verity block device as the block device
free(fstab->blk_device);
fstab->blk_device = verity_blk_name;
+ verity_blk_name = 0;
// make sure we've set everything up properly
if (test_access(fstab->blk_device) < 0) {
goto out;
}
- retval = 0;
+ retval = FS_MGR_SETUP_VERITY_SUCCESS;
out:
- close(fd);
+ if (fd != -1) {
+ close(fd);
+ }
+
+ free(verity_table);
+ free(verity_table_signature);
+ free(verity_blk_name);
+
return retval;
}
diff --git a/fs_mgr/include/fs_mgr.h b/fs_mgr/include/fs_mgr.h
index 0f90c32..a7a0bdb 100644
--- a/fs_mgr/include/fs_mgr.h
+++ b/fs_mgr/include/fs_mgr.h
@@ -20,10 +20,30 @@
#include <stdint.h>
#include <linux/dm-ioctl.h>
+// Magic number at start of verity metadata
+#define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
+
+// Replacement magic number at start of verity metadata to cleanly
+// turn verity off in userdebug builds.
+#define VERITY_METADATA_MAGIC_DISABLE 0x46464f56 // "VOFF"
+
+// Verity modes
+enum verity_mode {
+ VERITY_MODE_EIO = 0,
+ VERITY_MODE_LOGGING = 1,
+ VERITY_MODE_RESTART = 2,
+ VERITY_MODE_LAST = VERITY_MODE_RESTART
+};
+
#ifdef __cplusplus
extern "C" {
#endif
+/*
+ * The entries must be kept in the same order as they were seen in the fstab.
+ * Unless explicitly requested, a lookup on mount point should always
+ * return the 1st one.
+ */
struct fstab {
int num_entries;
struct fstab_rec *recs;
@@ -48,21 +68,33 @@
struct fstab *fs_mgr_read_fstab(const char *fstab_path);
void fs_mgr_free_fstab(struct fstab *fstab);
+
+#define FS_MGR_MNTALL_DEV_NEEDS_RECOVERY 3
+#define FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION 2
+#define FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED 1
+#define FS_MGR_MNTALL_DEV_NOT_ENCRYPTED 0
int fs_mgr_mount_all(struct fstab *fstab);
+
+#define FS_MGR_DOMNT_FAILED -1
+#define FS_MGR_DOMNT_BUSY -2
int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
char *tmp_mount_point);
int fs_mgr_do_tmpfs_mount(char *n_name);
int fs_mgr_unmount_all(struct fstab *fstab);
int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc,
char *real_blk_device, int size);
+int fs_mgr_load_verity_state(int *mode);
+int fs_mgr_update_verity_state();
int fs_mgr_add_entry(struct fstab *fstab,
const char *mount_point, const char *fs_type,
- const char *blk_device, long long length);
+ const char *blk_device);
struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path);
int fs_mgr_is_voldmanaged(struct fstab_rec *fstab);
int fs_mgr_is_nonremovable(struct fstab_rec *fstab);
+int fs_mgr_is_verified(struct fstab_rec *fstab);
int fs_mgr_is_encryptable(struct fstab_rec *fstab);
int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab);
+int fs_mgr_is_notrim(struct fstab_rec *fstab);
int fs_mgr_swapon_all(struct fstab *fstab);
#ifdef __cplusplus
}
diff --git a/gpttool/Android.mk b/gpttool/Android.mk
index b8f9844..64ad945 100644
--- a/gpttool/Android.mk
+++ b/gpttool/Android.mk
@@ -5,6 +5,7 @@
LOCAL_SRC_FILES := gpttool.c
LOCAL_STATIC_LIBRARIES := libz
+LOCAL_CFLAGS := -Werror
LOCAL_MODULE := gpttool
diff --git a/gpttool/gpttool.c b/gpttool/gpttool.c
index d3f08fe..398362f 100644
--- a/gpttool/gpttool.c
+++ b/gpttool/gpttool.c
@@ -1,5 +1,4 @@
-/* system/core/gpttool/gpttool.c
-**
+/*
** Copyright 2011, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,18 +14,18 @@
** limitations under the License.
*/
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include <zlib.h>
#include <linux/fs.h>
-#include <sys/stat.h>
-
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
@@ -252,11 +251,9 @@
int main(int argc, char **argv)
{
struct ptable ptbl;
- struct efi_entry *entry;
struct efi_header *hdr = &ptbl.header;
- struct stat s;
u32 n;
- u64 sz, blk;
+ u64 sz;
int fd;
const char *device;
int real_disk = 0;
diff --git a/healthd/Android.mk b/healthd/Android.mk
index 5cd5ce1..07e1d73 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -1,12 +1,11 @@
# Copyright 2013 The Android Open Source Project
-ifneq ($(BUILD_TINY_ANDROID),true)
-
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := healthd_board_default.cpp
LOCAL_MODULE := libhealthd.default
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
@@ -24,7 +23,7 @@
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
-LOCAL_CFLAGS := -D__STDC_LIMIT_MACROS
+LOCAL_CFLAGS := -D__STDC_LIMIT_MACROS -Werror
ifeq ($(strip $(BOARD_CHARGER_DISABLE_INIT_BLANK)),true)
LOCAL_CFLAGS += -DCHARGER_DISABLE_INIT_BLANK
@@ -36,7 +35,7 @@
LOCAL_C_INCLUDES := bootable/recovery
-LOCAL_STATIC_LIBRARIES := libbatteryservice libbinder libminui libpixelflinger_static libpng libz libutils libstdc++ libcutils liblog libm libc
+LOCAL_STATIC_LIBRARIES := libbatteryservice libbinder libminui libpng libz libutils libstdc++ libcutils liblog libm libc
ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
LOCAL_STATIC_LIBRARIES += libsuspend
@@ -76,5 +75,3 @@
_add-charger-image :=
_img_modules :=
-
-endif
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index 3e6a4b1..7ea8250 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -27,17 +27,21 @@
#include <unistd.h>
#include <batteryservice/BatteryService.h>
#include <cutils/klog.h>
+#include <cutils/properties.h>
+#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/Vector.h>
#define POWER_SUPPLY_SUBSYSTEM "power_supply"
#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
+#define FAKE_BATTERY_CAPACITY 42
+#define FAKE_BATTERY_TEMPERATURE 424
namespace android {
struct sysfsStringEnumMap {
- char* s;
+ const char* s;
int val;
};
@@ -183,10 +187,14 @@
else
props.batteryPresent = mBatteryDevicePresent;
- props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath);
+ props.batteryLevel = mBatteryFixedCapacity ?
+ mBatteryFixedCapacity :
+ getIntField(mHealthdConfig->batteryCapacityPath);
props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
- props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
+ props.batteryTemperature = mBatteryFixedTemperature ?
+ mBatteryFixedTemperature :
+ getIntField(mHealthdConfig->batteryTemperaturePath);
const int SIZE = 128;
char buf[SIZE];
@@ -257,10 +265,10 @@
"battery none");
}
- KLOG_INFO(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
- props.chargerAcOnline ? "a" : "",
- props.chargerUsbOnline ? "u" : "",
- props.chargerWirelessOnline ? "w" : "");
+ KLOG_WARNING(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
+ props.chargerAcOnline ? "a" : "",
+ props.chargerUsbOnline ? "u" : "",
+ props.chargerWirelessOnline ? "w" : "");
}
healthd_mode_ops->battery_update(&props);
@@ -271,10 +279,12 @@
status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
status_t ret = BAD_VALUE;
+ val->valueInt64 = LONG_MIN;
+
switch(id) {
case BATTERY_PROP_CHARGE_COUNTER:
if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
- val->valueInt =
+ val->valueInt64 =
getIntField(mHealthdConfig->batteryChargeCounterPath);
ret = NO_ERROR;
} else {
@@ -284,7 +294,7 @@
case BATTERY_PROP_CURRENT_NOW:
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
- val->valueInt =
+ val->valueInt64 =
getIntField(mHealthdConfig->batteryCurrentNowPath);
ret = NO_ERROR;
} else {
@@ -294,7 +304,7 @@
case BATTERY_PROP_CURRENT_AVG:
if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
- val->valueInt =
+ val->valueInt64 =
getIntField(mHealthdConfig->batteryCurrentAvgPath);
ret = NO_ERROR;
} else {
@@ -302,13 +312,28 @@
}
break;
+ case BATTERY_PROP_CAPACITY:
+ if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
+ val->valueInt64 =
+ getIntField(mHealthdConfig->batteryCapacityPath);
+ ret = NO_ERROR;
+ } else {
+ ret = NAME_NOT_FOUND;
+ }
+ break;
+
+ case BATTERY_PROP_ENERGY_COUNTER:
+ if (mHealthdConfig->energyCounter) {
+ ret = mHealthdConfig->energyCounter(&val->valueInt64);
+ } else {
+ ret = NAME_NOT_FOUND;
+ }
+ break;
+
default:
break;
}
- if (ret != NO_ERROR)
- val->valueInt = INT_MIN;
-
return ret;
}
@@ -349,6 +374,7 @@
void BatteryMonitor::init(struct healthd_config *hc) {
String8 path;
+ char pval[PROPERTY_VALUE_MAX];
mHealthdConfig = hc;
DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
@@ -363,7 +389,6 @@
if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;
- char buf[20];
// Look for "type" file in each subdirectory
path.clear();
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
@@ -486,7 +511,7 @@
if (!mChargerNames.size())
KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
if (!mBatteryDevicePresent) {
- KLOG_INFO(LOG_TAG, "No battery devices found\n");
+ KLOG_WARNING(LOG_TAG, "No battery devices found\n");
hc->periodic_chores_interval_fast = -1;
hc->periodic_chores_interval_slow = -1;
} else {
@@ -505,6 +530,12 @@
if (mHealthdConfig->batteryTechnologyPath.isEmpty())
KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
}
+
+ if (property_get("ro.boot.fake_battery", pval, NULL) > 0
+ && strtol(pval, NULL, 10) != 0) {
+ mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
+ mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
+ }
}
}; // namespace android
diff --git a/healthd/BatteryMonitor.h b/healthd/BatteryMonitor.h
index 4866cc2..3425f27 100644
--- a/healthd/BatteryMonitor.h
+++ b/healthd/BatteryMonitor.h
@@ -46,6 +46,8 @@
struct healthd_config *mHealthdConfig;
Vector<String8> mChargerNames;
bool mBatteryDevicePresent;
+ int mBatteryFixedCapacity;
+ int mBatteryFixedTemperature;
struct BatteryProperties props;
int getBatteryStatus(const char* status);
diff --git a/healthd/BatteryPropertiesRegistrar.cpp b/healthd/BatteryPropertiesRegistrar.cpp
index 58da4ee..09667a1 100644
--- a/healthd/BatteryPropertiesRegistrar.cpp
+++ b/healthd/BatteryPropertiesRegistrar.cpp
@@ -43,25 +43,29 @@
void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
{
+ if (listener == NULL)
+ return;
Mutex::Autolock _l(mRegistrationLock);
// check whether this is a duplicate
for (size_t i = 0; i < mListeners.size(); i++) {
- if (mListeners[i]->asBinder() == listener->asBinder()) {
+ if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
return;
}
}
mListeners.add(listener);
- listener->asBinder()->linkToDeath(this);
+ IInterface::asBinder(listener)->linkToDeath(this);
}
healthd_battery_update();
}
void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
+ if (listener == NULL)
+ return;
Mutex::Autolock _l(mRegistrationLock);
for (size_t i = 0; i < mListeners.size(); i++) {
- if (mListeners[i]->asBinder() == listener->asBinder()) {
- mListeners[i]->asBinder()->unlinkToDeath(this);
+ if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
+ IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
mListeners.removeAt(i);
break;
}
@@ -72,7 +76,7 @@
return healthd_get_property(id, val);
}
-status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& args) {
+status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
IPCThreadState* self = IPCThreadState::self();
const int pid = self->getCallingPid();
const int uid = self->getCallingUid();
@@ -89,7 +93,7 @@
Mutex::Autolock _l(mRegistrationLock);
for (size_t i = 0; i < mListeners.size(); i++) {
- if (mListeners[i]->asBinder() == who) {
+ if (IInterface::asBinder(mListeners[i]) == who) {
mListeners.removeAt(i);
break;
}
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
index 8363c41..b0002cc 100644
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -52,6 +52,9 @@
.batteryCurrentNowPath = String8(String8::kEmptyString),
.batteryCurrentAvgPath = String8(String8::kEmptyString),
.batteryChargeCounterPath = String8(String8::kEmptyString),
+ .energyCounter = NULL,
+ .boot_min_cap = 0,
+ .screen_on = NULL,
};
static int eventct;
@@ -63,7 +66,6 @@
#define MAX_EPOLL_EVENTS 40
static int uevent_fd;
static int wakealarm_fd;
-static int binder_fd;
// -1 for no epoll timeout
static int awake_poll_interval = -1;
@@ -118,7 +120,7 @@
.battery_update = healthd_mode_nop_battery_update,
};
-static void healthd_mode_nop_init(struct healthd_config *config) {
+static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
}
static int healthd_mode_nop_preparetowait(void) {
@@ -129,7 +131,7 @@
}
static void healthd_mode_nop_battery_update(
- struct android::BatteryProperties *props) {
+ struct android::BatteryProperties* /*props*/) {
}
int healthd_register_event(int fd, void (*handler)(uint32_t)) {
@@ -204,8 +206,8 @@
healthd_battery_update();
}
-#define UEVENT_MSG_LEN 1024
-static void uevent_event(uint32_t epevents) {
+#define UEVENT_MSG_LEN 2048
+static void uevent_event(uint32_t /*epevents*/) {
char msg[UEVENT_MSG_LEN+2];
char *cp;
int n;
@@ -246,7 +248,7 @@
"register for uevent events failed\n");
}
-static void wakealarm_event(uint32_t epevents) {
+static void wakealarm_event(uint32_t /*epevents*/) {
unsigned long long wakeups;
if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
@@ -313,8 +315,8 @@
return -1;
}
- healthd_mode_ops->init(&healthd_config);
healthd_board_init(&healthd_config);
+ healthd_mode_ops->init(&healthd_config);
wakealarm_init();
uevent_init();
gBatteryMonitor = new BatteryMonitor();
diff --git a/healthd/healthd.h b/healthd/healthd.h
index 23a54bf..84b6d76 100644
--- a/healthd/healthd.h
+++ b/healthd/healthd.h
@@ -18,6 +18,7 @@
#define _HEALTHD_H_
#include <batteryservice/BatteryService.h>
+#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/String8.h>
@@ -64,6 +65,10 @@
android::String8 batteryCurrentNowPath;
android::String8 batteryCurrentAvgPath;
android::String8 batteryChargeCounterPath;
+
+ int (*energyCounter)(int64_t *);
+ int boot_min_cap;
+ bool (*screen_on)(android::BatteryProperties *props);
};
// Global helper functions
diff --git a/healthd/healthd_board_default.cpp b/healthd/healthd_board_default.cpp
index b2bb516..ed4ddb4 100644
--- a/healthd/healthd_board_default.cpp
+++ b/healthd/healthd_board_default.cpp
@@ -16,13 +16,13 @@
#include <healthd.h>
-void healthd_board_init(struct healthd_config *config)
+void healthd_board_init(struct healthd_config*)
{
// use defaults
}
-int healthd_board_battery_update(struct android::BatteryProperties *props)
+int healthd_board_battery_update(struct android::BatteryProperties*)
{
// return 0 to log periodic polled battery status to kernel log
return 0;
diff --git a/healthd/healthd_mode_android.cpp b/healthd/healthd_mode_android.cpp
index 4887c8c..fd153a2 100644
--- a/healthd/healthd_mode_android.cpp
+++ b/healthd/healthd_mode_android.cpp
@@ -42,11 +42,11 @@
return -1;
}
-static void binder_event(uint32_t epevents) {
+static void binder_event(uint32_t /*epevents*/) {
IPCThreadState::self()->handlePolledCommands();
}
-void healthd_mode_android_init(struct healthd_config *config) {
+void healthd_mode_android_init(struct healthd_config* /*config*/) {
ProcessState::self()->setThreadPoolMaxThreadCount(0);
IPCThreadState::self()->disableBackgroundScheduling(true);
IPCThreadState::self()->setupPolling(&gBinderFd);
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index 025d530..352510b 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -17,6 +17,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <linux/input.h>
#include <stdbool.h>
#include <stdio.h>
@@ -36,6 +37,8 @@
#include <cutils/android_reboot.h>
#include <cutils/klog.h>
#include <cutils/misc.h>
+#include <cutils/uevent.h>
+#include <cutils/properties.h>
#ifdef CHARGER_ENABLE_SUSPEND
#include <suspend/autosuspend.h>
@@ -45,6 +48,8 @@
#include "healthd.h"
+char *locale;
+
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
@@ -69,7 +74,7 @@
#define LAST_KMSG_MAX_SZ (32 * 1024)
#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
-#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
+#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
struct key_state {
@@ -79,7 +84,6 @@
};
struct frame {
- const char *name;
int disp_time;
int min_capacity;
bool level_only;
@@ -104,7 +108,6 @@
struct charger {
bool have_battery_state;
bool charger_connected;
- int capacity;
int64_t next_screen_transition;
int64_t next_key_check;
int64_t next_pwr_check;
@@ -113,46 +116,41 @@
struct animation *batt_anim;
gr_surface surf_unknown;
+ int boot_min_cap;
};
static struct frame batt_anim_frames[] = {
{
- .name = "charger/battery_0",
.disp_time = 750,
.min_capacity = 0,
.level_only = false,
.surface = NULL,
},
{
- .name = "charger/battery_1",
.disp_time = 750,
.min_capacity = 20,
.level_only = false,
.surface = NULL,
},
{
- .name = "charger/battery_2",
.disp_time = 750,
.min_capacity = 40,
.level_only = false,
.surface = NULL,
},
{
- .name = "charger/battery_3",
.disp_time = 750,
.min_capacity = 60,
.level_only = false,
.surface = NULL,
},
{
- .name = "charger/battery_4",
.disp_time = 750,
.min_capacity = 80,
.level_only = true,
.surface = NULL,
},
{
- .name = "charger/battery_5",
.disp_time = 750,
.min_capacity = BATTERY_FULL_THRESH,
.level_only = false,
@@ -171,9 +169,11 @@
};
static struct charger charger_state;
-
+static struct healthd_config *healthd_config;
+static struct android::BatteryProperties *batt_prop;
static int char_width;
static int char_height;
+static bool minui_inited;
/* current time in milliseconds */
static int64_t curr_time_ms(void)
@@ -186,8 +186,8 @@
static void clear_screen(void)
{
gr_color(0, 0, 0, 255);
- gr_fill(0, 0, gr_fb_width(), gr_fb_height());
-};
+ gr_clear();
+}
#define MAX_KLOG_WRITE_BUF_SZ 256
@@ -198,15 +198,15 @@
unsigned sz = 0;
int len;
- LOGI("\n");
- LOGI("*************** LAST KMSG ***************\n");
- LOGI("\n");
+ LOGW("\n");
+ LOGW("*************** LAST KMSG ***************\n");
+ LOGW("\n");
buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz);
if (!buf || !sz) {
buf = (char *)load_file(LAST_KMSG_PATH, &sz);
if (!buf || !sz) {
- LOGI("last_kmsg not found. Cold reset?\n");
+ LOGW("last_kmsg not found. Cold reset?\n");
goto out;
}
}
@@ -225,7 +225,7 @@
yoink = ptr[cnt];
ptr[cnt] = '\0';
- klog_write(6, "<6>%s", ptr);
+ klog_write(6, "<4>%s", ptr);
ptr[cnt] = yoink;
len -= cnt;
@@ -235,14 +235,9 @@
free(buf);
out:
- LOGI("\n");
- LOGI("************* END LAST KMSG *************\n");
- LOGI("\n");
-}
-
-static int get_battery_capacity()
-{
- return charger_state.capacity;
+ LOGW("\n");
+ LOGW("************* END LAST KMSG *************\n");
+ LOGW("\n");
}
#ifdef CHARGER_ENABLE_SUSPEND
@@ -254,7 +249,7 @@
return autosuspend_disable();
}
#else
-static int request_suspend(bool enable)
+static int request_suspend(bool /*enable*/)
{
return 0;
}
@@ -279,7 +274,7 @@
}
/* returns the last y-offset of where the surface ends */
-static int draw_surface_centered(struct charger *charger, gr_surface surface)
+static int draw_surface_centered(struct charger* /*charger*/, gr_surface surface)
{
int w;
int h;
@@ -315,8 +310,8 @@
if (batt_anim->num_frames != 0) {
draw_surface_centered(charger, frame->surface);
- LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
- batt_anim->cur_frame, frame->name, frame->min_capacity,
+ LOGV("drawing frame #%d min_cap=%d time=%d\n",
+ batt_anim->cur_frame, frame->min_capacity,
frame->disp_time);
}
}
@@ -350,19 +345,40 @@
static void update_screen_state(struct charger *charger, int64_t now)
{
struct animation *batt_anim = charger->batt_anim;
- int cur_frame;
int disp_time;
if (!batt_anim->run || now < charger->next_screen_transition)
return;
+ if (!minui_inited) {
+
+ if (healthd_config && healthd_config->screen_on) {
+ if (!healthd_config->screen_on(batt_prop)) {
+ LOGV("[%" PRId64 "] leave screen off\n", now);
+ batt_anim->run = false;
+ charger->next_screen_transition = -1;
+ if (charger->charger_connected)
+ request_suspend(true);
+ return;
+ }
+ }
+
+ gr_init();
+ gr_font_size(&char_width, &char_height);
+
+#ifndef CHARGER_DISABLE_INIT_BLANK
+ gr_fb_blank(true);
+#endif
+ minui_inited = true;
+ }
+
/* animation is over, blank screen and leave */
if (batt_anim->cur_cycle == batt_anim->num_cycles) {
reset_animation(batt_anim);
charger->next_screen_transition = -1;
gr_fb_blank(true);
- LOGV("[%lld] animation done\n", now);
- if (!charger->charger_connected)
+ LOGV("[%" PRId64 "] animation done\n", now);
+ if (charger->charger_connected)
request_suspend(true);
return;
}
@@ -371,17 +387,14 @@
/* animation starting, set up the animation */
if (batt_anim->cur_frame == 0) {
- int batt_cap;
- int ret;
- LOGV("[%lld] animation starting\n", now);
- batt_cap = get_battery_capacity();
- if (batt_cap >= 0 && batt_anim->num_frames != 0) {
+ LOGV("[%" PRId64 "] animation starting\n", now);
+ if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
int i;
/* find first frame given current capacity */
for (i = 1; i < batt_anim->num_frames; i++) {
- if (batt_cap < batt_anim->frames[i].min_capacity)
+ if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity)
break;
}
batt_anim->cur_frame = i - 1;
@@ -389,8 +402,8 @@
/* show the first frame for twice as long */
disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
}
-
- batt_anim->capacity = batt_cap;
+ if (batt_prop)
+ batt_anim->capacity = batt_prop->batteryLevel;
}
/* unblank the screen on first cycle */
@@ -404,7 +417,7 @@
* the cycle counter and exit
*/
if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
- LOGV("[%lld] animation missing or unknown battery status\n", now);
+ LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now);
charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
batt_anim->cur_cycle++;
return;
@@ -413,24 +426,33 @@
/* schedule next screen transition */
charger->next_screen_transition = now + disp_time;
- /* advance frame cntr to the next valid frame
+ /* advance frame cntr to the next valid frame only if we are charging
* if necessary, advance cycle cntr, and reset frame cntr
*/
- batt_anim->cur_frame++;
-
- /* if the frame is used for level-only, that is only show it when it's
- * the current level, skip it during the animation.
- */
- while (batt_anim->cur_frame < batt_anim->num_frames &&
- batt_anim->frames[batt_anim->cur_frame].level_only)
+ if (charger->charger_connected) {
batt_anim->cur_frame++;
- if (batt_anim->cur_frame >= batt_anim->num_frames) {
- batt_anim->cur_cycle++;
- batt_anim->cur_frame = 0;
- /* don't reset the cycle counter, since we use that as a signal
- * in a test above to check if animation is over
+ /* if the frame is used for level-only, that is only show it when it's
+ * the current level, skip it during the animation.
*/
+ while (batt_anim->cur_frame < batt_anim->num_frames &&
+ batt_anim->frames[batt_anim->cur_frame].level_only)
+ batt_anim->cur_frame++;
+ if (batt_anim->cur_frame >= batt_anim->num_frames) {
+ batt_anim->cur_cycle++;
+ batt_anim->cur_frame = 0;
+
+ /* don't reset the cycle counter, since we use that as a signal
+ * in a test above to check if animation is over
+ */
+ }
+ } else {
+ /* Stop animating if we're not charging.
+ * If we stop it immediately instead of going through this loop, then
+ * the animation would stop somewhere in the middle.
+ */
+ batt_anim->cur_frame = 0;
+ batt_anim->cur_cycle++;
}
}
@@ -454,13 +476,13 @@
charger->keys[code].down = down;
charger->keys[code].pending = true;
if (down) {
- LOGV("[%lld] key[%d] down\n", now, code);
+ LOGV("[%" PRId64 "] key[%d] down\n", now, code);
} else {
int64_t duration = now - charger->keys[code].timestamp;
int64_t secs = duration / 1000;
int64_t msecs = duration - secs * 1000;
- LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
- code, secs, msecs);
+ LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n",
+ now, code, secs, msecs);
}
return 0;
@@ -487,24 +509,41 @@
static void process_key(struct charger *charger, int code, int64_t now)
{
struct key_state *key = &charger->keys[code];
- int64_t next_key_check;
if (code == KEY_POWER) {
if (key->down) {
int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
if (now >= reboot_timeout) {
- LOGI("[%lld] rebooting\n", now);
- android_reboot(ANDROID_RB_RESTART, 0, 0);
+ /* We do not currently support booting from charger mode on
+ all devices. Check the property and continue booting or reboot
+ accordingly. */
+ if (property_get_bool("ro.enable_boot_charger_mode", false)) {
+ LOGW("[%" PRId64 "] booting from charger mode\n", now);
+ property_set("sys.boot_from_charger_mode", "1");
+ } else {
+ if (charger->batt_anim->capacity >= charger->boot_min_cap) {
+ LOGW("[%" PRId64 "] rebooting\n", now);
+ android_reboot(ANDROID_RB_RESTART, 0, 0);
+ } else {
+ LOGV("[%" PRId64 "] ignore power-button press, battery level "
+ "less than minimum\n", now);
+ }
+ }
} else {
/* if the key is pressed but timeout hasn't expired,
* make sure we wake up at the right-ish time to check
*/
set_next_key_check(charger, key, POWER_ON_KEY_TIME);
+
+ /* Turn on the display and kick animation on power-key press
+ * rather than on key release
+ */
+ kick_animation(charger->batt_anim);
+ request_suspend(false);
}
} else {
/* if the power key got released, force screen state cycle */
if (key->pending) {
- request_suspend(false);
kick_animation(charger->batt_anim);
}
}
@@ -527,13 +566,18 @@
return;
if (!charger->charger_connected) {
+
+ /* Last cycle would have stopped at the extreme top of battery-icon
+ * Need to show the correct level corresponding to capacity.
+ */
+ kick_animation(charger->batt_anim);
request_suspend(false);
if (charger->next_pwr_check == -1) {
charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
- LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
- now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
+ LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
+ now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
} else if (now >= charger->next_pwr_check) {
- LOGI("[%lld] shutting down\n", now);
+ LOGW("[%" PRId64 "] shutting down\n", now);
android_reboot(ANDROID_RB_POWEROFF, 0, 0);
} else {
/* otherwise we already have a shutdown timer scheduled */
@@ -541,7 +585,7 @@
} else {
/* online supply present, reset shutdown timer if set */
if (charger->next_pwr_check != -1) {
- LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
+ LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
kick_animation(charger->batt_anim);
}
charger->next_pwr_check = -1;
@@ -552,7 +596,6 @@
{
struct charger *charger = &charger_state;
int64_t now = curr_time_ms();
- int ret;
handle_input_state(charger, now);
handle_power_supply_state(charger, now);
@@ -571,7 +614,6 @@
charger->charger_connected =
props->chargerAcOnline || props->chargerUsbOnline ||
props->chargerWirelessOnline;
- charger->capacity = props->batteryLevel;
if (!charger->have_battery_state) {
charger->have_battery_state = true;
@@ -579,6 +621,7 @@
reset_animation(charger->batt_anim);
kick_animation(charger->batt_anim);
}
+ batt_prop = props;
}
int healthd_mode_charger_preparetowait(void)
@@ -587,10 +630,8 @@
int64_t now = curr_time_ms();
int64_t next_event = INT64_MAX;
int64_t timeout;
- struct input_event ev;
- int ret;
- LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
+ LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now,
charger->next_screen_transition, charger->next_key_check,
charger->next_pwr_check);
@@ -622,7 +663,7 @@
return 0;
}
-static void charger_event_handler(uint32_t epevents)
+static void charger_event_handler(uint32_t /*epevents*/)
{
int ret;
@@ -631,7 +672,7 @@
ev_dispatch();
}
-void healthd_mode_charger_init(struct healthd_config *config)
+void healthd_mode_charger_init(struct healthd_config* config)
{
int ret;
struct charger *charger = &charger_state;
@@ -640,10 +681,7 @@
dump_last_kmsg();
- LOGI("--------------- STARTING CHARGER MODE ---------------\n");
-
- gr_init();
- gr_font_size(&char_width, &char_height);
+ LOGW("--------------- STARTING CHARGER MODE ---------------\n");
ret = ev_init(input_callback, charger);
if (!ret) {
@@ -651,34 +689,37 @@
healthd_register_event(epollfd, charger_event_handler);
}
- ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
+ ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
if (ret < 0) {
- LOGE("Cannot load image\n");
+ LOGE("Cannot load battery_fail image\n");
charger->surf_unknown = NULL;
}
charger->batt_anim = &battery_animation;
- for (i = 0; i < charger->batt_anim->num_frames; i++) {
- struct frame *frame = &charger->batt_anim->frames[i];
-
- ret = res_create_surface(frame->name, &frame->surface);
- if (ret < 0) {
- LOGE("Cannot load image %s\n", frame->name);
- /* TODO: free the already allocated surfaces... */
- charger->batt_anim->num_frames = 0;
- charger->batt_anim->num_cycles = 1;
- break;
+ gr_surface* scale_frames;
+ int scale_count;
+ ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
+ if (ret < 0) {
+ LOGE("Cannot load battery_scale image\n");
+ charger->batt_anim->num_frames = 0;
+ charger->batt_anim->num_cycles = 1;
+ } else if (scale_count != charger->batt_anim->num_frames) {
+ LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
+ scale_count, charger->batt_anim->num_frames);
+ charger->batt_anim->num_frames = 0;
+ charger->batt_anim->num_cycles = 1;
+ } else {
+ for (i = 0; i < charger->batt_anim->num_frames; i++) {
+ charger->batt_anim->frames[i].surface = scale_frames[i];
}
}
ev_sync_key_state(set_key_callback, charger);
-#ifndef CHARGER_DISABLE_INIT_BLANK
- gr_fb_blank(true);
-#endif
-
charger->next_screen_transition = -1;
charger->next_key_check = -1;
charger->next_pwr_check = -1;
+ healthd_config = config;
+ charger->boot_min_cap = config->boot_min_cap;
}
diff --git a/healthd/images/battery_0.png b/healthd/images/battery_0.png
deleted file mode 100644
index 2347074..0000000
--- a/healthd/images/battery_0.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_1.png b/healthd/images/battery_1.png
deleted file mode 100644
index cd34620..0000000
--- a/healthd/images/battery_1.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_2.png b/healthd/images/battery_2.png
deleted file mode 100644
index 3e4095e..0000000
--- a/healthd/images/battery_2.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_3.png b/healthd/images/battery_3.png
deleted file mode 100644
index 08c1551..0000000
--- a/healthd/images/battery_3.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_4.png b/healthd/images/battery_4.png
deleted file mode 100644
index 3a678da..0000000
--- a/healthd/images/battery_4.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_5.png b/healthd/images/battery_5.png
deleted file mode 100644
index d8dc40e..0000000
--- a/healthd/images/battery_5.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_charge.png b/healthd/images/battery_charge.png
deleted file mode 100644
index b501933..0000000
--- a/healthd/images/battery_charge.png
+++ /dev/null
Binary files differ
diff --git a/healthd/images/battery_fail.png b/healthd/images/battery_fail.png
index 36fc254..aded88a 100644
--- a/healthd/images/battery_fail.png
+++ b/healthd/images/battery_fail.png
Binary files differ
diff --git a/healthd/images/battery_scale.png b/healthd/images/battery_scale.png
new file mode 100644
index 0000000..2ae8f0f
--- /dev/null
+++ b/healthd/images/battery_scale.png
Binary files differ
diff --git a/include/android/log.h b/include/android/log.h
index 0ea4c29..1c171b7 100644
--- a/include/android/log.h
+++ b/include/android/log.h
@@ -98,8 +98,16 @@
*/
int __android_log_print(int prio, const char *tag, const char *fmt, ...)
#if defined(__GNUC__)
+#ifdef __USE_MINGW_ANSI_STDIO
+#if __USE_MINGW_ANSI_STDIO
+ __attribute__ ((format(gnu_printf, 3, 4)))
+#else
__attribute__ ((format(printf, 3, 4)))
#endif
+#else
+ __attribute__ ((format(printf, 3, 4)))
+#endif
+#endif
;
/*
@@ -110,15 +118,23 @@
const char *fmt, va_list ap);
/*
- * Log an assertion failure and SIGTRAP the process to have a chance
- * to inspect it, if a debugger is attached. This uses the FATAL priority.
+ * Log an assertion failure and abort the process to have a chance
+ * to inspect it if a debugger is attached. This uses the FATAL priority.
*/
void __android_log_assert(const char *cond, const char *tag,
- const char *fmt, ...)
+ const char *fmt, ...)
#if defined(__GNUC__)
__attribute__ ((noreturn))
+#ifdef __USE_MINGW_ANSI_STDIO
+#if __USE_MINGW_ANSI_STDIO
+ __attribute__ ((format(gnu_printf, 3, 4)))
+#else
__attribute__ ((format(printf, 3, 4)))
#endif
+#else
+ __attribute__ ((format(printf, 3, 4)))
+#endif
+#endif
;
#ifdef __cplusplus
diff --git a/include/backtrace/Backtrace.h b/include/backtrace/Backtrace.h
index 3c3a482..8c39acb 100644
--- a/include/backtrace/Backtrace.h
+++ b/include/backtrace/Backtrace.h
@@ -39,7 +39,7 @@
uintptr_t pc; // The absolute pc.
uintptr_t sp; // The top of the stack.
size_t stack_size; // The size of the stack, zero indicate an unknown stack size.
- const backtrace_map_t* map; // The map associated with the given pc.
+ backtrace_map_t map; // The map associated with the given pc.
std::string func_name; // The function name associated with this pc, NULL if not found.
uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL.
};
@@ -47,6 +47,14 @@
// Forward declarations.
class BacktraceImpl;
+#if defined(__APPLE__)
+struct __darwin_ucontext;
+typedef __darwin_ucontext ucontext_t;
+#else
+struct ucontext;
+typedef ucontext ucontext_t;
+#endif
+
class Backtrace {
public:
// Create the correct Backtrace object based on what is to be unwound.
@@ -64,18 +72,24 @@
virtual ~Backtrace();
// Get the current stack trace and store in the backtrace_ structure.
- virtual bool Unwind(size_t num_ignore_frames);
+ virtual bool Unwind(size_t num_ignore_frames, ucontext_t* context = NULL);
// Get the function name and offset into the function given the pc.
// If the string is empty, then no valid function name was found.
virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset);
- // Find the map associated with the given pc.
- virtual const backtrace_map_t* FindMap(uintptr_t pc);
+ // Fill in the map data associated with the given pc.
+ virtual void FillInMap(uintptr_t pc, backtrace_map_t* map);
// Read the data at a specific address.
virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0;
+ // Read arbitrary data from a specific address. If a read request would
+ // span from one map to another, this call only reads up until the end
+ // of the current map.
+ // Returns the total number of bytes actually read.
+ virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) = 0;
+
// Create a string representing the formatted line of backtrace information
// for a single frame.
virtual std::string FormatFrameData(size_t frame_num);
diff --git a/include/backtrace/BacktraceMap.h b/include/backtrace/BacktraceMap.h
index 06da2f4..da96307 100644
--- a/include/backtrace/BacktraceMap.h
+++ b/include/backtrace/BacktraceMap.h
@@ -18,6 +18,7 @@
#define _BACKTRACE_BACKTRACE_MAP_H
#include <stdint.h>
+#include <sys/types.h>
#ifdef USE_MINGW
// MINGW does not define these constants.
#define PROT_NONE 0
@@ -32,6 +33,8 @@
#include <string>
struct backtrace_map_t {
+ backtrace_map_t(): start(0), end(0), flags(0) {}
+
uintptr_t start;
uintptr_t end;
int flags;
@@ -40,19 +43,23 @@
class BacktraceMap {
public:
- static BacktraceMap* Create(pid_t pid);
+ // If uncached is true, then parse the current process map as of the call.
+ // Passing a map created with uncached set to true to Backtrace::Create()
+ // is unsupported.
+ static BacktraceMap* Create(pid_t pid, bool uncached = false);
virtual ~BacktraceMap();
- // Get the map data structure for the given address.
- const backtrace_map_t* Find(uintptr_t addr);
+ // Fill in the map data structure for the given address.
+ virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
// The flags returned are the same flags as used by the mmap call.
// The values are PROT_*.
int GetFlags(uintptr_t pc) {
- const backtrace_map_t* map = Find(pc);
- if (map) {
- return map->flags;
+ backtrace_map_t map;
+ FillIn(pc, &map);
+ if (IsValid(map)) {
+ return map.flags;
}
return PROT_NONE;
}
@@ -71,6 +78,10 @@
virtual bool Build();
+ static inline bool IsValid(const backtrace_map_t& map) {
+ return map.end > 0;
+ }
+
protected:
BacktraceMap(pid_t pid);
diff --git a/include/corkscrew/backtrace.h b/include/corkscrew/backtrace.h
deleted file mode 100644
index 556ad04..0000000
--- a/include/corkscrew/backtrace.h
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * 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.
- */
-
-/* A stack unwinder. */
-
-#ifndef _CORKSCREW_BACKTRACE_H
-#define _CORKSCREW_BACKTRACE_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <sys/types.h>
-#include <corkscrew/ptrace.h>
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-/*
- * Describes a single frame of a backtrace.
- */
-typedef struct {
- uintptr_t absolute_pc; /* absolute PC offset */
- uintptr_t stack_top; /* top of stack for this frame */
- size_t stack_size; /* size of this stack frame */
-} backtrace_frame_t;
-
-/*
- * Describes the symbols associated with a backtrace frame.
- */
-typedef struct {
- uintptr_t relative_pc; /* relative frame PC offset from the start of the library,
- or the absolute PC if the library is unknown */
- uintptr_t relative_symbol_addr; /* relative offset of the symbol from the start of the
- library or 0 if the library is unknown */
- char* map_name; /* executable or library name, or NULL if unknown */
- char* symbol_name; /* symbol name, or NULL if unknown */
- char* demangled_name; /* demangled symbol name, or NULL if unknown */
-} backtrace_symbol_t;
-
-/*
- * Unwinds the call stack for the current thread of execution.
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- */
-ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-/*
- * Unwinds the call stack for a thread within this process.
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- *
- * The task is briefly suspended while the backtrace is being collected.
- */
-ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth);
-
-/*
- * Unwinds the call stack of a task within a remote process using ptrace().
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- */
-ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-/*
- * Gets the symbols for each frame of a backtrace.
- * The symbols array must be big enough to hold one symbol record per frame.
- * The symbols must later be freed using free_backtrace_symbols.
- */
-void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
- backtrace_symbol_t* backtrace_symbols);
-
-/*
- * Gets the symbols for each frame of a backtrace from a remote process.
- * The symbols array must be big enough to hold one symbol record per frame.
- * The symbols must later be freed using free_backtrace_symbols.
- */
-void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
- const backtrace_frame_t* backtrace, size_t frames,
- backtrace_symbol_t* backtrace_symbols);
-
-/*
- * Frees the storage associated with backtrace symbols.
- */
-void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames);
-
-enum {
- // A hint for how big to make the line buffer for format_backtrace_line
- MAX_BACKTRACE_LINE_LENGTH = 800,
-};
-
-/**
- * Formats a line from a backtrace as a zero-terminated string into the specified buffer.
- */
-void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame,
- const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_H
diff --git a/include/corkscrew/demangle.h b/include/corkscrew/demangle.h
deleted file mode 100644
index 04b0225..0000000
--- a/include/corkscrew/demangle.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.
- */
-
-/* C++ symbol name demangling. */
-
-#ifndef _CORKSCREW_DEMANGLE_H
-#define _CORKSCREW_DEMANGLE_H
-
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Demangles a C++ symbol name.
- * If name is NULL or if the name cannot be demangled, returns NULL.
- * Otherwise, returns a newly allocated string that contains the demangled name.
- *
- * The caller must free the returned string using free().
- */
-char* demangle_symbol_name(const char* name);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_DEMANGLE_H
diff --git a/include/corkscrew/map_info.h b/include/corkscrew/map_info.h
deleted file mode 100644
index 14bfad6..0000000
--- a/include/corkscrew/map_info.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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.
- */
-
-/* Process memory map. */
-
-#ifndef _CORKSCREW_MAP_INFO_H
-#define _CORKSCREW_MAP_INFO_H
-
-#include <sys/types.h>
-#include <stdbool.h>
-#include <stdint.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct map_info {
- struct map_info* next;
- uintptr_t start;
- uintptr_t end;
- bool is_readable;
- bool is_writable;
- bool is_executable;
- void* data; // arbitrary data associated with the map by the user, initially NULL
- char name[];
-} map_info_t;
-
-/* Loads memory map from /proc/<tid>/maps. */
-map_info_t* load_map_info_list(pid_t tid);
-
-/* Frees memory map. */
-void free_map_info_list(map_info_t* milist);
-
-/* Finds the memory map that contains the specified address. */
-const map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr);
-
-/* Returns true if the addr is in a readable map. */
-bool is_readable_map(const map_info_t* milist, uintptr_t addr);
-/* Returns true if the addr is in a writable map. */
-bool is_writable_map(const map_info_t* milist, uintptr_t addr);
-/* Returns true if the addr is in an executable map. */
-bool is_executable_map(const map_info_t* milist, uintptr_t addr);
-
-/* Acquires a reference to the memory map for this process.
- * The result is cached and refreshed automatically.
- * Make sure to release the map info when done. */
-map_info_t* acquire_my_map_info_list();
-
-/* Releases a reference to the map info for this process that was
- * previous acquired using acquire_my_map_info_list(). */
-void release_my_map_info_list(map_info_t* milist);
-
-/* Flushes the cached memory map so the next call to
- * acquire_my_map_info_list() gets fresh data. */
-void flush_my_map_info_list();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_MAP_INFO_H
diff --git a/include/corkscrew/ptrace.h b/include/corkscrew/ptrace.h
deleted file mode 100644
index 76276d8..0000000
--- a/include/corkscrew/ptrace.h
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * 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.
- */
-
-/* Useful ptrace() utility functions. */
-
-#ifndef _CORKSCREW_PTRACE_H
-#define _CORKSCREW_PTRACE_H
-
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-#include <sys/types.h>
-#include <stdbool.h>
-#include <stdint.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Stores information about a process that is used for several different
- * ptrace() based operations. */
-typedef struct {
- map_info_t* map_info_list;
-} ptrace_context_t;
-
-/* Describes how to access memory from a process. */
-typedef struct {
- pid_t tid;
- const map_info_t* map_info_list;
-} memory_t;
-
-#if __i386__
-/* ptrace() register context. */
-typedef struct pt_regs_x86 {
- uint32_t ebx;
- uint32_t ecx;
- uint32_t edx;
- uint32_t esi;
- uint32_t edi;
- uint32_t ebp;
- uint32_t eax;
- uint32_t xds;
- uint32_t xes;
- uint32_t xfs;
- uint32_t xgs;
- uint32_t orig_eax;
- uint32_t eip;
- uint32_t xcs;
- uint32_t eflags;
- uint32_t esp;
- uint32_t xss;
-} pt_regs_x86_t;
-#endif
-
-#if __mips__
-/* ptrace() GET_REGS context. */
-typedef struct pt_regs_mips {
- uint64_t regs[32];
- uint64_t lo;
- uint64_t hi;
- uint64_t cp0_epc;
- uint64_t cp0_badvaddr;
- uint64_t cp0_status;
- uint64_t cp0_cause;
-} pt_regs_mips_t;
-#endif
-
-/*
- * Initializes a memory structure for accessing memory from this process.
- */
-void init_memory(memory_t* memory, const map_info_t* map_info_list);
-
-/*
- * Initializes a memory structure for accessing memory from another process
- * using ptrace().
- */
-void init_memory_ptrace(memory_t* memory, pid_t tid);
-
-/*
- * Reads a word of memory safely.
- * If the memory is local, ensures that the address is readable before dereferencing it.
- * Returns false and a value of 0xffffffff if the word could not be read.
- */
-bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value);
-
-/*
- * Reads a word of memory safely using ptrace().
- * Returns false and a value of 0xffffffff if the word could not be read.
- */
-bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value);
-
-/*
- * Loads information needed for examining a remote process using ptrace().
- * The caller must already have successfully attached to the process
- * using ptrace().
- *
- * The context can be used for any threads belonging to that process
- * assuming ptrace() is attached to them before performing the actual
- * unwinding. The context can continue to be used to decode backtraces
- * even after ptrace() has been detached from the process.
- */
-ptrace_context_t* load_ptrace_context(pid_t pid);
-
-/*
- * Frees a ptrace context.
- */
-void free_ptrace_context(ptrace_context_t* context);
-
-/*
- * Finds a symbol using ptrace.
- * Returns the containing map and information about the symbol, or
- * NULL if one or the other is not available.
- */
-void find_symbol_ptrace(const ptrace_context_t* context,
- uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_PTRACE_H
diff --git a/include/corkscrew/symbol_table.h b/include/corkscrew/symbol_table.h
deleted file mode 100644
index 4998750..0000000
--- a/include/corkscrew/symbol_table.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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 _CORKSCREW_SYMBOL_TABLE_H
-#define _CORKSCREW_SYMBOL_TABLE_H
-
-#include <stdint.h>
-#include <sys/types.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct {
- uintptr_t start;
- uintptr_t end;
- char* name;
-} symbol_t;
-
-typedef struct {
- symbol_t* symbols;
- size_t num_symbols;
-} symbol_table_t;
-
-/*
- * Loads a symbol table from a given file.
- * Returns NULL on error.
- */
-symbol_table_t* load_symbol_table(const char* filename);
-
-/*
- * Frees a symbol table.
- */
-void free_symbol_table(symbol_table_t* table);
-
-/*
- * Finds a symbol associated with an address in the symbol table.
- * Returns NULL if not found.
- */
-const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_SYMBOL_TABLE_H
diff --git a/include/ctest/ctest.h b/include/ctest/ctest.h
deleted file mode 100644
index 1a83b20..0000000
--- a/include/ctest/ctest.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.
- */
-
-/**
- * Very simple unit testing framework.
- */
-
-#ifndef __CUTILS_TEST_H
-#define __CUTILS_TEST_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * Adds a test to the test suite.
- */
-#define addTest(test) addNamedTest(#test, &test)
-
-/**
- * Asserts that a condition is true. The test fails if it isn't.
- */
-#define assertTrue(value, message) assertTrueWithSource(value, __FILE__, __LINE__, message);
-
-/**
- * Asserts that a condition is false. The test fails if the value is true.
- */
-#define assertFalse(value, message) assertTrueWithSource(!value, __FILE__, __LINE__, message);
-
-/** Fails a test with the given message. */
-#define fail(message) assertTrueWithSource(0, __FILE__, __LINE__, message);
-
-/**
- * Asserts that two values are ==.
- */
-#define assertSame(a, b) assertTrueWithSource(a == b, __FILE__, __LINE__, "Expected same value.");
-
-/**
- * Asserts that two values are !=.
- */
-#define assertNotSame(a, b) assertTrueWithSource(a != b, __FILE__, __LINE__,\
- "Expected different values");
-
-/**
- * Runs a test suite.
- */
-void runTests(void);
-
-// Do not call these functions directly. Use macros above instead.
-void addNamedTest(const char* name, void (*test)(void));
-void assertTrueWithSource(int value, const char* file, int line, char* message);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CUTILS_TEST_H */
diff --git a/include/cutils/android_reboot.h b/include/cutils/android_reboot.h
index 8c30e8e..85e1b7e 100644
--- a/include/cutils/android_reboot.h
+++ b/include/cutils/android_reboot.h
@@ -27,7 +27,7 @@
/* Properties */
#define ANDROID_RB_PROPERTY "sys.powerctl"
-int android_reboot(int cmd, int flags, char *arg);
+int android_reboot(int cmd, int flags, const char *arg);
__END_DECLS
diff --git a/include/cutils/aref.h b/include/cutils/aref.h
index 460ac02..3bd36ea 100644
--- a/include/cutils/aref.h
+++ b/include/cutils/aref.h
@@ -20,11 +20,7 @@
#include <stddef.h>
#include <sys/cdefs.h>
-#ifdef ANDROID_SMP
-#include <cutils/atomic-inline.h>
-#else
#include <cutils/atomic.h>
-#endif
__BEGIN_DECLS
diff --git a/include/cutils/atomic-arm.h b/include/cutils/atomic-arm.h
deleted file mode 100644
index 172a0cd..0000000
--- a/include/cutils/atomic-arm.h
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_ARM_H
-#define ANDROID_CUTILS_ATOMIC_ARM_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier()
-{
- __asm__ __volatile__ ("" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier()
-{
-#if ANDROID_SMP == 0
- android_compiler_barrier();
-#else
- __asm__ __volatile__ ("dmb" : : : "memory");
-#endif
-}
-
-extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier()
-{
-#if ANDROID_SMP == 0
- android_compiler_barrier();
-#else
- __asm__ __volatile__ ("dmb st" : : : "memory");
-#endif
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
- int32_t value = *ptr;
- android_memory_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
- android_memory_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- __asm__ __volatile__ ("ldrex %0, [%3]\n"
- "mov %1, #0\n"
- "teq %0, %4\n"
-#ifdef __thumb2__
- "it eq\n"
-#endif
- "strexeq %1, %5, [%3]"
- : "=&r" (prev), "=&r" (status), "+m"(*ptr)
- : "r" (ptr), "Ir" (old_value), "r" (new_value)
- : "cc");
- } while (__builtin_expect(status != 0, 0));
- return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- int status = android_atomic_cas(old_value, new_value, ptr);
- android_memory_barrier();
- return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- android_memory_barrier();
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
- int32_t prev, tmp, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ ("ldrex %0, [%4]\n"
- "add %1, %0, %5\n"
- "strex %2, %1, [%4]"
- : "=&r" (prev), "=&r" (tmp),
- "=&r" (status), "+m" (*ptr)
- : "r" (ptr), "Ir" (increment)
- : "cc");
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t android_atomic_inc(volatile int32_t *addr)
-{
- return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t android_atomic_dec(volatile int32_t *addr)
-{
- return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, tmp, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ ("ldrex %0, [%4]\n"
- "and %1, %0, %5\n"
- "strex %2, %1, [%4]"
- : "=&r" (prev), "=&r" (tmp),
- "=&r" (status), "+m" (*ptr)
- : "r" (ptr), "Ir" (value)
- : "cc");
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, tmp, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ ("ldrex %0, [%4]\n"
- "orr %1, %0, %5\n"
- "strex %2, %1, [%4]"
- : "=&r" (prev), "=&r" (tmp),
- "=&r" (status), "+m" (*ptr)
- : "r" (ptr), "Ir" (value)
- : "cc");
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_ARM_H */
diff --git a/include/cutils/atomic-arm64.h b/include/cutils/atomic-arm64.h
deleted file mode 100644
index 4562ad0..0000000
--- a/include/cutils/atomic-arm64.h
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_AARCH64_H
-#define ANDROID_CUTILS_ATOMIC_AARCH64_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-/*
- TODOAArch64: Revisit the below functions and check for potential
- optimizations using assembly code or otherwise.
-*/
-
-extern ANDROID_ATOMIC_INLINE
-void android_compiler_barrier(void)
-{
- __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
- android_compiler_barrier();
-}
-extern ANDROID_ATOMIC_INLINE
-void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
- __asm__ __volatile__ ("dmb ish" : : : "memory");
-}
-extern ANDROID_ATOMIC_INLINE
-void android_memory_store_barrier(void)
-{
- __asm__ __volatile__ ("dmb ishst" : : : "memory");
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
- int32_t value = *ptr;
- android_memory_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_acquire_load64(volatile const int64_t *ptr)
-{
- int64_t value = *ptr;
- android_memory_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_release_load64(volatile const int64_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
- android_memory_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store64(int64_t value, volatile int64_t *ptr)
-{
- android_memory_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- int status = android_atomic_cas(old_value, new_value, ptr);
- android_memory_barrier();
- return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- int status = android_atomic_cas64(old_value, new_value, ptr);
- android_memory_barrier();
- return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- android_memory_barrier();
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- android_memory_barrier();
- return android_atomic_cas64(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev + increment, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_inc(volatile int32_t *addr)
-{
- return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_dec(volatile int32_t *addr)
-{
- return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev & value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev | value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_AARCH64_H */
diff --git a/include/cutils/atomic-inline.h b/include/cutils/atomic-inline.h
deleted file mode 100644
index 4f90ef1..0000000
--- a/include/cutils/atomic-inline.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_INLINE_H
-#define ANDROID_CUTILS_ATOMIC_INLINE_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Inline declarations and macros for some special-purpose atomic
- * operations. These are intended for rare circumstances where a
- * memory barrier needs to be issued inline rather than as a function
- * call.
- *
- * Most code should not use these.
- *
- * Anything that does include this file must set ANDROID_SMP to either
- * 0 or 1, indicating compilation for UP or SMP, respectively.
- *
- * Macros defined in this header:
- *
- * void ANDROID_MEMBAR_FULL(void)
- * Full memory barrier. Provides a compiler reordering barrier, and
- * on SMP systems emits an appropriate instruction.
- */
-
-#if !defined(ANDROID_SMP)
-# error "Must define ANDROID_SMP before including atomic-inline.h"
-#endif
-
-#if defined(__aarch64__)
-#include <cutils/atomic-arm64.h>
-#elif defined(__arm__)
-#include <cutils/atomic-arm.h>
-#elif defined(__i386__)
-#include <cutils/atomic-x86.h>
-#elif defined(__x86_64__)
-#include <cutils/atomic-x86_64.h>
-#elif defined(__mips__)
-#include <cutils/atomic-mips.h>
-#else
-#error atomic operations are unsupported
-#endif
-
-#if ANDROID_SMP == 0
-#define ANDROID_MEMBAR_FULL android_compiler_barrier
-#else
-#define ANDROID_MEMBAR_FULL android_memory_barrier
-#endif
-
-#if ANDROID_SMP == 0
-#define ANDROID_MEMBAR_STORE android_compiler_barrier
-#else
-#define ANDROID_MEMBAR_STORE android_memory_store_barrier
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* ANDROID_CUTILS_ATOMIC_INLINE_H */
diff --git a/include/cutils/atomic-mips.h b/include/cutils/atomic-mips.h
deleted file mode 100644
index f9d3e25..0000000
--- a/include/cutils/atomic-mips.h
+++ /dev/null
@@ -1,199 +0,0 @@
-/*
- * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_MIPS_H
-#define ANDROID_CUTILS_ATOMIC_MIPS_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
-{
- __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
- android_compiler_barrier();
-}
-extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
- __asm__ __volatile__ ("sync" : : : "memory");
-}
-extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
-{
- __asm__ __volatile__ ("sync" : : : "memory");
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_acquire_load(volatile const int32_t *ptr)
-{
- int32_t value = *ptr;
- android_memory_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_release_load(volatile const int32_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
- android_memory_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- __asm__ __volatile__ (
- " ll %[prev], (%[ptr])\n"
- " li %[status], 1\n"
- " bne %[prev], %[old], 9f\n"
- " move %[status], %[new_value]\n"
- " sc %[status], (%[ptr])\n"
- "9:\n"
- : [prev] "=&r" (prev), [status] "=&r" (status)
- : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value)
- );
- } while (__builtin_expect(status == 0, 0));
- return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_acquire_cas(int32_t old_value,
- int32_t new_value,
- volatile int32_t *ptr)
-{
- int status = android_atomic_cas(old_value, new_value, ptr);
- android_memory_barrier();
- return status;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_release_cas(int32_t old_value,
- int32_t new_value,
- volatile int32_t *ptr)
-{
- android_memory_barrier();
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_swap(int32_t new_value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- __asm__ __volatile__ (
- " move %[status], %[new_value]\n"
- " ll %[prev], (%[ptr])\n"
- " sc %[status], (%[ptr])\n"
- : [prev] "=&r" (prev), [status] "=&r" (status)
- : [ptr] "r" (ptr), [new_value] "r" (new_value)
- );
- } while (__builtin_expect(status == 0, 0));
- android_memory_barrier();
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ (
- " ll %[prev], (%[ptr])\n"
- " addu %[status], %[prev], %[inc]\n"
- " sc %[status], (%[ptr])\n"
- : [status] "=&r" (status), [prev] "=&r" (prev)
- : [ptr] "r" (ptr), [inc] "Ir" (increment)
- );
- } while (__builtin_expect(status == 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_inc(volatile int32_t *addr)
-{
- return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_dec(volatile int32_t *addr)
-{
- return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ (
- " ll %[prev], (%[ptr])\n"
- " and %[status], %[prev], %[value]\n"
- " sc %[status], (%[ptr])\n"
- : [prev] "=&r" (prev), [status] "=&r" (status)
- : [ptr] "r" (ptr), [value] "Ir" (value)
- );
- } while (__builtin_expect(status == 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- android_memory_barrier();
- do {
- __asm__ __volatile__ (
- " ll %[prev], (%[ptr])\n"
- " or %[status], %[prev], %[value]\n"
- " sc %[status], (%[ptr])\n"
- : [prev] "=&r" (prev), [status] "=&r" (status)
- : [ptr] "r" (ptr), [value] "Ir" (value)
- );
- } while (__builtin_expect(status == 0, 0));
- return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */
diff --git a/include/cutils/atomic-x86.h b/include/cutils/atomic-x86.h
deleted file mode 100644
index 9480f57..0000000
--- a/include/cutils/atomic-x86.h
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_X86_H
-#define ANDROID_CUTILS_ATOMIC_X86_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
-{
- __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
- android_compiler_barrier();
-}
-extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
- __asm__ __volatile__ ("mfence" : : : "memory");
-}
-extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_acquire_load(volatile const int32_t *ptr)
-{
- int32_t value = *ptr;
- android_compiler_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_release_load(volatile const int32_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
- android_compiler_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
-{
- int32_t prev;
- __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
- : "=a" (prev)
- : "q" (new_value), "m" (*ptr), "0" (old_value)
- : "memory");
- return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_acquire_cas(int32_t old_value,
- int32_t new_value,
- volatile int32_t *ptr)
-{
- /* Loads are not reordered with other loads. */
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_release_cas(int32_t old_value,
- int32_t new_value,
- volatile int32_t *ptr)
-{
- /* Stores are not reordered with other stores. */
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
- __asm__ __volatile__ ("lock; xaddl %0, %1"
- : "+r" (increment), "+m" (*ptr)
- : : "memory");
- /* increment now holds the old value of *ptr */
- return increment;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_inc(volatile int32_t *addr)
-{
- return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_dec(volatile int32_t *addr)
-{
- return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev & value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev | value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_X86_H */
diff --git a/include/cutils/atomic-x86_64.h b/include/cutils/atomic-x86_64.h
deleted file mode 100644
index 5b5c203..0000000
--- a/include/cutils/atomic-x86_64.h
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_X86_64_H
-#define ANDROID_CUTILS_ATOMIC_X86_64_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE
-void android_compiler_barrier(void)
-{
- __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
- android_compiler_barrier();
-}
-extern ANDROID_ATOMIC_INLINE
-void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
- __asm__ __volatile__ ("mfence" : : : "memory");
-}
-extern ANDROID_ATOMIC_INLINE
-void android_memory_store_barrier(void)
-{
- android_compiler_barrier();
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
- int32_t value = *ptr;
- android_compiler_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_acquire_load64(volatile const int64_t *ptr)
-{
- int64_t value = *ptr;
- android_compiler_barrier();
- return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_release_load64(volatile const int64_t *ptr)
-{
- android_memory_barrier();
- return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr)
-{
- *ptr = value;
- android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
- android_compiler_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store64(int64_t value, volatile int64_t *ptr)
-{
- android_compiler_barrier();
- *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- int32_t prev;
- __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
- : "=a" (prev)
- : "q" (new_value), "m" (*ptr), "0" (old_value)
- : "memory");
- return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- int64_t prev;
- __asm__ __volatile__ ("lock; cmpxchgq %1, %2"
- : "=a" (prev)
- : "q" (new_value), "m" (*ptr), "0" (old_value)
- : "memory");
- return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- /* Loads are not reordered with other loads. */
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- /* Loads are not reordered with other loads. */
- return android_atomic_cas64(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
- volatile int32_t *ptr)
-{
- /* Stores are not reordered with other stores. */
- return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr)
-{
- /* Stores are not reordered with other stores. */
- return android_atomic_cas64(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
- __asm__ __volatile__ ("lock; xaddl %0, %1"
- : "+r" (increment), "+m" (*ptr)
- : : "memory");
- /* increment now holds the old value of *ptr */
- return increment;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_inc(volatile int32_t *addr)
-{
- return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_dec(volatile int32_t *addr)
-{
- return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev & value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
- int32_t prev, status;
- do {
- prev = *ptr;
- status = android_atomic_cas(prev, prev | value, ptr);
- } while (__builtin_expect(status != 0, 0));
- return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_X86_64_H */
diff --git a/include/cutils/atomic.h b/include/cutils/atomic.h
index 1787e34..ded972a 100644
--- a/include/cutils/atomic.h
+++ b/include/cutils/atomic.h
@@ -19,14 +19,27 @@
#include <stdint.h>
#include <sys/types.h>
+#include <stdatomic.h>
-#ifdef __cplusplus
-extern "C" {
+#ifndef ANDROID_ATOMIC_INLINE
+#define ANDROID_ATOMIC_INLINE static inline
#endif
/*
- * A handful of basic atomic operations. The appropriate pthread
- * functions should be used instead of these whenever possible.
+ * A handful of basic atomic operations.
+ * THESE ARE HERE FOR LEGACY REASONS ONLY. AVOID.
+ *
+ * PREFERRED ALTERNATIVES:
+ * - Use C++/C/pthread locks/mutexes whenever there is not a
+ * convincing reason to do otherwise. Note that very clever and
+ * complicated, but correct, lock-free code is often slower than
+ * using locks, especially where nontrivial data structures
+ * are involved.
+ * - C11 stdatomic.h.
+ * - Where supported, C++11 std::atomic<T> .
+ *
+ * PLEASE STOP READING HERE UNLESS YOU ARE TRYING TO UNDERSTAND
+ * OR UPDATE OLD CODE.
*
* The "acquire" and "release" terms can be defined intuitively in terms
* of the placement of memory barriers in a simple lock implementation:
@@ -65,39 +78,102 @@
* These have the same characteristics (e.g. what happens on overflow)
* as the equivalent non-atomic C operations.
*/
-int32_t android_atomic_inc(volatile int32_t* addr);
-int32_t android_atomic_dec(volatile int32_t* addr);
-int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
-int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
-int32_t android_atomic_or(int32_t value, volatile int32_t* addr);
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_inc(volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ /* Int32_t, if it exists, is the same as int_least32_t. */
+ return atomic_fetch_add_explicit(a, 1, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_dec(volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return atomic_fetch_sub_explicit(a, 1, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_add(int32_t value, volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return atomic_fetch_add_explicit(a, value, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_and(int32_t value, volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return atomic_fetch_and_explicit(a, value, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_or(int32_t value, volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return atomic_fetch_or_explicit(a, value, memory_order_release);
+}
/*
* Perform an atomic load with "acquire" or "release" ordering.
*
- * This is only necessary if you need the memory barrier. A 32-bit read
- * from a 32-bit aligned address is atomic on all supported platforms.
+ * Note that the notion of a "release" ordering for a load does not
+ * really fit into the C11 or C++11 memory model. The extra ordering
+ * is normally observable only by code using memory_order_relaxed
+ * atomics, or data races. In the rare cases in which such ordering
+ * is called for, use memory_order_relaxed atomics and a leading
+ * atomic_thread_fence (typically with memory_order_acquire,
+ * not memory_order_release!) instead. If you do not understand
+ * this comment, you are in the vast majority, and should not be
+ * using release loads or replacing them with anything other than
+ * locks or default sequentially consistent atomics.
*/
-int32_t android_atomic_acquire_load(volatile const int32_t* addr);
-int32_t android_atomic_release_load(volatile const int32_t* addr);
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_acquire_load(volatile const int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return atomic_load_explicit(a, memory_order_acquire);
+}
-#if defined (__LP64__)
-int64_t android_atomic_acquire_load64(volatile const int64_t* addr);
-int64_t android_atomic_release_load64(volatile const int64_t* addr);
-#endif
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_release_load(volatile const int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ atomic_thread_fence(memory_order_seq_cst);
+ /* Any reasonable clients of this interface would probably prefer */
+ /* something weaker. But some remaining clients seem to be */
+ /* abusing this API in strange ways, e.g. by using it as a fence. */
+ /* Thus we are conservative until we can get rid of remaining */
+ /* clients (and this function). */
+ return atomic_load_explicit(a, memory_order_relaxed);
+}
/*
* Perform an atomic store with "acquire" or "release" ordering.
*
- * This is only necessary if you need the memory barrier. A 32-bit write
- * to a 32-bit aligned address is atomic on all supported platforms.
+ * Note that the notion of an "acquire" ordering for a store does not
+ * really fit into the C11 or C++11 memory model. The extra ordering
+ * is normally observable only by code using memory_order_relaxed
+ * atomics, or data races. In the rare cases in which such ordering
+ * is called for, use memory_order_relaxed atomics and a trailing
+ * atomic_thread_fence (typically with memory_order_release,
+ * not memory_order_acquire!) instead.
*/
-void android_atomic_acquire_store(int32_t value, volatile int32_t* addr);
-void android_atomic_release_store(int32_t value, volatile int32_t* addr);
+ANDROID_ATOMIC_INLINE
+void android_atomic_acquire_store(int32_t value, volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ atomic_store_explicit(a, value, memory_order_relaxed);
+ atomic_thread_fence(memory_order_seq_cst);
+ /* Again overly conservative to accomodate weird clients. */
+}
-#if defined (__LP64__)
-void android_atomic_acquire_store64(int64_t value, volatile int64_t* addr);
-void android_atomic_release_store64(int64_t value, volatile int64_t* addr);
-#endif
+ANDROID_ATOMIC_INLINE
+void android_atomic_release_store(int32_t value, volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ atomic_store_explicit(a, value, memory_order_release);
+}
/*
* Compare-and-set operation with "acquire" or "release" ordering.
@@ -111,17 +187,44 @@
* Implementations that use the release CAS in a loop may be less efficient
* than possible, because we re-issue the memory barrier on each iteration.
*/
+ANDROID_ATOMIC_INLINE
int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue,
- volatile int32_t* addr);
-int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
- volatile int32_t* addr);
+ volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return (int)(!atomic_compare_exchange_strong_explicit(
+ a, &oldvalue, newvalue,
+ memory_order_acquire,
+ memory_order_acquire));
+}
-#if defined (__LP64__)
-int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr);
-int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value,
- volatile int64_t *ptr);
-#endif
+ANDROID_ATOMIC_INLINE
+int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
+ volatile int32_t* addr)
+{
+ volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+ return (int)(!atomic_compare_exchange_strong_explicit(
+ a, &oldvalue, newvalue,
+ memory_order_release,
+ memory_order_relaxed));
+}
+
+/*
+ * Fence primitives.
+ */
+ANDROID_ATOMIC_INLINE
+void android_compiler_barrier(void)
+{
+ __asm__ __volatile__ ("" : : : "memory");
+ /* Could probably also be: */
+ /* atomic_signal_fence(memory_order_seq_cst); */
+}
+
+ANDROID_ATOMIC_INLINE
+void android_memory_barrier(void)
+{
+ atomic_thread_fence(memory_order_seq_cst);
+}
/*
* Aliases for code using an older version of this header. These are now
@@ -131,8 +234,4 @@
#define android_atomic_write android_atomic_release_store
#define android_atomic_cmpxchg android_atomic_release_cas
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
#endif // ANDROID_CUTILS_ATOMIC_H
diff --git a/include/cutils/bitops.h b/include/cutils/bitops.h
index c26dc54..045830d 100644
--- a/include/cutils/bitops.h
+++ b/include/cutils/bitops.h
@@ -59,7 +59,7 @@
static inline int bitmask_ffz(unsigned int *bitmask, int num_bits)
{
int bit, result;
- unsigned int i;
+ size_t i;
for (i = 0; i < BITS_TO_WORDS(num_bits); i++) {
bit = ffs(~bitmask[i]);
@@ -77,7 +77,7 @@
static inline int bitmask_weight(unsigned int *bitmask, int num_bits)
{
- int i;
+ size_t i;
int weight = 0;
for (i = 0; i < BITS_TO_WORDS(num_bits); i++)
diff --git a/include/cutils/debugger.h b/include/cutils/debugger.h
index af80e2c..285e1af 100644
--- a/include/cutils/debugger.h
+++ b/include/cutils/debugger.h
@@ -17,17 +17,14 @@
#ifndef __CUTILS_DEBUGGER_H
#define __CUTILS_DEBUGGER_H
+#include <sys/cdefs.h>
#include <sys/types.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
-#if __LP64__
-#define DEBUGGER_SOCKET_NAME "android:debuggerd64"
-#else
#define DEBUGGER_SOCKET_NAME "android:debuggerd"
-#endif
+#define DEBUGGER32_SOCKET_NAME "android:debuggerd32"
+#define DEBUGGER64_SOCKET_NAME DEBUGGER_SOCKET_NAME
typedef enum {
// dump a crash
@@ -38,10 +35,15 @@
DEBUGGER_ACTION_DUMP_BACKTRACE,
} debugger_action_t;
-typedef struct {
- debugger_action_t action;
+// Make sure that all values have a fixed size so that this structure
+// is the same for 32 bit and 64 bit processes.
+// NOTE: Any changes to this structure must also be reflected in
+// bionic/linker/debugger.cpp.
+typedef struct __attribute__((packed)) {
+ int32_t action;
pid_t tid;
- uintptr_t abort_msg_address;
+ uint64_t abort_msg_address;
+ int32_t original_si_code;
} debugger_msg_t;
/* Dumps a process backtrace, registers, and stack to a tombstone file (requires root).
@@ -50,13 +52,26 @@
*/
int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen);
+/* Dumps a process backtrace, registers, and stack to a tombstone file (requires root).
+ * Stores the tombstone path in the provided buffer.
+ * If reading debugger data from debuggerd ever takes longer than timeout_secs
+ * seconds, then stop and return an error.
+ * Returns 0 on success, -1 on error.
+ */
+int dump_tombstone_timeout(pid_t tid, char* pathbuf, size_t pathlen, int timeout_secs);
+
/* Dumps a process backtrace only to the specified file (requires root).
* Returns 0 on success, -1 on error.
*/
int dump_backtrace_to_file(pid_t tid, int fd);
-#ifdef __cplusplus
-}
-#endif
+/* Dumps a process backtrace only to the specified file (requires root).
+ * If reading debugger data from debuggerd ever takes longer than timeout_secs
+ * seconds, then stop and return an error.
+ * Returns 0 on success, -1 on error.
+ */
+int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs);
+
+__END_DECLS
#endif /* __CUTILS_DEBUGGER_H */
diff --git a/include/cutils/fs.h b/include/cutils/fs.h
index d1d4cf2..70f0b92 100644
--- a/include/cutils/fs.h
+++ b/include/cutils/fs.h
@@ -18,6 +18,7 @@
#define __CUTILS_FS_H
#include <sys/types.h>
+#include <unistd.h>
/*
* TEMP_FAILURE_RETRY is defined by some, but not all, versions of
diff --git a/include/cutils/jstring.h b/include/cutils/jstring.h
index ee0018f..a342608 100644
--- a/include/cutils/jstring.h
+++ b/include/cutils/jstring.h
@@ -24,7 +24,10 @@
extern "C" {
#endif
-typedef uint16_t char16_t;
+#if __STDC_VERSION__ < 201112L && __cplusplus < 201103L
+ typedef uint16_t char16_t;
+#endif
+ // otherwise char16_t is a keyword with the right semantics
extern char * strndup16to8 (const char16_t* s, size_t n);
extern size_t strnlen16to8 (const char16_t* s, size_t n);
diff --git a/include/cutils/klog.h b/include/cutils/klog.h
index 3953904..d5ae6d7 100644
--- a/include/cutils/klog.h
+++ b/include/cutils/klog.h
@@ -18,22 +18,31 @@
#define _CUTILS_KLOG_H_
#include <sys/cdefs.h>
+#include <stdarg.h>
__BEGIN_DECLS
void klog_init(void);
+int klog_get_level(void);
void klog_set_level(int level);
/* TODO: void klog_close(void); - and make klog_fd users thread safe. */
void klog_write(int level, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
+void klog_vwrite(int level, const char *fmt, va_list ap);
__END_DECLS
-#define KLOG_ERROR(tag,x...) klog_write(3, "<3>" tag ": " x)
-#define KLOG_WARNING(tag,x...) klog_write(4, "<4>" tag ": " x)
-#define KLOG_NOTICE(tag,x...) klog_write(5, "<5>" tag ": " x)
-#define KLOG_INFO(tag,x...) klog_write(6, "<6>" tag ": " x)
-#define KLOG_DEBUG(tag,x...) klog_write(7, "<7>" tag ": " x)
+#define KLOG_ERROR_LEVEL 3
+#define KLOG_WARNING_LEVEL 4
+#define KLOG_NOTICE_LEVEL 5
+#define KLOG_INFO_LEVEL 6
+#define KLOG_DEBUG_LEVEL 7
+
+#define KLOG_ERROR(tag,x...) klog_write(KLOG_ERROR_LEVEL, "<3>" tag ": " x)
+#define KLOG_WARNING(tag,x...) klog_write(KLOG_WARNING_LEVEL, "<4>" tag ": " x)
+#define KLOG_NOTICE(tag,x...) klog_write(KLOG_NOTICE_LEVEL, "<5>" tag ": " x)
+#define KLOG_INFO(tag,x...) klog_write(KLOG_INFO_LEVEL, "<6>" tag ": " x)
+#define KLOG_DEBUG(tag,x...) klog_write(KLOG_DEBUG_LEVEL, "<7>" tag ": " x)
#define KLOG_DEFAULT_LEVEL 3 /* messages <= this level are logged */
diff --git a/include/cutils/list.h b/include/cutils/list.h
index 945729a..4ba2cfd 100644
--- a/include/cutils/list.h
+++ b/include/cutils/list.h
@@ -44,10 +44,10 @@
#define list_for_each_reverse(node, list) \
for (node = (list)->prev; node != (list); node = node->prev)
-#define list_for_each_safe(node, next, list) \
- for (node = (list)->next, next = node->next; \
+#define list_for_each_safe(node, n, list) \
+ for (node = (list)->next, n = node->next; \
node != (list); \
- node = next, next = node->next)
+ node = n, n = node->next)
static inline void list_init(struct listnode *node)
{
@@ -63,6 +63,14 @@
head->prev = item;
}
+static inline void list_add_head(struct listnode *head, struct listnode *item)
+{
+ item->next = head->next;
+ item->prev = head;
+ head->next->prev = item;
+ head->next = item;
+}
+
static inline void list_remove(struct listnode *item)
{
item->next->prev = item->prev;
diff --git a/include/cutils/open_memstream.h b/include/cutils/open_memstream.h
index b7998be..c1a81eb 100644
--- a/include/cutils/open_memstream.h
+++ b/include/cutils/open_memstream.h
@@ -19,7 +19,7 @@
#include <stdio.h>
-#ifndef HAVE_OPEN_MEMSTREAM
+#if defined(__APPLE__)
#ifdef __cplusplus
extern "C" {
@@ -31,6 +31,6 @@
}
#endif
-#endif /*!HAVE_OPEN_MEMSTREAM*/
+#endif /* __APPLE__ */
#endif /*__CUTILS_OPEN_MEMSTREAM_H__*/
diff --git a/include/cutils/properties.h b/include/cutils/properties.h
index 2c70165..24aa224 100644
--- a/include/cutils/properties.h
+++ b/include/cutils/properties.h
@@ -20,6 +20,7 @@
#include <sys/cdefs.h>
#include <stddef.h>
#include <sys/system_properties.h>
+#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@@ -44,6 +45,64 @@
*/
int property_get(const char *key, char *value, const char *default_value);
+/* property_get_bool: returns the value of key coerced into a
+** boolean. If the property is not set, then the default value is returned.
+**
+* The following is considered to be true (1):
+** "1", "true", "y", "yes", "on"
+**
+** The following is considered to be false (0):
+** "0", "false", "n", "no", "off"
+**
+** The conversion is whitespace-sensitive (e.g. " off" will not be false).
+**
+** If no property with this key is set (or the key is NULL) or the boolean
+** conversion fails, the default value is returned.
+**/
+int8_t property_get_bool(const char *key, int8_t default_value);
+
+/* property_get_int64: returns the value of key truncated and coerced into a
+** int64_t. If the property is not set, then the default value is used.
+**
+** The numeric conversion is identical to strtoimax with the base inferred:
+** - All digits up to the first non-digit characters are read
+** - The longest consecutive prefix of digits is converted to a long
+**
+** Valid strings of digits are:
+** - An optional sign character + or -
+** - An optional prefix indicating the base (otherwise base 10 is assumed)
+** -- 0 prefix is octal
+** -- 0x / 0X prefix is hex
+**
+** Leading/trailing whitespace is ignored. Overflow/underflow will cause
+** numeric conversion to fail.
+**
+** If no property with this key is set (or the key is NULL) or the numeric
+** conversion fails, the default value is returned.
+**/
+int64_t property_get_int64(const char *key, int64_t default_value);
+
+/* property_get_int32: returns the value of key truncated and coerced into an
+** int32_t. If the property is not set, then the default value is used.
+**
+** The numeric conversion is identical to strtoimax with the base inferred:
+** - All digits up to the first non-digit characters are read
+** - The longest consecutive prefix of digits is converted to a long
+**
+** Valid strings of digits are:
+** - An optional sign character + or -
+** - An optional prefix indicating the base (otherwise base 10 is assumed)
+** -- 0 prefix is octal
+** -- 0x / 0X prefix is hex
+**
+** Leading/trailing whitespace is ignored. Overflow/underflow will cause
+** numeric conversion to fail.
+**
+** If no property with this key is set (or the key is NULL) or the numeric
+** conversion fails, the default value is returned.
+**/
+int32_t property_get_int32(const char *key, int32_t default_value);
+
/* property_set: returns 0 on success, < 0 on failure
*/
int property_set(const char *key, const char *value);
@@ -67,22 +126,6 @@
#endif
-#ifdef HAVE_SYSTEM_PROPERTY_SERVER
-/*
- * We have an external property server instead of built-in libc support.
- * Used by the simulator.
- */
-#define SYSTEM_PROPERTY_PIPE_NAME "/tmp/android-sysprop"
-
-enum {
- kSystemPropertyUnknown = 0,
- kSystemPropertyGet,
- kSystemPropertySet,
- kSystemPropertyList
-};
-#endif /*HAVE_SYSTEM_PROPERTY_SERVER*/
-
-
#ifdef __cplusplus
}
#endif
diff --git a/include/cutils/sockets.h b/include/cutils/sockets.h
index 19cae0c..c47588c 100644
--- a/include/cutils/sockets.h
+++ b/include/cutils/sockets.h
@@ -25,7 +25,7 @@
#ifdef HAVE_WINSOCK
#include <winsock2.h>
typedef int socklen_t;
-#elif HAVE_SYS_SOCKET_H
+#else
#include <sys/socket.h>
#endif
@@ -86,6 +86,8 @@
extern int socket_loopback_client(int port, int type);
extern int socket_network_client(const char *host, int port, int type);
+extern int socket_network_client_timeout(const char *host, int port, int type,
+ int timeout);
extern int socket_loopback_server(int port, int type);
extern int socket_local_server(const char *name, int namespaceId, int type);
extern int socket_local_server_bind(int s, const char *name, int namespaceId);
diff --git a/include/cutils/str_parms.h b/include/cutils/str_parms.h
index 247c996..66f3637 100644
--- a/include/cutils/str_parms.h
+++ b/include/cutils/str_parms.h
@@ -34,6 +34,12 @@
int str_parms_add_float(struct str_parms *str_parms, const char *key,
float value);
+// Returns non-zero if the str_parms contains the specified key.
+int str_parms_has_key(struct str_parms *str_parms, const char *key);
+
+// Gets value associated with the specified key (if present), placing it in the buffer
+// pointed to by the out_val parameter. Returns the length of the returned string value.
+// If 'key' isn't in the parms, then return -ENOENT (-2) and leave 'out_val' untouched.
int str_parms_get_str(struct str_parms *str_parms, const char *key,
char *out_val, int len);
int str_parms_get_int(struct str_parms *str_parms, const char *key,
diff --git a/include/cutils/threads.h b/include/cutils/threads.h
index acf8f48..ade9a0c 100644
--- a/include/cutils/threads.h
+++ b/include/cutils/threads.h
@@ -29,7 +29,7 @@
/***********************************************************************/
/***********************************************************************/
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
#include <pthread.h>
@@ -42,7 +42,7 @@
#define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
-#elif defined HAVE_WIN32_THREADS
+#else // !defined(_WIN32)
#include <windows.h>
@@ -56,15 +56,13 @@
#define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
-#else
-# error "no thread_store_t implementation for your platform !!"
-#endif
+#endif // !defined(_WIN32)
typedef void (*thread_store_destruct_t)(void* value);
extern void* thread_store_get(thread_store_t* store);
-extern void thread_store_set(thread_store_t* store,
+extern void thread_store_set(thread_store_t* store,
void* value,
thread_store_destruct_t destroy);
@@ -76,7 +74,7 @@
/***********************************************************************/
/***********************************************************************/
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
typedef pthread_mutex_t mutex_t;
@@ -98,10 +96,10 @@
{
pthread_mutex_destroy(lock);
}
-#endif
-#ifdef HAVE_WIN32_THREADS
-typedef struct {
+#else // !defined(_WIN32)
+
+typedef struct {
int init;
CRITICAL_SECTION lock[1];
} mutex_t;
@@ -134,10 +132,10 @@
{
if (lock->init) {
lock->init = 0;
- DeleteCriticalSection(lock->lock);
+ DeleteCriticalSection(lock->lock);
}
}
-#endif
+#endif // !defined(_WIN32)
#ifdef __cplusplus
}
diff --git a/include/cutils/trace.h b/include/cutils/trace.h
index 1c8f107..59ff6c1 100644
--- a/include/cutils/trace.h
+++ b/include/cutils/trace.h
@@ -17,18 +17,16 @@
#ifndef _LIBS_CUTILS_TRACE_H
#define _LIBS_CUTILS_TRACE_H
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
#include <sys/cdefs.h>
#include <sys/types.h>
-#include <stdint.h>
-#include <stdbool.h>
#include <unistd.h>
-#include <cutils/compiler.h>
-#ifdef ANDROID_SMP
-#include <cutils/atomic-inline.h>
-#else
#include <cutils/atomic.h>
-#endif
+#include <cutils/compiler.h>
__BEGIN_DECLS
@@ -67,7 +65,9 @@
#define ATRACE_TAG_RESOURCES (1<<13)
#define ATRACE_TAG_DALVIK (1<<14)
#define ATRACE_TAG_RS (1<<15)
-#define ATRACE_TAG_LAST ATRACE_TAG_RS
+#define ATRACE_TAG_BIONIC (1<<16)
+#define ATRACE_TAG_POWER (1<<17)
+#define ATRACE_TAG_LAST ATRACE_TAG_POWER
// Reserved for initialization.
#define ATRACE_TAG_NOT_READY (1LL<<63)
@@ -82,13 +82,6 @@
#ifdef HAVE_ANDROID_OS
/**
- * Maximum size of a message that can be logged to the trace buffer.
- * Note this message includes a tag, the pid, and the string given as the name.
- * Names should be kept short to get the most use of the trace buffer.
- */
-#define ATRACE_MESSAGE_LENGTH 1024
-
-/**
* Opens the trace file for writing and reads the property for initial tags.
* The atrace.tags.enableflags property sets the tags to trace.
* This function should not be explicitly called, the first call to any normal
@@ -180,11 +173,8 @@
static inline void atrace_begin(uint64_t tag, const char* name)
{
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
- char buf[ATRACE_MESSAGE_LENGTH];
- size_t len;
-
- len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
- write(atrace_marker_fd, buf, len);
+ void atrace_begin_body(const char*);
+ atrace_begin_body(name);
}
}
@@ -214,12 +204,8 @@
int32_t cookie)
{
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
- char buf[ATRACE_MESSAGE_LENGTH];
- size_t len;
-
- len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(),
- name, cookie);
- write(atrace_marker_fd, buf, len);
+ void atrace_async_begin_body(const char*, int32_t);
+ atrace_async_begin_body(name, cookie);
}
}
@@ -228,20 +214,14 @@
* This should have a corresponding ATRACE_ASYNC_BEGIN.
*/
#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
-static inline void atrace_async_end(uint64_t tag, const char* name,
- int32_t cookie)
+static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie)
{
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
- char buf[ATRACE_MESSAGE_LENGTH];
- size_t len;
-
- len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(),
- name, cookie);
- write(atrace_marker_fd, buf, len);
+ void atrace_async_end_body(const char*, int32_t);
+ atrace_async_end_body(name, cookie);
}
}
-
/**
* Traces an integer counter value. name is used to identify the counter.
* This can be used to track how a value changes over time.
@@ -250,12 +230,8 @@
static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
{
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
- char buf[ATRACE_MESSAGE_LENGTH];
- size_t len;
-
- len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
- getpid(), name, value);
- write(atrace_marker_fd, buf, len);
+ void atrace_int_body(const char*, int32_t);
+ atrace_int_body(name, value);
}
}
@@ -267,12 +243,8 @@
static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
{
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
- char buf[ATRACE_MESSAGE_LENGTH];
- size_t len;
-
- len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%lld",
- getpid(), name, value);
- write(atrace_marker_fd, buf, len);
+ void atrace_int64_body(const char*, int64_t);
+ atrace_int64_body(name, value);
}
}
diff --git a/include/cutils/tztime.h b/include/cutils/tztime.h
deleted file mode 100644
index dbdbd60..0000000
--- a/include/cutils/tztime.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2006 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 _CUTILS_TZTIME_H
-#define _CUTILS_TZTIME_H
-
-// TODO: fix both callers to just include <bionic_time.h> themselves.
-#include <bionic_time.h>
-
-#endif /* __CUTILS_TZTIME_H */
-
diff --git a/include/cutils/uevent.h b/include/cutils/uevent.h
index 4cca7e5..da1c2aa 100644
--- a/include/cutils/uevent.h
+++ b/include/cutils/uevent.h
@@ -27,6 +27,7 @@
int uevent_open_socket(int buf_sz, bool passcred);
ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length);
ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid);
+ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid);
#ifdef __cplusplus
}
diff --git a/include/log/log.h b/include/log/log.h
index d469f40..99015db 100644
--- a/include/log/log.h
+++ b/include/log/log.h
@@ -28,14 +28,12 @@
#ifndef _LIBS_LOG_LOG_H
#define _LIBS_LOG_LOG_H
-#include <sys/types.h>
-#ifdef HAVE_PTHREADS
-#include <pthread.h>
-#endif
#include <stdarg.h>
#include <stdio.h>
+#include <sys/types.h>
#include <time.h>
#include <unistd.h>
+
#include <log/logd.h>
#include <log/uio.h>
@@ -81,14 +79,16 @@
#endif
#endif
-#define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
+#ifndef __predict_false
+#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
+#endif
#ifndef ALOGV_IF
#if LOG_NDEBUG
#define ALOGV_IF(cond, ...) ((void)0)
#else
#define ALOGV_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -103,7 +103,7 @@
#ifndef ALOGD_IF
#define ALOGD_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -117,7 +117,7 @@
#ifndef ALOGI_IF
#define ALOGI_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -131,7 +131,7 @@
#ifndef ALOGW_IF
#define ALOGW_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -145,7 +145,7 @@
#ifndef ALOGE_IF
#define ALOGE_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -203,7 +203,8 @@
* Simplified macro to send a verbose system log message using the current LOG_TAG.
*/
#ifndef SLOGV
-#define __SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
+#define __SLOGV(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#if LOG_NDEBUG
#define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
#else
@@ -211,14 +212,12 @@
#endif
#endif
-#define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
-
#ifndef SLOGV_IF
#if LOG_NDEBUG
#define SLOGV_IF(cond, ...) ((void)0)
#else
#define SLOGV_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -228,12 +227,13 @@
* Simplified macro to send a debug system log message using the current LOG_TAG.
*/
#ifndef SLOGD
-#define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
+#define SLOGD(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#endif
#ifndef SLOGD_IF
#define SLOGD_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -242,12 +242,13 @@
* Simplified macro to send an info system log message using the current LOG_TAG.
*/
#ifndef SLOGI
-#define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
+#define SLOGI(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#endif
#ifndef SLOGI_IF
#define SLOGI_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -256,12 +257,13 @@
* Simplified macro to send a warning system log message using the current LOG_TAG.
*/
#ifndef SLOGW
-#define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
+#define SLOGW(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
#endif
#ifndef SLOGW_IF
#define SLOGW_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -270,12 +272,13 @@
* Simplified macro to send an error system log message using the current LOG_TAG.
*/
#ifndef SLOGE
-#define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
+#define SLOGE(...) \
+ ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#endif
#ifndef SLOGE_IF
#define SLOGE_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -286,7 +289,8 @@
* Simplified macro to send a verbose radio log message using the current LOG_TAG.
*/
#ifndef RLOGV
-#define __RLOGV(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
+#define __RLOGV(...) \
+ ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#if LOG_NDEBUG
#define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
#else
@@ -294,14 +298,12 @@
#endif
#endif
-#define CONDITION(cond) (__builtin_expect((cond)!=0, 0))
-
#ifndef RLOGV_IF
#if LOG_NDEBUG
#define RLOGV_IF(cond, ...) ((void)0)
#else
#define RLOGV_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -311,12 +313,13 @@
* Simplified macro to send a debug radio log message using the current LOG_TAG.
*/
#ifndef RLOGD
-#define RLOGD(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
+#define RLOGD(...) \
+ ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#endif
#ifndef RLOGD_IF
#define RLOGD_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -325,12 +328,13 @@
* Simplified macro to send an info radio log message using the current LOG_TAG.
*/
#ifndef RLOGI
-#define RLOGI(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
+#define RLOGI(...) \
+ ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#endif
#ifndef RLOGI_IF
#define RLOGI_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -339,12 +343,13 @@
* Simplified macro to send a warning radio log message using the current LOG_TAG.
*/
#ifndef RLOGW
-#define RLOGW(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
+#define RLOGW(...) \
+ ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
#endif
#ifndef RLOGW_IF
#define RLOGW_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -353,12 +358,13 @@
* Simplified macro to send an error radio log message using the current LOG_TAG.
*/
#ifndef RLOGE
-#define RLOGE(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
+#define RLOGE(...) \
+ ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#endif
#ifndef RLOGE_IF
#define RLOGE_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
: (void)0 )
#endif
@@ -374,7 +380,7 @@
*/
#ifndef LOG_ALWAYS_FATAL_IF
#define LOG_ALWAYS_FATAL_IF(cond, ...) \
- ( (CONDITION(cond)) \
+ ( (__predict_false(cond)) \
? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
: (void)0 )
#endif
@@ -491,7 +497,7 @@
#endif
#ifndef LOG_EVENT_STRING
#define LOG_EVENT_STRING(_tag, _value) \
- ((void) 0) /* not implemented -- must combine len with string */
+ (void) __android_log_bswrite(_tag, _value);
#endif
/* TODO: something for LIST */
@@ -534,8 +540,23 @@
#define android_btWriteLog(tag, type, payload, len) \
__android_log_btwrite(tag, type, payload, len)
+/*
+ * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
+ * android_testLog will remain constant in its purpose as a wrapper
+ * for Android logging filter policy, and can be subject to
+ * change. It can be reused by the developers that override
+ * IF_ALOG as a convenient means to reimplement their policy
+ * over Android.
+ */
+#if LOG_NDEBUG /* Production */
+#define android_testLog(prio, tag) \
+ (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
+#else
+#define android_testLog(prio, tag) \
+ (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
+#endif
+
// TODO: remove these prototypes and their users
-#define android_testLog(prio, tag) (1)
#define android_writevLog(vec,num) do{}while(0)
#define android_write1Log(str,len) do{}while (0)
#define android_setMinPriority(tag, prio) do{}while(0)
@@ -550,6 +571,7 @@
LOG_ID_RADIO = 1,
LOG_ID_EVENTS = 2,
LOG_ID_SYSTEM = 3,
+ LOG_ID_CRASH = 4,
LOG_ID_MAX
} log_id_t;
@@ -557,6 +579,12 @@
#define typeof_log_id_t unsigned char
/*
+ * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
+ * result of non-zero to expose a log.
+ */
+int __android_log_is_loggable(int prio, const char *tag, int def);
+
+/*
* Send a simple string to the log.
*/
int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
diff --git a/include/log/log_read.h b/include/log/log_read.h
index 861c192..1b70aff 100644
--- a/include/log/log_read.h
+++ b/include/log/log_read.h
@@ -17,23 +17,45 @@
#ifndef _LIBS_LOG_LOG_READ_H
#define _LIBS_LOG_LOG_READ_H
+#include <stdint.h>
#include <time.h>
+/* struct log_time is a wire-format variant of struct timespec */
#define NS_PER_SEC 1000000000ULL
+
#ifdef __cplusplus
-struct log_time : public timespec {
+
+// NB: do NOT define a copy constructor. This will result in structure
+// no longer being compatible with pass-by-value which is desired
+// efficient behavior. Also, pass-by-reference breaks C/C++ ABI.
+struct log_time {
public:
- log_time(timespec &T)
+ uint32_t tv_sec; // good to Feb 5 2106
+ uint32_t tv_nsec;
+
+ static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
+ static const uint32_t tv_nsec_max = 999999999UL;
+
+ log_time(const timespec &T)
{
tv_sec = T.tv_sec;
tv_nsec = T.tv_nsec;
}
- log_time(void)
+ log_time(uint32_t sec, uint32_t nsec)
+ {
+ tv_sec = sec;
+ tv_nsec = nsec;
+ }
+ static const timespec EPOCH;
+ log_time()
{
}
log_time(clockid_t id)
{
- clock_gettime(id, (timespec *) this);
+ timespec T;
+ clock_gettime(id, &T);
+ tv_sec = T.tv_sec;
+ tv_nsec = T.tv_nsec;
}
log_time(const char *T)
{
@@ -41,9 +63,12 @@
tv_sec = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
tv_nsec = c[4] | (c[5] << 8) | (c[6] << 16) | (c[7] << 24);
}
+
+ // timespec
bool operator== (const timespec &T) const
{
- return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
+ return (tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec == static_cast<uint32_t>(T.tv_nsec));
}
bool operator!= (const timespec &T) const
{
@@ -51,8 +76,9 @@
}
bool operator< (const timespec &T) const
{
- return (tv_sec < T.tv_sec)
- || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
+ return (tv_sec < static_cast<uint32_t>(T.tv_sec))
+ || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec < static_cast<uint32_t>(T.tv_nsec)));
}
bool operator>= (const timespec &T) const
{
@@ -60,20 +86,85 @@
}
bool operator> (const timespec &T) const
{
- return (tv_sec > T.tv_sec)
- || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
+ return (tv_sec > static_cast<uint32_t>(T.tv_sec))
+ || ((tv_sec == static_cast<uint32_t>(T.tv_sec))
+ && (tv_nsec > static_cast<uint32_t>(T.tv_nsec)));
}
bool operator<= (const timespec &T) const
{
return !(*this > T);
}
- uint64_t nsec(void) const
+ log_time operator-= (const timespec &T);
+ log_time operator- (const timespec &T) const
+ {
+ log_time local(*this);
+ return local -= T;
+ }
+ log_time operator+= (const timespec &T);
+ log_time operator+ (const timespec &T) const
+ {
+ log_time local(*this);
+ return local += T;
+ }
+
+ // log_time
+ bool operator== (const log_time &T) const
+ {
+ return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
+ }
+ bool operator!= (const log_time &T) const
+ {
+ return !(*this == T);
+ }
+ bool operator< (const log_time &T) const
+ {
+ return (tv_sec < T.tv_sec)
+ || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
+ }
+ bool operator>= (const log_time &T) const
+ {
+ return !(*this < T);
+ }
+ bool operator> (const log_time &T) const
+ {
+ return (tv_sec > T.tv_sec)
+ || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
+ }
+ bool operator<= (const log_time &T) const
+ {
+ return !(*this > T);
+ }
+ log_time operator-= (const log_time &T);
+ log_time operator- (const log_time &T) const
+ {
+ log_time local(*this);
+ return local -= T;
+ }
+ log_time operator+= (const log_time &T);
+ log_time operator+ (const log_time &T) const
+ {
+ log_time local(*this);
+ return local += T;
+ }
+
+ uint64_t nsec() const
{
return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
}
-};
+
+ static const char default_format[];
+
+ // Add %#q for the fraction of a second to the standard library functions
+ char *strptime(const char *s, const char *format = default_format);
+} __attribute__((__packed__));
+
#else
-typedef struct timespec log_time;
+
+typedef struct log_time {
+ uint32_t tv_sec;
+ uint32_t tv_nsec;
+} __attribute__((__packed__)) log_time;
+
#endif
#endif /* define _LIBS_LOG_LOG_READ_H */
diff --git a/include/log/logd.h b/include/log/logd.h
index 379c373..0fe515f 100644
--- a/include/log/logd.h
+++ b/include/log/logd.h
@@ -23,16 +23,17 @@
#include <android/log.h>
/* the rest is only used internally by the system */
-#include <time.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdint.h>
-#include <sys/types.h>
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
#include <pthread.h>
#endif
-#include <log/uio.h>
#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <log/uio.h>
#ifdef __cplusplus
extern "C" {
@@ -41,6 +42,7 @@
int __android_log_bwrite(int32_t tag, const void *payload, size_t len);
int __android_log_btwrite(int32_t tag, char type, const void *payload,
size_t len);
+int __android_log_bswrite(int32_t tag, const char *payload);
#ifdef __cplusplus
}
diff --git a/include/log/logger.h b/include/log/logger.h
index 966397a..f030dab 100644
--- a/include/log/logger.h
+++ b/include/log/logger.h
@@ -12,6 +12,7 @@
#include <stdint.h>
#include <log/log.h>
+#include <log/log_read.h>
#ifdef __cplusplus
extern "C" {
@@ -30,12 +31,12 @@
int32_t sec; /* seconds since Epoch */
int32_t nsec; /* nanoseconds */
char msg[0]; /* the entry's payload */
-};
+} __attribute__((__packed__));
/*
* The userspace structure for version 2 of the logger_entry ABI.
* This structure is returned to userspace if ioctl(LOGGER_SET_VERSION)
- * is called with version==2
+ * is called with version==2; or used with the user space log daemon.
*/
struct logger_entry_v2 {
uint16_t len; /* length of the payload */
@@ -46,13 +47,23 @@
int32_t nsec; /* nanoseconds */
uint32_t euid; /* effective UID of logger */
char msg[0]; /* the entry's payload */
-};
+} __attribute__((__packed__));
+
+struct logger_entry_v3 {
+ uint16_t len; /* length of the payload */
+ uint16_t hdr_size; /* sizeof(struct logger_entry_v3) */
+ int32_t pid; /* generating process's pid */
+ int32_t tid; /* generating process's tid */
+ int32_t sec; /* seconds since Epoch */
+ int32_t nsec; /* nanoseconds */
+ uint32_t lid; /* log id of the payload */
+ char msg[0]; /* the entry's payload */
+} __attribute__((__packed__));
/*
* The maximum size of the log entry payload that can be
- * written to the kernel logger driver. An attempt to write
- * more than this amount to /dev/log/* will result in a
- * truncated log entry.
+ * written to the logger. An attempt to write more than
+ * this amount will result in a truncated log entry.
*/
#define LOGGER_ENTRY_MAX_PAYLOAD 4076
@@ -68,13 +79,10 @@
struct log_msg {
union {
unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
- struct logger_entry_v2 entry;
+ struct logger_entry_v3 entry;
+ struct logger_entry_v3 entry_v3;
struct logger_entry_v2 entry_v2;
struct logger_entry entry_v1;
- struct {
- unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
- log_id_t id;
- } extra;
} __attribute__((aligned(4)));
#ifdef __cplusplus
/* Matching log_time operators */
@@ -106,21 +114,21 @@
{
return !(*this > T);
}
- uint64_t nsec(void) const
+ uint64_t nsec() const
{
return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
}
/* packet methods */
- log_id_t id(void)
+ log_id_t id()
{
- return extra.id;
+ return (log_id_t) entry.lid;
}
- char *msg(void)
+ char *msg()
{
return entry.hdr_size ? (char *) buf + entry.hdr_size : entry_v1.msg;
}
- unsigned int len(void)
+ unsigned int len()
{
return (entry.hdr_size ? entry.hdr_size : sizeof(entry_v1)) + entry.len;
}
@@ -132,15 +140,33 @@
log_id_t android_logger_get_id(struct logger *logger);
int android_logger_clear(struct logger *logger);
-int android_logger_get_log_size(struct logger *logger);
-int android_logger_get_log_readable_size(struct logger *logger);
+long android_logger_get_log_size(struct logger *logger);
+int android_logger_set_log_size(struct logger *logger, unsigned long size);
+long android_logger_get_log_readable_size(struct logger *logger);
int android_logger_get_log_version(struct logger *logger);
struct logger_list;
+ssize_t android_logger_get_statistics(struct logger_list *logger_list,
+ char *buf, size_t len);
+ssize_t android_logger_get_prune_list(struct logger_list *logger_list,
+ char *buf, size_t len);
+int android_logger_set_prune_list(struct logger_list *logger_list,
+ char *buf, size_t len);
+
+#define ANDROID_LOG_RDONLY O_RDONLY
+#define ANDROID_LOG_WRONLY O_WRONLY
+#define ANDROID_LOG_RDWR O_RDWR
+#define ANDROID_LOG_ACCMODE O_ACCMODE
+#define ANDROID_LOG_NONBLOCK O_NONBLOCK
+#define ANDROID_LOG_PSTORE 0x80000000
+
struct logger_list *android_logger_list_alloc(int mode,
unsigned int tail,
pid_t pid);
+struct logger_list *android_logger_list_alloc_time(int mode,
+ log_time start,
+ pid_t pid);
void android_logger_list_free(struct logger_list *logger_list);
/* In the purest sense, the following two are orthogonal interfaces */
int android_logger_list_read(struct logger_list *logger_list,
diff --git a/include/log/logprint.h b/include/log/logprint.h
index 481c96e..1e42b47 100644
--- a/include/log/logprint.h
+++ b/include/log/logprint.h
@@ -36,6 +36,7 @@
FORMAT_TIME,
FORMAT_THREADTIME,
FORMAT_LONG,
+ FORMAT_COLOR,
} AndroidLogPrintFormat;
typedef struct AndroidLogFormat_t AndroidLogFormat;
diff --git a/include/log/uio.h b/include/log/uio.h
index a71f515..7059da5 100644
--- a/include/log/uio.h
+++ b/include/log/uio.h
@@ -14,20 +14,23 @@
* limitations under the License.
*/
-//
-// implementation of sys/uio.h for platforms that don't have it (Win32)
-//
#ifndef _LIBS_CUTILS_UIO_H
#define _LIBS_CUTILS_UIO_H
-#ifdef HAVE_SYS_UIO_H
+#if !defined(_WIN32)
+
#include <sys/uio.h>
+
#else
#ifdef __cplusplus
extern "C" {
#endif
+//
+// Implementation of sys/uio.h for Win32.
+//
+
#include <stddef.h>
struct iovec {
@@ -42,7 +45,7 @@
}
#endif
-#endif /* !HAVE_SYS_UIO_H */
+#endif
#endif /* _LIBS_UTILS_UIO_H */
diff --git a/include/nativebridge/native_bridge.h b/include/nativebridge/native_bridge.h
new file mode 100644
index 0000000..523dc49
--- /dev/null
+++ b/include/nativebridge/native_bridge.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2014 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 NATIVE_BRIDGE_H_
+#define NATIVE_BRIDGE_H_
+
+#include "jni.h"
+#include <stdint.h>
+#include <sys/types.h>
+
+namespace android {
+
+struct NativeBridgeRuntimeCallbacks;
+struct NativeBridgeRuntimeValues;
+
+// Open the native bridge, if any. Should be called by Runtime::Init(). A null library filename
+// signals that we do not want to load a native bridge.
+bool LoadNativeBridge(const char* native_bridge_library_filename,
+ const NativeBridgeRuntimeCallbacks* runtime_callbacks);
+
+// Quick check whether a native bridge will be needed. This is based off of the instruction set
+// of the process.
+bool NeedsNativeBridge(const char* instruction_set);
+
+// Do the early initialization part of the native bridge, if necessary. This should be done under
+// high privileges.
+bool PreInitializeNativeBridge(const char* app_data_dir, const char* instruction_set);
+
+// Initialize the native bridge, if any. Should be called by Runtime::DidForkFromZygote. The JNIEnv*
+// will be used to modify the app environment for the bridge.
+bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set);
+
+// Unload the native bridge, if any. Should be called by Runtime::DidForkFromZygote.
+void UnloadNativeBridge();
+
+// Check whether a native bridge is available (opened or initialized). Requires a prior call to
+// LoadNativeBridge.
+bool NativeBridgeAvailable();
+
+// Check whether a native bridge is available (initialized). Requires a prior call to
+// LoadNativeBridge & InitializeNativeBridge.
+bool NativeBridgeInitialized();
+
+// Load a shared library that is supported by the native bridge.
+void* NativeBridgeLoadLibrary(const char* libpath, int flag);
+
+// Get a native bridge trampoline for specified native method.
+void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len);
+
+// True if native library is valid and is for an ABI that is supported by native bridge.
+bool NativeBridgeIsSupported(const char* libpath);
+
+// Returns whether we have seen a native bridge error. This could happen because the library
+// was not found, rejected, could not be initialized and so on.
+//
+// This functionality is mainly for testing.
+bool NativeBridgeError();
+
+// Returns whether a given string is acceptable as a native bridge library filename.
+//
+// This functionality is exposed mainly for testing.
+bool NativeBridgeNameAcceptable(const char* native_bridge_library_filename);
+
+// Native bridge interfaces to runtime.
+struct NativeBridgeCallbacks {
+ // Version number of the interface.
+ uint32_t version;
+
+ // Initialize native bridge. Native bridge's internal implementation must ensure MT safety and
+ // that the native bridge is initialized only once. Thus it is OK to call this interface for an
+ // already initialized native bridge.
+ //
+ // Parameters:
+ // runtime_cbs [IN] the pointer to NativeBridgeRuntimeCallbacks.
+ // Returns:
+ // true iff initialization was successful.
+ bool (*initialize)(const NativeBridgeRuntimeCallbacks* runtime_cbs, const char* private_dir,
+ const char* instruction_set);
+
+ // Load a shared library that is supported by the native bridge.
+ //
+ // Parameters:
+ // libpath [IN] path to the shared library
+ // flag [IN] the stardard RTLD_XXX defined in bionic dlfcn.h
+ // Returns:
+ // The opaque handle of the shared library if sucessful, otherwise NULL
+ void* (*loadLibrary)(const char* libpath, int flag);
+
+ // Get a native bridge trampoline for specified native method. The trampoline has same
+ // sigature as the native method.
+ //
+ // Parameters:
+ // handle [IN] the handle returned from loadLibrary
+ // shorty [IN] short descriptor of native method
+ // len [IN] length of shorty
+ // Returns:
+ // address of trampoline if successful, otherwise NULL
+ void* (*getTrampoline)(void* handle, const char* name, const char* shorty, uint32_t len);
+
+ // Check whether native library is valid and is for an ABI that is supported by native bridge.
+ //
+ // Parameters:
+ // libpath [IN] path to the shared library
+ // Returns:
+ // TRUE if library is supported by native bridge, FALSE otherwise
+ bool (*isSupported)(const char* libpath);
+
+ // Provide environment values required by the app running with native bridge according to the
+ // instruction set.
+ //
+ // Parameters:
+ // instruction_set [IN] the instruction set of the app
+ // Returns:
+ // NULL if not supported by native bridge.
+ // Otherwise, return all environment values to be set after fork.
+ const struct NativeBridgeRuntimeValues* (*getAppEnv)(const char* instruction_set);
+};
+
+// Runtime interfaces to native bridge.
+struct NativeBridgeRuntimeCallbacks {
+ // Get shorty of a Java method. The shorty is supposed to be persistent in memory.
+ //
+ // Parameters:
+ // env [IN] pointer to JNIenv.
+ // mid [IN] Java methodID.
+ // Returns:
+ // short descriptor for method.
+ const char* (*getMethodShorty)(JNIEnv* env, jmethodID mid);
+
+ // Get number of native methods for specified class.
+ //
+ // Parameters:
+ // env [IN] pointer to JNIenv.
+ // clazz [IN] Java class object.
+ // Returns:
+ // number of native methods.
+ uint32_t (*getNativeMethodCount)(JNIEnv* env, jclass clazz);
+
+ // Get at most 'method_count' native methods for specified class 'clazz'. Results are outputed
+ // via 'methods' [OUT]. The signature pointer in JNINativeMethod is reused as the method shorty.
+ //
+ // Parameters:
+ // env [IN] pointer to JNIenv.
+ // clazz [IN] Java class object.
+ // methods [OUT] array of method with the name, shorty, and fnPtr.
+ // method_count [IN] max number of elements in methods.
+ // Returns:
+ // number of method it actually wrote to methods.
+ uint32_t (*getNativeMethods)(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
+ uint32_t method_count);
+};
+
+}; // namespace android
+
+#endif // NATIVE_BRIDGE_H_
diff --git a/include/netutils/dhcp.h b/include/netutils/dhcp.h
index de6bc82..008dbd8 100644
--- a/include/netutils/dhcp.h
+++ b/include/netutils/dhcp.h
@@ -23,26 +23,18 @@
__BEGIN_DECLS
extern int do_dhcp(char *iname);
-extern int dhcp_do_request(const char *ifname,
- char *ipaddr,
- char *gateway,
- uint32_t *prefixLength,
- char *dns[],
- char *server,
- uint32_t *lease,
- char *vendorInfo,
- char *domain,
- char *mtu);
-extern int dhcp_do_request_renew(const char *ifname,
- char *ipaddr,
- char *gateway,
- uint32_t *prefixLength,
- char *dns[],
- char *server,
- uint32_t *lease,
- char *vendorInfo,
- char *domain,
- char *mtu);
+extern int dhcp_start(const char *ifname);
+extern int dhcp_start_renew(const char *ifname);
+extern int dhcp_get_results(const char *ifname,
+ char *ipaddr,
+ char *gateway,
+ uint32_t *prefixLength,
+ char *dns[],
+ char *server,
+ uint32_t *lease,
+ char *vendorInfo,
+ char *domain,
+ char *mtu);
extern int dhcp_stop(const char *ifname);
extern int dhcp_release_lease(const char *ifname);
extern char *dhcp_get_errmsg();
diff --git a/include/netutils/ifc.h b/include/netutils/ifc.h
index 1f5421d..3b27234 100644
--- a/include/netutils/ifc.h
+++ b/include/netutils/ifc.h
@@ -36,6 +36,7 @@
#define RESET_IPV4_ADDRESSES 0x01
#define RESET_IPV6_ADDRESSES 0x02
+#define RESET_IGNORE_INTERFACE_ADDRESS 0x04
#define RESET_ALL_ADDRESSES (RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES)
extern int ifc_reset_connections(const char *ifname, const int reset_mask);
@@ -49,19 +50,8 @@
extern int ifc_set_hwaddr(const char *name, const void *ptr);
extern int ifc_clear_addresses(const char *name);
-/* This function is deprecated. Use ifc_add_route instead. */
-extern int ifc_add_host_route(const char *name, in_addr_t addr);
-extern int ifc_remove_host_routes(const char *name);
-extern int ifc_get_default_route(const char *ifname);
-/* This function is deprecated. Use ifc_add_route instead */
-extern int ifc_set_default_route(const char *ifname, in_addr_t gateway);
-/* This function is deprecated. Use ifc_add_route instead */
extern int ifc_create_default_route(const char *name, in_addr_t addr);
extern int ifc_remove_default_route(const char *ifname);
-extern int ifc_add_route(const char *name, const char *addr, int prefix_length,
- const char *gw);
-extern int ifc_remove_route(const char *ifname, const char *dst,
- int prefix_length, const char *gw);
extern int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength,
unsigned *flags);
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 20bfdbe..a3d11a7 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -76,11 +76,18 @@
#define AID_SDCARD_PICS 1033 /* external storage photos access */
#define AID_SDCARD_AV 1034 /* external storage audio/video access */
#define AID_SDCARD_ALL 1035 /* access all users external storage */
+#define AID_LOGD 1036 /* log daemon */
+#define AID_SHARED_RELRO 1037 /* creator of shared GNU RELRO files */
#define AID_SHELL 2000 /* adb and debug shell user */
#define AID_CACHE 2001 /* cache access */
#define AID_DIAG 2002 /* access to diagnostic resources */
+/* The range 2900-2999 is reserved for OEM, and must never be
+ * used here */
+#define AID_OEM_RESERVED_START 2900
+#define AID_OEM_RESERVED_END 2999
+
/* The 3000 series are intended for use as supplemental group id's only.
* They indicate special Android capabilities that the kernel is aware of. */
#define AID_NET_BT_ADMIN 3001 /* bluetooth: create any socket */
@@ -92,6 +99,7 @@
#define AID_NET_BW_ACCT 3007 /* change bandwidth statistics accounting */
#define AID_NET_BT_STACK 3008 /* bluetooth: access config files */
+#define AID_EVERYBODY 9997 /* shared between all apps in the same profile */
#define AID_MISC 9998 /* access to misc storage */
#define AID_NOBODY 9999
@@ -151,6 +159,8 @@
{ "sdcard_pics", AID_SDCARD_PICS, },
{ "sdcard_av", AID_SDCARD_AV, },
{ "sdcard_all", AID_SDCARD_ALL, },
+ { "logd", AID_LOGD, },
+ { "shared_relro", AID_SHARED_RELRO, },
{ "shell", AID_SHELL, },
{ "cache", AID_CACHE, },
@@ -165,6 +175,7 @@
{ "net_bw_acct", AID_NET_BW_ACCT, },
{ "net_bt_stack", AID_NET_BT_STACK, },
+ { "everybody", AID_EVERYBODY, },
{ "misc", AID_MISC, },
{ "nobody", AID_NOBODY, },
};
@@ -190,12 +201,13 @@
{ 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
- { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/dalvik-cache" },
+ { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
{ 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
{ 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
{ 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
{ 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
+ { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
{ 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
{ 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
@@ -218,30 +230,21 @@
static const struct fs_path_config android_files[] = {
{ 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
{ 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
- { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.trout.rc" },
{ 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
- { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.testmenu" },
{ 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
- { 00444, AID_RADIO, AID_AUDIO, 0, "system/etc/AudioPara4.csv" },
{ 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
{ 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
{ 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
{ 00644, AID_APP, AID_APP, 0, "data/data/*" },
- { 00755, AID_ROOT, AID_ROOT, 0, "system/bin/ping" },
-
- /* the following file is INTENTIONALLY set-gid and not set-uid.
- * Do not change. */
- { 02750, AID_ROOT, AID_INET, 0, "system/bin/netcfg" },
/* the following five files are INTENTIONALLY set-uid, but they
* are NOT included on user builds. */
- { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/su" },
+ { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
- { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/tcpdump" },
{ 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
/* the following files have enhanced capabilities and ARE included in user builds. */
@@ -251,6 +254,7 @@
{ 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
+ { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
diff --git a/include/private/android_logger.h b/include/private/android_logger.h
new file mode 100644
index 0000000..724ca51
--- /dev/null
+++ b/include/private/android_logger.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/* This file is used to define the internal protocol for the Android Logger */
+
+#ifndef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#define _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+
+#include <stdint.h>
+
+#include <log/log.h>
+#include <log/log_read.h>
+
+#define LOGGER_MAGIC 'l'
+
+/* Header Structure to pstore */
+typedef struct __attribute__((__packed__)) {
+ uint8_t magic;
+ uint16_t len;
+ uint16_t uid;
+ uint16_t pid;
+} android_pmsg_log_header_t;
+
+/* Header Structure to logd, and second header for pstore */
+typedef struct __attribute__((__packed__)) {
+ typeof_log_id_t id;
+ uint16_t tid;
+ log_time realtime;
+} android_log_header_t;
+
+/* Event Header Structure to logd */
+typedef struct __attribute__((__packed__)) {
+ int32_t tag; // Little Endian Order
+} android_event_header_t;
+
+/* Event payload EVENT_TYPE_INT */
+typedef struct __attribute__((__packed__)) {
+ int8_t type; // EVENT_TYPE_INT
+ int32_t data; // Little Endian Order
+} android_event_int_t;
+
+/* Event with single EVENT_TYPE_INT */
+typedef struct __attribute__((__packed__)) {
+ android_event_header_t header;
+ android_event_int_t payload;
+} android_log_event_int_t;
+
+/* Event payload EVENT_TYPE_LONG */
+typedef struct __attribute__((__packed__)) {
+ int8_t type; // EVENT_TYPE_LONG
+ int64_t data; // Little Endian Order
+} android_event_long_t;
+
+/* Event with single EVENT_TYPE_LONG */
+typedef struct __attribute__((__packed__)) {
+ android_event_header_t header;
+ android_event_long_t payload;
+} android_log_event_long_t;
+
+/* Event payload EVENT_TYPE_STRING */
+typedef struct __attribute__((__packed__)) {
+ int8_t type; // EVENT_TYPE_STRING;
+ int32_t length; // Little Endian Order
+ char data[];
+} android_event_string_t;
+
+/* Event with single EVENT_TYPE_STRING */
+typedef struct __attribute__((__packed__)) {
+ android_event_header_t header;
+ android_event_string_t payload;
+} android_log_event_string_t;
+
+#endif
diff --git a/include/system/audio.h b/include/system/audio.h
index afd7176..17bf260 100644
--- a/include/system/audio.h
+++ b/include/system/audio.h
@@ -20,6 +20,7 @@
#include <stdbool.h>
#include <stdint.h>
+#include <stdio.h>
#include <sys/cdefs.h>
#include <sys/types.h>
@@ -52,17 +53,76 @@
AUDIO_STREAM_ALARM = 4,
AUDIO_STREAM_NOTIFICATION = 5,
AUDIO_STREAM_BLUETOOTH_SCO = 6,
- AUDIO_STREAM_ENFORCED_AUDIBLE = 7, /* Sounds that cannot be muted by user and must be routed to speaker */
+ AUDIO_STREAM_ENFORCED_AUDIBLE = 7, /* Sounds that cannot be muted by user
+ * and must be routed to speaker
+ */
AUDIO_STREAM_DTMF = 8,
- AUDIO_STREAM_TTS = 9,
-
- AUDIO_STREAM_CNT,
- AUDIO_STREAM_MAX = AUDIO_STREAM_CNT - 1,
+ AUDIO_STREAM_TTS = 9, /* Transmitted Through Speaker.
+ * Plays over speaker only, silent on other devices.
+ */
+ AUDIO_STREAM_ACCESSIBILITY = 10, /* For accessibility talk back prompts */
+ AUDIO_STREAM_REROUTING = 11, /* For dynamic policy output mixes */
+ AUDIO_STREAM_PATCH = 12, /* For internal audio flinger tracks. Fixed volume */
+ AUDIO_STREAM_PUBLIC_CNT = AUDIO_STREAM_TTS + 1,
+ AUDIO_STREAM_CNT = AUDIO_STREAM_PATCH + 1,
} audio_stream_type_t;
/* Do not change these values without updating their counterparts
+ * in frameworks/base/media/java/android/media/AudioAttributes.java
+ */
+typedef enum {
+ AUDIO_CONTENT_TYPE_UNKNOWN = 0,
+ AUDIO_CONTENT_TYPE_SPEECH = 1,
+ AUDIO_CONTENT_TYPE_MUSIC = 2,
+ AUDIO_CONTENT_TYPE_MOVIE = 3,
+ AUDIO_CONTENT_TYPE_SONIFICATION = 4,
+
+ AUDIO_CONTENT_TYPE_CNT,
+ AUDIO_CONTENT_TYPE_MAX = AUDIO_CONTENT_TYPE_CNT - 1,
+} audio_content_type_t;
+
+/* Do not change these values without updating their counterparts
+ * in frameworks/base/media/java/android/media/AudioAttributes.java
+ */
+typedef enum {
+ AUDIO_USAGE_UNKNOWN = 0,
+ AUDIO_USAGE_MEDIA = 1,
+ AUDIO_USAGE_VOICE_COMMUNICATION = 2,
+ AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING = 3,
+ AUDIO_USAGE_ALARM = 4,
+ AUDIO_USAGE_NOTIFICATION = 5,
+ AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE = 6,
+ AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST = 7,
+ AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT = 8,
+ AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED = 9,
+ AUDIO_USAGE_NOTIFICATION_EVENT = 10,
+ AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY = 11,
+ AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12,
+ AUDIO_USAGE_ASSISTANCE_SONIFICATION = 13,
+ AUDIO_USAGE_GAME = 14,
+ AUDIO_USAGE_VIRTUAL_SOURCE = 15,
+
+ AUDIO_USAGE_CNT,
+ AUDIO_USAGE_MAX = AUDIO_USAGE_CNT - 1,
+} audio_usage_t;
+
+typedef uint32_t audio_flags_mask_t;
+
+/* Do not change these values without updating their counterparts
+ * in frameworks/base/media/java/android/media/AudioAttributes.java
+ */
+enum {
+ AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1,
+ AUDIO_FLAG_SECURE = 0x2,
+ AUDIO_FLAG_SCO = 0x4,
+ AUDIO_FLAG_BEACON = 0x8,
+ AUDIO_FLAG_HW_AV_SYNC = 0x10,
+ AUDIO_FLAG_HW_HOTWORD = 0x20,
+};
+
+/* Do not change these values without updating their counterparts
* in frameworks/base/media/java/android/media/MediaRecorder.java,
- * frameworks/av/services/audioflinger/AudioPolicyService.cpp,
+ * frameworks/av/services/audiopolicy/AudioPolicyService.cpp,
* and system/media/audio_effects/include/audio_effects/audio_effects_conf.h!
*/
typedef enum {
@@ -80,6 +140,7 @@
/* play the mix captured by this audio source. */
AUDIO_SOURCE_CNT,
AUDIO_SOURCE_MAX = AUDIO_SOURCE_CNT - 1,
+ AUDIO_SOURCE_FM_TUNER = 1998,
AUDIO_SOURCE_HOTWORD = 1999, /* A low-priority, preemptible audio source for
for background software hotword detection.
Same tuning as AUDIO_SOURCE_VOICE_RECOGNITION.
@@ -87,6 +148,16 @@
at the audio HAL. */
} audio_source_t;
+/* Audio attributes */
+#define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
+typedef struct {
+ audio_content_type_t content_type;
+ audio_usage_t usage;
+ audio_source_t source;
+ audio_flags_mask_t flags;
+ char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
+} audio_attributes_t;
+
/* special audio session values
* (XXX: should this be living in the audio effects land?)
*/
@@ -110,6 +181,11 @@
AUDIO_SESSION_ALLOCATE = 0,
} audio_session_t;
+/* a unique ID allocated by AudioFlinger for use as a audio_io_handle_t or audio_session_t */
+typedef int audio_unique_id_t;
+
+#define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
+
/* Audio sub formats (see enum audio_format). */
/* PCM sub formats */
@@ -118,7 +194,7 @@
AUDIO_FORMAT_PCM_SUB_16_BIT = 0x1, /* DO NOT CHANGE - PCM signed 16 bits */
AUDIO_FORMAT_PCM_SUB_8_BIT = 0x2, /* DO NOT CHANGE - PCM unsigned 8 bits */
AUDIO_FORMAT_PCM_SUB_32_BIT = 0x3, /* PCM signed .31 fixed point */
- AUDIO_FORMAT_PCM_SUB_8_24_BIT = 0x4, /* PCM signed 7.24 fixed point */
+ AUDIO_FORMAT_PCM_SUB_8_24_BIT = 0x4, /* PCM signed 8.23 fixed point */
AUDIO_FORMAT_PCM_SUB_FLOAT = 0x5, /* PCM single-precision floating point */
AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED = 0x6, /* PCM signed .23 fixed point packed in 3 bytes */
} audio_format_pcm_sub_fmt_t;
@@ -141,7 +217,16 @@
/* AAC sub format field definition: specify profile or bitrate for recording... */
typedef enum {
- AUDIO_FORMAT_AAC_SUB_NONE = 0x0,
+ AUDIO_FORMAT_AAC_SUB_MAIN = 0x1,
+ AUDIO_FORMAT_AAC_SUB_LC = 0x2,
+ AUDIO_FORMAT_AAC_SUB_SSR = 0x4,
+ AUDIO_FORMAT_AAC_SUB_LTP = 0x8,
+ AUDIO_FORMAT_AAC_SUB_HE_V1 = 0x10,
+ AUDIO_FORMAT_AAC_SUB_SCALABLE = 0x20,
+ AUDIO_FORMAT_AAC_SUB_ERLC = 0x40,
+ AUDIO_FORMAT_AAC_SUB_LD = 0x80,
+ AUDIO_FORMAT_AAC_SUB_HE_V2 = 0x100,
+ AUDIO_FORMAT_AAC_SUB_ELD = 0x200,
} audio_format_aac_sub_fmt_t;
/* VORBIS sub format field definition: specify quality for recording... */
@@ -166,9 +251,12 @@
AUDIO_FORMAT_AMR_NB = 0x02000000UL,
AUDIO_FORMAT_AMR_WB = 0x03000000UL,
AUDIO_FORMAT_AAC = 0x04000000UL,
- AUDIO_FORMAT_HE_AAC_V1 = 0x05000000UL,
- AUDIO_FORMAT_HE_AAC_V2 = 0x06000000UL,
+ AUDIO_FORMAT_HE_AAC_V1 = 0x05000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V1*/
+ AUDIO_FORMAT_HE_AAC_V2 = 0x06000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V2*/
AUDIO_FORMAT_VORBIS = 0x07000000UL,
+ AUDIO_FORMAT_OPUS = 0x08000000UL,
+ AUDIO_FORMAT_AC3 = 0x09000000UL,
+ AUDIO_FORMAT_E_AC3 = 0x0A000000UL,
AUDIO_FORMAT_MAIN_MASK = 0xFF000000UL,
AUDIO_FORMAT_SUB_MASK = 0x00FFFFFFUL,
@@ -187,9 +275,41 @@
AUDIO_FORMAT_PCM_SUB_FLOAT),
AUDIO_FORMAT_PCM_24_BIT_PACKED = (AUDIO_FORMAT_PCM |
AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED),
+ AUDIO_FORMAT_AAC_MAIN = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_MAIN),
+ AUDIO_FORMAT_AAC_LC = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_LC),
+ AUDIO_FORMAT_AAC_SSR = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_SSR),
+ AUDIO_FORMAT_AAC_LTP = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_LTP),
+ AUDIO_FORMAT_AAC_HE_V1 = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_HE_V1),
+ AUDIO_FORMAT_AAC_SCALABLE = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_SCALABLE),
+ AUDIO_FORMAT_AAC_ERLC = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_ERLC),
+ AUDIO_FORMAT_AAC_LD = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_LD),
+ AUDIO_FORMAT_AAC_HE_V2 = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_HE_V2),
+ AUDIO_FORMAT_AAC_ELD = (AUDIO_FORMAT_AAC |
+ AUDIO_FORMAT_AAC_SUB_ELD),
} audio_format_t;
+/* For the channel mask for position assignment representation */
enum {
+
+/* These can be a complete audio_channel_mask_t. */
+
+ AUDIO_CHANNEL_NONE = 0x0,
+ AUDIO_CHANNEL_INVALID = 0xC0000000,
+
+/* These can be the bits portion of an audio_channel_mask_t
+ * with representation AUDIO_CHANNEL_REPRESENTATION_POSITION.
+ * Using these bits as a complete audio_channel_mask_t is deprecated.
+ */
+
/* output channels */
AUDIO_CHANNEL_OUT_FRONT_LEFT = 0x1,
AUDIO_CHANNEL_OUT_FRONT_RIGHT = 0x2,
@@ -210,6 +330,8 @@
AUDIO_CHANNEL_OUT_TOP_BACK_CENTER = 0x10000,
AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT = 0x20000,
+/* TODO: should these be considered complete channel masks, or only bits? */
+
AUDIO_CHANNEL_OUT_MONO = AUDIO_CHANNEL_OUT_FRONT_LEFT,
AUDIO_CHANNEL_OUT_STEREO = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
AUDIO_CHANNEL_OUT_FRONT_RIGHT),
@@ -217,16 +339,26 @@
AUDIO_CHANNEL_OUT_FRONT_RIGHT |
AUDIO_CHANNEL_OUT_BACK_LEFT |
AUDIO_CHANNEL_OUT_BACK_RIGHT),
- AUDIO_CHANNEL_OUT_SURROUND = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
+ AUDIO_CHANNEL_OUT_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD,
+ /* like AUDIO_CHANNEL_OUT_QUAD_BACK with *_SIDE_* instead of *_BACK_* */
+ AUDIO_CHANNEL_OUT_QUAD_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
AUDIO_CHANNEL_OUT_FRONT_RIGHT |
- AUDIO_CHANNEL_OUT_FRONT_CENTER |
- AUDIO_CHANNEL_OUT_BACK_CENTER),
+ AUDIO_CHANNEL_OUT_SIDE_LEFT |
+ AUDIO_CHANNEL_OUT_SIDE_RIGHT),
AUDIO_CHANNEL_OUT_5POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
AUDIO_CHANNEL_OUT_FRONT_RIGHT |
AUDIO_CHANNEL_OUT_FRONT_CENTER |
AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
AUDIO_CHANNEL_OUT_BACK_LEFT |
AUDIO_CHANNEL_OUT_BACK_RIGHT),
+ AUDIO_CHANNEL_OUT_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1,
+ /* like AUDIO_CHANNEL_OUT_5POINT1_BACK with *_SIDE_* instead of *_BACK_* */
+ AUDIO_CHANNEL_OUT_5POINT1_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
+ AUDIO_CHANNEL_OUT_FRONT_RIGHT |
+ AUDIO_CHANNEL_OUT_FRONT_CENTER |
+ AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
+ AUDIO_CHANNEL_OUT_SIDE_LEFT |
+ AUDIO_CHANNEL_OUT_SIDE_RIGHT),
// matches the correct AudioFormat.CHANNEL_OUT_7POINT1_SURROUND definition for 7.1
AUDIO_CHANNEL_OUT_7POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
AUDIO_CHANNEL_OUT_FRONT_RIGHT |
@@ -255,6 +387,8 @@
AUDIO_CHANNEL_OUT_TOP_BACK_CENTER|
AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT),
+/* These are bits only, not complete values */
+
/* input channels */
AUDIO_CHANNEL_IN_LEFT = 0x4,
AUDIO_CHANNEL_IN_RIGHT = 0x8,
@@ -271,6 +405,8 @@
AUDIO_CHANNEL_IN_VOICE_UPLINK = 0x4000,
AUDIO_CHANNEL_IN_VOICE_DNLINK = 0x8000,
+/* TODO: should these be considered complete channel masks, or only bits, or deprecated? */
+
AUDIO_CHANNEL_IN_MONO = AUDIO_CHANNEL_IN_FRONT,
AUDIO_CHANNEL_IN_STEREO = (AUDIO_CHANNEL_IN_LEFT | AUDIO_CHANNEL_IN_RIGHT),
AUDIO_CHANNEL_IN_FRONT_BACK = (AUDIO_CHANNEL_IN_FRONT | AUDIO_CHANNEL_IN_BACK),
@@ -290,11 +426,105 @@
AUDIO_CHANNEL_IN_VOICE_DNLINK),
};
+/* A channel mask per se only defines the presence or absence of a channel, not the order.
+ * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
+ *
+ * audio_channel_mask_t is an opaque type and its internal layout should not
+ * be assumed as it may change in the future.
+ * Instead, always use the functions declared in this header to examine.
+ *
+ * These are the current representations:
+ *
+ * AUDIO_CHANNEL_REPRESENTATION_POSITION
+ * is a channel mask representation for position assignment.
+ * Each low-order bit corresponds to the spatial position of a transducer (output),
+ * or interpretation of channel (input).
+ * The user of a channel mask needs to know the context of whether it is for output or input.
+ * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
+ * It is not permitted for no bits to be set.
+ *
+ * AUDIO_CHANNEL_REPRESENTATION_INDEX
+ * is a channel mask representation for index assignment.
+ * Each low-order bit corresponds to a selected channel.
+ * There is no platform interpretation of the various bits.
+ * There is no concept of output or input.
+ * It is not permitted for no bits to be set.
+ *
+ * All other representations are reserved for future use.
+ *
+ * Warning: current representation distinguishes between input and output, but this will not the be
+ * case in future revisions of the platform. Wherever there is an ambiguity between input and output
+ * that is currently resolved by checking the channel mask, the implementer should look for ways to
+ * fix it with additional information outside of the mask.
+ */
typedef uint32_t audio_channel_mask_t;
+/* Maximum number of channels for all representations */
+#define AUDIO_CHANNEL_COUNT_MAX 30
+
+/* log(2) of maximum number of representations, not part of public API */
+#define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
+
+/* Representations */
+typedef enum {
+ AUDIO_CHANNEL_REPRESENTATION_POSITION = 0, // must be zero for compatibility
+ // 1 is reserved for future use
+ AUDIO_CHANNEL_REPRESENTATION_INDEX = 2,
+ // 3 is reserved for future use
+} audio_channel_representation_t;
+
+/* The return value is undefined if the channel mask is invalid. */
+static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
+{
+ return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
+}
+
+/* The return value is undefined if the channel mask is invalid. */
+static inline audio_channel_representation_t audio_channel_mask_get_representation(
+ audio_channel_mask_t channel)
+{
+ // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
+ return (audio_channel_representation_t)
+ ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
+}
+
+/* Returns true if the channel mask is valid,
+ * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
+ * This function is unable to determine whether a channel mask for position assignment
+ * is invalid because an output mask has an invalid output bit set,
+ * or because an input mask has an invalid input bit set.
+ * All other APIs that take a channel mask assume that it is valid.
+ */
+static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
+{
+ uint32_t bits = audio_channel_mask_get_bits(channel);
+ audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
+ switch (representation) {
+ case AUDIO_CHANNEL_REPRESENTATION_POSITION:
+ case AUDIO_CHANNEL_REPRESENTATION_INDEX:
+ break;
+ default:
+ bits = 0;
+ break;
+ }
+ return bits != 0;
+}
+
+/* Not part of public API */
+static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
+ audio_channel_representation_t representation, uint32_t bits)
+{
+ return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
+}
+
/* Expresses the convention when stereo audio samples are stored interleaved
* in an array. This should improve readability by allowing code to use
* symbolic indices instead of hard-coded [0] and [1].
+ *
+ * For multi-channel beyond stereo, the platform convention is that channels
+ * are interleaved in order from least significant channel mask bit
+ * to most significant channel mask bit, with unused bits skipped.
+ * Any exceptions to this convention will be noted at the appropriate API.
*/
enum {
AUDIO_INTERLEAVE_LEFT = 0,
@@ -341,6 +571,7 @@
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100,
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200,
AUDIO_DEVICE_OUT_AUX_DIGITAL = 0x400,
+ AUDIO_DEVICE_OUT_HDMI = AUDIO_DEVICE_OUT_AUX_DIGITAL,
/* uses an analog connection (multiplexed over the USB connector pins for instance) */
AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800,
AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000,
@@ -349,6 +580,20 @@
/* USB host mode: your Android device is a USB host and the dock is a USB device */
AUDIO_DEVICE_OUT_USB_DEVICE = 0x4000,
AUDIO_DEVICE_OUT_REMOTE_SUBMIX = 0x8000,
+ /* Telephony voice TX path */
+ AUDIO_DEVICE_OUT_TELEPHONY_TX = 0x10000,
+ /* Analog jack with line impedance detected */
+ AUDIO_DEVICE_OUT_LINE = 0x20000,
+ /* HDMI Audio Return Channel */
+ AUDIO_DEVICE_OUT_HDMI_ARC = 0x40000,
+ /* S/PDIF out */
+ AUDIO_DEVICE_OUT_SPDIF = 0x80000,
+ /* FM transmitter out */
+ AUDIO_DEVICE_OUT_FM = 0x100000,
+ /* Line out for av devices */
+ AUDIO_DEVICE_OUT_AUX_LINE = 0x200000,
+ /* limited-output speaker device for acoustic safety */
+ AUDIO_DEVICE_OUT_SPEAKER_SAFE = 0x400000,
AUDIO_DEVICE_OUT_DEFAULT = AUDIO_DEVICE_BIT_DEFAULT,
AUDIO_DEVICE_OUT_ALL = (AUDIO_DEVICE_OUT_EARPIECE |
AUDIO_DEVICE_OUT_SPEAKER |
@@ -360,12 +605,19 @@
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
- AUDIO_DEVICE_OUT_AUX_DIGITAL |
+ AUDIO_DEVICE_OUT_HDMI |
AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
AUDIO_DEVICE_OUT_USB_ACCESSORY |
AUDIO_DEVICE_OUT_USB_DEVICE |
AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
+ AUDIO_DEVICE_OUT_TELEPHONY_TX |
+ AUDIO_DEVICE_OUT_LINE |
+ AUDIO_DEVICE_OUT_HDMI_ARC |
+ AUDIO_DEVICE_OUT_SPDIF |
+ AUDIO_DEVICE_OUT_FM |
+ AUDIO_DEVICE_OUT_AUX_LINE |
+ AUDIO_DEVICE_OUT_SPEAKER_SAFE |
AUDIO_DEVICE_OUT_DEFAULT),
AUDIO_DEVICE_OUT_ALL_A2DP = (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
@@ -383,13 +635,26 @@
AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET = AUDIO_DEVICE_BIT_IN | 0x8,
AUDIO_DEVICE_IN_WIRED_HEADSET = AUDIO_DEVICE_BIT_IN | 0x10,
AUDIO_DEVICE_IN_AUX_DIGITAL = AUDIO_DEVICE_BIT_IN | 0x20,
+ AUDIO_DEVICE_IN_HDMI = AUDIO_DEVICE_IN_AUX_DIGITAL,
+ /* Telephony voice RX path */
AUDIO_DEVICE_IN_VOICE_CALL = AUDIO_DEVICE_BIT_IN | 0x40,
+ AUDIO_DEVICE_IN_TELEPHONY_RX = AUDIO_DEVICE_IN_VOICE_CALL,
AUDIO_DEVICE_IN_BACK_MIC = AUDIO_DEVICE_BIT_IN | 0x80,
AUDIO_DEVICE_IN_REMOTE_SUBMIX = AUDIO_DEVICE_BIT_IN | 0x100,
AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x200,
AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x400,
AUDIO_DEVICE_IN_USB_ACCESSORY = AUDIO_DEVICE_BIT_IN | 0x800,
AUDIO_DEVICE_IN_USB_DEVICE = AUDIO_DEVICE_BIT_IN | 0x1000,
+ /* FM tuner input */
+ AUDIO_DEVICE_IN_FM_TUNER = AUDIO_DEVICE_BIT_IN | 0x2000,
+ /* TV tuner input */
+ AUDIO_DEVICE_IN_TV_TUNER = AUDIO_DEVICE_BIT_IN | 0x4000,
+ /* Analog jack with line impedance detected */
+ AUDIO_DEVICE_IN_LINE = AUDIO_DEVICE_BIT_IN | 0x8000,
+ /* S/PDIF in */
+ AUDIO_DEVICE_IN_SPDIF = AUDIO_DEVICE_BIT_IN | 0x10000,
+ AUDIO_DEVICE_IN_BLUETOOTH_A2DP = AUDIO_DEVICE_BIT_IN | 0x20000,
+ AUDIO_DEVICE_IN_LOOPBACK = AUDIO_DEVICE_BIT_IN | 0x40000,
AUDIO_DEVICE_IN_DEFAULT = AUDIO_DEVICE_BIT_IN | AUDIO_DEVICE_BIT_DEFAULT,
AUDIO_DEVICE_IN_ALL = (AUDIO_DEVICE_IN_COMMUNICATION |
@@ -397,16 +662,24 @@
AUDIO_DEVICE_IN_BUILTIN_MIC |
AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET |
AUDIO_DEVICE_IN_WIRED_HEADSET |
- AUDIO_DEVICE_IN_AUX_DIGITAL |
- AUDIO_DEVICE_IN_VOICE_CALL |
+ AUDIO_DEVICE_IN_HDMI |
+ AUDIO_DEVICE_IN_TELEPHONY_RX |
AUDIO_DEVICE_IN_BACK_MIC |
AUDIO_DEVICE_IN_REMOTE_SUBMIX |
AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET |
AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET |
AUDIO_DEVICE_IN_USB_ACCESSORY |
AUDIO_DEVICE_IN_USB_DEVICE |
+ AUDIO_DEVICE_IN_FM_TUNER |
+ AUDIO_DEVICE_IN_TV_TUNER |
+ AUDIO_DEVICE_IN_LINE |
+ AUDIO_DEVICE_IN_SPDIF |
+ AUDIO_DEVICE_IN_BLUETOOTH_A2DP |
+ AUDIO_DEVICE_IN_LOOPBACK |
AUDIO_DEVICE_IN_DEFAULT),
AUDIO_DEVICE_IN_ALL_SCO = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
+ AUDIO_DEVICE_IN_ALL_USB = (AUDIO_DEVICE_IN_USB_ACCESSORY |
+ AUDIO_DEVICE_IN_USB_DEVICE),
};
typedef uint32_t audio_devices_t;
@@ -434,7 +707,8 @@
AUDIO_OUTPUT_FLAG_DEEP_BUFFER = 0x8, // use deep audio buffers
AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD = 0x10, // offload playback of compressed
// streams to hardware codec
- AUDIO_OUTPUT_FLAG_NON_BLOCKING = 0x20 // use non-blocking write
+ AUDIO_OUTPUT_FLAG_NON_BLOCKING = 0x20, // use non-blocking write
+ AUDIO_OUTPUT_FLAG_HW_AV_SYNC = 0x40 // output uses a hardware A/V synchronization source
} audio_output_flags_t;
/* The audio input flags are analogous to audio output flags.
@@ -443,8 +717,9 @@
* attributes corresponding to the specified flags.
*/
typedef enum {
- AUDIO_INPUT_FLAG_NONE = 0x0, // no attributes
- AUDIO_INPUT_FLAG_FAST = 0x1, // prefer an input that supports "fast tracks"
+ AUDIO_INPUT_FLAG_NONE = 0x0, // no attributes
+ AUDIO_INPUT_FLAG_FAST = 0x1, // prefer an input that supports "fast tracks"
+ AUDIO_INPUT_FLAG_HW_HOTWORD = 0x2, // prefer an input that captures from hw hotword source
} audio_input_flags_t;
/* Additional information about compressed streams offloaded to
@@ -474,8 +749,276 @@
static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
size: sizeof(audio_offload_info_t),
+ sample_rate: 0,
+ channel_mask: 0,
+ format: AUDIO_FORMAT_DEFAULT,
+ stream_type: AUDIO_STREAM_VOICE_CALL,
+ bit_rate: 0,
+ duration_us: 0,
+ has_video: false,
+ is_streaming: false
};
+/* common audio stream configuration parameters
+ * You should memset() the entire structure to zero before use to
+ * ensure forward compatibility
+ */
+struct audio_config {
+ uint32_t sample_rate;
+ audio_channel_mask_t channel_mask;
+ audio_format_t format;
+ audio_offload_info_t offload_info;
+ size_t frame_count;
+};
+typedef struct audio_config audio_config_t;
+
+static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
+ sample_rate: 0,
+ channel_mask: AUDIO_CHANNEL_NONE,
+ format: AUDIO_FORMAT_DEFAULT,
+ offload_info: {
+ version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
+ size: sizeof(audio_offload_info_t),
+ sample_rate: 0,
+ channel_mask: 0,
+ format: AUDIO_FORMAT_DEFAULT,
+ stream_type: AUDIO_STREAM_VOICE_CALL,
+ bit_rate: 0,
+ duration_us: 0,
+ has_video: false,
+ is_streaming: false
+ },
+ frame_count: 0,
+};
+
+
+/* audio hw module handle functions or structures referencing a module */
+typedef int audio_module_handle_t;
+
+/******************************
+ * Volume control
+ *****************************/
+
+/* If the audio hardware supports gain control on some audio paths,
+ * the platform can expose them in the audio_policy.conf file. The audio HAL
+ * will then implement gain control functions that will use the following data
+ * structures. */
+
+/* Type of gain control exposed by an audio port */
+#define AUDIO_GAIN_MODE_JOINT 0x1 /* supports joint channel gain control */
+#define AUDIO_GAIN_MODE_CHANNELS 0x2 /* supports separate channel gain control */
+#define AUDIO_GAIN_MODE_RAMP 0x4 /* supports gain ramps */
+
+typedef uint32_t audio_gain_mode_t;
+
+
+/* An audio_gain struct is a representation of a gain stage.
+ * A gain stage is always attached to an audio port. */
+struct audio_gain {
+ audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */
+ audio_channel_mask_t channel_mask; /* channels which gain an be controlled.
+ N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
+ int min_value; /* minimum gain value in millibels */
+ int max_value; /* maximum gain value in millibels */
+ int default_value; /* default gain value in millibels */
+ unsigned int step_value; /* gain step in millibels */
+ unsigned int min_ramp_ms; /* minimum ramp duration in ms */
+ unsigned int max_ramp_ms; /* maximum ramp duration in ms */
+};
+
+/* The gain configuration structure is used to get or set the gain values of a
+ * given port */
+struct audio_gain_config {
+ int index; /* index of the corresponding audio_gain in the
+ audio_port gains[] table */
+ audio_gain_mode_t mode; /* mode requested for this command */
+ audio_channel_mask_t channel_mask; /* channels which gain value follows.
+ N/A in joint mode */
+ int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
+ for each channel ordered from LSb to MSb in
+ channel mask. The number of values is 1 in joint
+ mode or popcount(channel_mask) */
+ unsigned int ramp_duration_ms; /* ramp duration in ms */
+};
+
+/******************************
+ * Routing control
+ *****************************/
+
+/* Types defined here are used to describe an audio source or sink at internal
+ * framework interfaces (audio policy, patch panel) or at the audio HAL.
+ * Sink and sources are grouped in a concept of “audio port” representing an
+ * audio end point at the edge of the system managed by the module exposing
+ * the interface. */
+
+/* Audio port role: either source or sink */
+typedef enum {
+ AUDIO_PORT_ROLE_NONE,
+ AUDIO_PORT_ROLE_SOURCE,
+ AUDIO_PORT_ROLE_SINK,
+} audio_port_role_t;
+
+/* Audio port type indicates if it is a session (e.g AudioTrack),
+ * a mix (e.g PlaybackThread output) or a physical device
+ * (e.g AUDIO_DEVICE_OUT_SPEAKER) */
+typedef enum {
+ AUDIO_PORT_TYPE_NONE,
+ AUDIO_PORT_TYPE_DEVICE,
+ AUDIO_PORT_TYPE_MIX,
+ AUDIO_PORT_TYPE_SESSION,
+} audio_port_type_t;
+
+/* Each port has a unique ID or handle allocated by policy manager */
+typedef int audio_port_handle_t;
+#define AUDIO_PORT_HANDLE_NONE 0
+
+/* the maximum length for the human-readable device name. i.e. "Alesis iO4"*/
+#define AUDIO_PORT_MAX_NAME_LEN 128
+
+/* maximum audio device address length */
+#define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
+
+/* extension for audio port configuration structure when the audio port is a
+ * hardware device */
+struct audio_port_config_device_ext {
+ audio_module_handle_t hw_module; /* module the device is attached to */
+ audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
+ char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
+};
+
+/* extension for audio port configuration structure when the audio port is a
+ * sub mix */
+struct audio_port_config_mix_ext {
+ audio_module_handle_t hw_module; /* module the stream is attached to */
+ audio_io_handle_t handle; /* I/O handle of the input/output stream */
+ union {
+ //TODO: change use case for output streams: use strategy and mixer attributes
+ audio_stream_type_t stream;
+ audio_source_t source;
+ } usecase;
+};
+
+/* extension for audio port configuration structure when the audio port is an
+ * audio session */
+struct audio_port_config_session_ext {
+ audio_session_t session; /* audio session */
+};
+
+/* Flags indicating which fields are to be considered in struct audio_port_config */
+#define AUDIO_PORT_CONFIG_SAMPLE_RATE 0x1
+#define AUDIO_PORT_CONFIG_CHANNEL_MASK 0x2
+#define AUDIO_PORT_CONFIG_FORMAT 0x4
+#define AUDIO_PORT_CONFIG_GAIN 0x8
+#define AUDIO_PORT_CONFIG_ALL (AUDIO_PORT_CONFIG_SAMPLE_RATE | \
+ AUDIO_PORT_CONFIG_CHANNEL_MASK | \
+ AUDIO_PORT_CONFIG_FORMAT | \
+ AUDIO_PORT_CONFIG_GAIN)
+
+/* audio port configuration structure used to specify a particular configuration of
+ * an audio port */
+struct audio_port_config {
+ audio_port_handle_t id; /* port unique ID */
+ audio_port_role_t role; /* sink or source */
+ audio_port_type_t type; /* device, mix ... */
+ unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */
+ unsigned int sample_rate; /* sampling rate in Hz */
+ audio_channel_mask_t channel_mask; /* channel mask if applicable */
+ audio_format_t format; /* format if applicable */
+ struct audio_gain_config gain; /* gain to apply if applicable */
+ union {
+ struct audio_port_config_device_ext device; /* device specific info */
+ struct audio_port_config_mix_ext mix; /* mix specific info */
+ struct audio_port_config_session_ext session; /* session specific info */
+ } ext;
+};
+
+
+/* max number of sampling rates in audio port */
+#define AUDIO_PORT_MAX_SAMPLING_RATES 16
+/* max number of channel masks in audio port */
+#define AUDIO_PORT_MAX_CHANNEL_MASKS 16
+/* max number of audio formats in audio port */
+#define AUDIO_PORT_MAX_FORMATS 16
+/* max number of gain controls in audio port */
+#define AUDIO_PORT_MAX_GAINS 16
+
+/* extension for audio port structure when the audio port is a hardware device */
+struct audio_port_device_ext {
+ audio_module_handle_t hw_module; /* module the device is attached to */
+ audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
+ char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
+};
+
+/* Latency class of the audio mix */
+typedef enum {
+ AUDIO_LATENCY_LOW,
+ AUDIO_LATENCY_NORMAL,
+} audio_mix_latency_class_t;
+
+/* extension for audio port structure when the audio port is a sub mix */
+struct audio_port_mix_ext {
+ audio_module_handle_t hw_module; /* module the stream is attached to */
+ audio_io_handle_t handle; /* I/O handle of the input.output stream */
+ audio_mix_latency_class_t latency_class; /* latency class */
+ // other attributes: routing strategies
+};
+
+/* extension for audio port structure when the audio port is an audio session */
+struct audio_port_session_ext {
+ audio_session_t session; /* audio session */
+};
+
+struct audio_port {
+ audio_port_handle_t id; /* port unique ID */
+ audio_port_role_t role; /* sink or source */
+ audio_port_type_t type; /* device, mix ... */
+ char name[AUDIO_PORT_MAX_NAME_LEN];
+ unsigned int num_sample_rates; /* number of sampling rates in following array */
+ unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
+ unsigned int num_channel_masks; /* number of channel masks in following array */
+ audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
+ unsigned int num_formats; /* number of formats in following array */
+ audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
+ unsigned int num_gains; /* number of gains in following array */
+ struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
+ struct audio_port_config active_config; /* current audio port configuration */
+ union {
+ struct audio_port_device_ext device;
+ struct audio_port_mix_ext mix;
+ struct audio_port_session_ext session;
+ } ext;
+};
+
+/* An audio patch represents a connection between one or more source ports and
+ * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
+ * applications via framework APIs.
+ * Each patch is identified by a handle at the interface used to create that patch. For instance,
+ * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
+ * This handle is unique to a given audio HAL hardware module.
+ * But the same patch receives another system wide unique handle allocated by the framework.
+ * This unique handle is used for all transactions inside the framework.
+ */
+typedef int audio_patch_handle_t;
+#define AUDIO_PATCH_HANDLE_NONE 0
+
+#define AUDIO_PATCH_PORTS_MAX 16
+
+struct audio_patch {
+ audio_patch_handle_t id; /* patch unique ID */
+ unsigned int num_sources; /* number of sources in following array */
+ struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
+ unsigned int num_sinks; /* number of sinks in following array */
+ struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
+};
+
+
+
+/* a HW synchronization source returned by the audio HAL */
+typedef uint32_t audio_hw_sync_t;
+
+/* an invalid HW synchronization source indicating an error */
+#define AUDIO_HW_SYNC_INVALID 0
+
static inline bool audio_is_output_device(audio_devices_t device)
{
if (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
@@ -500,8 +1043,17 @@
return (device & AUDIO_DEVICE_BIT_IN) == 0;
}
+static inline bool audio_is_a2dp_in_device(audio_devices_t device)
+{
+ if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
+ device &= ~AUDIO_DEVICE_BIT_IN;
+ if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP))
+ return true;
+ }
+ return false;
+}
-static inline bool audio_is_a2dp_device(audio_devices_t device)
+static inline bool audio_is_a2dp_out_device(audio_devices_t device)
{
if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP))
return true;
@@ -509,22 +1061,45 @@
return false;
}
-static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
+// Deprecated - use audio_is_a2dp_out_device() instead
+static inline bool audio_is_a2dp_device(audio_devices_t device)
{
- device &= ~AUDIO_DEVICE_BIT_IN;
- if ((popcount(device) == 1) && (device & (AUDIO_DEVICE_OUT_ALL_SCO |
- AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)))
- return true;
- else
- return false;
+ return audio_is_a2dp_out_device(device);
}
+static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
+{
+ if ((device & AUDIO_DEVICE_BIT_IN) == 0) {
+ if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0))
+ return true;
+ } else {
+ device &= ~AUDIO_DEVICE_BIT_IN;
+ if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0))
+ return true;
+ }
+
+ return false;
+}
+
+static inline bool audio_is_usb_out_device(audio_devices_t device)
+{
+ return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB));
+}
+
+static inline bool audio_is_usb_in_device(audio_devices_t device)
+{
+ if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
+ device &= ~AUDIO_DEVICE_BIT_IN;
+ if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0)
+ return true;
+ }
+ return false;
+}
+
+/* OBSOLETE - use audio_is_usb_out_device() instead. */
static inline bool audio_is_usb_device(audio_devices_t device)
{
- if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB))
- return true;
- else
- return false;
+ return audio_is_usb_out_device(device);
}
static inline bool audio_is_remote_submix_device(audio_devices_t device)
@@ -536,75 +1111,200 @@
return false;
}
+/* Returns true if:
+ * representation is valid, and
+ * there is at least one channel bit set which _could_ correspond to an input channel, and
+ * there are no channel bits set which could _not_ correspond to an input channel.
+ * Otherwise returns false.
+ */
static inline bool audio_is_input_channel(audio_channel_mask_t channel)
{
- if ((channel & ~AUDIO_CHANNEL_IN_ALL) == 0)
- return channel != 0;
- else
+ uint32_t bits = audio_channel_mask_get_bits(channel);
+ switch (audio_channel_mask_get_representation(channel)) {
+ case AUDIO_CHANNEL_REPRESENTATION_POSITION:
+ if (bits & ~AUDIO_CHANNEL_IN_ALL) {
+ bits = 0;
+ }
+ // fall through
+ case AUDIO_CHANNEL_REPRESENTATION_INDEX:
+ return bits != 0;
+ default:
return false;
+ }
}
+/* Returns true if:
+ * representation is valid, and
+ * there is at least one channel bit set which _could_ correspond to an output channel, and
+ * there are no channel bits set which could _not_ correspond to an output channel.
+ * Otherwise returns false.
+ */
static inline bool audio_is_output_channel(audio_channel_mask_t channel)
{
- if ((channel & ~AUDIO_CHANNEL_OUT_ALL) == 0)
- return channel != 0;
- else
+ uint32_t bits = audio_channel_mask_get_bits(channel);
+ switch (audio_channel_mask_get_representation(channel)) {
+ case AUDIO_CHANNEL_REPRESENTATION_POSITION:
+ if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
+ bits = 0;
+ }
+ // fall through
+ case AUDIO_CHANNEL_REPRESENTATION_INDEX:
+ return bits != 0;
+ default:
return false;
+ }
}
-/* Derive an output channel mask from a channel count.
+/* Returns the number of channels from an input channel mask,
+ * used in the context of audio input or recording.
+ * If a channel bit is set which could _not_ correspond to an input channel,
+ * it is excluded from the count.
+ * Returns zero if the representation is invalid.
+ */
+static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
+{
+ uint32_t bits = audio_channel_mask_get_bits(channel);
+ switch (audio_channel_mask_get_representation(channel)) {
+ case AUDIO_CHANNEL_REPRESENTATION_POSITION:
+ // TODO: We can now merge with from_out_mask and remove anding
+ bits &= AUDIO_CHANNEL_IN_ALL;
+ // fall through
+ case AUDIO_CHANNEL_REPRESENTATION_INDEX:
+ return popcount(bits);
+ default:
+ return 0;
+ }
+}
+
+/* Returns the number of channels from an output channel mask,
+ * used in the context of audio output or playback.
+ * If a channel bit is set which could _not_ correspond to an output channel,
+ * it is excluded from the count.
+ * Returns zero if the representation is invalid.
+ */
+static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
+{
+ uint32_t bits = audio_channel_mask_get_bits(channel);
+ switch (audio_channel_mask_get_representation(channel)) {
+ case AUDIO_CHANNEL_REPRESENTATION_POSITION:
+ // TODO: We can now merge with from_in_mask and remove anding
+ bits &= AUDIO_CHANNEL_OUT_ALL;
+ // fall through
+ case AUDIO_CHANNEL_REPRESENTATION_INDEX:
+ return popcount(bits);
+ default:
+ return 0;
+ }
+}
+
+/* Derive an output channel mask for position assignment from a channel count.
* This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
* cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
* and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
* for continuity with stereo.
- * Returns the matching channel mask, or 0 if the number of channels exceeds that of the
- * configurations for which a default channel mask is defined.
+ * Returns the matching channel mask,
+ * or AUDIO_CHANNEL_NONE if the channel count is zero,
+ * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
+ * configurations for which a default output channel mask is defined.
*/
static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
{
+ uint32_t bits;
switch (channel_count) {
+ case 0:
+ return AUDIO_CHANNEL_NONE;
case 1:
- return AUDIO_CHANNEL_OUT_MONO;
+ bits = AUDIO_CHANNEL_OUT_MONO;
+ break;
case 2:
- return AUDIO_CHANNEL_OUT_STEREO;
+ bits = AUDIO_CHANNEL_OUT_STEREO;
+ break;
case 3:
- return (AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER);
+ bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER;
+ break;
case 4: // 4.0
- return AUDIO_CHANNEL_OUT_QUAD;
+ bits = AUDIO_CHANNEL_OUT_QUAD;
+ break;
case 5: // 5.0
- return (AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER);
+ bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
+ break;
case 6: // 5.1
- return AUDIO_CHANNEL_OUT_5POINT1;
+ bits = AUDIO_CHANNEL_OUT_5POINT1;
+ break;
case 7: // 6.1
- return (AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER);
+ bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
+ break;
case 8:
- return AUDIO_CHANNEL_OUT_7POINT1;
+ bits = AUDIO_CHANNEL_OUT_7POINT1;
+ break;
default:
- return 0;
+ return AUDIO_CHANNEL_INVALID;
}
+ return audio_channel_mask_from_representation_and_bits(
+ AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
}
-/* Similar to above, but for input. Currently handles only mono and stereo. */
+/* Derive an input channel mask for position assignment from a channel count.
+ * Currently handles only mono and stereo.
+ * Returns the matching channel mask,
+ * or AUDIO_CHANNEL_NONE if the channel count is zero,
+ * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
+ * configurations for which a default input channel mask is defined.
+ */
static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count)
{
+ uint32_t bits;
switch (channel_count) {
+ case 0:
+ return AUDIO_CHANNEL_NONE;
case 1:
- return AUDIO_CHANNEL_IN_MONO;
+ bits = AUDIO_CHANNEL_IN_MONO;
+ break;
case 2:
- return AUDIO_CHANNEL_IN_STEREO;
+ bits = AUDIO_CHANNEL_IN_STEREO;
+ break;
default:
- return 0;
+ return AUDIO_CHANNEL_INVALID;
}
+ return audio_channel_mask_from_representation_and_bits(
+ AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
+}
+
+/* Derive a channel mask for index assignment from a channel count.
+ * Returns the matching channel mask,
+ * or AUDIO_CHANNEL_NONE if the channel count is zero,
+ * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
+ */
+static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
+ uint32_t channel_count)
+{
+ if (channel_count == 0) {
+ return AUDIO_CHANNEL_NONE;
+ }
+ if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
+ return AUDIO_CHANNEL_INVALID;
+ }
+ uint32_t bits = (1 << channel_count) - 1;
+ return audio_channel_mask_from_representation_and_bits(
+ AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
}
static inline bool audio_is_valid_format(audio_format_t format)
{
switch (format & AUDIO_FORMAT_MAIN_MASK) {
case AUDIO_FORMAT_PCM:
- if (format != AUDIO_FORMAT_PCM_16_BIT &&
- format != AUDIO_FORMAT_PCM_8_BIT && format != AUDIO_FORMAT_PCM_FLOAT) {
+ switch (format) {
+ case AUDIO_FORMAT_PCM_16_BIT:
+ case AUDIO_FORMAT_PCM_8_BIT:
+ case AUDIO_FORMAT_PCM_32_BIT:
+ case AUDIO_FORMAT_PCM_8_24_BIT:
+ case AUDIO_FORMAT_PCM_FLOAT:
+ case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+ return true;
+ default:
return false;
}
+ /* not reached */
case AUDIO_FORMAT_MP3:
case AUDIO_FORMAT_AMR_NB:
case AUDIO_FORMAT_AMR_WB:
@@ -612,6 +1312,9 @@
case AUDIO_FORMAT_HE_AAC_V1:
case AUDIO_FORMAT_HE_AAC_V2:
case AUDIO_FORMAT_VORBIS:
+ case AUDIO_FORMAT_OPUS:
+ case AUDIO_FORMAT_AC3:
+ case AUDIO_FORMAT_E_AC3:
return true;
default:
return false;
@@ -632,6 +1335,9 @@
case AUDIO_FORMAT_PCM_8_24_BIT:
size = sizeof(int32_t);
break;
+ case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+ size = sizeof(uint8_t) * 3;
+ break;
case AUDIO_FORMAT_PCM_16_BIT:
size = sizeof(int16_t);
break;
@@ -647,6 +1353,23 @@
return size;
}
+/* converts device address to string sent to audio HAL via set_parameters */
+static char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
+{
+ const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address=");
+ char param[kSize];
+
+ if (device & AUDIO_DEVICE_OUT_ALL_A2DP)
+ snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
+ else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
+ snprintf(param, kSize, "%s=%s", "mix", address);
+ else
+ snprintf(param, kSize, "%s", address);
+
+ return strdup(param);
+}
+
+
__END_DECLS
#endif // ANDROID_AUDIO_CORE_H
diff --git a/include/system/audio_policy.h b/include/system/audio_policy.h
index a6554de..2881104 100644
--- a/include/system/audio_policy.h
+++ b/include/system/audio_policy.h
@@ -44,6 +44,7 @@
AUDIO_POLICY_FORCE_DIGITAL_DOCK,
AUDIO_POLICY_FORCE_NO_BT_A2DP, /* A2DP sink is not preferred to speaker or wired HS */
AUDIO_POLICY_FORCE_SYSTEM_ENFORCED,
+ AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED,
AUDIO_POLICY_FORCE_CFG_CNT,
AUDIO_POLICY_FORCE_CFG_MAX = AUDIO_POLICY_FORCE_CFG_CNT - 1,
@@ -58,6 +59,7 @@
AUDIO_POLICY_FORCE_FOR_RECORD,
AUDIO_POLICY_FORCE_FOR_DOCK,
AUDIO_POLICY_FORCE_FOR_SYSTEM,
+ AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO,
AUDIO_POLICY_FORCE_USE_CNT,
AUDIO_POLICY_FORCE_USE_MAX = AUDIO_POLICY_FORCE_USE_CNT - 1,
diff --git a/include/system/camera.h b/include/system/camera.h
index 7a4dd53..09c915d 100644
--- a/include/system/camera.h
+++ b/include/system/camera.h
@@ -194,7 +194,12 @@
/** The facing of the camera is opposite to that of the screen. */
CAMERA_FACING_BACK = 0,
/** The facing of the camera is the same as that of the screen. */
- CAMERA_FACING_FRONT = 1
+ CAMERA_FACING_FRONT = 1,
+ /**
+ * The facing of the camera is not fixed relative to the screen.
+ * The cameras with this facing are external cameras, e.g. USB cameras.
+ */
+ CAMERA_FACING_EXTERNAL = 2
};
enum {
diff --git a/include/system/graphics.h b/include/system/graphics.h
index 6cef3b2..c9f5950 100644
--- a/include/system/graphics.h
+++ b/include/system/graphics.h
@@ -45,9 +45,12 @@
/*
* "linear" color pixel formats:
*
- * The pixel formats below contain sRGB data but are otherwise treated
- * as linear formats, i.e.: no special operation is performed when
- * reading or writing into a buffer in one of these formats
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer.
+ *
+ * The color space determines, for example, if the formats are linear or
+ * gamma-corrected; or whether any special operations are performed when
+ * reading or writing into a buffer in one of these formats.
*/
HAL_PIXEL_FORMAT_RGBA_8888 = 1,
HAL_PIXEL_FORMAT_RGBX_8888 = 2,
@@ -56,28 +59,6 @@
HAL_PIXEL_FORMAT_BGRA_8888 = 5,
/*
- * sRGB color pixel formats:
- *
- * The red, green and blue components are stored in sRGB space, and converted
- * to linear space when read, using the standard sRGB to linear equation:
- *
- * Clinear = Csrgb / 12.92 for Csrgb <= 0.04045
- * = (Csrgb + 0.055 / 1.055)^2.4 for Csrgb > 0.04045
- *
- * When written the inverse transformation is performed:
- *
- * Csrgb = 12.92 * Clinear for Clinear <= 0.0031308
- * = 1.055 * Clinear^(1/2.4) - 0.055 for Clinear > 0.0031308
- *
- *
- * The alpha component, if present, is always stored in linear space and
- * is left unmodified when read or written.
- *
- */
- HAL_PIXEL_FORMAT_sRGB_A_8888 = 0xC,
- HAL_PIXEL_FORMAT_sRGB_X_8888 = 0xD,
-
- /*
* 0x100 - 0x1FF
*
* This range is reserved for pixel formats that are specific to the HAL
@@ -111,6 +92,8 @@
* cr_offset = y_size
* cb_offset = y_size + c_size
*
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer.
*/
HAL_PIXEL_FORMAT_YV12 = 0x32315659, // YCrCb 4:2:0 Planar
@@ -135,6 +118,8 @@
*
* size = stride * height
*
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer.
*/
HAL_PIXEL_FORMAT_Y8 = 0x20203859,
@@ -159,6 +144,10 @@
*
* size = stride * height * 2
*
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer, except that dataSpace field
+ * HAL_DATASPACE_DEPTH indicates that this buffer contains a depth
+ * image where each sample is a distance value measured by a depth camera.
*/
HAL_PIXEL_FORMAT_Y16 = 0x20363159,
@@ -167,7 +156,7 @@
*
* This format is exposed outside of the camera HAL to applications.
*
- * RAW_SENSOR is a single-channel, 16-bit, little endian format, typically
+ * RAW16 is a single-channel, 16-bit, little endian format, typically
* representing raw Bayer-pattern images from an image sensor, with minimal
* processing.
*
@@ -193,9 +182,66 @@
* - GRALLOC_USAGE_HW_CAMERA_*
* - GRALLOC_USAGE_SW_*
* - GRALLOC_USAGE_RENDERSCRIPT
+ *
+ * When used with ANativeWindow, the dataSpace should be
+ * HAL_DATASPACE_ARBITRARY, as raw image sensor buffers require substantial
+ * extra metadata to define.
*/
HAL_PIXEL_FORMAT_RAW16 = 0x20,
- HAL_PIXEL_FORMAT_RAW_SENSOR = 0x20, // TODO(rubenbrunk): Remove RAW_SENSOR.
+
+ /*
+ * Android RAW10 format:
+ *
+ * This format is exposed outside of the camera HAL to applications.
+ *
+ * RAW10 is a single-channel, 10-bit per pixel, densely packed in each row,
+ * unprocessed format, usually representing raw Bayer-pattern images coming from
+ * an image sensor.
+ *
+ * In an image buffer with this format, starting from the first pixel of each
+ * row, each 4 consecutive pixels are packed into 5 bytes (40 bits). Each one
+ * of the first 4 bytes contains the top 8 bits of each pixel, The fifth byte
+ * contains the 2 least significant bits of the 4 pixels, the exact layout data
+ * for each 4 consecutive pixels is illustrated below (Pi[j] stands for the jth
+ * bit of the ith pixel):
+ *
+ * bit 7 bit 0
+ * =====|=====|=====|=====|=====|=====|=====|=====|
+ * Byte 0: |P0[9]|P0[8]|P0[7]|P0[6]|P0[5]|P0[4]|P0[3]|P0[2]|
+ * |-----|-----|-----|-----|-----|-----|-----|-----|
+ * Byte 1: |P1[9]|P1[8]|P1[7]|P1[6]|P1[5]|P1[4]|P1[3]|P1[2]|
+ * |-----|-----|-----|-----|-----|-----|-----|-----|
+ * Byte 2: |P2[9]|P2[8]|P2[7]|P2[6]|P2[5]|P2[4]|P2[3]|P2[2]|
+ * |-----|-----|-----|-----|-----|-----|-----|-----|
+ * Byte 3: |P3[9]|P3[8]|P3[7]|P3[6]|P3[5]|P3[4]|P3[3]|P3[2]|
+ * |-----|-----|-----|-----|-----|-----|-----|-----|
+ * Byte 4: |P3[1]|P3[0]|P2[1]|P2[0]|P1[1]|P1[0]|P0[1]|P0[0]|
+ * ===============================================
+ *
+ * This format assumes
+ * - a width multiple of 4 pixels
+ * - an even height
+ * - a vertical stride equal to the height
+ * - strides are specified in bytes, not in pixels
+ *
+ * size = stride * height
+ *
+ * When stride is equal to width * (10 / 8), there will be no padding bytes at
+ * the end of each row, the entire image data is densely packed. When stride is
+ * larger than width * (10 / 8), padding bytes will be present at the end of each
+ * row (including the last row).
+ *
+ * This format must be accepted by the gralloc module when used with the
+ * following usage flags:
+ * - GRALLOC_USAGE_HW_CAMERA_*
+ * - GRALLOC_USAGE_SW_*
+ * - GRALLOC_USAGE_RENDERSCRIPT
+ *
+ * When used with ANativeWindow, the dataSpace field should be
+ * HAL_DATASPACE_ARBITRARY, as raw image sensor buffers require substantial
+ * extra metadata to define.
+ */
+ HAL_PIXEL_FORMAT_RAW10 = 0x25,
/*
* Android opaque RAW format:
@@ -211,6 +257,10 @@
* - GRALLOC_USAGE_HW_CAMERA_*
* - GRALLOC_USAGE_SW_*
* - GRALLOC_USAGE_RENDERSCRIPT
+ *
+ * When used with ANativeWindow, the dataSpace field should be
+ * HAL_DATASPACE_ARBITRARY, as raw image sensor buffers require substantial
+ * extra metadata to define.
*/
HAL_PIXEL_FORMAT_RAW_OPAQUE = 0x24,
@@ -226,6 +276,16 @@
*
* Buffers of this format must have a height of 1, and width equal to their
* size in bytes.
+ *
+ * When used with ANativeWindow, the mapping of the dataSpace field to
+ * buffer contents for BLOB is as follows:
+ *
+ * dataSpace value | Buffer contents
+ * -------------------------------+-----------------------------------------
+ * HAL_DATASPACE_JFIF | An encoded JPEG image
+ * HAL_DATASPACE_DEPTH | An android_depth_points buffer
+ * Other | Unsupported
+ *
*/
HAL_PIXEL_FORMAT_BLOB = 0x21,
@@ -242,6 +302,8 @@
* framework will assume that sampling the texture will always return an
* alpha value of 1.0 (i.e. the buffer contains only opaque pixel values).
*
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer.
*/
HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22,
@@ -261,6 +323,9 @@
*
* This format is locked for use by gralloc's (*lock_ycbcr) method, and
* locking with the (*lock) method will return an error.
+ *
+ * When used with ANativeWindow, the dataSpace field describes the color
+ * space of the buffer.
*/
HAL_PIXEL_FORMAT_YCbCr_420_888 = 0x23,
@@ -305,6 +370,42 @@
};
/**
+ * Structure used to define depth point clouds for format HAL_PIXEL_FORMAT_BLOB
+ * with dataSpace value of HAL_DATASPACE_DEPTH.
+ * When locking a native buffer of the above format and dataSpace value,
+ * the vaddr pointer can be cast to this structure.
+ *
+ * A variable-length list of (x,y,z) 3D points, as floats.
+ *
+ * @num_points is the number of points in the list
+ *
+ * @xyz_points is the flexible array of floating-point values.
+ * It contains (num_points) * 3 floats.
+ *
+ * For example:
+ * android_depth_points d = get_depth_buffer();
+ * struct {
+ * float x; float y; float z;
+ * } firstPoint, lastPoint;
+ *
+ * firstPoint.x = d.xyz_points[0];
+ * firstPoint.y = d.xyz_points[1];
+ * firstPoint.z = d.xyz_points[2];
+ * lastPoint.x = d.xyz_points[(d.num_points - 1) * 3 + 0];
+ * lastPoint.y = d.xyz_points[(d.num_points - 1) * 3 + 1];
+ * lastPoint.z = d.xyz_points[(d.num_points - 1) * 3 + 2];
+ */
+
+struct android_depth_points {
+ uint32_t num_points;
+
+ /** reserved for future use, set to 0 by gralloc's (*lock)() */
+ uint32_t reserved[8];
+
+ float xyz_points[];
+};
+
+/**
* Transformation definitions
*
* IMPORTANT NOTE:
@@ -327,6 +428,212 @@
HAL_TRANSFORM_RESERVED = 0x08,
};
+/**
+ * Dataspace Definitions
+ * ======================
+ *
+ * Dataspace is the definition of how pixel values should be interpreted.
+ *
+ * For many formats, this is the colorspace of the image data, which includes
+ * primaries (including white point) and the transfer characteristic function,
+ * which describes both gamma curve and numeric range (within the bit depth).
+ *
+ * Other dataspaces include depth measurement data from a depth camera.
+ */
+
+typedef enum android_dataspace {
+ /*
+ * Default-assumption data space, when not explicitly specified.
+ *
+ * It is safest to assume the buffer is an image with sRGB primaries and
+ * encoding ranges, but the consumer and/or the producer of the data may
+ * simply be using defaults. No automatic gamma transform should be
+ * expected, except for a possible display gamma transform when drawn to a
+ * screen.
+ */
+ HAL_DATASPACE_UNKNOWN = 0x0,
+
+ /*
+ * Arbitrary dataspace with manually defined characteristics. Definition
+ * for colorspaces or other meaning must be communicated separately.
+ *
+ * This is used when specifying primaries, transfer characteristics,
+ * etc. separately.
+ *
+ * A typical use case is in video encoding parameters (e.g. for H.264),
+ * where a colorspace can have separately defined primaries, transfer
+ * characteristics, etc.
+ */
+ HAL_DATASPACE_ARBITRARY = 0x1,
+
+ /*
+ * RGB Colorspaces
+ * -----------------
+ *
+ * Primaries are given using (x,y) coordinates in the CIE 1931 definition
+ * of x and y specified by ISO 11664-1.
+ *
+ * Transfer characteristics are the opto-electronic transfer characteristic
+ * at the source as a function of linear optical intensity (luminance).
+ */
+
+ /*
+ * sRGB linear encoding:
+ *
+ * The red, green, and blue components are stored in sRGB space, but
+ * are linear, not gamma-encoded.
+ * The RGB primaries and the white point are the same as BT.709.
+ *
+ * The values are encoded using the full range ([0,255] for 8-bit) for all
+ * components.
+ */
+ HAL_DATASPACE_SRGB_LINEAR = 0x200,
+
+ /*
+ * sRGB gamma encoding:
+ *
+ * The red, green and blue components are stored in sRGB space, and
+ * converted to linear space when read, using the standard sRGB to linear
+ * equation:
+ *
+ * Clinear = Csrgb / 12.92 for Csrgb <= 0.04045
+ * = (Csrgb + 0.055 / 1.055)^2.4 for Csrgb > 0.04045
+ *
+ * When written the inverse transformation is performed:
+ *
+ * Csrgb = 12.92 * Clinear for Clinear <= 0.0031308
+ * = 1.055 * Clinear^(1/2.4) - 0.055 for Clinear > 0.0031308
+ *
+ *
+ * The alpha component, if present, is always stored in linear space and
+ * is left unmodified when read or written.
+ *
+ * The RGB primaries and the white point are the same as BT.709.
+ *
+ * The values are encoded using the full range ([0,255] for 8-bit) for all
+ * components.
+ *
+ */
+ HAL_DATASPACE_SRGB = 0x201,
+
+ /*
+ * YCbCr Colorspaces
+ * -----------------
+ *
+ * Primaries are given using (x,y) coordinates in the CIE 1931 definition
+ * of x and y specified by ISO 11664-1.
+ *
+ * Transfer characteristics are the opto-electronic transfer characteristic
+ * at the source as a function of linear optical intensity (luminance).
+ */
+
+ /*
+ * JPEG File Interchange Format (JFIF)
+ *
+ * Same model as BT.601-625, but all values (Y, Cb, Cr) range from 0 to 255
+ *
+ * Transfer characteristic curve:
+ * E = 1.099 * L ^ 0.45 - 0.099, 1.00 >= L >= 0.018
+ * E = 4.500 L, 0.018 > L >= 0
+ * L - luminance of image 0 <= L <= 1 for conventional colorimetry
+ * E - corresponding electrical signal
+ *
+ * Primaries: x y
+ * green 0.290 0.600
+ * blue 0.150 0.060
+ * red 0.640 0.330
+ * white (D65) 0.3127 0.3290
+ */
+ HAL_DATASPACE_JFIF = 0x101,
+
+ /*
+ * ITU-R Recommendation 601 (BT.601) - 625-line
+ *
+ * Standard-definition television, 625 Lines (PAL)
+ *
+ * For 8-bit-depth formats:
+ * Luma (Y) samples should range from 16 to 235, inclusive
+ * Chroma (Cb, Cr) samples should range from 16 to 240, inclusive
+ *
+ * For 10-bit-depth formats:
+ * Luma (Y) samples should range from 64 to 940, inclusive
+ * Chroma (Cb, Cr) samples should range from 64 to 960, inclusive
+ *
+ * Transfer characteristic curve:
+ * E = 1.099 * L ^ 0.45 - 0.099, 1.00 >= L >= 0.018
+ * E = 4.500 L, 0.018 > L >= 0
+ * L - luminance of image 0 <= L <= 1 for conventional colorimetry
+ * E - corresponding electrical signal
+ *
+ * Primaries: x y
+ * green 0.290 0.600
+ * blue 0.150 0.060
+ * red 0.640 0.330
+ * white (D65) 0.3127 0.3290
+ */
+ HAL_DATASPACE_BT601_625 = 0x102,
+
+ /*
+ * ITU-R Recommendation 601 (BT.601) - 525-line
+ *
+ * Standard-definition television, 525 Lines (NTSC)
+ *
+ * For 8-bit-depth formats:
+ * Luma (Y) samples should range from 16 to 235, inclusive
+ * Chroma (Cb, Cr) samples should range from 16 to 240, inclusive
+ *
+ * For 10-bit-depth formats:
+ * Luma (Y) samples should range from 64 to 940, inclusive
+ * Chroma (Cb, Cr) samples should range from 64 to 960, inclusive
+ *
+ * Transfer characteristic curve:
+ * E = 1.099 * L ^ 0.45 - 0.099, 1.00 >= L >= 0.018
+ * E = 4.500 L, 0.018 > L >= 0
+ * L - luminance of image 0 <= L <= 1 for conventional colorimetry
+ * E - corresponding electrical signal
+ *
+ * Primaries: x y
+ * green 0.310 0.595
+ * blue 0.155 0.070
+ * red 0.630 0.340
+ * white (D65) 0.3127 0.3290
+ */
+ HAL_DATASPACE_BT601_525 = 0x103,
+
+ /*
+ * ITU-R Recommendation 709 (BT.709)
+ *
+ * High-definition television
+ *
+ * For 8-bit-depth formats:
+ * Luma (Y) samples should range from 16 to 235, inclusive
+ * Chroma (Cb, Cr) samples should range from 16 to 240, inclusive
+ *
+ * For 10-bit-depth formats:
+ * Luma (Y) samples should range from 64 to 940, inclusive
+ * Chroma (Cb, Cr) samples should range from 64 to 960, inclusive
+ *
+ * Primaries: x y
+ * green 0.300 0.600
+ * blue 0.150 0.060
+ * red 0.640 0.330
+ * white (D65) 0.3127 0.3290
+ */
+ HAL_DATASPACE_BT709 = 0x104,
+
+ /*
+ * The buffer contains depth ranging measurements from a depth camera.
+ * This value is valid with formats:
+ * HAL_PIXEL_FORMAT_Y16: 16-bit single channel depth image.
+ * HAL_PIXEL_FORMAT_BLOB: A depth point cloud, as
+ * a variable-length float (x,y,z) coordinate point list.
+ * The point cloud will be represented with the android_depth_points
+ * structure.
+ */
+ HAL_DATASPACE_DEPTH = 0x1000
+
+} android_dataspace_t;
+
#ifdef __cplusplus
}
#endif
diff --git a/include/system/radio.h b/include/system/radio.h
new file mode 100644
index 0000000..a088526
--- /dev/null
+++ b/include/system/radio.h
@@ -0,0 +1,247 @@
+/*
+ * Copyright (C) 2015 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 ANDROID_RADIO_H
+#define ANDROID_RADIO_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+
+#define RADIO_NUM_BANDS_MAX 16
+#define RADIO_NUM_SPACINGS_MAX 16
+#define RADIO_STRING_LEN_MAX 128
+
+/*
+ * Radio hardware module class. A given radio hardware module HAL is of one class
+ * only. The platform can not have more than one hardware module of each class.
+ * Current version of the framework only supports RADIO_CLASS_AM_FM.
+ */
+typedef enum {
+ RADIO_CLASS_AM_FM = 0, /* FM (including HD radio) and AM */
+ RADIO_CLASS_SAT = 1, /* Satellite Radio */
+ RADIO_CLASS_DT = 2, /* Digital Radio (DAB) */
+} radio_class_t;
+
+/* value for field "type" of radio band described in struct radio_hal_band_config */
+typedef enum {
+ RADIO_BAND_AM = 0, /* Amplitude Modulation band: LW, MW, SW */
+ RADIO_BAND_FM = 1, /* Frequency Modulation band: FM */
+ RADIO_BAND_FM_HD = 2, /* FM HD Radio / DRM (IBOC) */
+ RADIO_BAND_AM_HD = 3, /* AM HD Radio / DRM (IBOC) */
+} radio_band_t;
+
+/* RDS variant implemented. A struct radio_hal_fm_band_config can list none or several. */
+enum {
+ RADIO_RDS_NONE = 0x0,
+ RADIO_RDS_WORLD = 0x01,
+ RADIO_RDS_US = 0x02,
+};
+typedef unsigned int radio_rds_t;
+
+/* FM deemphasis variant implemented. A struct radio_hal_fm_band_config can list one or more. */
+enum {
+ RADIO_DEEMPHASIS_50 = 0x1,
+ RADIO_DEEMPHASIS_75 = 0x2,
+};
+typedef unsigned int radio_deemphasis_t;
+
+/* Region a particular radio band configuration corresponds to. Not used at the HAL.
+ * Derived by the framework when converting the band descriptors retrieved from the HAL to
+ * individual band descriptors for each supported region. */
+typedef enum {
+ RADIO_REGION_NONE = -1,
+ RADIO_REGION_ITU_1 = 0,
+ RADIO_REGION_ITU_2 = 1,
+ RADIO_REGION_OIRT = 2,
+ RADIO_REGION_JAPAN = 3,
+ RADIO_REGION_KOREA = 4,
+} radio_region_t;
+
+/* scanning direction for scan() and step() tuner APIs */
+typedef enum {
+ RADIO_DIRECTION_UP,
+ RADIO_DIRECTION_DOWN
+} radio_direction_t;
+
+/* unique handle allocated to a radio module */
+typedef unsigned int radio_handle_t;
+
+/* Opaque meta data structure used by radio meta data API (see system/radio_metadata.h) */
+typedef struct radio_medtadata radio_metadata_t;
+
+
+/* Additional attributes for an FM band configuration */
+typedef struct radio_hal_fm_band_config {
+ radio_deemphasis_t deemphasis; /* deemphasis variant */
+ bool stereo; /* stereo supported */
+ radio_rds_t rds; /* RDS variants supported */
+ bool ta; /* Traffic Announcement supported */
+ bool af; /* Alternate Frequency supported */
+} radio_hal_fm_band_config_t;
+
+/* Additional attributes for an AM band configuration */
+typedef struct radio_hal_am_band_config {
+ bool stereo; /* stereo supported */
+} radio_hal_am_band_config_t;
+
+/* Radio band configuration. Describes a given band supported by the radio module.
+ * The HAL can expose only one band per type with the the maximum range supported and all options.
+ * THe framework will derive the actual regions were this module can operate and expose separate
+ * band configurations for applications to chose from. */
+typedef struct radio_hal_band_config {
+ radio_band_t type;
+ bool antenna_connected;
+ unsigned int lower_limit;
+ unsigned int upper_limit;
+ unsigned int num_spacings;
+ unsigned int spacings[RADIO_NUM_SPACINGS_MAX];
+ union {
+ radio_hal_fm_band_config_t fm;
+ radio_hal_am_band_config_t am;
+ };
+} radio_hal_band_config_t;
+
+/* Used internally by the framework to represent a band for s specific region */
+typedef struct radio_band_config {
+ radio_region_t region;
+ radio_hal_band_config_t band;
+} radio_band_config_t;
+
+
+/* Exposes properties of a given hardware radio module.
+ * NOTE: current framework implementation supports only one audio source (num_audio_sources = 1).
+ * The source corresponds to AUDIO_DEVICE_IN_FM_TUNER.
+ * If more than one tuner is supported (num_tuners > 1), only one can be connected to the audio
+ * source. */
+typedef struct radio_hal_properties {
+ radio_class_t class_id; /* Class of this module. E.g RADIO_CLASS_AM_FM */
+ char implementor[RADIO_STRING_LEN_MAX]; /* implementor name */
+ char product[RADIO_STRING_LEN_MAX]; /* product name */
+ char version[RADIO_STRING_LEN_MAX]; /* product version */
+ char serial[RADIO_STRING_LEN_MAX]; /* serial number (for subscription services) */
+ unsigned int num_tuners; /* number of tuners controllable independently */
+ unsigned int num_audio_sources; /* number of audio sources driven simultaneously */
+ bool supports_capture; /* the hardware supports capture of audio source audio HAL */
+ unsigned int num_bands; /* number of band descriptors */
+ radio_hal_band_config_t bands[RADIO_NUM_BANDS_MAX]; /* band descriptors */
+} radio_hal_properties_t;
+
+/* Used internally by the framework. Same information as in struct radio_hal_properties plus a
+ * unique handle and one band configuration per region. */
+typedef struct radio_properties {
+ radio_handle_t handle;
+ radio_class_t class_id;
+ char implementor[RADIO_STRING_LEN_MAX];
+ char product[RADIO_STRING_LEN_MAX];
+ char version[RADIO_STRING_LEN_MAX];
+ char serial[RADIO_STRING_LEN_MAX];
+ unsigned int num_tuners;
+ unsigned int num_audio_sources;
+ bool supports_capture;
+ unsigned int num_bands;
+ radio_band_config_t bands[RADIO_NUM_BANDS_MAX];
+} radio_properties_t;
+
+/* Radio program information. Returned by the HAL with event RADIO_EVENT_TUNED.
+ * Contains information on currently tuned channel.
+ */
+typedef struct radio_program_info {
+ unsigned int channel; /* current channel. (e.g kHz for band type RADIO_BAND_FM) */
+ unsigned int sub_channel; /* current sub channel. (used for RADIO_BAND_FM_HD) */
+ bool tuned; /* tuned to a program or not */
+ bool stereo; /* program is stereo or not */
+ bool digital; /* digital program or not (e.g HD Radio program) */
+ unsigned int signal_strength; /* signal strength from 0 to 100 */
+ radio_metadata_t *metadata; /* non null if meta data are present (e.g PTY, song title ...) */
+} radio_program_info_t;
+
+
+/* Events sent to the framework via the HAL callback. An event can notify the completion of an
+ * asynchronous command (configuration, tune, scan ...) or a spontaneous change (antenna connection,
+ * failure, AF switching, meta data reception... */
+enum {
+ RADIO_EVENT_HW_FAILURE = 0, /* hardware module failure. Requires reopening the tuner */
+ RADIO_EVENT_CONFIG = 1, /* configuration change completed */
+ RADIO_EVENT_ANTENNA = 2, /* Antenna connected, disconnected */
+ RADIO_EVENT_TUNED = 3, /* tune, step, scan completed */
+ RADIO_EVENT_METADATA = 4, /* New meta data received */
+ RADIO_EVENT_TA = 5, /* Traffic announcement start or stop */
+ RADIO_EVENT_AF_SWITCH = 6, /* Switch to Alternate Frequency */
+ // begin framework only events
+ RADIO_EVENT_CONTROL = 100, /* loss/gain of tuner control */
+ RADIO_EVENT_SERVER_DIED = 101, /* radio service died */
+};
+typedef unsigned int radio_event_type_t;
+
+/* Event passed to the framework by the HAL callback */
+typedef struct radio_hal_event {
+ radio_event_type_t type; /* event type */
+ int status; /* used by RADIO_EVENT_CONFIG, RADIO_EVENT_TUNED */
+ union {
+ bool on; /* RADIO_EVENT_ANTENNA, RADIO_EVENT_TA */
+ radio_hal_band_config_t config; /* RADIO_EVENT_CONFIG */
+ radio_program_info_t info; /* RADIO_EVENT_TUNED, RADIO_EVENT_AF_SWITCH */
+ radio_metadata_t *metadata; /* RADIO_EVENT_METADATA */
+ };
+} radio_hal_event_t;
+
+/* Used internally by the framework. Same information as in struct radio_hal_event */
+typedef struct radio_event {
+ radio_event_type_t type;
+ int status;
+ union {
+ bool on;
+ radio_band_config_t config;
+ radio_program_info_t info;
+ radio_metadata_t *metadata; /* offset from start of struct when in shared memory */
+ };
+} radio_event_t;
+
+
+static radio_rds_t radio_rds_for_region(bool rds, radio_region_t region) {
+ if (!rds)
+ return RADIO_RDS_NONE;
+ switch(region) {
+ case RADIO_REGION_ITU_1:
+ case RADIO_REGION_OIRT:
+ case RADIO_REGION_JAPAN:
+ case RADIO_REGION_KOREA:
+ return RADIO_RDS_WORLD;
+ case RADIO_REGION_ITU_2:
+ return RADIO_RDS_US;
+ default:
+ return RADIO_REGION_NONE;
+ }
+}
+
+static radio_deemphasis_t radio_demephasis_for_region(radio_region_t region) {
+ switch(region) {
+ case RADIO_REGION_KOREA:
+ case RADIO_REGION_ITU_2:
+ return RADIO_DEEMPHASIS_75;
+ case RADIO_REGION_ITU_1:
+ case RADIO_REGION_OIRT:
+ case RADIO_REGION_JAPAN:
+ default:
+ return RADIO_DEEMPHASIS_50;
+ }
+}
+
+#endif // ANDROID_RADIO_H
diff --git a/include/system/sound_trigger.h b/include/system/sound_trigger.h
new file mode 100644
index 0000000..773e4f7
--- /dev/null
+++ b/include/system/sound_trigger.h
@@ -0,0 +1,223 @@
+/*
+ * Copyright (C) 2014 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 ANDROID_SOUND_TRIGGER_H
+#define ANDROID_SOUND_TRIGGER_H
+
+#include <stdbool.h>
+#include <system/audio.h>
+
+#define SOUND_TRIGGER_MAX_STRING_LEN 64 /* max length of strings in properties or
+ descriptor structs */
+#define SOUND_TRIGGER_MAX_LOCALE_LEN 6 /* max length of locale string. e.g en_US */
+#define SOUND_TRIGGER_MAX_USERS 10 /* max number of concurrent users */
+#define SOUND_TRIGGER_MAX_PHRASES 10 /* max number of concurrent phrases */
+
+typedef enum {
+ SOUND_TRIGGER_STATE_NO_INIT = -1, /* The sound trigger service is not initialized */
+ SOUND_TRIGGER_STATE_ENABLED = 0, /* The sound trigger service is enabled */
+ SOUND_TRIGGER_STATE_DISABLED = 1 /* The sound trigger service is disabled */
+} sound_trigger_service_state_t;
+
+#define RECOGNITION_MODE_VOICE_TRIGGER 0x1 /* simple voice trigger */
+#define RECOGNITION_MODE_USER_IDENTIFICATION 0x2 /* trigger only if one user in model identified */
+#define RECOGNITION_MODE_USER_AUTHENTICATION 0x4 /* trigger only if one user in mode
+ authenticated */
+#define RECOGNITION_STATUS_SUCCESS 0
+#define RECOGNITION_STATUS_ABORT 1
+#define RECOGNITION_STATUS_FAILURE 2
+
+#define SOUND_MODEL_STATUS_UPDATED 0
+
+typedef enum {
+ SOUND_MODEL_TYPE_UNKNOWN = -1, /* use for unspecified sound model type */
+ SOUND_MODEL_TYPE_KEYPHRASE = 0 /* use for key phrase sound models */
+} sound_trigger_sound_model_type_t;
+
+typedef struct sound_trigger_uuid_s {
+ unsigned int timeLow;
+ unsigned short timeMid;
+ unsigned short timeHiAndVersion;
+ unsigned short clockSeq;
+ unsigned char node[6];
+} sound_trigger_uuid_t;
+
+/*
+ * sound trigger implementation descriptor read by the framework via get_properties().
+ * Used by SoundTrigger service to report to applications and manage concurrency and policy.
+ */
+struct sound_trigger_properties {
+ char implementor[SOUND_TRIGGER_MAX_STRING_LEN]; /* implementor name */
+ char description[SOUND_TRIGGER_MAX_STRING_LEN]; /* implementation description */
+ unsigned int version; /* implementation version */
+ sound_trigger_uuid_t uuid; /* unique implementation ID.
+ Must change with version each version */
+ unsigned int max_sound_models; /* maximum number of concurrent sound models
+ loaded */
+ unsigned int max_key_phrases; /* maximum number of key phrases */
+ unsigned int max_users; /* maximum number of concurrent users detected */
+ unsigned int recognition_modes; /* all supported modes.
+ e.g RECOGNITION_MODE_VOICE_TRIGGER */
+ bool capture_transition; /* supports seamless transition from detection
+ to capture */
+ unsigned int max_buffer_ms; /* maximum buffering capacity in ms if
+ capture_transition is true*/
+ bool concurrent_capture; /* supports capture by other use cases while
+ detection is active */
+ bool trigger_in_event; /* returns the trigger capture in event */
+ unsigned int power_consumption_mw; /* Rated power consumption when detection is active
+ with TDB silence/sound/speech ratio */
+};
+
+typedef int sound_trigger_module_handle_t;
+
+struct sound_trigger_module_descriptor {
+ sound_trigger_module_handle_t handle;
+ struct sound_trigger_properties properties;
+};
+
+typedef int sound_model_handle_t;
+
+/*
+ * Generic sound model descriptor. This struct is the header of a larger block passed to
+ * load_sound_model() and containing the binary data of the sound model.
+ * Proprietary representation of users in binary data must match information indicated
+ * by users field
+ */
+struct sound_trigger_sound_model {
+ sound_trigger_sound_model_type_t type; /* model type. e.g. SOUND_MODEL_TYPE_KEYPHRASE */
+ sound_trigger_uuid_t uuid; /* unique sound model ID. */
+ sound_trigger_uuid_t vendor_uuid; /* unique vendor ID. Identifies the engine the
+ sound model was build for */
+ unsigned int data_size; /* size of opaque model data */
+ unsigned int data_offset; /* offset of opaque data start from head of struct
+ (e.g sizeof struct sound_trigger_sound_model) */
+};
+
+/* key phrase descriptor */
+struct sound_trigger_phrase {
+ unsigned int id; /* keyphrase ID */
+ unsigned int recognition_mode; /* recognition modes supported by this key phrase */
+ unsigned int num_users; /* number of users in the key phrase */
+ unsigned int users[SOUND_TRIGGER_MAX_USERS]; /* users ids: (not uid_t but sound trigger
+ specific IDs */
+ char locale[SOUND_TRIGGER_MAX_LOCALE_LEN]; /* locale - JAVA Locale style (e.g. en_US) */
+ char text[SOUND_TRIGGER_MAX_STRING_LEN]; /* phrase text in UTF-8 format. */
+};
+
+/*
+ * Specialized sound model for key phrase detection.
+ * Proprietary representation of key phrases in binary data must match information indicated
+ * by phrases field
+ */
+struct sound_trigger_phrase_sound_model {
+ struct sound_trigger_sound_model common;
+ unsigned int num_phrases; /* number of key phrases in model */
+ struct sound_trigger_phrase phrases[SOUND_TRIGGER_MAX_PHRASES];
+};
+
+
+/*
+ * Generic recognition event sent via recognition callback
+ */
+struct sound_trigger_recognition_event {
+ int status; /* recognition status e.g.
+ RECOGNITION_STATUS_SUCCESS */
+ sound_trigger_sound_model_type_t type; /* event type, same as sound model type.
+ e.g. SOUND_MODEL_TYPE_KEYPHRASE */
+ sound_model_handle_t model; /* loaded sound model that triggered the
+ event */
+ bool capture_available; /* it is possible to capture audio from this
+ utterance buffered by the
+ implementation */
+ int capture_session; /* audio session ID. framework use */
+ int capture_delay_ms; /* delay in ms between end of model
+ detection and start of audio available
+ for capture. A negative value is possible
+ (e.g. if key phrase is also available for
+ capture */
+ int capture_preamble_ms; /* duration in ms of audio captured
+ before the start of the trigger.
+ 0 if none. */
+ bool trigger_in_data; /* the opaque data is the capture of
+ the trigger sound */
+ audio_config_t audio_config; /* audio format of either the trigger in
+ event data or to use for capture of the
+ rest of the utterance */
+ unsigned int data_size; /* size of opaque event data */
+ unsigned int data_offset; /* offset of opaque data start from start of
+ this struct (e.g sizeof struct
+ sound_trigger_phrase_recognition_event) */
+};
+
+/*
+ * Confidence level for each user in struct sound_trigger_phrase_recognition_extra
+ */
+struct sound_trigger_confidence_level {
+ unsigned int user_id; /* user ID */
+ unsigned int level; /* confidence level in percent (0 - 100).
+ - min level for recognition configuration
+ - detected level for recognition event */
+};
+
+/*
+ * Specialized recognition event for key phrase detection
+ */
+struct sound_trigger_phrase_recognition_extra {
+ unsigned int id; /* keyphrase ID */
+ unsigned int recognition_modes; /* recognition modes used for this keyphrase */
+ unsigned int confidence_level; /* confidence level for mode RECOGNITION_MODE_VOICE_TRIGGER */
+ unsigned int num_levels; /* number of user confidence levels */
+ struct sound_trigger_confidence_level levels[SOUND_TRIGGER_MAX_USERS];
+};
+
+struct sound_trigger_phrase_recognition_event {
+ struct sound_trigger_recognition_event common;
+ unsigned int num_phrases;
+ struct sound_trigger_phrase_recognition_extra phrase_extras[SOUND_TRIGGER_MAX_PHRASES];
+};
+
+/*
+ * configuration for sound trigger capture session passed to start_recognition()
+ */
+struct sound_trigger_recognition_config {
+ audio_io_handle_t capture_handle; /* IO handle that will be used for capture.
+ N/A if capture_requested is false */
+ audio_devices_t capture_device; /* input device requested for detection capture */
+ bool capture_requested; /* capture and buffer audio for this recognition
+ instance */
+ unsigned int num_phrases; /* number of key phrases recognition extras */
+ struct sound_trigger_phrase_recognition_extra phrases[SOUND_TRIGGER_MAX_PHRASES];
+ /* configuration for each key phrase */
+ unsigned int data_size; /* size of opaque capture configuration data */
+ unsigned int data_offset; /* offset of opaque data start from start of this struct
+ (e.g sizeof struct sound_trigger_recognition_config) */
+};
+
+/*
+ * Event sent via load sound model callback
+ */
+struct sound_trigger_model_event {
+ int status; /* sound model status e.g. SOUND_MODEL_STATUS_UPDATED */
+ sound_model_handle_t model; /* loaded sound model that triggered the event */
+ unsigned int data_size; /* size of event data if any. Size of updated sound model if
+ status is SOUND_MODEL_STATUS_UPDATED */
+ unsigned int data_offset; /* offset of data start from start of this struct
+ (e.g sizeof struct sound_trigger_model_event) */
+};
+
+
+#endif // ANDROID_SOUND_TRIGGER_H
diff --git a/include/system/window.h b/include/system/window.h
index 588f9c6..af0418b 100644
--- a/include/system/window.h
+++ b/include/system/window.h
@@ -26,6 +26,13 @@
#include <system/graphics.h>
#include <unistd.h>
+#ifndef __UNUSED
+#define __UNUSED __attribute__((__unused__))
+#endif
+#ifndef __deprecated
+#define __deprecated __attribute__((__deprecated__))
+#endif
+
__BEGIN_DECLS
/*****************************************************************************/
@@ -89,10 +96,10 @@
// Implement the methods that sp<ANativeWindowBuffer> expects so that it
// can be used to automatically refcount ANativeWindowBuffer's.
- void incStrong(const void* id) const {
+ void incStrong(const void* /*id*/) const {
common.incRef(const_cast<android_native_base_t*>(&common));
}
- void decStrong(const void* id) const {
+ void decStrong(const void* /*id*/) const {
common.decRef(const_cast<android_native_base_t*>(&common));
}
#endif
@@ -235,7 +242,32 @@
* The consumer gralloc usage bits currently set by the consumer.
* The values are defined in hardware/libhardware/include/gralloc.h.
*/
- NATIVE_WINDOW_CONSUMER_USAGE_BITS = 10
+ NATIVE_WINDOW_CONSUMER_USAGE_BITS = 10,
+
+ /**
+ * Transformation that will by applied to buffers by the hwcomposer.
+ * This must not be set or checked by producer endpoints, and will
+ * disable the transform hint set in SurfaceFlinger (see
+ * NATIVE_WINDOW_TRANSFORM_HINT).
+ *
+ * INTENDED USE:
+ * Temporary - Please do not use this. This is intended only to be used
+ * by the camera's LEGACY mode.
+ *
+ * In situations where a SurfaceFlinger client wishes to set a transform
+ * that is not visible to the producer, and will always be applied in the
+ * hardware composer, the client can set this flag with
+ * native_window_set_buffers_sticky_transform. This can be used to rotate
+ * and flip buffers consumed by hardware composer without actually changing
+ * the aspect ratio of the buffers produced.
+ */
+ NATIVE_WINDOW_STICKY_TRANSFORM = 11,
+
+ /**
+ * The default data space for the buffers as set by the consumer.
+ * The values are defined in graphics.h.
+ */
+ NATIVE_WINDOW_DEFAULT_DATASPACE = 12
};
/* Valid operations for the (*perform)() hook.
@@ -266,6 +298,9 @@
NATIVE_WINDOW_API_DISCONNECT = 14, /* private */
NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS = 15, /* private */
NATIVE_WINDOW_SET_POST_TRANSFORM_CROP = 16, /* private */
+ NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM = 17,/* private */
+ NATIVE_WINDOW_SET_SIDEBAND_STREAM = 18,
+ NATIVE_WINDOW_SET_BUFFERS_DATASPACE = 19
};
/* parameter for NATIVE_WINDOW_[API_][DIS]CONNECT */
@@ -352,10 +387,10 @@
/* Implement the methods that sp<ANativeWindow> expects so that it
can be used to automatically refcount ANativeWindow's. */
- void incStrong(const void* id) const {
+ void incStrong(const void* /*id*/) const {
common.incRef(const_cast<android_native_base_t*>(&common));
}
- void decStrong(const void* id) const {
+ void decStrong(const void* /*id*/) const {
common.decRef(const_cast<android_native_base_t*>(&common));
}
#endif
@@ -470,6 +505,7 @@
* NATIVE_WINDOW_SET_BUFFERS_GEOMETRY (deprecated)
* NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
* NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
+ * NATIVE_WINDOW_SET_BUFFERS_DATASPACE
* NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS
* NATIVE_WINDOW_SET_BUFFERS_FORMAT
* NATIVE_WINDOW_SET_SCALING_MODE (private)
@@ -582,7 +618,7 @@
* android_native_window_t is deprecated.
*/
typedef struct ANativeWindow ANativeWindow;
-typedef struct ANativeWindow android_native_window_t;
+typedef struct ANativeWindow android_native_window_t __deprecated;
/*
* native_window_set_usage(..., usage)
@@ -603,13 +639,19 @@
/* deprecated. Always returns 0. Don't call. */
static inline int native_window_connect(
- struct ANativeWindow* window, int api) {
+ struct ANativeWindow* window __UNUSED, int api __UNUSED) __deprecated;
+
+static inline int native_window_connect(
+ struct ANativeWindow* window __UNUSED, int api __UNUSED) {
return 0;
}
/* deprecated. Always returns 0. Don't call. */
static inline int native_window_disconnect(
- struct ANativeWindow* window, int api) {
+ struct ANativeWindow* window __UNUSED, int api __UNUSED) __deprecated;
+
+static inline int native_window_disconnect(
+ struct ANativeWindow* window __UNUSED, int api __UNUSED) {
return 0;
}
@@ -664,6 +706,10 @@
*/
static inline int native_window_set_active_rect(
struct ANativeWindow* window,
+ android_native_rect_t const * active_rect) __deprecated;
+
+static inline int native_window_set_active_rect(
+ struct ANativeWindow* window,
android_native_rect_t const * active_rect)
{
return native_window_set_post_transform_crop(window, active_rect);
@@ -691,6 +737,10 @@
*/
static inline int native_window_set_buffers_geometry(
struct ANativeWindow* window,
+ int w, int h, int format) __deprecated;
+
+static inline int native_window_set_buffers_geometry(
+ struct ANativeWindow* window,
int w, int h, int format)
{
return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
@@ -757,6 +807,26 @@
}
/*
+ * native_window_set_buffers_data_space(..., int dataSpace)
+ * All buffers queued after this call will be associated with the dataSpace
+ * parameter specified.
+ *
+ * dataSpace specifies additional information about the buffer that's dependent
+ * on the buffer format and the endpoints. For example, it can be used to convey
+ * the color space of the image data in the buffer, or it can be used to
+ * indicate that the buffers contain depth measurement data instead of color
+ * images. The default dataSpace is 0, HAL_DATASPACE_UNKNOWN, unless it has been
+ * overridden by the consumer.
+ */
+static inline int native_window_set_buffers_data_space(
+ struct ANativeWindow* window,
+ android_dataspace_t dataSpace)
+{
+ return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_DATASPACE,
+ dataSpace);
+}
+
+/*
* native_window_set_buffers_transform(..., int transform)
* All buffers queued after this call will be displayed transformed according
* to the transform parameter specified.
@@ -770,6 +840,23 @@
}
/*
+ * native_window_set_buffers_sticky_transform(..., int transform)
+ * All buffers queued after this call will be displayed transformed according
+ * to the transform parameter specified applied on top of the regular buffer
+ * transform. Setting this transform will disable the transform hint.
+ *
+ * Temporary - This is only intended to be used by the LEGACY camera mode, do
+ * not use this for anything else.
+ */
+static inline int native_window_set_buffers_sticky_transform(
+ struct ANativeWindow* window,
+ int transform)
+{
+ return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM,
+ transform);
+}
+
+/*
* native_window_set_buffers_timestamp(..., int64_t timestamp)
* All buffers queued after this call will be associated with the timestamp
* parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
@@ -835,6 +922,17 @@
return anw->dequeueBuffer_DEPRECATED(anw, anb);
}
+/*
+ * native_window_set_sideband_stream(..., native_handle_t*)
+ * Attach a sideband buffer stream to a native window.
+ */
+static inline int native_window_set_sideband_stream(
+ struct ANativeWindow* window,
+ native_handle_t* sidebandHandle)
+{
+ return window->perform(window, NATIVE_WINDOW_SET_SIDEBAND_STREAM,
+ sidebandHandle);
+}
__END_DECLS
diff --git a/include/sysutils/FrameworkListener.h b/include/sysutils/FrameworkListener.h
index f1a4b43..18049cd 100644
--- a/include/sysutils/FrameworkListener.h
+++ b/include/sysutils/FrameworkListener.h
@@ -36,6 +36,7 @@
public:
FrameworkListener(const char *socketName);
FrameworkListener(const char *socketName, bool withSeq);
+ FrameworkListener(int sock);
virtual ~FrameworkListener() {}
protected:
diff --git a/include/sysutils/NetlinkEvent.h b/include/sysutils/NetlinkEvent.h
index c0a9418..a428d37 100644
--- a/include/sysutils/NetlinkEvent.h
+++ b/include/sysutils/NetlinkEvent.h
@@ -21,13 +21,30 @@
#define NL_PARAMS_MAX 32
class NetlinkEvent {
+public:
+ enum class Action {
+ kUnknown = 0,
+ kAdd = 1,
+ kRemove = 2,
+ kChange = 3,
+ kLinkUp = 4,
+ kLinkDown = 5,
+ kAddressUpdated = 6,
+ kAddressRemoved = 7,
+ kRdnss = 8,
+ kRouteUpdated = 9,
+ kRouteRemoved = 10,
+ };
+
+private:
int mSeq;
char *mPath;
- int mAction;
+ Action mAction;
char *mSubsystem;
char *mParams[NL_PARAMS_MAX];
public:
+ // STOPSHIP: remove these deprecated constants once we have updated prebuilts
const static int NlActionUnknown;
const static int NlActionAdd;
const static int NlActionRemove;
@@ -37,6 +54,8 @@
const static int NlActionAddressUpdated;
const static int NlActionAddressRemoved;
const static int NlActionRdnss;
+ const static int NlActionRouteUpdated;
+ const static int NlActionRouteRemoved;
NetlinkEvent();
virtual ~NetlinkEvent();
@@ -45,15 +64,19 @@
const char *findParam(const char *paramName);
const char *getSubsystem() { return mSubsystem; }
- int getAction() { return mAction; }
+ Action getAction() { return mAction; }
void dump();
protected:
bool parseBinaryNetlinkMessage(char *buffer, int size);
bool parseAsciiNetlinkMessage(char *buffer, int size);
- bool parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, int rtasize);
- bool parseNdUserOptMessage(struct nduseroptmsg *msg, int optsize);
+ bool parseIfInfoMessage(const struct nlmsghdr *nh);
+ bool parseIfAddrMessage(const struct nlmsghdr *nh);
+ bool parseUlogPacketMessage(const struct nlmsghdr *nh);
+ bool parseNfPacketMessage(struct nlmsghdr *nh);
+ bool parseRtMessage(const struct nlmsghdr *nh);
+ bool parseNdUserOptMessage(const struct nlmsghdr *nh);
};
#endif
diff --git a/include/sysutils/NetlinkListener.h b/include/sysutils/NetlinkListener.h
index 6e52c3b..82465d6 100644
--- a/include/sysutils/NetlinkListener.h
+++ b/include/sysutils/NetlinkListener.h
@@ -27,6 +27,7 @@
public:
static const int NETLINK_FORMAT_ASCII = 0;
static const int NETLINK_FORMAT_BINARY = 1;
+ static const int NETLINK_FORMAT_BINARY_UNICAST = 2;
#if 1
/* temporary version until we can get Motorola to update their
diff --git a/include/sysutils/SocketListener.h b/include/sysutils/SocketListener.h
index c204a0f..bc93b86 100644
--- a/include/sysutils/SocketListener.h
+++ b/include/sysutils/SocketListener.h
@@ -38,6 +38,7 @@
virtual ~SocketListener();
int startListener();
+ int startListener(int backlog);
int stopListener();
void sendBroadcast(int code, const char *msg, bool addErrno);
diff --git a/include/utils/AndroidThreads.h b/include/utils/AndroidThreads.h
index 4eee14d..aad1e82 100644
--- a/include/utils/AndroidThreads.h
+++ b/include/utils/AndroidThreads.h
@@ -20,7 +20,7 @@
#include <stdint.h>
#include <sys/types.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
@@ -73,9 +73,6 @@
// ------------------------------------------------------------------
// Extra functions working with raw pids.
-// Get pid for the current thread.
-extern pid_t androidGetTid();
-
#ifdef HAVE_ANDROID_OS
// Change the priority AND scheduling group of a particular thread. The priority
// should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION
diff --git a/include/utils/BitSet.h b/include/utils/BitSet.h
index 19c03d1..8c61293 100644
--- a/include/utils/BitSet.h
+++ b/include/utils/BitSet.h
@@ -30,73 +30,103 @@
struct BitSet32 {
uint32_t value;
- inline BitSet32() : value(0) { }
+ inline BitSet32() : value(0UL) { }
explicit inline BitSet32(uint32_t value) : value(value) { }
// Gets the value associated with a particular bit index.
- static inline uint32_t valueForBit(uint32_t n) { return 0x80000000 >> n; }
+ static inline uint32_t valueForBit(uint32_t n) { return 0x80000000UL >> n; }
// Clears the bit set.
- inline void clear() { value = 0; }
+ inline void clear() { clear(value); }
+
+ static inline void clear(uint32_t& value) { value = 0UL; }
// Returns the number of marked bits in the set.
- inline uint32_t count() const { return __builtin_popcount(value); }
+ inline uint32_t count() const { return count(value); }
+
+ static inline uint32_t count(uint32_t value) { return __builtin_popcountl(value); }
// Returns true if the bit set does not contain any marked bits.
- inline bool isEmpty() const { return ! value; }
+ inline bool isEmpty() const { return isEmpty(value); }
+
+ static inline bool isEmpty(uint32_t value) { return ! value; }
// Returns true if the bit set does not contain any unmarked bits.
- inline bool isFull() const { return value == 0xffffffff; }
+ inline bool isFull() const { return isFull(value); }
+
+ static inline bool isFull(uint32_t value) { return value == 0xffffffffUL; }
// Returns true if the specified bit is marked.
- inline bool hasBit(uint32_t n) const { return value & valueForBit(n); }
+ inline bool hasBit(uint32_t n) const { return hasBit(value, n); }
+
+ static inline bool hasBit(uint32_t value, uint32_t n) { return value & valueForBit(n); }
// Marks the specified bit.
- inline void markBit(uint32_t n) { value |= valueForBit(n); }
+ inline void markBit(uint32_t n) { markBit(value, n); }
+
+ static inline void markBit (uint32_t& value, uint32_t n) { value |= valueForBit(n); }
// Clears the specified bit.
- inline void clearBit(uint32_t n) { value &= ~ valueForBit(n); }
+ inline void clearBit(uint32_t n) { clearBit(value, n); }
+
+ static inline void clearBit(uint32_t& value, uint32_t n) { value &= ~ valueForBit(n); }
// Finds the first marked bit in the set.
// Result is undefined if all bits are unmarked.
- inline uint32_t firstMarkedBit() const { return __builtin_clz(value); }
+ inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); }
+
+ static uint32_t firstMarkedBit(uint32_t value) { return clz_checked(value); }
// Finds the first unmarked bit in the set.
// Result is undefined if all bits are marked.
- inline uint32_t firstUnmarkedBit() const { return __builtin_clz(~ value); }
+ inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); }
+
+ static inline uint32_t firstUnmarkedBit(uint32_t value) { return clz_checked(~ value); }
// Finds the last marked bit in the set.
// Result is undefined if all bits are unmarked.
- inline uint32_t lastMarkedBit() const { return 31 - __builtin_ctz(value); }
+ inline uint32_t lastMarkedBit() const { return lastMarkedBit(value); }
+
+ static inline uint32_t lastMarkedBit(uint32_t value) { return 31 - ctz_checked(value); }
// Finds the first marked bit in the set and clears it. Returns the bit index.
// Result is undefined if all bits are unmarked.
- inline uint32_t clearFirstMarkedBit() {
- uint32_t n = firstMarkedBit();
- clearBit(n);
+ inline uint32_t clearFirstMarkedBit() { return clearFirstMarkedBit(value); }
+
+ static inline uint32_t clearFirstMarkedBit(uint32_t& value) {
+ uint32_t n = firstMarkedBit(value);
+ clearBit(value, n);
return n;
}
// Finds the first unmarked bit in the set and marks it. Returns the bit index.
// Result is undefined if all bits are marked.
- inline uint32_t markFirstUnmarkedBit() {
- uint32_t n = firstUnmarkedBit();
- markBit(n);
+ inline uint32_t markFirstUnmarkedBit() { return markFirstUnmarkedBit(value); }
+
+ static inline uint32_t markFirstUnmarkedBit(uint32_t& value) {
+ uint32_t n = firstUnmarkedBit(value);
+ markBit(value, n);
return n;
}
// Finds the last marked bit in the set and clears it. Returns the bit index.
// Result is undefined if all bits are unmarked.
- inline uint32_t clearLastMarkedBit() {
- uint32_t n = lastMarkedBit();
- clearBit(n);
+ inline uint32_t clearLastMarkedBit() { return clearLastMarkedBit(value); }
+
+ static inline uint32_t clearLastMarkedBit(uint32_t& value) {
+ uint32_t n = lastMarkedBit(value);
+ clearBit(value, n);
return n;
}
// Gets the index of the specified bit in the set, which is the number of
// marked bits that appear before the specified bit.
inline uint32_t getIndexOfBit(uint32_t n) const {
- return __builtin_popcount(value & ~(0xffffffffUL >> n));
+ return getIndexOfBit(value, n);
+ }
+
+ static inline uint32_t getIndexOfBit(uint32_t value, uint32_t n) {
+ return __builtin_popcountl(value & ~(0xffffffffUL >> n));
}
inline bool operator== (const BitSet32& other) const { return value == other.value; }
@@ -115,10 +145,150 @@
value |= other.value;
return *this;
}
+
+private:
+ // We use these helpers as the signature of __builtin_c{l,t}z has "unsigned int" for the
+ // input, which is only guaranteed to be 16b, not 32. The compiler should optimize this away.
+ static inline uint32_t clz_checked(uint32_t value) {
+ if (sizeof(unsigned int) == sizeof(uint32_t)) {
+ return __builtin_clz(value);
+ } else {
+ return __builtin_clzl(value);
+ }
+ }
+
+ static inline uint32_t ctz_checked(uint32_t value) {
+ if (sizeof(unsigned int) == sizeof(uint32_t)) {
+ return __builtin_ctz(value);
+ } else {
+ return __builtin_ctzl(value);
+ }
+ }
};
ANDROID_BASIC_TYPES_TRAITS(BitSet32)
+// A simple set of 64 bits that can be individually marked or cleared.
+struct BitSet64 {
+ uint64_t value;
+
+ inline BitSet64() : value(0ULL) { }
+ explicit inline BitSet64(uint64_t value) : value(value) { }
+
+ // Gets the value associated with a particular bit index.
+ static inline uint64_t valueForBit(uint32_t n) { return 0x8000000000000000ULL >> n; }
+
+ // Clears the bit set.
+ inline void clear() { clear(value); }
+
+ static inline void clear(uint64_t& value) { value = 0ULL; }
+
+ // Returns the number of marked bits in the set.
+ inline uint32_t count() const { return count(value); }
+
+ static inline uint32_t count(uint64_t value) { return __builtin_popcountll(value); }
+
+ // Returns true if the bit set does not contain any marked bits.
+ inline bool isEmpty() const { return isEmpty(value); }
+
+ static inline bool isEmpty(uint64_t value) { return ! value; }
+
+ // Returns true if the bit set does not contain any unmarked bits.
+ inline bool isFull() const { return isFull(value); }
+
+ static inline bool isFull(uint64_t value) { return value == 0xffffffffffffffffULL; }
+
+ // Returns true if the specified bit is marked.
+ inline bool hasBit(uint32_t n) const { return hasBit(value, n); }
+
+ static inline bool hasBit(uint64_t value, uint32_t n) { return value & valueForBit(n); }
+
+ // Marks the specified bit.
+ inline void markBit(uint32_t n) { markBit(value, n); }
+
+ static inline void markBit(uint64_t& value, uint32_t n) { value |= valueForBit(n); }
+
+ // Clears the specified bit.
+ inline void clearBit(uint32_t n) { clearBit(value, n); }
+
+ static inline void clearBit(uint64_t& value, uint32_t n) { value &= ~ valueForBit(n); }
+
+ // Finds the first marked bit in the set.
+ // Result is undefined if all bits are unmarked.
+ inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); }
+
+ static inline uint32_t firstMarkedBit(uint64_t value) { return __builtin_clzll(value); }
+
+ // Finds the first unmarked bit in the set.
+ // Result is undefined if all bits are marked.
+ inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); }
+
+ static inline uint32_t firstUnmarkedBit(uint64_t value) { return __builtin_clzll(~ value); }
+
+ // Finds the last marked bit in the set.
+ // Result is undefined if all bits are unmarked.
+ inline uint32_t lastMarkedBit() const { return lastMarkedBit(value); }
+
+ static inline uint32_t lastMarkedBit(uint64_t value) { return 63 - __builtin_ctzll(value); }
+
+ // Finds the first marked bit in the set and clears it. Returns the bit index.
+ // Result is undefined if all bits are unmarked.
+ inline uint32_t clearFirstMarkedBit() { return clearFirstMarkedBit(value); }
+
+ static inline uint32_t clearFirstMarkedBit(uint64_t& value) {
+ uint64_t n = firstMarkedBit(value);
+ clearBit(value, n);
+ return n;
+ }
+
+ // Finds the first unmarked bit in the set and marks it. Returns the bit index.
+ // Result is undefined if all bits are marked.
+ inline uint32_t markFirstUnmarkedBit() { return markFirstUnmarkedBit(value); }
+
+ static inline uint32_t markFirstUnmarkedBit(uint64_t& value) {
+ uint64_t n = firstUnmarkedBit(value);
+ markBit(value, n);
+ return n;
+ }
+
+ // Finds the last marked bit in the set and clears it. Returns the bit index.
+ // Result is undefined if all bits are unmarked.
+ inline uint32_t clearLastMarkedBit() { return clearLastMarkedBit(value); }
+
+ static inline uint32_t clearLastMarkedBit(uint64_t& value) {
+ uint64_t n = lastMarkedBit(value);
+ clearBit(value, n);
+ return n;
+ }
+
+ // Gets the index of the specified bit in the set, which is the number of
+ // marked bits that appear before the specified bit.
+ inline uint32_t getIndexOfBit(uint32_t n) const { return getIndexOfBit(value, n); }
+
+ static inline uint32_t getIndexOfBit(uint64_t value, uint32_t n) {
+ return __builtin_popcountll(value & ~(0xffffffffffffffffULL >> n));
+ }
+
+ inline bool operator== (const BitSet64& other) const { return value == other.value; }
+ inline bool operator!= (const BitSet64& other) const { return value != other.value; }
+ inline BitSet64 operator& (const BitSet64& other) const {
+ return BitSet64(value & other.value);
+ }
+ inline BitSet64& operator&= (const BitSet64& other) {
+ value &= other.value;
+ return *this;
+ }
+ inline BitSet64 operator| (const BitSet64& other) const {
+ return BitSet64(value | other.value);
+ }
+ inline BitSet64& operator|= (const BitSet64& other) {
+ value |= other.value;
+ return *this;
+ }
+};
+
+ANDROID_BASIC_TYPES_TRAITS(BitSet64)
+
} // namespace android
#endif // UTILS_BITSET_H
diff --git a/include/utils/Compat.h b/include/utils/Compat.h
index fb7748e..a238afe 100644
--- a/include/utils/Compat.h
+++ b/include/utils/Compat.h
@@ -19,11 +19,9 @@
#include <unistd.h>
-/* Compatibility definitions for non-Linux (i.e., BSD-based) hosts. */
-#ifndef HAVE_OFF64_T
-#if _FILE_OFFSET_BITS < 64
-#error "_FILE_OFFSET_BITS < 64; large files are not supported on this platform"
-#endif /* _FILE_OFFSET_BITS < 64 */
+#if defined(__APPLE__)
+
+/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
typedef off_t off64_t;
@@ -31,13 +29,17 @@
return lseek(fd, offset, whence);
}
-#ifdef HAVE_PREAD
static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
return pread(fd, buf, nbytes, offset);
}
-#endif
-#endif /* !HAVE_OFF64_T */
+#endif /* __APPLE__ */
+
+#if defined(_WIN32)
+#define O_CLOEXEC 0
+#define O_NOFOLLOW 0
+#define DEFFILEMODE 0666
+#endif /* _WIN32 */
#if HAVE_PRINTF_ZD
# define ZD "%zd"
@@ -48,6 +50,17 @@
#endif
/*
+ * Needed for cases where something should be constexpr if possible, but not
+ * being constexpr is fine if in pre-C++11 code (such as a const static float
+ * member variable).
+ */
+#if __cplusplus >= 201103L
+#define CONSTEXPR constexpr
+#else
+#define CONSTEXPR
+#endif
+
+/*
* TEMP_FAILURE_RETRY is defined by some, but not all, versions of
* <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
* not already defined, then define it here.
diff --git a/include/utils/Condition.h b/include/utils/Condition.h
index e63ba7e..5a72519 100644
--- a/include/utils/Condition.h
+++ b/include/utils/Condition.h
@@ -21,7 +21,7 @@
#include <sys/types.h>
#include <time.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
@@ -60,7 +60,7 @@
status_t wait(Mutex& mutex);
// same with relative timeout
status_t waitRelative(Mutex& mutex, nsecs_t reltime);
- // Signal the condition variable, allowing one thread to continue.
+ // Signal the condition variable, allowing exactly one thread to continue.
void signal();
// Signal the condition variable, allowing one or all threads to continue.
void signal(WakeUpType type) {
@@ -74,7 +74,7 @@
void broadcast();
private:
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
pthread_cond_t mCond;
#else
void* mState;
@@ -83,7 +83,7 @@
// ---------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
inline Condition::Condition() {
pthread_cond_init(&mCond, NULL);
@@ -113,15 +113,15 @@
return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts);
#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
struct timespec ts;
-#if defined(HAVE_POSIX_CLOCKS)
+#if defined(__linux__)
clock_gettime(CLOCK_REALTIME, &ts);
-#else // HAVE_POSIX_CLOCKS
+#else // __APPLE__
// we don't support the clocks here.
struct timeval t;
gettimeofday(&t, NULL);
ts.tv_sec = t.tv_sec;
ts.tv_nsec= t.tv_usec*1000;
-#endif // HAVE_POSIX_CLOCKS
+#endif
ts.tv_sec += reltime/1000000000;
ts.tv_nsec+= reltime%1000000000;
if (ts.tv_nsec >= 1000000000) {
@@ -132,13 +132,24 @@
#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
}
inline void Condition::signal() {
+ /*
+ * POSIX says pthread_cond_signal wakes up "one or more" waiting threads.
+ * However bionic follows the glibc guarantee which wakes up "exactly one"
+ * waiting thread.
+ *
+ * man 3 pthread_cond_signal
+ * pthread_cond_signal restarts one of the threads that are waiting on
+ * the condition variable cond. If no threads are waiting on cond,
+ * nothing happens. If several threads are waiting on cond, exactly one
+ * is restarted, but it is not specified which.
+ */
pthread_cond_signal(&mCond);
}
inline void Condition::broadcast() {
pthread_cond_broadcast(&mCond);
}
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
// ---------------------------------------------------------------------------
}; // namespace android
diff --git a/include/utils/Endian.h b/include/utils/Endian.h
index 19f2504..591cae0 100644
--- a/include/utils/Endian.h
+++ b/include/utils/Endian.h
@@ -20,21 +20,16 @@
#ifndef _LIBS_UTILS_ENDIAN_H
#define _LIBS_UTILS_ENDIAN_H
-#if defined(HAVE_ENDIAN_H)
-
-#include <endian.h>
-
-#else /*not HAVE_ENDIAN_H*/
+#if defined(__APPLE__) || defined(_WIN32)
#define __BIG_ENDIAN 0x1000
#define __LITTLE_ENDIAN 0x0001
+#define __BYTE_ORDER __LITTLE_ENDIAN
-#if defined(HAVE_LITTLE_ENDIAN)
-# define __BYTE_ORDER __LITTLE_ENDIAN
#else
-# define __BYTE_ORDER __BIG_ENDIAN
-#endif
-#endif /*not HAVE_ENDIAN_H*/
+#include <endian.h>
+
+#endif
#endif /*_LIBS_UTILS_ENDIAN_H*/
diff --git a/include/utils/Errors.h b/include/utils/Errors.h
index 0b75b19..46173db 100644
--- a/include/utils/Errors.h
+++ b/include/utils/Errors.h
@@ -41,23 +41,23 @@
#ifdef _WIN32
# undef NO_ERROR
#endif
-
+
enum {
OK = 0, // Everything's swell.
NO_ERROR = 0, // No errors.
-
- UNKNOWN_ERROR = 0x80000000,
+
+ UNKNOWN_ERROR = (-2147483647-1), // INT32_MIN value
NO_MEMORY = -ENOMEM,
INVALID_OPERATION = -ENOSYS,
BAD_VALUE = -EINVAL,
- BAD_TYPE = 0x80000001,
+ BAD_TYPE = (UNKNOWN_ERROR + 1),
NAME_NOT_FOUND = -ENOENT,
PERMISSION_DENIED = -EPERM,
NO_INIT = -ENODEV,
ALREADY_EXISTS = -EEXIST,
DEAD_OBJECT = -EPIPE,
- FAILED_TRANSACTION = 0x80000002,
+ FAILED_TRANSACTION = (UNKNOWN_ERROR + 2),
JPARKS_BROKE_IT = -EPIPE,
#if !defined(HAVE_MS_C_RUNTIME)
BAD_INDEX = -EOVERFLOW,
@@ -67,12 +67,12 @@
UNKNOWN_TRANSACTION = -EBADMSG,
#else
BAD_INDEX = -E2BIG,
- NOT_ENOUGH_DATA = 0x80000003,
- WOULD_BLOCK = 0x80000004,
- TIMED_OUT = 0x80000005,
- UNKNOWN_TRANSACTION = 0x80000006,
+ NOT_ENOUGH_DATA = (UNKNOWN_ERROR + 3),
+ WOULD_BLOCK = (UNKNOWN_ERROR + 4),
+ TIMED_OUT = (UNKNOWN_ERROR + 5),
+ UNKNOWN_TRANSACTION = (UNKNOWN_ERROR + 6),
#endif
- FDS_NOT_ALLOWED = 0x80000007,
+ FDS_NOT_ALLOWED = (UNKNOWN_ERROR + 7),
};
// Restore define; enumeration is in "android" namespace, so the value defined
diff --git a/include/utils/FileMap.h b/include/utils/FileMap.h
index dfe6d51..f70fc92 100644
--- a/include/utils/FileMap.h
+++ b/include/utils/FileMap.h
@@ -24,7 +24,11 @@
#include <utils/Compat.h>
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
+// Ensure that we always pull in winsock2.h before windows.h
+#ifdef HAVE_WINSOCK
+#include <winsock2.h>
+#endif
#include <windows.h>
#endif
@@ -59,6 +63,8 @@
bool create(const char* origFileName, int fd,
off64_t offset, size_t length, bool readOnly);
+ ~FileMap(void);
+
/*
* Return the name of the file this map came from, if known.
*/
@@ -80,19 +86,6 @@
off64_t getDataOffset(void) const { return mDataOffset; }
/*
- * Get a "copy" of the object.
- */
- FileMap* acquire(void) { mRefCount++; return this; }
-
- /*
- * Call this when mapping is no longer needed.
- */
- void release(void) {
- if (--mRefCount <= 0)
- delete this;
- }
-
- /*
* This maps directly to madvise() values, but allows us to avoid
* including <sys/mman.h> everywhere.
*/
@@ -108,22 +101,19 @@
int advise(MapAdvice advice);
protected:
- // don't delete objects; call release()
- ~FileMap(void);
private:
// these are not implemented
FileMap(const FileMap& src);
const FileMap& operator=(const FileMap& src);
- int mRefCount; // reference count
char* mFileName; // original file name, if known
void* mBasePtr; // base of mmap area; page aligned
size_t mBaseLength; // length, measured from "mBasePtr"
off64_t mDataOffset; // offset used when map was created
void* mDataPtr; // start of requested data, offset from base
size_t mDataLength; // length, measured from "mDataPtr"
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
HANDLE mFileHandle; // Win32 file handle
HANDLE mFileMapping; // Win32 file mapping handle
#endif
diff --git a/include/utils/Functor.h b/include/utils/Functor.h
index e24ded4..09ea614 100644
--- a/include/utils/Functor.h
+++ b/include/utils/Functor.h
@@ -25,7 +25,7 @@
public:
Functor() {}
virtual ~Functor() {}
- virtual status_t operator ()(int what, void* data) { return NO_ERROR; }
+ virtual status_t operator ()(int /*what*/, void* /*data*/) { return NO_ERROR; }
};
}; // namespace android
diff --git a/include/utils/Looper.h b/include/utils/Looper.h
index 15c9891..a381251 100644
--- a/include/utils/Looper.h
+++ b/include/utils/Looper.h
@@ -386,11 +386,12 @@
void removeMessages(const sp<MessageHandler>& handler, int what);
/**
- * Return whether this looper's thread is currently idling -- that is, whether it
- * stopped waiting for more work to do. Note that this is intrinsically racy, since
- * its state can change before you get the result back.
+ * Returns whether this looper's thread is currently polling for more work to do.
+ * This is a good signal that the loop is still alive rather than being stuck
+ * handling a callback. Note that this method is intrinsically racy, since the
+ * state of the loop can change before you get the result back.
*/
- bool isIdling() const;
+ bool isPolling() const;
/**
* Prepares a looper associated with the calling thread, and returns it.
@@ -419,8 +420,12 @@
struct Request {
int fd;
int ident;
+ int events;
+ int seq;
sp<LooperCallback> callback;
void* data;
+
+ void initEventItem(struct epoll_event* eventItem) const;
};
struct Response {
@@ -451,12 +456,14 @@
// Whether we are currently waiting for work. Not protected by a lock,
// any use of it is racy anyway.
- volatile bool mIdling;
+ volatile bool mPolling;
- int mEpollFd; // immutable
+ int mEpollFd; // guarded by mLock but only modified on the looper thread
+ bool mEpollRebuildRequired; // guarded by mLock
// Locked list of file descriptor monitoring requests.
KeyedVector<int, Request> mRequests; // guarded by mLock
+ int mNextRequestSeq;
// This state is only used privately by pollOnce and does not require a lock since
// it runs on a single thread.
@@ -465,11 +472,15 @@
nsecs_t mNextMessageUptime; // set to LLONG_MAX when none
int pollInner(int timeoutMillis);
+ int removeFd(int fd, int seq);
void awoken();
void pushResponse(int events, const Request& request);
+ void rebuildEpollLocked();
+ void scheduleEpollRebuildLocked();
static void initTLSKey();
static void threadDestructor(void *st);
+ static void initEpollEvent(struct epoll_event* eventItem);
};
} // namespace android
diff --git a/include/utils/LruCache.h b/include/utils/LruCache.h
index fa8f03f..cd9d7f9 100644
--- a/include/utils/LruCache.h
+++ b/include/utils/LruCache.h
@@ -48,6 +48,7 @@
bool remove(const TKey& key);
bool removeOldest();
void clear();
+ const TValue& peekOldestValue();
class Iterator {
public:
@@ -56,7 +57,7 @@
bool next() {
mIndex = mCache.mTable->next(mIndex);
- return mIndex != -1;
+ return (ssize_t)mIndex != -1;
}
size_t index() const {
@@ -103,9 +104,13 @@
// Implementation is here, because it's fully templated
template <typename TKey, typename TValue>
-LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity),
- mNullValue(NULL), mTable(new BasicHashtable<TKey, Entry>), mYoungest(NULL), mOldest(NULL),
- mListener(NULL) {
+LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity)
+ : mTable(new BasicHashtable<TKey, Entry>)
+ , mListener(NULL)
+ , mOldest(NULL)
+ , mYoungest(NULL)
+ , mMaxCapacity(maxCapacity)
+ , mNullValue(NULL) {
};
template<typename K, typename V>
@@ -180,6 +185,14 @@
}
template <typename TKey, typename TValue>
+const TValue& LruCache<TKey, TValue>::peekOldestValue() {
+ if (mOldest) {
+ return mOldest->value;
+ }
+ return mNullValue;
+}
+
+template <typename TKey, typename TValue>
void LruCache<TKey, TValue>::clear() {
if (mListener) {
for (Entry* p = mOldest; p != NULL; p = p->child) {
diff --git a/include/utils/Mutex.h b/include/utils/Mutex.h
index dd201c8..757519b 100644
--- a/include/utils/Mutex.h
+++ b/include/utils/Mutex.h
@@ -21,11 +21,12 @@
#include <sys/types.h>
#include <time.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
#include <utils/Errors.h>
+#include <utils/Timers.h>
// ---------------------------------------------------------------------------
namespace android {
@@ -45,7 +46,7 @@
PRIVATE = 0,
SHARED = 1
};
-
+
Mutex();
Mutex(const char* name);
Mutex(int type, const char* name = NULL);
@@ -58,6 +59,16 @@
// lock if possible; returns 0 on success, error otherwise
status_t tryLock();
+#if HAVE_ANDROID_OS
+ // lock the mutex, but don't wait longer than timeoutMilliseconds.
+ // Returns 0 on success, TIMED_OUT for failure due to timeout expiration.
+ //
+ // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep
+ // capabilities consistent across host OSes, this method is only available
+ // when building Android binaries.
+ status_t timedLock(nsecs_t timeoutMilliseconds);
+#endif
+
// Manages the mutex automatically. It'll be locked when Autolock is
// constructed and released when Autolock goes out of scope.
class Autolock {
@@ -71,12 +82,12 @@
private:
friend class Condition;
-
+
// A mutex cannot be copied
Mutex(const Mutex&);
Mutex& operator = (const Mutex&);
-
-#if defined(HAVE_PTHREADS)
+
+#if !defined(_WIN32)
pthread_mutex_t mMutex;
#else
void _init();
@@ -86,7 +97,7 @@
// ---------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
inline Mutex::Mutex() {
pthread_mutex_init(&mMutex, NULL);
@@ -117,8 +128,17 @@
inline status_t Mutex::tryLock() {
return -pthread_mutex_trylock(&mMutex);
}
+#if HAVE_ANDROID_OS
+inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
+ const struct timespec ts = {
+ /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
+ /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
+ };
+ return -pthread_mutex_timedlock(&mMutex, &ts);
+}
+#endif
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
// ---------------------------------------------------------------------------
@@ -127,7 +147,7 @@
* When the function returns, it will go out of scope, and release the
* mutex.
*/
-
+
typedef Mutex::Autolock AutoMutex;
// ---------------------------------------------------------------------------
diff --git a/include/utils/NativeHandle.h b/include/utils/NativeHandle.h
new file mode 100644
index 0000000..b825168
--- /dev/null
+++ b/include/utils/NativeHandle.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2014 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 ANDROID_NATIVE_HANDLE_H
+#define ANDROID_NATIVE_HANDLE_H
+
+#include <utils/RefBase.h>
+#include <utils/StrongPointer.h>
+
+typedef struct native_handle native_handle_t;
+
+namespace android {
+
+class NativeHandle: public LightRefBase<NativeHandle> {
+public:
+ // Create a refcounted wrapper around a native_handle_t, and declare
+ // whether the wrapper owns the handle (so that it should clean up the
+ // handle upon destruction) or not.
+ // If handle is NULL, no NativeHandle will be created.
+ static sp<NativeHandle> create(native_handle_t* handle, bool ownsHandle);
+
+ const native_handle_t* handle() const {
+ return mHandle;
+ }
+
+private:
+ // for access to the destructor
+ friend class LightRefBase<NativeHandle>;
+
+ NativeHandle(native_handle_t* handle, bool ownsHandle);
+ virtual ~NativeHandle();
+
+ native_handle_t* mHandle;
+ bool mOwnsHandle;
+
+ // non-copyable
+ NativeHandle(const NativeHandle&);
+ NativeHandle& operator=(const NativeHandle&);
+};
+
+} // namespace android
+
+#endif // ANDROID_NATIVE_HANDLE_H
diff --git a/include/utils/RWLock.h b/include/utils/RWLock.h
index 90beb5f..e743b1c 100644
--- a/include/utils/RWLock.h
+++ b/include/utils/RWLock.h
@@ -20,7 +20,7 @@
#include <stdint.h>
#include <sys/types.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
@@ -31,7 +31,7 @@
namespace android {
// ---------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
/*
* Simple mutex class. The implementation is system-dependent.
@@ -117,7 +117,7 @@
pthread_rwlock_unlock(&mRWLock);
}
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
// ---------------------------------------------------------------------------
}; // namespace android
diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h
index cbfe13a..eac6a78 100644
--- a/include/utils/RefBase.h
+++ b/include/utils/RefBase.h
@@ -203,6 +203,13 @@
mutable volatile int32_t mCount;
};
+// This is a wrapper around LightRefBase that simply enforces a virtual
+// destructor to eliminate the template requirement of LightRefBase
+class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
+public:
+ virtual ~VirtualLightRefBase() {}
+};
+
// ---------------------------------------------------------------------------
template <typename T>
@@ -484,7 +491,8 @@
TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
}
public:
- Renamer(sp<TYPE>* d, sp<TYPE> const* s) : s(s), d(d) { }
+ Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { }
+ virtual ~Renamer() { }
};
memmove(d, s, n*sizeof(sp<TYPE>));
@@ -503,7 +511,8 @@
TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
}
public:
- Renamer(wp<TYPE>* d, wp<TYPE> const* s) : s(s), d(d) { }
+ Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { }
+ virtual ~Renamer() { }
};
memmove(d, s, n*sizeof(wp<TYPE>));
diff --git a/include/utils/Singleton.h b/include/utils/Singleton.h
index c60680e..ffc03cb 100644
--- a/include/utils/Singleton.h
+++ b/include/utils/Singleton.h
@@ -65,9 +65,10 @@
*/
#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \
- template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE); \
- template<> TYPE* Singleton< TYPE >::sInstance(0); \
- template class Singleton< TYPE >;
+ template<> ::android::Mutex \
+ (::android::Singleton< TYPE >::sLock)(::android::Mutex::PRIVATE); \
+ template<> TYPE* ::android::Singleton< TYPE >::sInstance(0); \
+ template class ::android::Singleton< TYPE >;
// ---------------------------------------------------------------------------
diff --git a/include/utils/String8.h b/include/utils/String8.h
index ef59470..ecfcf10 100644
--- a/include/utils/String8.h
+++ b/include/utils/String8.h
@@ -130,11 +130,19 @@
// start, or -1 if not found
ssize_t find(const char* other, size_t start = 0) const;
+ // return true if this string contains the specified substring
+ inline bool contains(const char* other) const;
+
+ // removes all occurrence of the specified substring
+ // returns true if any were found and removed
+ bool removeAll(const char* other);
+
void toLower();
void toLower(size_t start, size_t numChars);
void toUpper();
void toUpper(size_t start, size_t numChars);
+
/*
* These methods operate on the string as if it were a path name.
*/
@@ -280,6 +288,11 @@
return SharedBuffer::bufferFromData(mString);
}
+inline bool String8::contains(const char* other) const
+{
+ return find(other) >= 0;
+}
+
inline String8& String8::operator=(const String8& other)
{
setTo(other);
diff --git a/include/utils/Thread.h b/include/utils/Thread.h
index df30611..28839fd 100644
--- a/include/utils/Thread.h
+++ b/include/utils/Thread.h
@@ -21,7 +21,7 @@
#include <sys/types.h>
#include <time.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
@@ -71,8 +71,8 @@
bool isRunning() const;
#ifdef HAVE_ANDROID_OS
- // Return the thread's kernel ID, same as the thread itself calling gettid() or
- // androidGetTid(), or -1 if the thread is not running.
+ // Return the thread's kernel ID, same as the thread itself calling gettid(),
+ // or -1 if the thread is not running.
pid_t getTid() const;
#endif
diff --git a/include/utils/Unicode.h b/include/utils/Unicode.h
index c8c87c3..b76a5e2 100644
--- a/include/utils/Unicode.h
+++ b/include/utils/Unicode.h
@@ -22,9 +22,6 @@
extern "C" {
-typedef uint32_t char32_t;
-typedef uint16_t char16_t;
-
// Standard string functions on char16_t strings.
int strcmp16(const char16_t *, const char16_t *);
int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
diff --git a/include/ziparchive/zip_archive.h b/include/ziparchive/zip_archive.h
index 1877494..386a390 100644
--- a/include/ziparchive/zip_archive.h
+++ b/include/ziparchive/zip_archive.h
@@ -21,6 +21,7 @@
#define LIBZIPARCHIVE_ZIPARCHIVE_H_
#include <stdint.h>
+#include <string.h>
#include <sys/types.h>
#include <utils/Compat.h>
@@ -33,8 +34,16 @@
};
struct ZipEntryName {
- const char* name;
+ const uint8_t* name;
uint16_t name_length;
+
+ ZipEntryName() {}
+
+ /*
+ * entry_name has to be an c-style string with only ASCII characters.
+ */
+ explicit ZipEntryName(const char* entry_name)
+ : name(reinterpret_cast<const uint8_t*>(entry_name)), name_length(strlen(entry_name)) {}
};
/*
@@ -92,6 +101,9 @@
* Sets handle to the value of the opaque handle for this file descriptor.
* This handle must be released by calling CloseArchive with this handle.
*
+ * If assume_ownership parameter is 'true' calling CloseArchive will close
+ * the file.
+ *
* This function maps and scans the central directory and builds a table
* of entries for future lookups.
*
@@ -100,7 +112,7 @@
* Returns 0 on success, and negative values on failure.
*/
int32_t OpenArchiveFd(const int fd, const char* debugFileName,
- ZipArchiveHandle *handle);
+ ZipArchiveHandle *handle, bool assume_ownership = true);
/*
* Close archive, releasing resources associated with it. This will
@@ -124,24 +136,24 @@
* and length, a call to VerifyCrcAndLengths must be made after entry data
* has been processed.
*/
-int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
+int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
ZipEntry* data);
/*
* Start iterating over all entries of a zip file. The order of iteration
* is not guaranteed to be the same as the order of elements
- * in the central directory but is stable for a given zip file. |cookie|
- * must point to a writeable memory location, and will be set to the value
- * of an opaque cookie which can be used to make one or more calls to
- * Next.
+ * in the central directory but is stable for a given zip file. |cookie| will
+ * contain the value of an opaque cookie which can be used to make one or more
+ * calls to Next. All calls to StartIteration must be matched by a call to
+ * EndIteration to free any allocated memory.
*
* This method also accepts an optional prefix to restrict iteration to
- * entry names that start with |prefix|.
+ * entry names that start with |optional_prefix|.
*
* Returns 0 on success and negative values on failure.
*/
int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
- const char* prefix);
+ const ZipEntryName* optional_prefix);
/*
* Advance to the next element in the zipfile in iteration order.
@@ -152,6 +164,12 @@
int32_t Next(void* cookie, ZipEntry* data, ZipEntryName *name);
/*
+ * End iteration over all entries of a zip file and frees the memory allocated
+ * in StartIteration.
+ */
+void EndIteration(void* cookie);
+
+/*
* Uncompress and write an entry to an open file identified by |fd|.
* |entry->uncompressed_length| bytes will be written to the file at
* its current offset, and the file will be truncated at the end of
diff --git a/init/Android.mk b/init/Android.mk
index 1f43ba6..5b8094f 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -1,31 +1,50 @@
# Copyright 2005 The Android Open Source Project
LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= \
- builtins.c \
- init.c \
- devices.c \
- property_service.c \
- util.c \
- parser.c \
- keychords.c \
- signal_handler.c \
- init_parser.c \
- ueventd.c \
- ueventd_parser.c \
- watchdogd.c
-
-ifeq ($(strip $(INIT_BOOTCHART)),true)
-LOCAL_SRC_FILES += bootchart.c
-LOCAL_CFLAGS += -DBOOTCHART=1
-endif
+# --
ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-LOCAL_CFLAGS += -DALLOW_LOCAL_PROP_OVERRIDE=1
+init_options += -DALLOW_LOCAL_PROP_OVERRIDE=1 -DALLOW_DISABLE_SELINUX=1
+else
+init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=0
endif
+init_options += -DLOG_UEVENTS=0
+
+init_cflags += \
+ $(init_options) \
+ -Wall -Wextra \
+ -Wno-unused-parameter \
+ -Werror \
+
+# --
+
+include $(CLEAR_VARS)
+LOCAL_CPPFLAGS := $(init_cflags)
+LOCAL_SRC_FILES:= \
+ init_parser.cpp \
+ parser.cpp \
+ util.cpp \
+
+LOCAL_STATIC_LIBRARIES := libbase
+LOCAL_MODULE := libinit
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_CPPFLAGS := $(init_cflags)
+LOCAL_SRC_FILES:= \
+ bootchart.cpp \
+ builtins.cpp \
+ devices.cpp \
+ init.cpp \
+ keychords.cpp \
+ property_service.cpp \
+ signal_handler.cpp \
+ ueventd.cpp \
+ ueventd_parser.cpp \
+ watchdogd.cpp \
+
LOCAL_MODULE:= init
LOCAL_FORCE_STATIC_EXECUTABLE := true
@@ -33,32 +52,36 @@
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
LOCAL_STATIC_LIBRARIES := \
- libfs_mgr \
- liblogwrap \
- libcutils \
- liblog \
- libc \
- libselinux \
- libmincrypt \
- libext4_utils_static
+ libinit \
+ libfs_mgr \
+ liblogwrap \
+ libcutils \
+ libbase \
+ liblog \
+ libc \
+ libselinux \
+ libmincrypt \
+ libext4_utils_static
+
+# Create symlinks
+LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
+ ln -sf ../init $(TARGET_ROOT_OUT)/sbin/ueventd; \
+ ln -sf ../init $(TARGET_ROOT_OUT)/sbin/watchdogd
include $(BUILD_EXECUTABLE)
-# Make a symlink from /sbin/ueventd and /sbin/watchdogd to /init
-SYMLINKS := \
- $(TARGET_ROOT_OUT)/sbin/ueventd \
- $(TARGET_ROOT_OUT)/sbin/watchdogd
-$(SYMLINKS): INIT_BINARY := $(LOCAL_MODULE)
-$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
- @echo "Symlink: $@ -> ../$(INIT_BINARY)"
- @mkdir -p $(dir $@)
- @rm -rf $@
- $(hide) ln -sf ../$(INIT_BINARY) $@
-ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
- $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
+include $(CLEAR_VARS)
+LOCAL_MODULE := init_tests
+LOCAL_SRC_FILES := \
+ init_parser_test.cpp \
+ util_test.cpp \
+
+LOCAL_SHARED_LIBRARIES += \
+ libcutils \
+ libbase \
+
+LOCAL_STATIC_LIBRARIES := libinit
+include $(BUILD_NATIVE_TEST)
diff --git a/init/README.BOOTCHART b/init/README.BOOTCHART
deleted file mode 100644
index 70cf2c3..0000000
--- a/init/README.BOOTCHART
+++ /dev/null
@@ -1,52 +0,0 @@
-This version of init contains code to perform "bootcharting", i.e. generating log
-files that can be later processed by the tools provided by www.bootchart.org.
-
-To activate it, you need to define build 'init' with the INIT_BOOTCHART environment
-variable defined to 'true', for example:
-
- touch system/init/init.c
- m INIT_BOOTCHART=true
-
-On the emulator, use the new -bootchart <timeout> option to boot with bootcharting
-activated for <timeout> seconds.
-
-Otherwise, flash your device, and start it. Then create a file on the /data partition
-with a command like the following:
-
- adb shell 'echo $TIMEOUT > /data/bootchart-start'
-
-Where the value of $TIMEOUT corresponds to the wanted bootcharted period in seconds;
-for example, to bootchart for 2 minutes, do:
-
- adb shell 'echo 120 > /data/bootchart-start'
-
-Reboot your device, bootcharting will begin and stop after the period you gave.
-You can also stop the bootcharting at any moment by doing the following:
-
- adb shell 'echo 1 > /data/bootchart-stop'
-
-Note that /data/bootchart-stop is deleted automatically by init at the end of the
-bootcharting. This is not the case of /data/bootchart-start, so don't forget to delete it
-when you're done collecting data:
-
- adb shell rm /data/bootchart-start
-
-The log files are placed in /data/bootchart/. you must run the script tools/grab-bootchart.sh
-which will use ADB to retrieve them and create a bootchart.tgz file that can be used with
-the bootchart parser/renderer, or even uploaded directly to the form located at:
-
- http://www.bootchart.org/download.html
-
-NOTE: the bootchart.org webform doesn't seem to work at the moment, you can generate an
- image on your machine by doing the following:
-
- 1/ download the sources from www.bootchart.org
- 2/ unpack them
- 3/ in the source directory, type 'ant' to build the bootchart program
- 4/ type 'java -jar bootchart.jar /path/to/bootchart.tgz
-
-technical note:
-
-this implementation of bootcharting does use the 'bootchartd' script provided by
-www.bootchart.org, but a C re-implementation that is directly compiled into our init
-program.
diff --git a/init/bootchart.c b/init/bootchart.c
deleted file mode 100644
index f72fcaa..0000000
--- a/init/bootchart.c
+++ /dev/null
@@ -1,378 +0,0 @@
-/*
- * Copyright (C) 2008 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.
- */
-
-/* this code is used to generate a boot sequence profile that can be used
- * with the 'bootchart' graphics generation tool. see www.bootchart.org
- * note that unlike the original bootchartd, this is not a Bash script but
- * some C code that is run right from the init script.
- */
-
-#include <stdio.h>
-#include <time.h>
-#include <dirent.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include "bootchart.h"
-
-#define VERSION "0.8"
-#define SAMPLE_PERIOD 0.2
-#define LOG_ROOT "/data/bootchart"
-#define LOG_STAT LOG_ROOT"/proc_stat.log"
-#define LOG_PROCS LOG_ROOT"/proc_ps.log"
-#define LOG_DISK LOG_ROOT"/proc_diskstats.log"
-#define LOG_HEADER LOG_ROOT"/header"
-#define LOG_ACCT LOG_ROOT"/kernel_pacct"
-
-#define LOG_STARTFILE "/data/bootchart-start"
-#define LOG_STOPFILE "/data/bootchart-stop"
-
-static int
-unix_read(int fd, void* buff, int len)
-{
- int ret;
- do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
- return ret;
-}
-
-static int
-unix_write(int fd, const void* buff, int len)
-{
- int ret;
- do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
- return ret;
-}
-
-static int
-proc_read(const char* filename, char* buff, size_t buffsize)
-{
- int len = 0;
- int fd = open(filename, O_RDONLY);
- if (fd >= 0) {
- len = unix_read(fd, buff, buffsize-1);
- close(fd);
- }
- buff[len > 0 ? len : 0] = 0;
- return len;
-}
-
-#define FILE_BUFF_SIZE 65536
-
-typedef struct {
- int count;
- int fd;
- char data[FILE_BUFF_SIZE];
-} FileBuffRec, *FileBuff;
-
-static void
-file_buff_open( FileBuff buff, const char* path )
-{
- buff->count = 0;
- buff->fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0755);
-}
-
-static void
-file_buff_write( FileBuff buff, const void* src, int len )
-{
- while (len > 0) {
- int avail = sizeof(buff->data) - buff->count;
- if (avail > len)
- avail = len;
-
- memcpy( buff->data + buff->count, src, avail );
- len -= avail;
- src = (char*)src + avail;
-
- buff->count += avail;
- if (buff->count == FILE_BUFF_SIZE) {
- unix_write( buff->fd, buff->data, buff->count );
- buff->count = 0;
- }
- }
-}
-
-static void
-file_buff_done( FileBuff buff )
-{
- if (buff->count > 0) {
- unix_write( buff->fd, buff->data, buff->count );
- buff->count = 0;
- }
-}
-
-static void
-log_header(void)
-{
- FILE* out;
- char cmdline[1024];
- char uname[128];
- char cpuinfo[128];
- char* cpu;
- char date[32];
- time_t now_t = time(NULL);
- struct tm now = *localtime(&now_t);
- strftime(date, sizeof(date), "%x %X", &now);
-
- out = fopen( LOG_HEADER, "w" );
- if (out == NULL)
- return;
-
- proc_read("/proc/cmdline", cmdline, sizeof(cmdline));
- proc_read("/proc/version", uname, sizeof(uname));
- proc_read("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo));
-
- cpu = strchr( cpuinfo, ':' );
- if (cpu) {
- char* p = strchr(cpu, '\n');
- cpu += 2;
- if (p)
- *p = 0;
- }
-
- fprintf(out, "version = %s\n", VERSION);
- fprintf(out, "title = Boot chart for Android ( %s )\n", date);
- fprintf(out, "system.uname = %s\n", uname);
- fprintf(out, "system.release = 0.0\n");
- fprintf(out, "system.cpu = %s\n", cpu);
- fprintf(out, "system.kernel.options = %s\n", cmdline);
- fclose(out);
-}
-
-static void
-close_on_exec(int fd)
-{
- fcntl(fd, F_SETFD, FD_CLOEXEC);
-}
-
-static void
-open_log_file(int* plogfd, const char* logfile)
-{
- int logfd = *plogfd;
-
- /* create log file if needed */
- if (logfd < 0)
- {
- logfd = open(logfile,O_WRONLY|O_CREAT|O_TRUNC,0755);
- if (logfd < 0) {
- *plogfd = -2;
- return;
- }
- close_on_exec(logfd);
- *plogfd = logfd;
- }
-}
-
-static void
-do_log_uptime(FileBuff log)
-{
- char buff[65];
- int fd, ret, len;
-
- fd = open("/proc/uptime",O_RDONLY);
- if (fd >= 0) {
- int ret;
- ret = unix_read(fd, buff, 64);
- close(fd);
- buff[64] = 0;
- if (ret >= 0) {
- long long jiffies = 100LL*strtod(buff,NULL);
- int len;
- snprintf(buff,sizeof(buff),"%lld\n",jiffies);
- len = strlen(buff);
- file_buff_write(log, buff, len);
- }
- }
-}
-
-static void
-do_log_ln(FileBuff log)
-{
- file_buff_write(log, "\n", 1);
-}
-
-
-static void
-do_log_file(FileBuff log, const char* procfile)
-{
- char buff[1024];
- int fd;
-
- do_log_uptime(log);
-
- /* append file content */
- fd = open(procfile,O_RDONLY);
- if (fd >= 0) {
- close_on_exec(fd);
- for (;;) {
- int ret;
- ret = unix_read(fd, buff, sizeof(buff));
- if (ret <= 0)
- break;
-
- file_buff_write(log, buff, ret);
- if (ret < (int)sizeof(buff))
- break;
- }
- close(fd);
- }
-
- do_log_ln(log);
-}
-
-static void
-do_log_procs(FileBuff log)
-{
- DIR* dir = opendir("/proc");
- struct dirent* entry;
-
- do_log_uptime(log);
-
- while ((entry = readdir(dir)) != NULL) {
- /* only match numeric values */
- char* end;
- int pid = strtol( entry->d_name, &end, 10);
- if (end != NULL && end > entry->d_name && *end == 0) {
- char filename[32];
- char buff[1024];
- char cmdline[1024];
- int len;
- int fd;
-
- /* read command line and extract program name */
- snprintf(filename,sizeof(filename),"/proc/%d/cmdline",pid);
- proc_read(filename, cmdline, sizeof(cmdline));
-
- /* read process stat line */
- snprintf(filename,sizeof(filename),"/proc/%d/stat",pid);
- fd = open(filename,O_RDONLY);
- if (fd >= 0) {
- len = unix_read(fd, buff, sizeof(buff)-1);
- close(fd);
- if (len > 0) {
- int len2 = strlen(cmdline);
- if (len2 > 0) {
- /* we want to substitute the process name with its real name */
- const char* p1;
- const char* p2;
- buff[len] = 0;
- p1 = strchr(buff, '(');
- p2 = strchr(p1, ')');
- file_buff_write(log, buff, p1+1-buff);
- file_buff_write(log, cmdline, strlen(cmdline));
- file_buff_write(log, p2, strlen(p2));
- } else {
- /* no substitution */
- file_buff_write(log,buff,len);
- }
- }
- }
- }
- }
- closedir(dir);
- do_log_ln(log);
-}
-
-static FileBuffRec log_stat[1];
-static FileBuffRec log_procs[1];
-static FileBuffRec log_disks[1];
-
-/* called to setup bootcharting */
-int bootchart_init( void )
-{
- int ret;
- char buff[4];
- int timeout = 0, count = 0;
-
- buff[0] = 0;
- proc_read( LOG_STARTFILE, buff, sizeof(buff) );
- if (buff[0] != 0) {
- timeout = atoi(buff);
- }
- else {
- /* when running with emulator, androidboot.bootchart=<timeout>
- * might be passed by as kernel parameters to specify the bootchart
- * timeout. this is useful when using -wipe-data since the /data
- * partition is fresh
- */
- char cmdline[1024];
- char* s;
-#define KERNEL_OPTION "androidboot.bootchart="
- proc_read( "/proc/cmdline", cmdline, sizeof(cmdline) );
- s = strstr(cmdline, KERNEL_OPTION);
- if (s) {
- s += sizeof(KERNEL_OPTION)-1;
- timeout = atoi(s);
- }
- }
- if (timeout == 0)
- return 0;
-
- if (timeout > BOOTCHART_MAX_TIME_SEC)
- timeout = BOOTCHART_MAX_TIME_SEC;
-
- count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
-
- do {ret=mkdir(LOG_ROOT,0755);}while (ret < 0 && errno == EINTR);
-
- file_buff_open(log_stat, LOG_STAT);
- file_buff_open(log_procs, LOG_PROCS);
- file_buff_open(log_disks, LOG_DISK);
-
- /* create kernel process accounting file */
- {
- int fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC,0644);
- if (fd >= 0) {
- close(fd);
- acct( LOG_ACCT );
- }
- }
-
- log_header();
- return count;
-}
-
-/* called each time you want to perform a bootchart sampling op */
-int bootchart_step( void )
-{
- do_log_file(log_stat, "/proc/stat");
- do_log_file(log_disks, "/proc/diskstats");
- do_log_procs(log_procs);
-
- /* we stop when /data/bootchart-stop contains 1 */
- {
- char buff[2];
- if (proc_read(LOG_STOPFILE,buff,sizeof(buff)) > 0 && buff[0] == '1') {
- return -1;
- }
- }
-
- return 0;
-}
-
-void bootchart_finish( void )
-{
- unlink( LOG_STOPFILE );
- file_buff_done(log_stat);
- file_buff_done(log_disks);
- file_buff_done(log_procs);
- acct(NULL);
-}
diff --git a/init/bootchart.cpp b/init/bootchart.cpp
new file mode 100644
index 0000000..530eba8
--- /dev/null
+++ b/init/bootchart.cpp
@@ -0,0 +1,277 @@
+/*
+ * Copyright (C) 2008 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 "bootchart.h"
+#include "keywords.h"
+#include "log.h"
+#include "property_service.h"
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/utsname.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <base/file.h>
+
+#define LOG_ROOT "/data/bootchart"
+#define LOG_STAT LOG_ROOT"/proc_stat.log"
+#define LOG_PROCS LOG_ROOT"/proc_ps.log"
+#define LOG_DISK LOG_ROOT"/proc_diskstats.log"
+#define LOG_HEADER LOG_ROOT"/header"
+#define LOG_ACCT LOG_ROOT"/kernel_pacct"
+
+#define LOG_STARTFILE LOG_ROOT"/start"
+#define LOG_STOPFILE LOG_ROOT"/stop"
+
+// Polling period in ms.
+static const int BOOTCHART_POLLING_MS = 200;
+
+// Max polling time in seconds.
+static const int BOOTCHART_MAX_TIME_SEC = 10*60;
+
+static long long g_last_bootchart_time;
+static int g_remaining_samples;
+
+static FILE* log_stat;
+static FILE* log_procs;
+static FILE* log_disks;
+
+static long long get_uptime_jiffies() {
+ std::string uptime;
+ if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
+ return 0;
+ }
+ return 100LL * strtod(uptime.c_str(), NULL);
+}
+
+static void log_header() {
+ char date[32];
+ time_t now_t = time(NULL);
+ struct tm now = *localtime(&now_t);
+ strftime(date, sizeof(date), "%F %T", &now);
+
+ utsname uts;
+ if (uname(&uts) == -1) {
+ return;
+ }
+
+ char fingerprint[PROP_VALUE_MAX];
+ if (property_get("ro.build.fingerprint", fingerprint) == -1) {
+ return;
+ }
+
+ std::string kernel_cmdline;
+ android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
+
+ FILE* out = fopen(LOG_HEADER, "we");
+ if (out == NULL) {
+ return;
+ }
+ fprintf(out, "version = Android init 0.8 " __TIME__ "\n");
+ fprintf(out, "title = Boot chart for Android (%s)\n", date);
+ fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
+ fprintf(out, "system.release = %s\n", fingerprint);
+ // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
+ fprintf(out, "system.cpu = %s\n", uts.machine);
+ fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str());
+ fclose(out);
+}
+
+static void do_log_uptime(FILE* log) {
+ fprintf(log, "%lld\n", get_uptime_jiffies());
+}
+
+static void do_log_file(FILE* log, const char* procfile) {
+ do_log_uptime(log);
+
+ std::string content;
+ if (android::base::ReadFileToString(procfile, &content)) {
+ fprintf(log, "%s\n", content.c_str());
+ }
+}
+
+static void do_log_procs(FILE* log) {
+ do_log_uptime(log);
+
+ DIR* dir = opendir("/proc");
+ struct dirent* entry;
+ while ((entry = readdir(dir)) != NULL) {
+ // Only match numeric values.
+ char* end;
+ int pid = strtol(entry->d_name, &end, 10);
+ if (end != NULL && end > entry->d_name && *end == 0) {
+ char filename[32];
+
+ // /proc/<pid>/stat only has truncated task names, so get the full
+ // name from /proc/<pid>/cmdline.
+ snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
+ std::string cmdline;
+ android::base::ReadFileToString(filename, &cmdline);
+ const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
+
+ // Read process stat line.
+ snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
+ std::string stat;
+ if (android::base::ReadFileToString(filename, &stat)) {
+ if (!cmdline.empty()) {
+ // Substitute the process name with its real name.
+ size_t open = stat.find('(');
+ size_t close = stat.find_last_of(')');
+ if (open != std::string::npos && close != std::string::npos) {
+ stat.replace(open + 1, close - open - 1, full_name);
+ }
+ }
+ fputs(stat.c_str(), log);
+ }
+ }
+ }
+ closedir(dir);
+
+ fputc('\n', log);
+}
+
+static int bootchart_init() {
+ int timeout = 0;
+
+ std::string start;
+ android::base::ReadFileToString(LOG_STARTFILE, &start);
+ if (!start.empty()) {
+ timeout = atoi(start.c_str());
+ } else {
+ // When running with emulator, androidboot.bootchart=<timeout>
+ // might be passed by as kernel parameters to specify the bootchart
+ // timeout. this is useful when using -wipe-data since the /data
+ // partition is fresh.
+ std::string cmdline;
+ android::base::ReadFileToString("/proc/cmdline", &cmdline);
+#define KERNEL_OPTION "androidboot.bootchart="
+ if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
+ timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
+ }
+ }
+ if (timeout == 0)
+ return 0;
+
+ if (timeout > BOOTCHART_MAX_TIME_SEC)
+ timeout = BOOTCHART_MAX_TIME_SEC;
+
+ int count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
+
+ log_stat = fopen(LOG_STAT, "we");
+ if (log_stat == NULL) {
+ return -1;
+ }
+ log_procs = fopen(LOG_PROCS, "we");
+ if (log_procs == NULL) {
+ fclose(log_stat);
+ return -1;
+ }
+ log_disks = fopen(LOG_DISK, "we");
+ if (log_disks == NULL) {
+ fclose(log_stat);
+ fclose(log_procs);
+ return -1;
+ }
+
+ // Create kernel process accounting file.
+ {
+ int fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC,0644);
+ if (fd >= 0) {
+ close(fd);
+ acct( LOG_ACCT );
+ }
+ }
+
+ log_header();
+ return count;
+}
+
+int do_bootchart_init(int nargs, char** args) {
+ g_remaining_samples = bootchart_init();
+ if (g_remaining_samples < 0) {
+ ERROR("bootcharting init failure: %s\n", strerror(errno));
+ } else if (g_remaining_samples > 0) {
+ NOTICE("bootcharting started (will run for %d ms)\n", g_remaining_samples*BOOTCHART_POLLING_MS);
+ } else {
+ NOTICE("bootcharting ignored\n");
+ }
+ return 0;
+}
+
+static int bootchart_step() {
+ do_log_file(log_stat, "/proc/stat");
+ do_log_file(log_disks, "/proc/diskstats");
+ do_log_procs(log_procs);
+
+ // Stop if /data/bootchart/stop contains 1.
+ std::string stop;
+ if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
+ return -1;
+ }
+
+ return 0;
+}
+
+/* called to get time (in ms) used by bootchart */
+static long long bootchart_gettime() {
+ return 10LL*get_uptime_jiffies();
+}
+
+static void bootchart_finish() {
+ unlink(LOG_STOPFILE);
+ fclose(log_stat);
+ fclose(log_disks);
+ fclose(log_procs);
+ acct(NULL);
+}
+
+void bootchart_sample(int* timeout) {
+ // Do we have any more bootcharting to do?
+ if (g_remaining_samples <= 0) {
+ return;
+ }
+
+ long long current_time = bootchart_gettime();
+ int elapsed_time = current_time - g_last_bootchart_time;
+
+ if (elapsed_time >= BOOTCHART_POLLING_MS) {
+ /* count missed samples */
+ while (elapsed_time >= BOOTCHART_POLLING_MS) {
+ elapsed_time -= BOOTCHART_POLLING_MS;
+ g_remaining_samples--;
+ }
+ /* count may be negative, take a sample anyway */
+ g_last_bootchart_time = current_time;
+ if (bootchart_step() < 0 || g_remaining_samples <= 0) {
+ bootchart_finish();
+ g_remaining_samples = 0;
+ }
+ }
+ if (g_remaining_samples > 0) {
+ int remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
+ if (*timeout < 0 || *timeout > remaining_time) {
+ *timeout = remaining_time;
+ }
+ }
+}
diff --git a/init/bootchart.h b/init/bootchart.h
index 39d2d4f..cf61d83 100644
--- a/init/bootchart.h
+++ b/init/bootchart.h
@@ -17,20 +17,6 @@
#ifndef _BOOTCHART_H
#define _BOOTCHART_H
-#ifndef BOOTCHART
-# define BOOTCHART 0
-#endif
-
-#if BOOTCHART
-
-extern int bootchart_init(void);
-extern int bootchart_step(void);
-extern void bootchart_finish(void);
-
-# define BOOTCHART_POLLING_MS 200 /* polling period in ms */
-# define BOOTCHART_DEFAULT_TIME_SEC (2*60) /* default polling time in seconds */
-# define BOOTCHART_MAX_TIME_SEC (10*60) /* max polling time in seconds */
-
-#endif /* BOOTCHART */
+void bootchart_sample(int* timeout);
#endif /* _BOOTCHART_H */
diff --git a/init/builtins.c b/init/builtins.cpp
similarity index 70%
rename from init/builtins.c
rename to init/builtins.cpp
index e2932d5..fb1aa7c 100644
--- a/init/builtins.c
+++ b/init/builtins.cpp
@@ -29,11 +29,11 @@
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/resource.h>
+#include <sys/time.h>
#include <sys/wait.h>
#include <linux/loop.h>
#include <cutils/partition_utils.h>
#include <cutils/android_reboot.h>
-#include <sys/system_properties.h>
#include <fs_mgr.h>
#include <selinux/selinux.h>
@@ -49,119 +49,22 @@
#include <private/android_filesystem_config.h>
-void add_environment(const char *name, const char *value);
+#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
-extern int init_module(void *, unsigned long, const char *);
+int add_environment(const char *name, const char *value);
-static int write_file(const char *path, const char *value)
-{
- int fd, ret, len;
-
- fd = open(path, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600);
-
- if (fd < 0)
- return -errno;
-
- len = strlen(value);
-
- do {
- ret = write(fd, value, len);
- } while (ret < 0 && errno == EINTR);
-
- close(fd);
- if (ret < 0) {
- return -errno;
- } else {
- return 0;
- }
-}
-
-static int _open(const char *path)
-{
- int fd;
-
- fd = open(path, O_RDONLY | O_NOFOLLOW);
- if (fd < 0)
- fd = open(path, O_WRONLY | O_NOFOLLOW);
-
- return fd;
-}
-
-static int _chown(const char *path, unsigned int uid, unsigned int gid)
-{
- int fd;
- int ret;
-
- fd = _open(path);
- if (fd < 0) {
- return -1;
- }
-
- ret = fchown(fd, uid, gid);
- if (ret < 0) {
- int errno_copy = errno;
- close(fd);
- errno = errno_copy;
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-static int _chmod(const char *path, mode_t mode)
-{
- int fd;
- int ret;
-
- fd = _open(path);
- if (fd < 0) {
- return -1;
- }
-
- ret = fchmod(fd, mode);
- if (ret < 0) {
- int errno_copy = errno;
- close(fd);
- errno = errno_copy;
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
+// System call provided by bionic but not in any header file.
+extern "C" int init_module(void *, unsigned long, const char *);
static int insmod(const char *filename, char *options)
{
- void *module;
- unsigned size;
- int ret;
-
- module = read_file(filename, &size);
- if (!module)
+ std::string module;
+ if (!read_file(filename, &module)) {
return -1;
+ }
- ret = init_module(module, size, options);
-
- free(module);
-
- return ret;
-}
-
-static int setkey(struct kbentry *kbe)
-{
- int fd, ret;
-
- fd = open("/dev/tty0", O_RDWR | O_SYNC);
- if (fd < 0)
- return -1;
-
- ret = ioctl(fd, KDSKBENT, kbe);
-
- close(fd);
- return ret;
+ // TODO: use finit_module for >= 3.8 kernels.
+ return init_module(&module[0], module.size(), options);
}
static int __ifupdown(const char *interface, int up)
@@ -186,7 +89,7 @@
ifr.ifr_flags &= ~IFF_UP;
ret = ioctl(s, SIOCSIFFLAGS, &ifr);
-
+
done:
close(s);
return ret;
@@ -196,21 +99,11 @@
{
if (!(svc->flags & SVC_DISABLED)) {
service_start(svc, NULL);
+ } else {
+ svc->flags |= SVC_DISABLED_START;
}
}
-int do_chdir(int nargs, char **args)
-{
- chdir(args[1]);
- return 0;
-}
-
-int do_chroot(int nargs, char **args)
-{
- chroot(args[1]);
- return 0;
-}
-
int do_class_start(int nargs, char **args)
{
/* Starting a class does not start services
@@ -238,15 +131,95 @@
return write_file("/proc/sys/kernel/domainname", args[1]);
}
-int do_exec(int nargs, char **args)
+int do_enable(int nargs, char **args)
{
+ struct service *svc;
+ svc = service_find_by_name(args[1]);
+ if (svc) {
+ svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
+ if (svc->flags & SVC_DISABLED_START) {
+ service_start(svc, NULL);
+ }
+ } else {
+ return -1;
+ }
+ return 0;
+}
+
+int do_exec(int nargs, char** args) {
+ service* svc = make_exec_oneshot_service(nargs, args);
+ if (svc == NULL) {
+ return -1;
+ }
+ service_start(svc, NULL);
+ return 0;
+}
+
+// TODO: remove execonce when exec is available.
+int do_execonce(int nargs, char **args)
+{
+ pid_t child;
+ int child_status = 0;
+ static int already_done;
+
+ if (already_done) {
+ return -1;
+ }
+ already_done = 1;
+ if (!(child = fork())) {
+ /*
+ * Child process.
+ */
+ zap_stdio();
+ char *exec_args[100];
+ size_t num_process_args = nargs;
+
+ memset(exec_args, 0, sizeof(exec_args));
+ if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
+ ERROR("exec called with %zu args, limit is %zu", num_process_args,
+ ARRAY_SIZE(exec_args) - 1);
+ _exit(1);
+ }
+ for (size_t i = 1; i < num_process_args; i++)
+ exec_args[i - 1] = args[i];
+
+ if (execv(exec_args[0], exec_args) == -1) {
+ ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
+ _exit(1);
+ }
+ ERROR("Returned from execv()!");
+ _exit(1);
+ }
+
+ /*
+ * Parent process.
+ */
+ if (child == -1) {
+ ERROR("Fork failed\n");
+ return -1;
+ }
+
+ if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
+ ERROR("waitpid(): failed (%s)\n", strerror(errno));
+ return -1;
+ }
+
+ if (WIFSIGNALED(child_status)) {
+ INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
+ return -1;
+ } else if (WIFEXITED(child_status)) {
+ INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
+ return WEXITSTATUS(child_status);
+ }
+
+ ERROR("Abnormal child process exit\n");
+
return -1;
}
int do_export(int nargs, char **args)
{
- add_environment(args[1], args[2]);
- return 0;
+ return add_environment(args[1], args[2]);
}
int do_hostname(int nargs, char **args)
@@ -304,7 +277,7 @@
ret = make_dir(args[1], mode);
/* chmod in case the directory already exists */
if (ret == -1 && errno == EEXIST) {
- ret = _chmod(args[1], mode);
+ ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
}
if (ret == -1) {
return -errno;
@@ -318,13 +291,13 @@
gid = decode_uid(args[4]);
}
- if (_chown(args[1], uid, gid) < 0) {
+ if (lchown(args[1], uid, gid) == -1) {
return -errno;
}
/* chown may have cleared S_ISUID and S_ISGID, chmod again */
if (mode & (S_ISUID | S_ISGID)) {
- ret = _chmod(args[1], mode);
+ ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
if (ret == -1) {
return -errno;
}
@@ -395,7 +368,7 @@
return -1;
}
- sprintf(tmp, "/dev/block/mtdblock%d", n);
+ snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
if (wait)
wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
@@ -409,15 +382,16 @@
struct loop_info info;
mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
- fd = open(source + 5, mode);
+ fd = open(source + 5, mode | O_CLOEXEC);
if (fd < 0) {
return -1;
}
for (n = 0; ; n++) {
- sprintf(tmp, "/dev/block/loop%d", n);
- loop = open(tmp, mode);
+ snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
+ loop = open(tmp, mode | O_CLOEXEC);
if (loop < 0) {
+ close(fd);
return -1;
}
@@ -458,13 +432,33 @@
}
+static int wipe_data_via_recovery()
+{
+ mkdir("/cache/recovery", 0700);
+ int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
+ if (fd >= 0) {
+ write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
+ write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
+ close(fd);
+ } else {
+ ERROR("could not open /cache/recovery/command\n");
+ return -1;
+ }
+ android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
+ while (1) { pause(); } // never reached
+}
+
+
+/*
+ * This function might request a reboot, in which case it will
+ * not return.
+ */
int do_mount_all(int nargs, char **args)
{
pid_t pid;
int ret = -1;
int child_ret = -1;
int status;
- const char *prop;
struct fstab *fstab;
if (nargs != 2) {
@@ -480,7 +474,12 @@
pid = fork();
if (pid > 0) {
/* Parent. Wait for the child to return */
- waitpid(pid, &status, 0);
+ int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
+ if (wp_ret < 0) {
+ /* Unexpected error code. We will continue anyway. */
+ NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno);
+ }
+
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
} else {
@@ -495,23 +494,32 @@
if (child_ret == -1) {
ERROR("fs_mgr_mount_all returned an error\n");
}
- exit(child_ret);
+ _exit(child_ret);
} else {
/* fork failed, return an error */
return -1;
}
- /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */
- if (ret == 1) {
+ if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
+ property_set("vold.decrypt", "trigger_encryption");
+ } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
property_set("ro.crypto.state", "encrypted");
- property_set("vold.decrypt", "1");
- } else if (ret == 0) {
+ property_set("vold.decrypt", "trigger_default_encryption");
+ } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
property_set("ro.crypto.state", "unencrypted");
/* If fs_mgr determined this is an unencrypted device, then trigger
* that action.
*/
action_for_each_trigger("nonencrypted", action_add_queue_tail);
+ } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
+ /* Setup a wipe via recovery, and reboot into recovery */
+ ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
+ ret = wipe_data_via_recovery();
+ /* If reboot worked, there is no return. */
+ } else if (ret > 0) {
+ ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
}
+ /* else ... < 0: error */
return ret;
}
@@ -537,24 +545,6 @@
return 0;
}
-int do_setenforce(int nargs, char **args) {
- if (is_selinux_enabled() <= 0)
- return 0;
- if (security_setenforce(atoi(args[1])) < 0) {
- return -errno;
- }
- return 0;
-}
-
-int do_setkey(int nargs, char **args)
-{
- struct kbentry kbe;
- kbe.kb_table = strtoul(args[1], 0, 0);
- kbe.kb_index = strtoul(args[2], 0, 0);
- kbe.kb_value = strtoul(args[3], 0, 0);
- return setkey(&kbe);
-}
-
int do_setprop(int nargs, char **args)
{
const char *name = args[1];
@@ -617,7 +607,7 @@
int res;
int len = 0;
int cmd = 0;
- char *reboot_target;
+ const char *reboot_target;
res = expand_props(command, args[1], sizeof(command));
if (res) {
@@ -677,25 +667,37 @@
return -1;
memset(&tz, 0, sizeof(tz));
- tz.tz_minuteswest = atoi(args[1]);
+ tz.tz_minuteswest = atoi(args[1]);
if (settimeofday(NULL, &tz))
return -1;
return 0;
}
+int do_verity_load_state(int nargs, char **args) {
+ if (nargs == 1) {
+ int mode = -1;
+ int rc = fs_mgr_load_verity_state(&mode);
+
+ if (rc == 0 && mode == VERITY_MODE_LOGGING) {
+ action_for_each_trigger("verity-logging", action_add_queue_tail);
+ }
+
+ return rc;
+ }
+ return -1;
+}
+
int do_write(int nargs, char **args)
{
const char *path = args[1];
const char *value = args[2];
- char prop_val[PROP_VALUE_MAX];
- int ret;
- ret = expand_props(prop_val, value, sizeof(prop_val));
- if (ret) {
+ char expanded_value[256];
+ if (expand_props(expanded_value, value, sizeof(expanded_value))) {
ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
return -EINVAL;
}
- return write_file(path, prop_val);
+ return write_file(path, expanded_value);
}
int do_copy(int nargs, char **args)
@@ -710,16 +712,16 @@
if (nargs != 3)
return -1;
- if (stat(args[1], &info) < 0)
+ if (stat(args[1], &info) < 0)
return -1;
- if ((fd1 = open(args[1], O_RDONLY)) < 0)
+ if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
goto out_err;
- if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
+ if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
goto out_err;
- if (!(buffer = malloc(info.st_size)))
+ if (!(buffer = (char*) malloc(info.st_size)))
goto out_err;
p = buffer;
@@ -763,10 +765,10 @@
int do_chown(int nargs, char **args) {
/* GID is optional. */
if (nargs == 3) {
- if (_chown(args[2], decode_uid(args[1]), -1) < 0)
+ if (lchown(args[2], decode_uid(args[1]), -1) == -1)
return -errno;
} else if (nargs == 4) {
- if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
+ if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
return -errno;
} else {
return -1;
@@ -789,7 +791,7 @@
int do_chmod(int nargs, char **args) {
mode_t mode = get_mode(args[1]);
- if (_chmod(args[2], mode) < 0) {
+ if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
return -errno;
}
return 0;
@@ -817,42 +819,27 @@
return ret;
}
-int do_setsebool(int nargs, char **args) {
- const char *name = args[1];
- const char *value = args[2];
- SELboolean b;
- int ret;
-
- if (is_selinux_enabled() <= 0)
- return 0;
-
- b.name = name;
- if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
- b.value = 1;
- else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
- b.value = 0;
- else {
- ERROR("setsebool: invalid value %s\n", value);
+int do_loglevel(int nargs, char **args) {
+ int log_level;
+ char log_level_str[PROP_VALUE_MAX] = "";
+ if (nargs != 2) {
+ ERROR("loglevel: missing argument\n");
return -EINVAL;
}
- if (security_set_boolean_list(1, &b, 0) < 0) {
- ret = -errno;
- ERROR("setsebool: could not set %s to %s\n", name, value);
- return ret;
+ if (expand_props(log_level_str, args[1], sizeof(log_level_str))) {
+ ERROR("loglevel: cannot expand '%s'\n", args[1]);
+ return -EINVAL;
}
-
+ log_level = atoi(log_level_str);
+ if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
+ ERROR("loglevel: invalid log level'%d'\n", log_level);
+ return -EINVAL;
+ }
+ klog_set_level(log_level);
return 0;
}
-int do_loglevel(int nargs, char **args) {
- if (nargs == 2) {
- klog_set_level(atoi(args[1]));
- return 0;
- }
- return -1;
-}
-
int do_load_persist_props(int nargs, char **args) {
if (nargs == 1) {
load_persist_props();
@@ -861,6 +848,14 @@
return -1;
}
+int do_load_all_props(int nargs, char **args) {
+ if (nargs == 1) {
+ load_all_props();
+ return 0;
+ }
+ return -1;
+}
+
int do_wait(int nargs, char **args)
{
if (nargs == 2) {
diff --git a/init/devices.c b/init/devices.cpp
similarity index 82%
rename from init/devices.c
rename to init/devices.cpp
index f7df453..3a9b753 100644
--- a/init/devices.c
+++ b/init/devices.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2007-2014 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.
@@ -15,6 +15,7 @@
*/
#include <errno.h>
+#include <fnmatch.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -37,7 +38,6 @@
#include <private/android_filesystem_config.h>
#include <sys/time.h>
-#include <asm/page.h>
#include <sys/wait.h>
#include <cutils/list.h>
@@ -76,6 +76,7 @@
unsigned int uid;
unsigned int gid;
unsigned short prefix;
+ unsigned short wildcard;
};
struct perm_node {
@@ -96,8 +97,9 @@
int add_dev_perms(const char *name, const char *attr,
mode_t perm, unsigned int uid, unsigned int gid,
- unsigned short prefix) {
- struct perm_node *node = calloc(1, sizeof(*node));
+ unsigned short prefix,
+ unsigned short wildcard) {
+ struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
if (!node)
return -ENOMEM;
@@ -115,6 +117,7 @@
node->dp.uid = uid;
node->dp.gid = gid;
node->dp.prefix = prefix;
+ node->dp.wildcard = wildcard;
if (attr)
list_add_tail(&sys_perms, &node->plist);
@@ -129,42 +132,63 @@
char buf[512];
struct listnode *node;
struct perms_ *dp;
- char *secontext;
- /* upaths omit the "/sys" that paths in this list
- * contain, so we add 4 when comparing...
- */
+ /* upaths omit the "/sys" that paths in this list
+ * contain, so we add 4 when comparing...
+ */
list_for_each(node, &sys_perms) {
dp = &(node_to_item(node, struct perm_node, plist))->dp;
if (dp->prefix) {
if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
continue;
+ } else if (dp->wildcard) {
+ if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
+ continue;
} else {
if (strcmp(upath, dp->name + 4))
continue;
}
if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
- return;
+ break;
- sprintf(buf,"/sys%s/%s", upath, dp->attr);
+ snprintf(buf, sizeof(buf), "/sys%s/%s", upath, dp->attr);
INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
chown(buf, dp->uid, dp->gid);
chmod(buf, dp->perm);
- if (sehandle) {
- secontext = NULL;
- selabel_lookup(sehandle, &secontext, buf, 0);
- if (secontext) {
- setfilecon(buf, secontext);
- freecon(secontext);
- }
- }
+ }
+
+ // Now fixup SELinux file labels
+ int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
+ if ((len < 0) || ((size_t) len >= sizeof(buf))) {
+ // Overflow
+ return;
+ }
+ if (access(buf, F_OK) == 0) {
+ INFO("restorecon_recursive: %s\n", buf);
+ restorecon_recursive(buf);
}
}
-static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
+static bool perm_path_matches(const char *path, struct perms_ *dp)
{
- mode_t perm;
+ if (dp->prefix) {
+ if (strncmp(path, dp->name, strlen(dp->name)) == 0)
+ return true;
+ } else if (dp->wildcard) {
+ if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
+ return true;
+ } else {
+ if (strcmp(path, dp->name) == 0)
+ return true;
+ }
+
+ return false;
+}
+
+static mode_t get_device_perm(const char *path, const char **links,
+ unsigned *uid, unsigned *gid)
+{
struct listnode *node;
struct perm_node *perm_node;
struct perms_ *dp;
@@ -173,19 +197,30 @@
* override ueventd.rc
*/
list_for_each_reverse(node, &dev_perms) {
+ bool match = false;
+
perm_node = node_to_item(node, struct perm_node, plist);
dp = &perm_node->dp;
- if (dp->prefix) {
- if (strncmp(path, dp->name, strlen(dp->name)))
- continue;
+ if (perm_path_matches(path, dp)) {
+ match = true;
} else {
- if (strcmp(path, dp->name))
- continue;
+ if (links) {
+ int i;
+ for (i = 0; links[i]; i++) {
+ if (perm_path_matches(links[i], dp)) {
+ match = true;
+ break;
+ }
+ }
+ }
}
- *uid = dp->uid;
- *gid = dp->gid;
- return dp->perm;
+
+ if (match) {
+ *uid = dp->uid;
+ *gid = dp->gid;
+ return dp->perm;
+ }
}
/* Default if nothing found. */
*uid = 0;
@@ -194,8 +229,9 @@
}
static void make_device(const char *path,
- const char *upath,
- int block, int major, int minor)
+ const char */*upath*/,
+ int block, int major, int minor,
+ const char **links)
{
unsigned uid;
unsigned gid;
@@ -203,10 +239,10 @@
dev_t dev;
char *secontext = NULL;
- mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
+ mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
if (sehandle) {
- selabel_lookup(sehandle, &secontext, path, mode);
+ selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
setfscreatecon(secontext);
}
@@ -251,7 +287,7 @@
INFO("adding platform device %s (%s)\n", name, path);
- bus = calloc(1, sizeof(struct platform_node));
+ bus = (platform_node*) calloc(1, sizeof(struct platform_node));
bus->path = strdup(path);
bus->path_len = path_len;
bus->name = bus->path + (name - path);
@@ -297,7 +333,36 @@
}
}
-#if LOG_UEVENTS
+/* Given a path that may start with a PCI device, populate the supplied buffer
+ * with the PCI domain/bus number and the peripheral ID and return 0.
+ * If it doesn't start with a PCI device, or there is some error, return -1 */
+static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
+{
+ const char *start, *end;
+
+ if (strncmp(path, "/devices/pci", 12))
+ return -1;
+
+ /* Beginning of the prefix is the initial "pci" after "/devices/" */
+ start = path + 9;
+
+ /* End of the prefix is two path '/' later, capturing the domain/bus number
+ * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
+ end = strchr(start, '/');
+ if (!end)
+ return -1;
+ end = strchr(end + 1, '/');
+ if (!end)
+ return -1;
+
+ /* Make sure we have enough room for the string plus null terminator */
+ if (end - start + 1 > buf_sz)
+ return -1;
+
+ strncpy(buf, start, end - start);
+ buf[end - start] = '\0';
+ return 0;
+}
static inline suseconds_t get_usecs(void)
{
@@ -306,15 +371,6 @@
return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
}
-#define log_event_print(x...) INFO(x)
-
-#else
-
-#define log_event_print(fmt, args...) do { } while (0)
-#define get_usecs() 0
-
-#endif
-
static void parse_event(const char *msg, struct uevent *uevent)
{
uevent->action = "";
@@ -363,9 +419,11 @@
;
}
- log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
- uevent->action, uevent->path, uevent->subsystem,
- uevent->firmware, uevent->major, uevent->minor);
+ if (LOG_UEVENTS) {
+ INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
+ uevent->action, uevent->path, uevent->subsystem,
+ uevent->firmware, uevent->major, uevent->minor);
+ }
}
static char **get_character_device_symlinks(struct uevent *uevent)
@@ -381,14 +439,14 @@
if (!pdev)
return NULL;
- links = malloc(sizeof(char *) * 2);
+ links = (char**) malloc(sizeof(char *) * 2);
if (!links)
return NULL;
memset(links, 0, sizeof(char *) * 2);
/* skip "/devices/platform/<driver>" */
parent = strchr(uevent->path + pdev->path_len, '/');
- if (!*parent)
+ if (!parent)
goto err;
if (!strncmp(parent, "/usb", 4)) {
@@ -421,34 +479,36 @@
return NULL;
}
-static char **parse_platform_block_device(struct uevent *uevent)
+static char **get_block_device_symlinks(struct uevent *uevent)
{
const char *device;
struct platform_node *pdev;
char *slash;
- int width;
+ const char *type;
char buf[256];
char link_path[256];
- int fd;
int link_num = 0;
- int ret;
char *p;
- unsigned int size;
- struct stat info;
pdev = find_platform_device(uevent->path);
- if (!pdev)
+ if (pdev) {
+ device = pdev->name;
+ type = "platform";
+ } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
+ device = buf;
+ type = "pci";
+ } else {
return NULL;
- device = pdev->name;
+ }
- char **links = malloc(sizeof(char *) * 4);
+ char **links = (char**) malloc(sizeof(char *) * 4);
if (!links)
return NULL;
memset(links, 0, sizeof(char *) * 4);
- INFO("found platform device %s\n", device);
+ INFO("found %s device %s\n", type, device);
- snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
+ snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
if (uevent->partition_name) {
p = strdup(uevent->partition_name);
@@ -484,7 +544,7 @@
int i;
if(!strcmp(action, "add")) {
- make_device(devpath, path, block, major, minor);
+ make_device(devpath, path, block, major, minor, (const char **)links);
if (links) {
for (i = 0; links[i]; i++)
make_link(devpath, links[i]);
@@ -555,7 +615,7 @@
make_dir(base, 0755);
if (!strncmp(uevent->path, "/devices/", 9))
- links = parse_platform_block_device(uevent);
+ links = get_block_device_symlinks(uevent);
handle_device(uevent->action, devpath, uevent->path, 1,
uevent->major, uevent->minor, links);
@@ -592,7 +652,7 @@
static void handle_generic_device_event(struct uevent *uevent)
{
- char *base;
+ const char *base;
const char *name;
char devpath[DEVPATH_LEN] = {0};
char **links = NULL;
@@ -676,6 +736,7 @@
make_dir(base, 0755);
} else if(!strncmp(uevent->subsystem, "misc", 4) &&
!strncmp(name, "log_", 4)) {
+ INFO("kernel logger is deprecated\n");
base = "/dev/log/";
make_dir(base, 0755);
name += 4;
@@ -692,7 +753,7 @@
static void handle_device_event(struct uevent *uevent)
{
- if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
+ if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
fixup_sys_perms(uevent->path);
if (!strncmp(uevent->subsystem, "block", 5)) {
@@ -788,20 +849,20 @@
if (l == -1)
goto data_free_out;
- loading_fd = open(loading, O_WRONLY);
+ loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
if(loading_fd < 0)
goto file_free_out;
- data_fd = open(data, O_WRONLY);
+ data_fd = open(data, O_WRONLY|O_CLOEXEC);
if(data_fd < 0)
goto loading_close_out;
try_loading_again:
- fw_fd = open(file1, O_RDONLY);
+ fw_fd = open(file1, O_RDONLY|O_CLOEXEC);
if(fw_fd < 0) {
- fw_fd = open(file2, O_RDONLY);
+ fw_fd = open(file2, O_RDONLY|O_CLOEXEC);
if (fw_fd < 0) {
- fw_fd = open(file3, O_RDONLY);
+ fw_fd = open(file3, O_RDONLY|O_CLOEXEC);
if (fw_fd < 0) {
if (booting) {
/* If we're not fully booted, we may be missing
@@ -843,7 +904,6 @@
static void handle_firmware_event(struct uevent *uevent)
{
pid_t pid;
- int ret;
if(strcmp(uevent->subsystem, "firmware"))
return;
@@ -855,11 +915,13 @@
pid = fork();
if (!pid) {
process_firmware_event(uevent);
- exit(EXIT_SUCCESS);
+ _exit(EXIT_SUCCESS);
+ } else if (pid < 0) {
+ ERROR("could not fork to process firmware event: %s\n", strerror(errno));
}
}
-#define UEVENT_MSG_LEN 1024
+#define UEVENT_MSG_LEN 2048
void handle_device_fd()
{
char msg[UEVENT_MSG_LEN+2];
@@ -894,7 +956,7 @@
**
** We drain any pending events from the netlink socket every time
** we poke another uevent file to make sure we don't overrun the
-** socket's buffer.
+** socket's buffer.
*/
static void do_coldboot(DIR *d)
@@ -960,17 +1022,19 @@
fcntl(device_fd, F_SETFD, FD_CLOEXEC);
fcntl(device_fd, F_SETFL, O_NONBLOCK);
- if (stat(coldboot_done, &info) < 0) {
+ if (stat(COLDBOOT_DONE, &info) < 0) {
t0 = get_usecs();
coldboot("/sys/class");
coldboot("/sys/block");
coldboot("/sys/devices");
t1 = get_usecs();
- fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
+ fd = open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000);
close(fd);
- log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
- } else {
- log_event_print("skipping coldboot, already done\n");
+ if (LOG_UEVENTS) {
+ INFO("coldboot %ld uS\n", ((long) (t1 - t0)));
+ }
+ } else if (LOG_UEVENTS) {
+ INFO("skipping coldboot, already done\n");
}
}
diff --git a/init/devices.h b/init/devices.h
index a84fa58..6cb0a77 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -23,6 +23,8 @@
extern void device_init(void);
extern int add_dev_perms(const char *name, const char *attr,
mode_t perm, unsigned int uid,
- unsigned int gid, unsigned short prefix);
+ unsigned int gid, unsigned short prefix,
+ unsigned short wildcard);
int get_device_fd();
+
#endif /* _INIT_DEVICES_H */
diff --git a/init/grab-bootchart.sh b/init/grab-bootchart.sh
index 7fe8904..d6082aa 100755
--- a/init/grab-bootchart.sh
+++ b/init/grab-bootchart.sh
@@ -1,10 +1,9 @@
#!/bin/sh
#
-# this script is used to retrieve the bootchart log generated
-# by init when compiled with INIT_BOOTCHART=true.
-#
-# for all details, see //device/system/init/README.BOOTCHART
-#
+# This script is used to retrieve a bootchart log generated by init.
+# All options are passed to adb, for better or for worse.
+# See the readme in this directory for more on bootcharting.
+
TMPDIR=/tmp/android-bootchart
rm -rf $TMPDIR
mkdir -p $TMPDIR
@@ -15,8 +14,9 @@
FILES="header proc_stat.log proc_ps.log proc_diskstats.log kernel_pacct"
for f in $FILES; do
- adb pull $LOGROOT/$f $TMPDIR/$f 2>&1 > /dev/null
+ adb "${@}" pull $LOGROOT/$f $TMPDIR/$f 2>&1 > /dev/null
done
(cd $TMPDIR && tar -czf $TARBALL $FILES)
-cp -f $TMPDIR/$TARBALL ./$TARBALL
-echo "look at $TARBALL"
+bootchart ${TMPDIR}/${TARBALL}
+gnome-open ${TARBALL%.tgz}.png
+echo "Clean up ${TMPDIR}/ and ./${TARBALL%.tgz}.png when done"
diff --git a/init/init.c b/init/init.cpp
similarity index 70%
rename from init/init.c
rename to init/init.cpp
index 00f4558..5a40fd3 100644
--- a/init/init.c
+++ b/init/init.cpp
@@ -14,39 +14,43 @@
* limitations under the License.
*/
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <paths.h>
+#include <signal.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <ctype.h>
-#include <signal.h>
-#include <sys/wait.h>
#include <sys/mount.h>
-#include <sys/stat.h>
#include <sys/poll.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <mtd/mtd-user.h>
-#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <sys/un.h>
+#include <sys/wait.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <mtd/mtd-user.h>
#include <selinux/selinux.h>
#include <selinux/label.h>
#include <selinux/android.h>
-#include <libgen.h>
-
-#include <cutils/list.h>
+#include <base/file.h>
+#include <base/stringprintf.h>
#include <cutils/android_reboot.h>
-#include <cutils/sockets.h>
-#include <cutils/iosched_policy.h>
#include <cutils/fs.h>
+#include <cutils/iosched_policy.h>
+#include <cutils/list.h>
+#include <cutils/sockets.h>
#include <private/android_filesystem_config.h>
-#include <termios.h>
-#include <sys/system_properties.h>
+#include <memory>
#include "devices.h"
#include "init.h"
@@ -65,29 +69,12 @@
static int property_triggers_enabled = 0;
-#if BOOTCHART
-static int bootchart_count;
-#endif
-
static char console[32];
static char bootmode[32];
-static char hardware[32];
-static unsigned revision = 0;
static char qemu[32];
static struct action *cur_action = NULL;
static struct command *cur_command = NULL;
-static struct listnode *command_queue = NULL;
-
-void notify_service_state(const char *name, const char *state)
-{
- char pname[PROP_NAME_MAX];
- int len = strlen(name);
- if ((len + 10) > PROP_NAME_MAX)
- return;
- snprintf(pname, sizeof(pname), "init.svc.%s", name);
- property_set(pname, state);
-}
static int have_console;
static char console_name[PROP_VALUE_MAX] = "/dev/console";
@@ -95,25 +82,62 @@
static const char *ENV[32];
+bool waiting_for_exec = false;
+
+void service::NotifyStateChange(const char* new_state) {
+ if (!properties_inited()) {
+ // If properties aren't available yet, we can't set them.
+ return;
+ }
+
+ if ((flags & SVC_EXEC) != 0) {
+ // 'exec' commands don't have properties tracking their state.
+ return;
+ }
+
+ char prop_name[PROP_NAME_MAX];
+ if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
+ // If the property name would be too long, we can't set it.
+ ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
+ return;
+ }
+
+ property_set(prop_name, new_state);
+}
+
/* add_environment - add "key=value" to the current environment */
int add_environment(const char *key, const char *val)
{
- int n;
+ size_t n;
+ size_t key_len = strlen(key);
- for (n = 0; n < 31; n++) {
- if (!ENV[n]) {
- size_t len = strlen(key) + strlen(val) + 2;
- char *entry = malloc(len);
- snprintf(entry, len, "%s=%s", key, val);
+ /* The last environment entry is reserved to terminate the list */
+ for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
+
+ /* Delete any existing entry for this key */
+ if (ENV[n] != NULL) {
+ size_t entry_key_len = strcspn(ENV[n], "=");
+ if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
+ free((char*)ENV[n]);
+ ENV[n] = NULL;
+ }
+ }
+
+ /* Add entry if a free slot is available */
+ if (ENV[n] == NULL) {
+ char* entry;
+ asprintf(&entry, "%s=%s", key, val);
ENV[n] = entry;
return 0;
}
}
- return 1;
+ ERROR("No env. room to store: '%s':'%s'\n", key, val);
+
+ return -1;
}
-static void zap_stdio(void)
+void zap_stdio(void)
{
int fd;
fd = open("/dev/null", O_RDWR);
@@ -153,36 +177,26 @@
void service_start(struct service *svc, const char *dynamic_args)
{
- struct stat s;
- pid_t pid;
- int needs_console;
- int n;
- char *scon = NULL;
- int rc;
-
- /* starting a service removes it from the disabled or reset
- * state and immediately takes it out of the restarting
- * state if it was in there
- */
- svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART));
+ // Starting a service removes it from the disabled or reset state and
+ // immediately takes it out of the restarting state if it was in there.
+ svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
svc->time_started = 0;
- /* running processes require no additional work -- if
- * they're in the process of exiting, we've ensured
- * that they will immediately restart on exit, unless
- * they are ONESHOT
- */
+ // Running processes require no additional work --- if they're in the
+ // process of exiting, we've ensured that they will immediately restart
+ // on exit, unless they are ONESHOT.
if (svc->flags & SVC_RUNNING) {
return;
}
- needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
- if (needs_console && (!have_console)) {
+ bool needs_console = (svc->flags & SVC_CONSOLE);
+ if (needs_console && !have_console) {
ERROR("service '%s' requires console\n", svc->name);
svc->flags |= SVC_DISABLED;
return;
}
+ struct stat s;
if (stat(svc->args[0], &s) != 0) {
ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
svc->flags |= SVC_DISABLED;
@@ -196,6 +210,7 @@
return;
}
+ char* scon = NULL;
if (is_selinux_enabled() > 0) {
if (svc->seclabel) {
scon = strdup(svc->seclabel);
@@ -207,7 +222,7 @@
char *mycon = NULL, *fcon = NULL;
INFO("computing context for service '%s'\n", svc->args[0]);
- rc = getcon(&mycon);
+ int rc = getcon(&mycon);
if (rc < 0) {
ERROR("could not get context while starting '%s'\n", svc->name);
return;
@@ -235,8 +250,7 @@
NOTICE("starting '%s'\n", svc->name);
- pid = fork();
-
+ pid_t pid = fork();
if (pid == 0) {
struct socketinfo *si;
struct svcenvinfo *ei;
@@ -246,7 +260,7 @@
umask(077);
if (properties_inited()) {
get_property_workspace(&fd, &sz);
- sprintf(tmp, "%d,%d", dup(fd), sz);
+ snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
}
@@ -281,18 +295,18 @@
zap_stdio();
}
-#if 0
- for (n = 0; svc->args[n]; n++) {
- INFO("args[%d] = '%s'\n", n, svc->args[n]);
+ if (false) {
+ for (size_t n = 0; svc->args[n]; n++) {
+ INFO("args[%zu] = '%s'\n", n, svc->args[n]);
+ }
+ for (size_t n = 0; ENV[n]; n++) {
+ INFO("env[%zu] = '%s'\n", n, ENV[n]);
+ }
}
- for (n = 0; ENV[n]; n++) {
- INFO("env[%d] = '%s'\n", n, ENV[n]);
- }
-#endif
setpgid(0, getpid());
- /* as requested, set our gid, supplemental gids, and uid */
+ // As requested, set our gid, supplemental gids, and uid.
if (svc->gid) {
if (setgid(svc->gid) != 0) {
ERROR("setgid failed: %s\n", strerror(errno));
@@ -337,7 +351,7 @@
if (arg_idx == INIT_PARSER_MAXARGS)
break;
}
- arg_ptrs[arg_idx] = '\0';
+ arg_ptrs[arg_idx] = NULL;
execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
}
_exit(127);
@@ -355,8 +369,13 @@
svc->pid = pid;
svc->flags |= SVC_RUNNING;
- if (properties_inited())
- notify_service_state(svc->name, "running");
+ if ((svc->flags & SVC_EXEC) != 0) {
+ INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
+ svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, svc->seclabel);
+ waiting_for_exec = true;
+ }
+
+ svc->NotifyStateChange("running");
}
/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
@@ -364,7 +383,7 @@
{
/* The service is still SVC_RUNNING until its process exits, but if it has
* already exited it shoudn't attempt a restart yet. */
- svc->flags &= (~SVC_RESTARTING);
+ svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
/* Hrm, an illegal flag. Default to SVC_DISABLED */
@@ -382,9 +401,9 @@
if (svc->pid) {
NOTICE("service '%s' is being killed\n", svc->name);
kill(-svc->pid, SIGKILL);
- notify_service_state(svc->name, "stopping");
+ svc->NotifyStateChange("stopping");
} else {
- notify_service_state(svc->name, "stopped");
+ svc->NotifyStateChange("stopped");
}
}
@@ -528,16 +547,35 @@
return (list_tail(&act->commands) == &cmd->clist);
}
+
+void build_triggers_string(char *name_str, int length, struct action *cur_action) {
+ struct listnode *node;
+ struct trigger *cur_trigger;
+
+ list_for_each(node, &cur_action->triggers) {
+ cur_trigger = node_to_item(node, struct trigger, nlist);
+ if (node != cur_action->triggers.next) {
+ strlcat(name_str, " " , length);
+ }
+ strlcat(name_str, cur_trigger->name , length);
+ }
+}
+
void execute_one_command(void)
{
- int ret;
+ int ret, i;
+ char cmd_str[256] = "";
+ char name_str[256] = "";
if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
cur_action = action_remove_queue_head();
cur_command = NULL;
if (!cur_action)
return;
- INFO("processing action %p (%s)\n", cur_action, cur_action->name);
+
+ build_triggers_string(name_str, sizeof(name_str), cur_action);
+
+ INFO("processing action %p (%s)\n", cur_action, name_str);
cur_command = get_first_command(cur_action);
} else {
cur_command = get_next_command(cur_action, cur_command);
@@ -547,16 +585,26 @@
return;
ret = cur_command->func(cur_command->nargs, cur_command->args);
- INFO("command '%s' r=%d\n", cur_command->args[0], ret);
+ if (klog_get_level() >= KLOG_INFO_LEVEL) {
+ for (i = 0; i < cur_command->nargs; i++) {
+ strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
+ if (i < cur_command->nargs - 1) {
+ strlcat(cmd_str, " ", sizeof(cmd_str));
+ }
+ }
+ INFO("command '%s' action=%s status=%d (%s:%d)\n",
+ cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
+ cur_command->line);
+ }
}
static int wait_for_coldboot_done_action(int nargs, char **args)
{
int ret;
- INFO("wait for %s\n", coldboot_done);
- ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
+ INFO("wait for %s\n", COLDBOOT_DONE);
+ ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
if (ret)
- ERROR("Timed out waiting for %s\n", coldboot_done);
+ ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
return ret;
}
@@ -585,7 +633,7 @@
size_t total_bytes_written = 0;
hwrandom_fd = TEMP_FAILURE_RETRY(
- open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
+ open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
if (hwrandom_fd == -1) {
if (errno == ENOENT) {
ERROR("/dev/hw_random not found\n");
@@ -598,7 +646,7 @@
}
urandom_fd = TEMP_FAILURE_RETRY(
- open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
+ open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
if (urandom_fd == -1) {
ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
goto ret;
@@ -634,7 +682,6 @@
if (urandom_fd != -1) {
close(urandom_fd);
}
- memset(buf, 0, sizeof(buf));
return result;
}
@@ -652,12 +699,12 @@
snprintf(console_name, sizeof(console_name), "/dev/%s", console);
}
- fd = open(console_name, O_RDWR);
+ fd = open(console_name, O_RDWR | O_CLOEXEC);
if (fd >= 0)
have_console = 1;
close(fd);
- fd = open("/dev/tty0", O_WRONLY);
+ fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
if (fd >= 0) {
const char *msg;
msg = "\n"
@@ -729,6 +776,8 @@
{ "ro.boot.mode", "ro.bootmode", "unknown", },
{ "ro.boot.baseband", "ro.baseband", "unknown", },
{ "ro.boot.bootloader", "ro.bootloader", "unknown", },
+ { "ro.boot.hardware", "ro.hardware", "unknown", },
+ { "ro.boot.revision", "ro.revision", "0", },
};
for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
@@ -747,16 +796,6 @@
property_get("ro.bootmode", tmp);
strlcpy(bootmode, tmp, sizeof(bootmode));
- /* if this was given on kernel command line, override what we read
- * before (e.g. from /proc/cpuinfo), if anything */
- ret = property_get("ro.boot.hardware", tmp);
- if (ret)
- strlcpy(hardware, tmp, sizeof(hardware));
- property_set("ro.hardware", hardware);
-
- snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
- property_set("ro.revision", tmp);
-
/* TODO: these are obsolete. We should delete them */
if (!strcmp(bootmode,"factory"))
property_set("ro.factorytest", "1");
@@ -766,6 +805,40 @@
property_set("ro.factorytest", "0");
}
+static void process_kernel_dt(void)
+{
+ static const char android_dir[] = "/proc/device-tree/firmware/android";
+
+ std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
+
+ std::string dt_file;
+ android::base::ReadFileToString(file_name, &dt_file);
+ if (!dt_file.compare("android,firmware")) {
+ ERROR("firmware/android is not compatible with 'android,firmware'\n");
+ return;
+ }
+
+ std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
+ if (!dir)
+ return;
+
+ struct dirent *dp;
+ while ((dp = readdir(dir.get())) != NULL) {
+ if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible"))
+ continue;
+
+ file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
+
+ android::base::ReadFileToString(file_name, &dt_file);
+ std::replace(dt_file.begin(), dt_file.end(), ',', '.');
+
+ std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
+ if (property_set(property_name.c_str(), dt_file.c_str())) {
+ ERROR("Could not set property %s to value %s", property_name.c_str(), dt_file.c_str());
+ }
+ }
+}
+
static void process_kernel_cmdline(void)
{
/* don't expose the raw commandline to nonpriv processes */
@@ -778,11 +851,6 @@
import_kernel_cmdline(0, import_kernel_nv);
if (qemu[0])
import_kernel_cmdline(1, import_kernel_nv);
-
- /* now propogate the info given on command line to internal variables
- * used by init as well as the current required properties
- */
- export_kernel_boot_props();
}
static int property_service_init_action(int nargs, char **args)
@@ -793,27 +861,21 @@
* that /data/local.prop cannot interfere with them.
*/
start_property_service();
+ if (get_property_set_fd() < 0) {
+ ERROR("start_property_service() failed\n");
+ exit(1);
+ }
+
return 0;
}
static int signal_init_action(int nargs, char **args)
{
signal_init();
- return 0;
-}
-
-static int check_startup_action(int nargs, char **args)
-{
- /* make sure we actually have all the pieces we need */
- if ((get_property_set_fd() < 0) ||
- (get_signal_fd() < 0)) {
- ERROR("init startup failure\n");
+ if (get_signal_fd() < 0) {
+ ERROR("signal_init() failed\n");
exit(1);
}
-
- /* signal that we hit this point */
- unlink("/dev/.booting");
-
return 0;
}
@@ -825,45 +887,6 @@
return 0;
}
-#if BOOTCHART
-static int bootchart_init_action(int nargs, char **args)
-{
- bootchart_count = bootchart_init();
- if (bootchart_count < 0) {
- ERROR("bootcharting init failure\n");
- } else if (bootchart_count > 0) {
- NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
- } else {
- NOTICE("bootcharting ignored\n");
- }
-
- return 0;
-}
-#endif
-
-static const struct selinux_opt seopts_prop[] = {
- { SELABEL_OPT_PATH, "/property_contexts" },
- { 0, NULL }
-};
-
-struct selabel_handle* selinux_android_prop_context_handle(void)
-{
- int i = 0;
- struct selabel_handle* sehandle = NULL;
- while ((sehandle == NULL) && seopts_prop[i].value) {
- sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
- i++;
- }
-
- if (!sehandle) {
- ERROR("SELinux: Could not load property_contexts: %s\n",
- strerror(errno));
- return NULL;
- }
- INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
- return sehandle;
-}
-
void selinux_init_all_handles(void)
{
sehandle = selinux_android_file_context_handle();
@@ -873,18 +896,18 @@
static bool selinux_is_disabled(void)
{
- char tmp[PROP_VALUE_MAX];
+ if (ALLOW_DISABLE_SELINUX) {
+ if (access("/sys/fs/selinux", F_OK) != 0) {
+ // SELinux is not compiled into the kernel, or has been disabled
+ // via the kernel command line "selinux=0".
+ return true;
+ }
- if (access("/sys/fs/selinux", F_OK) != 0) {
- /* SELinux is not compiled into the kernel, or has been disabled
- * via the kernel command line "selinux=0".
- */
- return true;
- }
-
- if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
- /* SELinux is compiled into the kernel, but we've been told to disable it. */
- return true;
+ char tmp[PROP_VALUE_MAX];
+ if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
+ // SELinux is compiled into the kernel, but we've been told to disable it.
+ return true;
+ }
}
return false;
@@ -892,22 +915,22 @@
static bool selinux_is_enforcing(void)
{
- char tmp[PROP_VALUE_MAX];
+ if (ALLOW_DISABLE_SELINUX) {
+ char tmp[PROP_VALUE_MAX];
+ if (property_get("ro.boot.selinux", tmp) == 0) {
+ // Property is not set. Assume enforcing.
+ return true;
+ }
- if (property_get("ro.boot.selinux", tmp) == 0) {
- /* Property is not set. Assume enforcing */
- return true;
+ if (strcmp(tmp, "permissive") == 0) {
+ // SELinux is in the kernel, but we've been told to go into permissive mode.
+ return false;
+ }
+
+ if (strcmp(tmp, "enforcing") != 0) {
+ ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
+ }
}
-
- if (strcmp(tmp, "permissive") == 0) {
- /* SELinux is in the kernel, but we've been told to go into permissive mode */
- return false;
- }
-
- if (strcmp(tmp, "enforcing") != 0) {
- ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
- }
-
return true;
}
@@ -933,12 +956,33 @@
return 0;
}
-int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
+static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
{
snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
return 0;
}
+int log_callback(int type, const char *fmt, ...)
+{
+ int level;
+ va_list ap;
+ switch (type) {
+ case SELINUX_WARNING:
+ level = KLOG_WARNING_LEVEL;
+ break;
+ case SELINUX_INFO:
+ level = KLOG_INFO_LEVEL;
+ break;
+ default:
+ level = KLOG_ERROR_LEVEL;
+ break;
+ }
+ va_start(ap, fmt);
+ klog_vwrite(level, fmt, ap);
+ va_end(ap);
+ return 0;
+}
+
static void selinux_initialize(void)
{
if (selinux_is_disabled()) {
@@ -958,31 +1002,20 @@
security_setenforce(is_enforcing);
}
-int main(int argc, char **argv)
-{
- int fd_count = 0;
- struct pollfd ufds[4];
- char *tmpdev;
- char* debuggable;
- char tmp[32];
- int property_set_fd_init = 0;
- int signal_fd_init = 0;
- int keychord_fd_init = 0;
- bool is_charger = false;
-
+int main(int argc, char** argv) {
if (!strcmp(basename(argv[0]), "ueventd"))
return ueventd_main(argc, argv);
if (!strcmp(basename(argv[0]), "watchdogd"))
return watchdogd_main(argc, argv);
- /* clear the umask */
+ // Clear the umask.
umask(0);
- /* Get the basic filesystem setup we need put
- * together in the initramdisk on / and then we'll
- * let the rc file figure out the rest.
- */
+ add_environment("PATH", _PATH_DEFPATH);
+
+ // Get the basic filesystem setup we need put together in the initramdisk
+ // on / and then we'll let the rc file figure out the rest.
mkdir("/dev", 0755);
mkdir("/proc", 0755);
mkdir("/sys", 0755);
@@ -994,47 +1027,47 @@
mount("proc", "/proc", "proc", 0, NULL);
mount("sysfs", "/sys", "sysfs", 0, NULL);
- /* indicate that booting is in progress to background fw loaders, etc */
- close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
+ // Indicate that booting is in progress to background fw loaders, etc.
+ close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
- /* We must have some place other than / to create the
- * device nodes for kmsg and null, otherwise we won't
- * be able to remount / read-only later on.
- * Now that tmpfs is mounted on /dev, we can actually
- * talk to the outside world.
- */
+ // We must have some place other than / to create the device nodes for
+ // kmsg and null, otherwise we won't be able to remount / read-only
+ // later on. Now that tmpfs is mounted on /dev, we can actually talk
+ // to the outside world.
open_devnull_stdio();
klog_init();
property_init();
- get_hardware_name(hardware, &revision);
-
+ process_kernel_dt();
+ /* in case one is passing arguments both on the command line and in DT
+ * Properties set in DT always have priority over the command-line ones
+ */
process_kernel_cmdline();
- union selinux_callback cb;
- cb.func_log = klog_write;
- selinux_set_callback(SELINUX_CB_LOG, cb);
+ /* now propogate the kernel variables to internal variables
+ * used by init as well as the current required properties
+ */
+ export_kernel_boot_props();
+ selinux_callback cb;
+ cb.func_log = log_callback;
+ selinux_set_callback(SELINUX_CB_LOG, cb);
cb.func_audit = audit_callback;
selinux_set_callback(SELINUX_CB_AUDIT, cb);
selinux_initialize();
- /* These directories were necessarily created before initial policy load
- * and therefore need their security context restored to the proper value.
- * This must happen before /dev is populated by ueventd.
- */
+
+ // These directories were necessarily created before initial policy load
+ // and therefore need their security context restored to the proper value.
+ // This must happen before /dev is populated by ueventd.
restorecon("/dev");
restorecon("/dev/socket");
restorecon("/dev/__properties__");
restorecon_recursive("/sys");
- is_charger = !strcmp(bootmode, "charger");
-
INFO("property init\n");
- if (!is_charger)
- property_load_boot_defaults();
+ property_load_boot_defaults();
- INFO("reading config file\n");
init_parse_config_file("/init.rc");
action_for_each_trigger("early-init", action_add_queue_tail);
@@ -1044,101 +1077,87 @@
queue_builtin_action(keychord_init_action, "keychord_init");
queue_builtin_action(console_init_action, "console_init");
- /* execute all the boot actions to get us started */
+ // Execute all the boot actions to get us started.
action_for_each_trigger("init", action_add_queue_tail);
- /* skip mounting filesystems in charger mode */
- if (!is_charger) {
- action_for_each_trigger("early-fs", action_add_queue_tail);
- action_for_each_trigger("fs", action_add_queue_tail);
- action_for_each_trigger("post-fs", action_add_queue_tail);
- action_for_each_trigger("post-fs-data", action_add_queue_tail);
- }
-
- /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
- * wasn't ready immediately after wait_for_coldboot_done
- */
+ // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
+ // wasn't ready immediately after wait_for_coldboot_done
queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
-
queue_builtin_action(property_service_init_action, "property_service_init");
queue_builtin_action(signal_init_action, "signal_init");
- queue_builtin_action(check_startup_action, "check_startup");
- if (is_charger) {
+ // Don't mount filesystems or start core system services in charger mode.
+ if (strcmp(bootmode, "charger") == 0) {
action_for_each_trigger("charger", action_add_queue_tail);
} else {
- action_for_each_trigger("early-boot", action_add_queue_tail);
- action_for_each_trigger("boot", action_add_queue_tail);
+ action_for_each_trigger("late-init", action_add_queue_tail);
}
- /* run all property triggers based on current state of the properties */
+ // Run all property triggers based on current state of the properties.
queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
+ // TODO: why do we only initialize ufds after execute_one_command and restart_processes?
+ size_t fd_count = 0;
+ struct pollfd ufds[3];
+ bool property_set_fd_init = false;
+ bool signal_fd_init = false;
+ bool keychord_fd_init = false;
-#if BOOTCHART
- queue_builtin_action(bootchart_init_action, "bootchart_init");
-#endif
-
- for(;;) {
- int nr, i, timeout = -1;
-
- execute_one_command();
- restart_processes();
+ for (;;) {
+ if (!waiting_for_exec) {
+ execute_one_command();
+ restart_processes();
+ }
if (!property_set_fd_init && get_property_set_fd() > 0) {
ufds[fd_count].fd = get_property_set_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
- property_set_fd_init = 1;
+ property_set_fd_init = true;
}
if (!signal_fd_init && get_signal_fd() > 0) {
ufds[fd_count].fd = get_signal_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
- signal_fd_init = 1;
+ signal_fd_init = true;
}
if (!keychord_fd_init && get_keychord_fd() > 0) {
ufds[fd_count].fd = get_keychord_fd();
ufds[fd_count].events = POLLIN;
ufds[fd_count].revents = 0;
fd_count++;
- keychord_fd_init = 1;
+ keychord_fd_init = true;
}
+ int timeout = -1;
if (process_needs_restart) {
timeout = (process_needs_restart - gettime()) * 1000;
if (timeout < 0)
timeout = 0;
}
- if (!action_queue_empty() || cur_action)
+ if (!action_queue_empty() || cur_action) {
timeout = 0;
-
-#if BOOTCHART
- if (bootchart_count > 0) {
- if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
- timeout = BOOTCHART_POLLING_MS;
- if (bootchart_step() < 0 || --bootchart_count == 0) {
- bootchart_finish();
- bootchart_count = 0;
- }
}
-#endif
- nr = poll(ufds, fd_count, timeout);
- if (nr <= 0)
+ bootchart_sample(&timeout);
+
+ int nr = poll(ufds, fd_count, timeout);
+ if (nr <= 0) {
continue;
+ }
- for (i = 0; i < fd_count; i++) {
+ for (size_t i = 0; i < fd_count; i++) {
if (ufds[i].revents & POLLIN) {
- if (ufds[i].fd == get_property_set_fd())
+ if (ufds[i].fd == get_property_set_fd()) {
handle_property_set_fd();
- else if (ufds[i].fd == get_keychord_fd())
+ } else if (ufds[i].fd == get_keychord_fd()) {
handle_keychord();
- else if (ufds[i].fd == get_signal_fd())
+ } else if (ufds[i].fd == get_signal_fd()) {
handle_signal();
+ }
}
}
}
diff --git a/init/init.h b/init/init.h
index 736b75b..a104af6 100644
--- a/init/init.h
+++ b/init/init.h
@@ -17,11 +17,10 @@
#ifndef _INIT_INIT_H
#define _INIT_INIT_H
+#include <sys/types.h>
+
#include <cutils/list.h>
-
-#include <sys/stat.h>
-
-void handle_control_message(const char *msg, const char *arg);
+#include <cutils/iosched_policy.h>
struct command
{
@@ -29,10 +28,19 @@
struct listnode clist;
int (*func)(int nargs, char **args);
+
+ int line;
+ const char *filename;
+
int nargs;
char *args[1];
};
-
+
+struct trigger {
+ struct listnode nlist;
+ const char *name;
+};
+
struct action {
/* node in list of all actions */
struct listnode alist;
@@ -42,8 +50,9 @@
struct listnode tlist;
unsigned hash;
- const char *name;
-
+
+ /* list of actions which triggers the commands*/
+ struct listnode triggers;
struct listnode commands;
struct command *current;
};
@@ -64,26 +73,29 @@
const char *value;
};
-#define SVC_DISABLED 0x01 /* do not autostart with class */
-#define SVC_ONESHOT 0x02 /* do not restart on exit */
-#define SVC_RUNNING 0x04 /* currently active */
-#define SVC_RESTARTING 0x08 /* waiting to restart */
-#define SVC_CONSOLE 0x10 /* requires console */
-#define SVC_CRITICAL 0x20 /* will reboot into recovery if keeps crashing */
-#define SVC_RESET 0x40 /* Use when stopping a process, but not disabling
- so it can be restarted with its class */
-#define SVC_RC_DISABLED 0x80 /* Remember if the disabled flag was set in the rc script */
-#define SVC_RESTART 0x100 /* Use to safely restart (stop, wait, start) a service */
+#define SVC_DISABLED 0x001 // do not autostart with class
+#define SVC_ONESHOT 0x002 // do not restart on exit
+#define SVC_RUNNING 0x004 // currently active
+#define SVC_RESTARTING 0x008 // waiting to restart
+#define SVC_CONSOLE 0x010 // requires console
+#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
+#define SVC_RESET 0x040 // Use when stopping a process, but not disabling so it can be restarted with its class.
+#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
+#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
+#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
+#define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'.
#define NR_SVC_SUPP_GIDS 12 /* twelve supplementary groups */
#define COMMAND_RETRY_TIMEOUT 5
struct service {
+ void NotifyStateChange(const char* new_state);
+
/* list of all services */
struct listnode slist;
- const char *name;
+ char *name;
const char *classname;
unsigned flags;
@@ -91,25 +103,25 @@
time_t time_started; /* time of last start */
time_t time_crashed; /* first crash within inspection window */
int nr_crashed; /* number of times crashed within window */
-
+
uid_t uid;
gid_t gid;
gid_t supp_gids[NR_SVC_SUPP_GIDS];
size_t nr_supp_gids;
- char *seclabel;
+ const char* seclabel;
struct socketinfo *sockets;
struct svcenvinfo *envvars;
struct action onrestart; /* Actions to execute on restart. */
-
+
/* keycodes for triggering this service via /dev/keychord */
int *keycodes;
int nkeycodes;
int keychord_id;
- int ioprio_class;
+ IoSchedClass ioprio_class;
int ioprio_pri;
int nargs;
@@ -117,7 +129,13 @@
char *args[1];
}; /* ^-------'args' MUST be at the end of this struct! */
-void notify_service_state(const char *name, const char *state);
+extern bool waiting_for_exec;
+extern struct selabel_handle *sehandle;
+extern struct selabel_handle *sehandle_prop;
+
+void build_triggers_string(char *name_str, int length, struct action *cur_action);
+
+void handle_control_message(const char *msg, const char *arg);
struct service *service_find_by_name(const char *name);
struct service *service_find_by_pid(pid_t pid);
@@ -133,8 +151,8 @@
void service_start(struct service *svc, const char *dynamic_args);
void property_changed(const char *name, const char *value);
-extern struct selabel_handle *sehandle;
-extern struct selabel_handle *sehandle_prop;
-extern int selinux_reload_policy(void);
+int selinux_reload_policy(void);
+
+void zap_stdio(void);
#endif /* _INIT_INIT_H */
diff --git a/init/init_parser.c b/init/init_parser.cpp
similarity index 72%
rename from init/init_parser.c
rename to init/init_parser.cpp
index f49e698..f3d34b2 100644
--- a/init/init_parser.c
+++ b/init/init_parser.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -33,9 +34,6 @@
#include <cutils/iosched_policy.h>
#include <cutils/list.h>
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-#include <sys/_system_properties.h>
-
static list_declare(service_list);
static list_declare(action_list);
static list_declare(action_queue);
@@ -76,14 +74,53 @@
#define kw_func(kw) (keyword_info[kw].func)
#define kw_nargs(kw) (keyword_info[kw].nargs)
+void dump_parser_state() {
+ if (false) {
+ struct listnode* node;
+ list_for_each(node, &service_list) {
+ service* svc = node_to_item(node, struct service, slist);
+ INFO("service %s\n", svc->name);
+ INFO(" class '%s'\n", svc->classname);
+ INFO(" exec");
+ for (int n = 0; n < svc->nargs; n++) {
+ INFO(" '%s'", svc->args[n]);
+ }
+ INFO("\n");
+ for (socketinfo* si = svc->sockets; si; si = si->next) {
+ INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
+ }
+ }
+
+ list_for_each(node, &action_list) {
+ action* act = node_to_item(node, struct action, alist);
+ INFO("on ");
+ char name_str[256] = "";
+ build_triggers_string(name_str, sizeof(name_str), act);
+ INFO("%s", name_str);
+ INFO("\n");
+
+ struct listnode* node2;
+ list_for_each(node2, &act->commands) {
+ command* cmd = node_to_item(node2, struct command, clist);
+ INFO(" %p", cmd->func);
+ for (int n = 0; n < cmd->nargs; n++) {
+ INFO(" %s", cmd->args[n]);
+ }
+ INFO("\n");
+ }
+ INFO("\n");
+ }
+ }
+}
+
static int lookup_keyword(const char *s)
{
switch (*s++) {
+ case 'b':
+ if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
case 'c':
- if (!strcmp(s, "opy")) return K_copy;
+ if (!strcmp(s, "opy")) return K_copy;
if (!strcmp(s, "apability")) return K_capability;
- if (!strcmp(s, "hdir")) return K_chdir;
- if (!strcmp(s, "hroot")) return K_chroot;
if (!strcmp(s, "lass")) return K_class;
if (!strcmp(s, "lass_start")) return K_class_start;
if (!strcmp(s, "lass_stop")) return K_class_stop;
@@ -98,7 +135,9 @@
if (!strcmp(s, "omainname")) return K_domainname;
break;
case 'e':
+ if (!strcmp(s, "nable")) return K_enable;
if (!strcmp(s, "xec")) return K_exec;
+ if (!strcmp(s, "xeconce")) return K_execonce;
if (!strcmp(s, "xport")) return K_export;
break;
case 'g':
@@ -119,6 +158,7 @@
case 'l':
if (!strcmp(s, "oglevel")) return K_loglevel;
if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
+ if (!strcmp(s, "oad_all_props")) return K_load_all_props;
break;
case 'm':
if (!strcmp(s, "kdir")) return K_mkdir;
@@ -132,6 +172,7 @@
break;
case 'p':
if (!strcmp(s, "owerctl")) return K_powerctl;
+ break;
case 'r':
if (!strcmp(s, "estart")) return K_restart;
if (!strcmp(s, "estorecon")) return K_restorecon;
@@ -143,12 +184,9 @@
if (!strcmp(s, "eclabel")) return K_seclabel;
if (!strcmp(s, "ervice")) return K_service;
if (!strcmp(s, "etcon")) return K_setcon;
- if (!strcmp(s, "etenforce")) return K_setenforce;
if (!strcmp(s, "etenv")) return K_setenv;
- if (!strcmp(s, "etkey")) return K_setkey;
if (!strcmp(s, "etprop")) return K_setprop;
if (!strcmp(s, "etrlimit")) return K_setrlimit;
- if (!strcmp(s, "etsebool")) return K_setsebool;
if (!strcmp(s, "ocket")) return K_socket;
if (!strcmp(s, "tart")) return K_start;
if (!strcmp(s, "top")) return K_stop;
@@ -162,6 +200,9 @@
case 'u':
if (!strcmp(s, "ser")) return K_user;
break;
+ case 'v':
+ if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
+ break;
case 'w':
if (!strcmp(s, "rite")) return K_write;
if (!strcmp(s, "ait")) return K_wait;
@@ -170,8 +211,7 @@
return K_UNKNOWN;
}
-static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
-{
+static void parse_line_no_op(struct parse_state*, int, char**) {
}
static int push_chars(char **dst, int *len, const char *chars, int cnt)
@@ -188,19 +228,14 @@
int expand_props(char *dst, const char *src, int dst_size)
{
- int cnt = 0;
char *dst_ptr = dst;
const char *src_ptr = src;
- int src_len;
- int idx = 0;
int ret = 0;
int left = dst_size - 1;
if (!src || !dst || dst_size == 0)
return -1;
- src_len = strlen(src);
-
/* - variables can either be $x.y or ${x.y}, in case they are only part
* of the string.
* - will accept $$ as a literal $.
@@ -295,8 +330,7 @@
static void parse_import(struct parse_state *state, int nargs, char **args)
{
- struct listnode *import_list = state->priv;
- struct import *import;
+ struct listnode *import_list = (listnode*) state->priv;
char conf_file[PATH_MAX];
int ret;
@@ -312,7 +346,7 @@
return;
}
- import = calloc(1, sizeof(struct import));
+ struct import* import = (struct import*) calloc(1, sizeof(struct import));
import->filename = strdup(conf_file);
list_add_tail(import_list, &import->list);
INFO("found import '%s', adding to import list", import->filename);
@@ -345,7 +379,7 @@
state->parse_line = parse_line_no_op;
}
-static void parse_config(const char *fn, char *s)
+static void parse_config(const char *fn, const std::string& data)
{
struct parse_state state;
struct listnode import_list;
@@ -356,7 +390,7 @@
nargs = 0;
state.filename = fn;
state.line = 0;
- state.ptr = s;
+ state.ptr = strdup(data.c_str()); // TODO: fix this code!
state.nexttoken = 0;
state.parse_line = parse_line_no_op;
@@ -394,7 +428,6 @@
struct import *import = node_to_item(node, struct import, list);
int ret;
- INFO("importing '%s'", import->filename);
ret = init_parse_config_file(import->filename);
if (ret)
ERROR("could not import file '%s' from '%s'\n",
@@ -402,14 +435,15 @@
}
}
-int init_parse_config_file(const char *fn)
-{
- char *data;
- data = read_file(fn, 0);
- if (!data) return -1;
+int init_parse_config_file(const char* path) {
+ INFO("Parsing %s...\n", path);
+ std::string data;
+ if (!read_file(path, &data)) {
+ return -1;
+ }
- parse_config(fn, data);
- DUMP();
+ parse_config(path, data);
+ dump_parser_state();
return 0;
}
@@ -505,83 +539,95 @@
void action_for_each_trigger(const char *trigger,
void (*func)(struct action *act))
{
- struct listnode *node;
+ struct listnode *node, *node2;
struct action *act;
+ struct trigger *cur_trigger;
+
list_for_each(node, &action_list) {
act = node_to_item(node, struct action, alist);
- if (!strcmp(act->name, trigger)) {
- func(act);
+ list_for_each(node2, &act->triggers) {
+ cur_trigger = node_to_item(node2, struct trigger, nlist);
+ if (!strcmp(cur_trigger->name, trigger)) {
+ func(act);
+ }
}
}
}
+
void queue_property_triggers(const char *name, const char *value)
{
- struct listnode *node;
+ struct listnode *node, *node2;
struct action *act;
+ struct trigger *cur_trigger;
+ bool match;
+ int name_length;
+
list_for_each(node, &action_list) {
act = node_to_item(node, struct action, alist);
- if (!strncmp(act->name, "property:", strlen("property:"))) {
- const char *test = act->name + strlen("property:");
- int name_length = strlen(name);
+ match = !name;
+ list_for_each(node2, &act->triggers) {
+ cur_trigger = node_to_item(node2, struct trigger, nlist);
+ if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
+ const char *test = cur_trigger->name + strlen("property:");
+ if (!match) {
+ name_length = strlen(name);
+ if (!strncmp(name, test, name_length) &&
+ test[name_length] == '=' &&
+ (!strcmp(test + name_length + 1, value) ||
+ !strcmp(test + name_length + 1, "*"))) {
+ match = true;
+ continue;
+ }
+ } else {
+ const char* equals = strchr(test, '=');
+ if (equals) {
+ char prop_name[PROP_NAME_MAX + 1];
+ char value[PROP_VALUE_MAX];
+ int length = equals - test;
+ if (length <= PROP_NAME_MAX) {
+ int ret;
+ memcpy(prop_name, test, length);
+ prop_name[length] = 0;
- if (!strncmp(name, test, name_length) &&
- test[name_length] == '=' &&
- (!strcmp(test + name_length + 1, value) ||
- !strcmp(test + name_length + 1, "*"))) {
- action_add_queue_tail(act);
- }
+ /* does the property exist, and match the trigger value? */
+ ret = property_get(prop_name, value);
+ if (ret > 0 && (!strcmp(equals + 1, value) ||
+ !strcmp(equals + 1, "*"))) {
+ continue;
+ }
+ }
+ }
+ }
+ }
+ match = false;
+ break;
+ }
+ if (match) {
+ action_add_queue_tail(act);
}
}
}
void queue_all_property_triggers()
{
- struct listnode *node;
- struct action *act;
- list_for_each(node, &action_list) {
- act = node_to_item(node, struct action, alist);
- if (!strncmp(act->name, "property:", strlen("property:"))) {
- /* parse property name and value
- syntax is property:<name>=<value> */
- const char* name = act->name + strlen("property:");
- const char* equals = strchr(name, '=');
- if (equals) {
- char prop_name[PROP_NAME_MAX + 1];
- char value[PROP_VALUE_MAX];
- int length = equals - name;
- if (length > PROP_NAME_MAX) {
- ERROR("property name too long in trigger %s", act->name);
- } else {
- int ret;
- memcpy(prop_name, name, length);
- prop_name[length] = 0;
-
- /* does the property exist, and match the trigger value? */
- ret = property_get(prop_name, value);
- if (ret > 0 && (!strcmp(equals + 1, value) ||
- !strcmp(equals + 1, "*"))) {
- action_add_queue_tail(act);
- }
- }
- }
- }
- }
+ queue_property_triggers(NULL, NULL);
}
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
{
- struct action *act;
- struct command *cmd;
-
- act = calloc(1, sizeof(*act));
- act->name = name;
+ action* act = (action*) calloc(1, sizeof(*act));
+ trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
+ cur_trigger->name = name;
+ list_init(&act->triggers);
+ list_add_tail(&act->triggers, &cur_trigger->nlist);
list_init(&act->commands);
list_init(&act->qlist);
- cmd = calloc(1, sizeof(*cmd));
+ command* cmd = (command*) calloc(1, sizeof(*cmd));
cmd->func = func;
- cmd->args[0] = name;
+ cmd->args[0] = const_cast<char*>(name);
+ cmd->nargs = 1;
list_add_tail(&act->commands, &cmd->clist);
list_add_tail(&action_list, &act->alist);
@@ -613,9 +659,67 @@
return list_empty(&action_queue);
}
+service* make_exec_oneshot_service(int nargs, char** args) {
+ // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
+ int command_arg = 1;
+ for (int i = 1; i < nargs; ++i) {
+ if (strcmp(args[i], "--") == 0) {
+ command_arg = i + 1;
+ break;
+ }
+ }
+ if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
+ ERROR("exec called with too many supplementary group ids\n");
+ return NULL;
+ }
+
+ int argc = nargs - command_arg;
+ char** argv = (args + command_arg);
+ if (argc < 1) {
+ ERROR("exec called without command\n");
+ return NULL;
+ }
+
+ service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
+ if (svc == NULL) {
+ ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
+ return NULL;
+ }
+
+ if (command_arg > 2) {
+ svc->seclabel = args[1];
+ }
+ if (command_arg > 3) {
+ svc->uid = decode_uid(args[2]);
+ }
+ if (command_arg > 4) {
+ svc->gid = decode_uid(args[3]);
+ svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
+ for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
+ svc->supp_gids[i] = decode_uid(args[4 + i]);
+ }
+ }
+
+ static int exec_count; // Every service needs a unique name.
+ char* name = NULL;
+ asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
+ if (name == NULL) {
+ ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
+ free(svc);
+ return NULL;
+ }
+ svc->name = name;
+ svc->classname = "default";
+ svc->flags = SVC_EXEC | SVC_ONESHOT;
+ svc->nargs = argc;
+ memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
+ svc->args[argc] = NULL;
+ list_add_tail(&service_list, &svc->slist);
+ return svc;
+}
+
static void *parse_service(struct parse_state *state, int nargs, char **args)
{
- struct service *svc;
if (nargs < 3) {
parse_error(state, "services must have a name and a program\n");
return 0;
@@ -625,24 +729,27 @@
return 0;
}
- svc = service_find_by_name(args[1]);
+ service* svc = (service*) service_find_by_name(args[1]);
if (svc) {
parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
return 0;
}
nargs -= 2;
- svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
+ svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
if (!svc) {
parse_error(state, "out of memory\n");
return 0;
}
- svc->name = args[1];
+ svc->name = strdup(args[1]);
svc->classname = "default";
memcpy(svc->args, args + 2, sizeof(char*) * nargs);
+ trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
svc->args[nargs] = 0;
svc->nargs = nargs;
- svc->onrestart.name = "onrestart";
+ list_init(&svc->onrestart.triggers);
+ cur_trigger->name = "onrestart";
+ list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
list_init(&svc->onrestart.commands);
list_add_tail(&service_list, &svc->slist);
return svc;
@@ -650,7 +757,7 @@
static void parse_line_service(struct parse_state *state, int nargs, char **args)
{
- struct service *svc = state->context;
+ struct service *svc = (service*) state->context;
struct command *cmd;
int i, kw, kw_nargs;
@@ -719,7 +826,7 @@
if (nargs < 2) {
parse_error(state, "keycodes option requires atleast one keycode\n");
} else {
- svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
+ svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
if (!svc->keycodes) {
parse_error(state, "could not allocate keycodes\n");
} else {
@@ -748,7 +855,7 @@
break;
}
- cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+ cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
cmd->func = kw_func(kw);
cmd->nargs = nargs;
memcpy(cmd->args, args, sizeof(char*) * nargs);
@@ -758,12 +865,11 @@
svc->flags |= SVC_CRITICAL;
break;
case K_setenv: { /* name value */
- struct svcenvinfo *ei;
- if (nargs < 2) {
+ if (nargs < 3) {
parse_error(state, "setenv option requires name and value arguments\n");
break;
}
- ei = calloc(1, sizeof(*ei));
+ svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
if (!ei) {
parse_error(state, "out of memory\n");
break;
@@ -775,7 +881,6 @@
break;
}
case K_socket: {/* name type perm [ uid gid context ] */
- struct socketinfo *si;
if (nargs < 4) {
parse_error(state, "socket option requires name, type, perm arguments\n");
break;
@@ -785,7 +890,7 @@
parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
break;
}
- si = calloc(1, sizeof(*si));
+ socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
if (!si) {
parse_error(state, "out of memory\n");
break;
@@ -825,17 +930,29 @@
static void *parse_action(struct parse_state *state, int nargs, char **args)
{
- struct action *act;
+ struct trigger *cur_trigger;
+ int i;
if (nargs < 2) {
parse_error(state, "actions must have a trigger\n");
return 0;
}
- if (nargs > 2) {
- parse_error(state, "actions may not have extra parameters\n");
- return 0;
+
+ action* act = (action*) calloc(1, sizeof(*act));
+ list_init(&act->triggers);
+
+ for (i = 1; i < nargs; i++) {
+ if (!(i % 2)) {
+ if (strcmp(args[i], "&&")) {
+ parse_error(state, "& is the only symbol allowed to concatenate actions\n");
+ return 0;
+ } else
+ continue;
+ }
+ cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
+ cur_trigger->name = args[i];
+ list_add_tail(&act->triggers, &cur_trigger->nlist);
}
- act = calloc(1, sizeof(*act));
- act->name = args[1];
+
list_init(&act->commands);
list_init(&act->qlist);
list_add_tail(&action_list, &act->alist);
@@ -845,9 +962,7 @@
static void parse_line_action(struct parse_state* state, int nargs, char **args)
{
- struct command *cmd;
- struct action *act = state->context;
- int (*func)(int nargs, char **args);
+ struct action *act = (action*) state->context;
int kw, n;
if (nargs == 0) {
@@ -866,8 +981,10 @@
n > 2 ? "arguments" : "argument");
return;
}
- cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+ command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
cmd->func = kw_func(kw);
+ cmd->line = state->line;
+ cmd->filename = state->filename;
cmd->nargs = nargs;
memcpy(cmd->args, args, sizeof(char*) * nargs);
list_add_tail(&act->commands, &cmd->clist);
diff --git a/init/init_parser.h b/init/init_parser.h
index b078cad..6348607 100644
--- a/init/init_parser.h
+++ b/init/init_parser.h
@@ -20,6 +20,7 @@
#define INIT_PARSER_MAXARGS 64
struct action;
+struct service;
struct action *action_remove_queue_head(void);
void action_add_queue_tail(struct action *act);
@@ -28,9 +29,11 @@
int action_queue_empty(void);
void queue_property_triggers(const char *name, const char *value);
void queue_all_property_triggers();
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name);
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name);
int init_parse_config_file(const char *fn);
int expand_props(char *dst, const char *src, int len);
+service* make_exec_oneshot_service(int argc, char** argv);
+
#endif
diff --git a/init/init_parser_test.cpp b/init/init_parser_test.cpp
new file mode 100644
index 0000000..170a73a
--- /dev/null
+++ b/init/init_parser_test.cpp
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2015 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 "init_parser.h"
+
+#include "init.h"
+#include "util.h"
+
+#include <errno.h>
+#include <gtest/gtest.h>
+
+TEST(init_parser, make_exec_oneshot_service_invalid_syntax) {
+ char* argv[10];
+ memset(argv, 0, sizeof(argv));
+
+ // Nothing.
+ ASSERT_EQ(nullptr, make_exec_oneshot_service(0, argv));
+
+ // No arguments to 'exec'.
+ argv[0] = const_cast<char*>("exec");
+ ASSERT_EQ(nullptr, make_exec_oneshot_service(1, argv));
+
+ // No command in "exec --".
+ argv[1] = const_cast<char*>("--");
+ ASSERT_EQ(nullptr, make_exec_oneshot_service(2, argv));
+}
+
+TEST(init_parser, make_exec_oneshot_service_too_many_supplementary_gids) {
+ int argc = 0;
+ char* argv[4 + NR_SVC_SUPP_GIDS + 3];
+ argv[argc++] = const_cast<char*>("exec");
+ argv[argc++] = const_cast<char*>("seclabel");
+ argv[argc++] = const_cast<char*>("root"); // uid.
+ argv[argc++] = const_cast<char*>("root"); // gid.
+ for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
+ argv[argc++] = const_cast<char*>("root"); // Supplementary gid.
+ }
+ argv[argc++] = const_cast<char*>("--");
+ argv[argc++] = const_cast<char*>("/system/bin/id");
+ argv[argc] = nullptr;
+ ASSERT_EQ(nullptr, make_exec_oneshot_service(argc, argv));
+}
+
+static void Test_make_exec_oneshot_service(bool dash_dash, bool seclabel, bool uid, bool gid, bool supplementary_gids) {
+ int argc = 0;
+ char* argv[10];
+ argv[argc++] = const_cast<char*>("exec");
+ if (seclabel) {
+ argv[argc++] = const_cast<char*>("u:r:su:s0"); // seclabel
+ if (uid) {
+ argv[argc++] = const_cast<char*>("log"); // uid
+ if (gid) {
+ argv[argc++] = const_cast<char*>("shell"); // gid
+ if (supplementary_gids) {
+ argv[argc++] = const_cast<char*>("system"); // supplementary gid 0
+ argv[argc++] = const_cast<char*>("adb"); // supplementary gid 1
+ }
+ }
+ }
+ }
+ if (dash_dash) {
+ argv[argc++] = const_cast<char*>("--");
+ }
+ argv[argc++] = const_cast<char*>("/system/bin/toybox");
+ argv[argc++] = const_cast<char*>("id");
+ argv[argc] = nullptr;
+ service* svc = make_exec_oneshot_service(argc, argv);
+ ASSERT_NE(nullptr, svc);
+
+ if (seclabel) {
+ ASSERT_STREQ("u:r:su:s0", svc->seclabel);
+ } else {
+ ASSERT_EQ(nullptr, svc->seclabel);
+ }
+ if (uid) {
+ ASSERT_EQ(decode_uid("log"), svc->uid);
+ } else {
+ ASSERT_EQ(0U, svc->uid);
+ }
+ if (gid) {
+ ASSERT_EQ(decode_uid("shell"), svc->gid);
+ } else {
+ ASSERT_EQ(0U, svc->gid);
+ }
+ if (supplementary_gids) {
+ ASSERT_EQ(2U, svc->nr_supp_gids);
+ ASSERT_EQ(decode_uid("system"), svc->supp_gids[0]);
+ ASSERT_EQ(decode_uid("adb"), svc->supp_gids[1]);
+ } else {
+ ASSERT_EQ(0U, svc->nr_supp_gids);
+ }
+
+ ASSERT_EQ(2, svc->nargs);
+ ASSERT_EQ("/system/bin/toybox", svc->args[0]);
+ ASSERT_EQ("id", svc->args[1]);
+ ASSERT_EQ(nullptr, svc->args[2]);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_everything) {
+ Test_make_exec_oneshot_service(true, true, true, true, true);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid_gid) {
+ Test_make_exec_oneshot_service(true, true, true, true, false);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid) {
+ Test_make_exec_oneshot_service(true, true, true, false, false);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_seclabel) {
+ Test_make_exec_oneshot_service(true, true, false, false, false);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_just_command) {
+ Test_make_exec_oneshot_service(true, false, false, false, false);
+}
+
+TEST(init_parser, make_exec_oneshot_service_with_just_command_no_dash) {
+ Test_make_exec_oneshot_service(false, false, false, false, false);
+}
diff --git a/init/keychords.c b/init/keychords.cpp
similarity index 95%
rename from init/keychords.c
rename to init/keychords.cpp
index 4a64042..d6464bd 100644
--- a/init/keychords.c
+++ b/init/keychords.cpp
@@ -40,7 +40,7 @@
if (svc->keycodes) {
/* add a new keychord to the list */
size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]);
- keychords = realloc(keychords, keychords_length + size);
+ keychords = (input_keychord*) realloc(keychords, keychords_length + size);
if (!keychords) {
ERROR("could not allocate keychords\n");
keychords_length = 0;
@@ -72,12 +72,11 @@
if (!keychords)
return;
- fd = open("/dev/keychord", O_RDWR);
+ fd = open("/dev/keychord", O_RDWR | O_CLOEXEC);
if (fd < 0) {
ERROR("could not open /dev/keychord\n");
return;
}
- fcntl(fd, F_SETFD, FD_CLOEXEC);
ret = write(fd, keychords, keychords_length);
if (ret != keychords_length) {
diff --git a/init/keywords.h b/init/keywords.h
index 97fe50c..c8327c3 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -1,12 +1,12 @@
-
#ifndef KEYWORD
-int do_chroot(int nargs, char **args);
-int do_chdir(int nargs, char **args);
+int do_bootchart_init(int nargs, char **args);
int do_class_start(int nargs, char **args);
int do_class_stop(int nargs, char **args);
int do_class_reset(int nargs, char **args);
int do_domainname(int nargs, char **args);
+int do_enable(int nargs, char **args);
int do_exec(int nargs, char **args);
+int do_execonce(int nargs, char **args);
int do_export(int nargs, char **args);
int do_hostname(int nargs, char **args);
int do_ifup(int nargs, char **args);
@@ -21,11 +21,8 @@
int do_rm(int nargs, char **args);
int do_rmdir(int nargs, char **args);
int do_setcon(int nargs, char **args);
-int do_setenforce(int nargs, char **args);
-int do_setkey(int nargs, char **args);
int do_setprop(int nargs, char **args);
int do_setrlimit(int nargs, char **args);
-int do_setsebool(int nargs, char **args);
int do_start(int nargs, char **args);
int do_stop(int nargs, char **args);
int do_swapon_all(int nargs, char **args);
@@ -38,6 +35,8 @@
int do_chmod(int nargs, char **args);
int do_loglevel(int nargs, char **args);
int do_load_persist_props(int nargs, char **args);
+int do_load_all_props(int nargs, char **args);
+int do_verity_load_state(int nargs, char **args);
int do_wait(int nargs, char **args);
#define __MAKE_KEYWORD_ENUM__
#define KEYWORD(symbol, flags, nargs, func) K_##symbol,
@@ -45,8 +44,6 @@
K_UNKNOWN,
#endif
KEYWORD(capability, OPTION, 0, 0)
- KEYWORD(chdir, COMMAND, 1, do_chdir)
- KEYWORD(chroot, COMMAND, 1, do_chroot)
KEYWORD(class, OPTION, 0, 0)
KEYWORD(class_start, COMMAND, 1, do_class_start)
KEYWORD(class_stop, COMMAND, 1, do_class_stop)
@@ -55,7 +52,9 @@
KEYWORD(critical, OPTION, 0, 0)
KEYWORD(disabled, OPTION, 0, 0)
KEYWORD(domainname, COMMAND, 1, do_domainname)
+ KEYWORD(enable, COMMAND, 1, do_enable)
KEYWORD(exec, COMMAND, 1, do_exec)
+ KEYWORD(execonce, COMMAND, 1, do_execonce)
KEYWORD(export, COMMAND, 2, do_export)
KEYWORD(group, OPTION, 0, 0)
KEYWORD(hostname, COMMAND, 1, do_hostname)
@@ -78,12 +77,9 @@
KEYWORD(seclabel, OPTION, 0, 0)
KEYWORD(service, SECTION, 0, 0)
KEYWORD(setcon, COMMAND, 1, do_setcon)
- KEYWORD(setenforce, COMMAND, 1, do_setenforce)
KEYWORD(setenv, OPTION, 2, 0)
- KEYWORD(setkey, COMMAND, 0, do_setkey)
KEYWORD(setprop, COMMAND, 2, do_setprop)
KEYWORD(setrlimit, COMMAND, 3, do_setrlimit)
- KEYWORD(setsebool, COMMAND, 2, do_setsebool)
KEYWORD(socket, OPTION, 0, 0)
KEYWORD(start, COMMAND, 1, do_start)
KEYWORD(stop, COMMAND, 1, do_stop)
@@ -92,6 +88,7 @@
KEYWORD(symlink, COMMAND, 1, do_symlink)
KEYWORD(sysclktz, COMMAND, 1, do_sysclktz)
KEYWORD(user, OPTION, 0, 0)
+ KEYWORD(verity_load_state, COMMAND, 0, do_verity_load_state)
KEYWORD(wait, COMMAND, 1, do_wait)
KEYWORD(write, COMMAND, 2, do_write)
KEYWORD(copy, COMMAND, 2, do_copy)
@@ -99,11 +96,12 @@
KEYWORD(chmod, COMMAND, 2, do_chmod)
KEYWORD(loglevel, COMMAND, 1, do_loglevel)
KEYWORD(load_persist_props, COMMAND, 0, do_load_persist_props)
+ KEYWORD(load_all_props, COMMAND, 0, do_load_all_props)
KEYWORD(ioprio, OPTION, 0, 0)
+ KEYWORD(bootchart_init, COMMAND, 0, do_bootchart_init)
#ifdef __MAKE_KEYWORD_ENUM__
KEYWORD_COUNT,
};
#undef __MAKE_KEYWORD_ENUM__
#undef KEYWORD
#endif
-
diff --git a/init/log.h b/init/log.h
index 4aac3df..e9cb65a 100644
--- a/init/log.h
+++ b/init/log.h
@@ -23,6 +23,6 @@
#define NOTICE(x...) KLOG_NOTICE("init", x)
#define INFO(x...) KLOG_INFO("init", x)
-#define LOG_UEVENTS 0 /* log uevent messages if 1. verbose */
+extern int log_callback(int type, const char *fmt, ...);
#endif
diff --git a/init/parser.c b/init/parser.cpp
similarity index 73%
rename from init/parser.c
rename to init/parser.cpp
index 48e7aec..8193729 100644
--- a/init/parser.c
+++ b/init/parser.cpp
@@ -1,59 +1,17 @@
-#include <stdio.h>
+#include "parser.h"
+
#include <stdarg.h>
+#include <stdio.h>
#include <string.h>
-#include "parser.h"
#include "log.h"
-#define RAW(x...) log_write(6, x)
-
-void DUMP(void)
-{
-#if 0
- struct service *svc;
- struct action *act;
- struct command *cmd;
- struct listnode *node;
- struct listnode *node2;
- struct socketinfo *si;
- int n;
-
- list_for_each(node, &service_list) {
- svc = node_to_item(node, struct service, slist);
- RAW("service %s\n", svc->name);
- RAW(" class '%s'\n", svc->classname);
- RAW(" exec");
- for (n = 0; n < svc->nargs; n++) {
- RAW(" '%s'", svc->args[n]);
- }
- RAW("\n");
- for (si = svc->sockets; si; si = si->next) {
- RAW(" socket %s %s 0%o\n", si->name, si->type, si->perm);
- }
- }
-
- list_for_each(node, &action_list) {
- act = node_to_item(node, struct action, alist);
- RAW("on %s\n", act->name);
- list_for_each(node2, &act->commands) {
- cmd = node_to_item(node2, struct command, clist);
- RAW(" %p", cmd->func);
- for (n = 0; n < cmd->nargs; n++) {
- RAW(" %s", cmd->args[n]);
- }
- RAW("\n");
- }
- RAW("\n");
- }
-#endif
-}
-
void parse_error(struct parse_state *state, const char *fmt, ...)
{
va_list ap;
char buf[128];
int off;
-
+
snprintf(buf, 128, "%s: %d: ", state->filename, state->line);
buf[127] = 0;
off = strlen(buf);
diff --git a/init/parser.h b/init/parser.h
index a58272a..95e1164 100644
--- a/init/parser.h
+++ b/init/parser.h
@@ -33,7 +33,7 @@
void *priv;
};
-void DUMP(void);
+void dump_parser_state(void);
int next_token(struct parse_state *state);
void parse_error(struct parse_state *state, const char *fmt, ...);
diff --git a/init/property_service.c b/init/property_service.cpp
similarity index 71%
rename from init/property_service.c
rename to init/property_service.cpp
index 4af0c17..ddb8050 100644
--- a/init/property_service.c
+++ b/init/property_service.cpp
@@ -39,7 +39,6 @@
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/mman.h>
-#include <sys/atomics.h>
#include <private/android_filesystem_config.h>
#include <selinux/selinux.h>
@@ -57,72 +56,13 @@
static int property_set_fd = -1;
-/* White list of permissions for setting property services. */
-struct {
- const char *prefix;
- unsigned int uid;
- unsigned int gid;
-} property_perms[] = {
- { "net.rmnet0.", AID_RADIO, 0 },
- { "net.gprs.", AID_RADIO, 0 },
- { "net.ppp", AID_RADIO, 0 },
- { "net.qmi", AID_RADIO, 0 },
- { "net.lte", AID_RADIO, 0 },
- { "net.cdma", AID_RADIO, 0 },
- { "ril.", AID_RADIO, 0 },
- { "gsm.", AID_RADIO, 0 },
- { "persist.radio", AID_RADIO, 0 },
- { "net.dns", AID_RADIO, 0 },
- { "sys.usb.config", AID_RADIO, 0 },
- { "net.", AID_SYSTEM, 0 },
- { "dev.", AID_SYSTEM, 0 },
- { "runtime.", AID_SYSTEM, 0 },
- { "hw.", AID_SYSTEM, 0 },
- { "sys.", AID_SYSTEM, 0 },
- { "sys.powerctl", AID_SHELL, 0 },
- { "service.", AID_SYSTEM, 0 },
- { "wlan.", AID_SYSTEM, 0 },
- { "gps.", AID_GPS, 0 },
- { "bluetooth.", AID_BLUETOOTH, 0 },
- { "dhcp.", AID_SYSTEM, 0 },
- { "dhcp.", AID_DHCP, 0 },
- { "debug.", AID_SYSTEM, 0 },
- { "debug.", AID_SHELL, 0 },
- { "log.", AID_SHELL, 0 },
- { "service.adb.root", AID_SHELL, 0 },
- { "service.adb.tcp.port", AID_SHELL, 0 },
- { "persist.sys.", AID_SYSTEM, 0 },
- { "persist.service.", AID_SYSTEM, 0 },
- { "persist.security.", AID_SYSTEM, 0 },
- { "persist.gps.", AID_GPS, 0 },
- { "persist.service.bdroid.", AID_BLUETOOTH, 0 },
- { "selinux." , AID_SYSTEM, 0 },
- { NULL, 0, 0 }
-};
-
-/*
- * White list of UID that are allowed to start/stop services.
- * Currently there are no user apps that require.
- */
-struct {
- const char *service;
- unsigned int uid;
- unsigned int gid;
-} control_perms[] = {
- { "dumpstate",AID_SHELL, AID_LOG },
- { "ril-daemon",AID_RADIO, AID_RADIO },
- { "pre-recovery", AID_SYSTEM, AID_SYSTEM },
- {NULL, 0, 0 }
-};
-
-typedef struct {
+struct workspace {
size_t size;
int fd;
-} workspace;
+};
static int init_workspace(workspace *w, size_t size)
{
- void *data;
int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
if (fd < 0)
return -1;
@@ -157,8 +97,6 @@
return 1;
char *tctx = NULL;
- const char *class = "property_service";
- const char *perm = "set";
int result = 0;
if (!sctx)
@@ -170,7 +108,7 @@
if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
goto err;
- if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0)
+ if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
result = 1;
freecon(tctx);
@@ -196,61 +134,15 @@
}
/*
- * Checks permissions for starting/stoping system services.
- * AID_SYSTEM and AID_ROOT are always allowed.
- *
- * Returns 1 if uid allowed, 0 otherwise.
- */
-static int check_control_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx) {
-
- int i;
- if (uid == AID_SYSTEM || uid == AID_ROOT)
- return check_control_mac_perms(name, sctx);
-
- /* Search the ACL */
- for (i = 0; control_perms[i].service; i++) {
- if (strcmp(control_perms[i].service, name) == 0) {
- if ((uid && control_perms[i].uid == uid) ||
- (gid && control_perms[i].gid == gid)) {
- return check_control_mac_perms(name, sctx);
- }
- }
- }
- return 0;
-}
-
-/*
* Checks permissions for setting system properties.
* Returns 1 if uid allowed, 0 otherwise.
*/
-static int check_perms(const char *name, unsigned int uid, unsigned int gid, char *sctx)
+static int check_perms(const char *name, char *sctx)
{
- int i;
- unsigned int app_id;
-
if(!strncmp(name, "ro.", 3))
name +=3;
- if (uid == 0)
- return check_mac_perms(name, sctx);
-
- app_id = multiuser_get_app_id(uid);
- if (app_id == AID_BLUETOOTH) {
- uid = app_id;
- }
-
- for (i = 0; property_perms[i].prefix; i++) {
- if (strncmp(property_perms[i].prefix, name,
- strlen(property_perms[i].prefix)) == 0) {
- if ((uid && property_perms[i].uid == uid) ||
- (gid && property_perms[i].gid == gid)) {
-
- return check_mac_perms(name, sctx);
- }
- }
- }
-
- return 0;
+ return check_mac_perms(name, sctx);
}
int __property_get(const char *name, char *value)
@@ -271,6 +163,7 @@
return;
}
write(fd, value, strlen(value));
+ fsync(fd);
close(fd);
snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
@@ -362,7 +255,6 @@
prop_msg msg;
int s;
int r;
- int res;
struct ucred cr;
struct sockaddr_un addr;
socklen_t addr_size = sizeof(addr);
@@ -422,14 +314,14 @@
// Keep the old close-socket-early behavior when handling
// ctl.* properties.
close(s);
- if (check_control_perms(msg.value, cr.uid, cr.gid, source_ctx)) {
+ if (check_control_mac_perms(msg.value, source_ctx)) {
handle_control_message((char*) msg.name + 4, (char*) msg.value);
} else {
ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
}
} else {
- if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) {
+ if (check_perms(msg.name, source_ctx)) {
property_set((char*) msg.name, (char*) msg.value);
} else {
ERROR("sys_prop: permission denied uid:%d name:%s\n",
@@ -456,49 +348,77 @@
*sz = pa_workspace.size;
}
-static void load_properties(char *data, char *prefix)
-{
- char *key, *value, *eol, *sol, *tmp;
- size_t plen;
+static void load_properties_from_file(const char *, const char *);
- if (prefix)
- plen = strlen(prefix);
+/*
+ * Filter is used to decide which properties to load: NULL loads all keys,
+ * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
+ */
+static void load_properties(char *data, const char *filter)
+{
+ char *key, *value, *eol, *sol, *tmp, *fn;
+ size_t flen = 0;
+
+ if (filter) {
+ flen = strlen(filter);
+ }
+
sol = data;
- while((eol = strchr(sol, '\n'))) {
+ while ((eol = strchr(sol, '\n'))) {
key = sol;
*eol++ = 0;
sol = eol;
- value = strchr(key, '=');
- if(value == 0) continue;
- *value++ = 0;
+ while (isspace(*key)) key++;
+ if (*key == '#') continue;
- while(isspace(*key)) key++;
- if(*key == '#') continue;
- tmp = value - 2;
- while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
-
- if (prefix && strncmp(key, prefix, plen))
- continue;
-
- while(isspace(*value)) value++;
tmp = eol - 2;
- while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
+ while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
- property_set(key, value);
+ if (!strncmp(key, "import ", 7) && flen == 0) {
+ fn = key + 7;
+ while (isspace(*fn)) fn++;
+
+ key = strchr(fn, ' ');
+ if (key) {
+ *key++ = 0;
+ while (isspace(*key)) key++;
+ }
+
+ load_properties_from_file(fn, key);
+
+ } else {
+ value = strchr(key, '=');
+ if (!value) continue;
+ *value++ = 0;
+
+ tmp = value - 2;
+ while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
+
+ while (isspace(*value)) value++;
+
+ if (flen > 0) {
+ if (filter[flen - 1] == '*') {
+ if (strncmp(key, filter, flen - 1)) continue;
+ } else {
+ if (strcmp(key, filter)) continue;
+ }
+ }
+
+ property_set(key, value);
+ }
}
}
-static void load_properties_from_file(const char *fn, char *prefix)
+/*
+ * Filter is used to decide which properties to load: NULL loads all keys,
+ * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
+ */
+static void load_properties_from_file(const char *fn, const char *filter)
{
- char *data;
- unsigned sz;
-
- data = read_file(fn, &sz);
-
- if(data != 0) {
- load_properties(data, prefix);
- free(data);
+ std::string data;
+ if (read_file(fn, &data)) {
+ load_properties(&data[0], filter);
}
}
@@ -516,10 +436,8 @@
while ((entry = readdir(dir)) != NULL) {
if (strncmp("persist.", entry->d_name, strlen("persist.")))
continue;
-#if HAVE_DIRENT_D_TYPE
if (entry->d_type != DT_REG)
continue;
-#endif
/* open the file and read the property value */
fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
if (fd < 0) {
@@ -539,8 +457,9 @@
|| (sb.st_uid != 0)
|| (sb.st_gid != 0)
|| (sb.st_nlink != 1)) {
- ERROR("skipping insecure property file %s (uid=%lu gid=%lu nlink=%d mode=%o)\n",
- entry->d_name, sb.st_uid, sb.st_gid, sb.st_nlink, sb.st_mode);
+ ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
+ entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
+ (unsigned int)sb.st_nlink, sb.st_mode);
close(fd);
continue;
}
@@ -579,15 +498,13 @@
}
static void load_override_properties() {
-#ifdef ALLOW_LOCAL_PROP_OVERRIDE
- char debuggable[PROP_VALUE_MAX];
- int ret;
-
- ret = property_get("ro.debuggable", debuggable);
- if (ret && (strcmp(debuggable, "1") == 0)) {
- load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
+ if (ALLOW_LOCAL_PROP_OVERRIDE) {
+ char debuggable[PROP_VALUE_MAX];
+ int ret = property_get("ro.debuggable", debuggable);
+ if (ret && (strcmp(debuggable, "1") == 0)) {
+ load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
+ }
}
-#endif /* ALLOW_LOCAL_PROP_OVERRIDE */
}
@@ -603,17 +520,24 @@
load_persistent_properties();
}
+void load_all_props(void)
+{
+ load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
+ load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
+ load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
+ load_properties_from_file(PROP_PATH_BOOTIMAGE_BUILD, NULL);
+ load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
+
+ load_override_properties();
+
+ /* Read persistent properties after all default values have been loaded. */
+ load_persistent_properties();
+}
+
void start_property_service(void)
{
int fd;
- load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
- load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
- load_properties_from_file(PROP_PATH_FACTORY, "ro.");
- load_override_properties();
- /* Read persistent properties after all default values have been loaded. */
- load_persistent_properties();
-
fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
if(fd < 0) return;
fcntl(fd, F_SETFD, FD_CLOEXEC);
diff --git a/init/property_service.h b/init/property_service.h
index 46cbd8f..6e7fc00 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -17,13 +17,14 @@
#ifndef _INIT_PROPERTY_H
#define _INIT_PROPERTY_H
-#include <stdbool.h>
+#include <stddef.h>
#include <sys/system_properties.h>
extern void handle_property_set_fd(void);
extern void property_init(void);
extern void property_load_boot_defaults(void);
extern void load_persist_props(void);
+extern void load_all_props(void);
extern void start_property_service(void);
void get_property_workspace(int *fd, int *sz);
extern int __property_get(const char *name, char *value);
@@ -31,13 +32,19 @@
extern int properties_inited();
int get_property_set_fd(void);
+#ifndef __clang__
extern void __property_get_size_error()
__attribute__((__error__("property_get called with too small buffer")));
+#else
+extern void __property_get_size_error();
+#endif
static inline
__attribute__ ((always_inline))
__attribute__ ((gnu_inline))
+#ifndef __clang__
__attribute__ ((artificial))
+#endif
int property_get(const char *name, char *value)
{
size_t value_len = __builtin_object_size(value, 0);
diff --git a/init/readme.txt b/init/readme.txt
index 42a09cb..7443330 100644
--- a/init/readme.txt
+++ b/init/readme.txt
@@ -70,11 +70,11 @@
setenv <name> <value>
Set the environment variable <name> to <value> in the launched process.
-socket <name> <type> <perm> [ <user> [ <group> [ <context> ] ] ]
+socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]
Create a unix domain socket named /dev/socket/<name> and pass
its fd to the launched process. <type> must be "dgram", "stream" or "seqpacket".
User and group default to 0.
- Context is the SELinux security context for the socket.
+ 'seclabel' is the SELinux security context for the socket.
It defaults to the service security context, as specified by seclabel or
computed based on the service executable file security context.
@@ -91,8 +91,8 @@
supplemental groups of the process (via setgroups()).
Currently defaults to root. (??? probably should default to nobody)
-seclabel <securitycontext>
- Change to securitycontext before exec'ing this service.
+seclabel <seclabel>
+ Change to 'seclabel' before exec'ing this service.
Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
Services on the system partition can instead use policy-defined transitions
based on their file security context.
@@ -110,6 +110,7 @@
onrestart
Execute a Command (see below) when service restarts.
+
Triggers
--------
Triggers are strings which can be used to match certain kinds
@@ -123,23 +124,31 @@
Triggers of this form occur when the property <name> is set
to the specific value <value>.
-device-added-<path>
-device-removed-<path>
- Triggers of these forms occur when a device node is added
- or removed.
+ One can also test multiple properties to execute a group
+ of commands. For example:
-service-exited-<name>
- Triggers of this form occur when the specified service exits.
+ on property:test.a=1 && property:test.b=1
+ setprop test.c 1
+
+ The above stub sets test.c to 1 only when
+ both test.a=1 and test.b=1
Commands
--------
-exec <path> [ <argument> ]*
+exec [ <seclabel> [ <user> [ <group> ]* ] ] -- <command> [ <argument> ]*
+ Fork and execute command with the given arguments. The command starts
+ after "--" so that an optional security context, user, and supplementary
+ groups can be provided. No other commands will be run until this one
+ finishes.
+
+execonce <path> [ <argument> ]*
Fork and execute a program (<path>). This will block until
- the program completes execution. It is best to avoid exec
- as unlike the builtin commands, it runs the risk of getting
- init "stuck". (??? maybe there should be a timeout?)
+ the program completes execution. This command can be run at most
+ once during init's lifetime. Subsequent invocations are ignored.
+ It is best to avoid execonce as unlike the builtin commands, it runs
+ the risk of getting init "stuck".
export <name> <value>
Set the environment variable <name> equal to <value> in the
@@ -155,18 +164,12 @@
hostname <name>
Set the host name.
-chdir <directory>
- Change working directory.
-
chmod <octal-mode> <path>
Change file access permissions.
chown <owner> <group> <path>
Change file owner and group.
-chroot <directory>
- Change process root directory.
-
class_start <serviceclass>
Start all services of the specified class if they are
not already running.
@@ -178,19 +181,34 @@
domainname <name>
Set the domain name.
+enable <servicename>
+ Turns a disabled service into an enabled one as if the service did not
+ specify disabled.
+ If the service is supposed to be running, it will be started now.
+ Typically used when the bootloader sets a variable that indicates a specific
+ service should be started when needed. E.g.
+ on property:ro.boot.myfancyhardware=1
+ enable my_fancy_service_for_my_fancy_hardware
+
insmod <path>
Install the module at <path>
+loglevel <level>
+ Sets the kernel log level to level. Properties are expanded within <level>.
+
mkdir <path> [mode] [owner] [group]
Create a directory at <path>, optionally with the given mode, owner, and
group. If not provided, the directory is created with permissions 755 and
- owned by the root user and root group.
+ owned by the root user and root group. If provided, the mode, owner and group
+ will be updated if the directory exists already.
-mount <type> <device> <dir> [ <mountoption> ]*
+mount <type> <device> <dir> [ <flag> ]* [<options>]
Attempt to mount the named device at the directory <dir>
<device> may be of the form mtd@name to specify a mtd block
device by name.
- <mountoption>s include "ro", "rw", "remount", "noatime", ...
+ <flag>s include "ro", "rw", "remount", "noatime", ...
+ <options> include "barrier=1", "noauto_da_alloc", "discard", ... as
+ a comma separated string, eg: barrier=1,noauto_da_alloc
restorecon <path> [ <path> ]*
Restore the file named by <path> to the security context specified
@@ -201,31 +219,19 @@
restorecon_recursive <path> [ <path> ]*
Recursively restore the directory tree named by <path> to the
security contexts specified in the file_contexts configuration.
- Do NOT use this with paths leading to shell-writable or app-writable
- directories, e.g. /data/local/tmp, /data/data or any prefix thereof.
-setcon <securitycontext>
+setcon <seclabel>
Set the current process security context to the specified string.
This is typically only used from early-init to set the init context
before any other process is started.
-setenforce 0|1
- Set the SELinux system-wide enforcing status.
- 0 is permissive (i.e. log but do not deny), 1 is enforcing.
-
-setkey
- TBD
-
setprop <name> <value>
- Set system property <name> to <value>.
+ Set system property <name> to <value>. Properties are expanded
+ within <value>.
setrlimit <resource> <cur> <max>
Set the rlimit for a resource.
-setsebool <name> <value>
- Set SELinux boolean <name> to <value>.
- <value> may be 1|true|on or 0|false|off
-
start <service>
Start a service running if it is not already running.
@@ -247,9 +253,10 @@
or the timeout has been reached. If timeout is not specified it
currently defaults to five seconds.
-write <path> <string> [ <string> ]*
- Open the file at <path> and write one or more strings
- to it with write(2)
+write <path> <content>
+ Open the file at <path> and write a string to it with write(2).
+ If the file does not exist, it will be created. If it does exist,
+ it will be truncated. Properties are expanded within <content>.
Properties
@@ -257,7 +264,7 @@
Init updates some system properties to provide some insight into
what it's doing:
-init.action
+init.action
Equal to the name of the action currently being executed or "" if none
init.command
@@ -267,73 +274,59 @@
State of a named service ("stopped", "running", "restarting")
-Example init.conf
------------------
+Bootcharting
+------------
-# not complete -- just providing some examples of usage
-#
-on boot
- export PATH /sbin:/system/sbin:/system/bin
- export LD_LIBRARY_PATH /system/lib
+This version of init contains code to perform "bootcharting": generating log
+files that can be later processed by the tools provided by www.bootchart.org.
- mkdir /dev
- mkdir /proc
- mkdir /sys
+On the emulator, use the new -bootchart <timeout> option to boot with
+bootcharting activated for <timeout> seconds.
- mount tmpfs tmpfs /dev
- mkdir /dev/pts
- mkdir /dev/socket
- mount devpts devpts /dev/pts
- mount proc proc /proc
- mount sysfs sysfs /sys
+On a device, create /data/bootchart/start with a command like the following:
- write /proc/cpu/alignment 4
+ adb shell 'echo $TIMEOUT > /data/bootchart/start'
- ifup lo
+Where the value of $TIMEOUT corresponds to the desired bootcharted period in
+seconds. Bootcharting will stop after that many seconds have elapsed.
+You can also stop the bootcharting at any moment by doing the following:
- hostname localhost
- domainname localhost
+ adb shell 'echo 1 > /data/bootchart/stop'
- mount yaffs2 mtd@system /system
- mount yaffs2 mtd@userdata /data
+Note that /data/bootchart/stop is deleted automatically by init at the end of
+the bootcharting. This is not the case with /data/bootchart/start, so don't
+forget to delete it when you're done collecting data.
- import /system/etc/init.conf
+The log files are written to /data/bootchart/. A script is provided to
+retrieve them and create a bootchart.tgz file that can be used with the
+bootchart command-line utility:
- class_start default
+ sudo apt-get install pybootchartgui
+ ANDROID_SERIAL=<device serial number>
+ $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
-service adbd /sbin/adbd
- user adb
- group adb
-service usbd /system/bin/usbd -r
- user usbd
- group usbd
- socket usbd 666
-
-service zygote /system/bin/app_process -Xzygote /system/bin --zygote
- socket zygote 666
-
-service runtime /system/bin/runtime
- user system
- group system
-
-on device-added-/dev/compass
- start akmd
-
-on device-removed-/dev/compass
- stop akmd
-
-service akmd /sbin/akmd
- disabled
- user akmd
- group akmd
-
-Debugging notes
----------------
+Debugging init
+--------------
By default, programs executed by init will drop stdout and stderr into
/dev/null. To help with debugging, you can execute your program via the
-Andoird program logwrapper. This will redirect stdout/stderr into the
+Android program logwrapper. This will redirect stdout/stderr into the
Android logging system (accessed via logcat).
For example
service akmd /system/bin/logwrapper /sbin/akmd
+
+For quicker turnaround when working on init itself, use:
+
+ mm -j
+ m ramdisk-nodeps
+ m bootimage-nodeps
+ adb reboot bootloader
+ fastboot boot $ANDROID_PRODUCT_OUT/boot.img
+
+Alternatively, use the emulator:
+
+ emulator -partition-size 1024 -verbose -show-kernel -no-window
+
+You might want to call klog_set_level(6) after the klog_init() call
+so you see the kernel logging in dmesg (or the emulator output).
diff --git a/init/signal_handler.c b/init/signal_handler.cpp
similarity index 70%
rename from init/signal_handler.c
rename to init/signal_handler.cpp
index 7e8e1a7..c428b96 100644
--- a/init/signal_handler.c
+++ b/init/signal_handler.cpp
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-#include <stdio.h>
#include <errno.h>
-#include <signal.h>
-#include <unistd.h>
#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
@@ -27,35 +27,28 @@
#include <cutils/list.h>
#include "init.h"
-#include "util.h"
#include "log.h"
+#include "util.h"
static int signal_fd = -1;
static int signal_recv_fd = -1;
-static void sigchld_handler(int s)
-{
+static void sigchld_handler(int s) {
write(signal_fd, &s, 1);
}
#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
-#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery*/
+#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */
-static int wait_for_one_process(int block)
-{
- pid_t pid;
+static int wait_for_one_process() {
int status;
- struct service *svc;
- struct socketinfo *si;
- time_t now;
- struct listnode *node;
- struct command *cmd;
-
- while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR );
- if (pid <= 0) return -1;
+ pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
+ if (pid <= 0) {
+ return -1;
+ }
INFO("waitpid returned pid %d, status = %08x\n", pid, status);
- svc = service_find_by_pid(pid);
+ service* svc = service_find_by_pid(pid);
if (!svc) {
if (WIFEXITED(status)) {
ERROR("untracked pid %d exited with status %d\n", pid, WEXITSTATUS(status));
@@ -69,36 +62,47 @@
return 0;
}
+ // TODO: all the code from here down should be a member function on service.
+
NOTICE("process '%s', pid %d exited\n", svc->name, pid);
if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) {
- kill(-pid, SIGKILL);
NOTICE("process '%s' killing any children in process group\n", svc->name);
+ kill(-pid, SIGKILL);
}
- /* remove any sockets we may have created */
- for (si = svc->sockets; si; si = si->next) {
+ // Remove any sockets we may have created.
+ for (socketinfo* si = svc->sockets; si; si = si->next) {
char tmp[128];
snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name);
unlink(tmp);
}
+ if (svc->flags & SVC_EXEC) {
+ INFO("SVC_EXEC pid %d finished...\n", svc->pid);
+ waiting_for_exec = false;
+ list_remove(&svc->slist);
+ free(svc->name);
+ free(svc);
+ return 0;
+ }
+
svc->pid = 0;
svc->flags &= (~SVC_RUNNING);
- /* oneshot processes go into the disabled state on exit,
- * except when manually restarted. */
+ // Oneshot processes go into the disabled state on exit,
+ // except when manually restarted.
if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) {
svc->flags |= SVC_DISABLED;
}
- /* disabled and reset processes do not get restarted automatically */
- if (svc->flags & (SVC_DISABLED | SVC_RESET) ) {
- notify_service_state(svc->name, "stopped");
+ // Disabled and reset processes do not get restarted automatically.
+ if (svc->flags & (SVC_DISABLED | SVC_RESET)) {
+ svc->NotifyStateChange("stopped");
return 0;
}
- now = gettime();
+ time_t now = gettime();
if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) {
if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) {
if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) {
@@ -117,49 +121,41 @@
svc->flags &= (~SVC_RESTART);
svc->flags |= SVC_RESTARTING;
- /* Execute all onrestart commands for this service. */
+ // Execute all onrestart commands for this service.
+ struct listnode* node;
list_for_each(node, &svc->onrestart.commands) {
- cmd = node_to_item(node, struct command, clist);
+ command* cmd = node_to_item(node, struct command, clist);
cmd->func(cmd->nargs, cmd->args);
}
- notify_service_state(svc->name, "restarting");
+ svc->NotifyStateChange("restarting");
return 0;
}
-void handle_signal(void)
-{
+void handle_signal() {
+ // We got a SIGCHLD - reap and restart as needed.
char tmp[32];
-
- /* we got a SIGCHLD - reap and restart as needed */
read(signal_recv_fd, tmp, sizeof(tmp));
- while (!wait_for_one_process(0))
- ;
+ while (!wait_for_one_process()) {
+ }
}
-void signal_init(void)
-{
- int s[2];
-
+void signal_init() {
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sigchld_handler;
act.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &act, 0);
- /* create a signalling mechanism for the sigchld handler */
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0) {
+ // Create a signalling mechanism for the sigchld handler.
+ int s[2];
+ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == 0) {
signal_fd = s[0];
signal_recv_fd = s[1];
- fcntl(s[0], F_SETFD, FD_CLOEXEC);
- fcntl(s[0], F_SETFL, O_NONBLOCK);
- fcntl(s[1], F_SETFD, FD_CLOEXEC);
- fcntl(s[1], F_SETFL, O_NONBLOCK);
}
handle_signal();
}
-int get_signal_fd()
-{
+int get_signal_fd() {
return signal_recv_fd;
}
diff --git a/init/ueventd.c b/init/ueventd.cpp
similarity index 82%
rename from init/ueventd.c
rename to init/ueventd.cpp
index 3d01836..5af6e3d 100644
--- a/init/ueventd.c
+++ b/init/ueventd.cpp
@@ -21,6 +21,7 @@
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
+#include <selinux/selinux.h>
#include <private/android_filesystem_config.h>
@@ -29,28 +30,13 @@
#include "util.h"
#include "devices.h"
#include "ueventd_parser.h"
-
-static char hardware[32];
-static unsigned revision = 0;
-
-static void import_kernel_nv(char *name, int in_qemu)
-{
- if (*name != '\0') {
- char *value = strchr(name, '=');
- if (value != NULL) {
- *value++ = 0;
- if (!strcmp(name,"androidboot.hardware"))
- {
- strlcpy(hardware, value, sizeof(hardware));
- }
- }
- }
-}
+#include "property_service.h"
int ueventd_main(int argc, char **argv)
{
struct pollfd ufd;
int nr;
+ char hardware[PROP_VALUE_MAX];
char tmp[32];
/*
@@ -69,15 +55,20 @@
open_devnull_stdio();
klog_init();
+ if (LOG_UEVENTS) {
+ /* Ensure we're at a logging level that will show the events */
+ if (klog_get_level() < KLOG_INFO_LEVEL) {
+ klog_set_level(KLOG_INFO_LEVEL);
+ }
+ }
+
+ union selinux_callback cb;
+ cb.func_log = log_callback;
+ selinux_set_callback(SELINUX_CB_LOG, cb);
INFO("starting ueventd\n");
- /* Respect hardware passed in through the kernel cmd line. Here we will look
- * for androidboot.hardware param in kernel cmdline, and save its value in
- * hardware[]. */
- import_kernel_cmdline(0, import_kernel_nv);
-
- get_hardware_name(hardware, &revision);
+ property_get("ro.hardware", hardware);
ueventd_parse_config_file("/ueventd.rc");
@@ -97,6 +88,8 @@
if (ufd.revents & POLLIN)
handle_device_fd();
}
+
+ return 0;
}
static int get_android_id(const char *id)
@@ -116,6 +109,7 @@
uid_t uid;
gid_t gid;
int prefix = 0;
+ int wildcard = 0;
char *endptr;
int ret;
char *tmp = 0;
@@ -148,9 +142,13 @@
name = tmp;
} else {
int len = strlen(name);
- if (name[len - 1] == '*') {
+ char *wildcard_chr = strchr(name, '*');
+ if ((name[len - 1] == '*') &&
+ (wildcard_chr == (name + len - 1))) {
prefix = 1;
name[len - 1] = '\0';
+ } else if (wildcard_chr) {
+ wildcard = 1;
}
}
@@ -177,6 +175,6 @@
}
gid = ret;
- add_dev_perms(name, attr, perm, uid, gid, prefix);
+ add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
free(tmp);
}
diff --git a/init/ueventd.h b/init/ueventd.h
index 0a454c5..d12d7fe 100644
--- a/init/ueventd.h
+++ b/init/ueventd.h
@@ -20,16 +20,18 @@
#include <cutils/list.h>
#include <sys/types.h>
+enum devname_src_t {
+ DEVNAME_UNKNOWN = 0,
+ DEVNAME_UEVENT_DEVNAME,
+ DEVNAME_UEVENT_DEVPATH,
+};
+
struct ueventd_subsystem {
struct listnode slist;
const char *name;
- enum {
- DEVNAME_UNKNOWN = 0,
- DEVNAME_UEVENT_DEVNAME,
- DEVNAME_UEVENT_DEVPATH,
- } devname_src;
const char *dirname;
+ devname_src_t devname_src;
};
int ueventd_main(int argc, char **argv);
diff --git a/init/ueventd_parser.c b/init/ueventd_parser.cpp
similarity index 88%
rename from init/ueventd_parser.c
rename to init/ueventd_parser.cpp
index e447006..7a4841f 100644
--- a/init/ueventd_parser.c
+++ b/init/ueventd_parser.cpp
@@ -67,9 +67,7 @@
return K_UNKNOWN;
}
-static void parse_line_no_op(struct parse_state *state __attribute__((unused)),
- int nargs __attribute__((unused)), char **args __attribute__((unused)))
-{
+static void parse_line_no_op(struct parse_state*, int, char**) {
}
static int valid_name(const char *name)
@@ -97,24 +95,20 @@
return 0;
}
-static void *parse_subsystem(struct parse_state *state,
- int nargs __attribute__((unused)), char **args)
-{
- struct ueventd_subsystem *s;
-
+static void *parse_subsystem(parse_state* state, int /*nargs*/, char** args) {
if (!valid_name(args[1])) {
parse_error(state, "invalid subsystem name '%s'\n", args[1]);
return 0;
}
- s = ueventd_subsystem_find_by_name(args[1]);
+ ueventd_subsystem* s = ueventd_subsystem_find_by_name(args[1]);
if (s) {
parse_error(state, "ignored duplicate definition of subsystem '%s'\n",
args[1]);
return 0;
}
- s = calloc(1, sizeof(*s));
+ s = (ueventd_subsystem*) calloc(1, sizeof(*s));
if (!s) {
parse_error(state, "out of memory\n");
return 0;
@@ -128,7 +122,7 @@
static void parse_line_subsystem(struct parse_state *state, int nargs,
char **args)
{
- struct ueventd_subsystem *s = state->context;
+ struct ueventd_subsystem *s = (ueventd_subsystem*) state->context;
int kw;
if (nargs == 0) {
@@ -197,7 +191,7 @@
}
}
-static void parse_config(const char *fn, char *s)
+static void parse_config(const char *fn, const std::string& data)
{
struct parse_state state;
char *args[UEVENTD_PARSER_MAXARGS];
@@ -205,7 +199,7 @@
nargs = 0;
state.filename = fn;
state.line = 1;
- state.ptr = s;
+ state.ptr = strdup(data.c_str()); // TODO: fix this code!
state.nexttoken = 0;
state.parse_line = parse_line_no_op;
for (;;) {
@@ -232,17 +226,16 @@
int ueventd_parse_config_file(const char *fn)
{
- char *data;
- data = read_file(fn, 0);
- if (!data) return -1;
+ std::string data;
+ if (!read_file(fn, &data)) {
+ return -1;
+ }
parse_config(fn, data);
- DUMP();
+ dump_parser_state();
return 0;
}
-static void parse_line_device(struct parse_state *state __attribute__((unused)),
- int nargs, char **args)
-{
+static void parse_line_device(parse_state*, int nargs, char** args) {
set_device_permission(nargs, args);
}
diff --git a/init/util.c b/init/util.cpp
similarity index 80%
rename from init/util.c
rename to init/util.cpp
index 0f69e1c..8b238d4 100644
--- a/init/util.c
+++ b/init/util.cpp
@@ -32,6 +32,8 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include <base/file.h>
+
/* for ANDROID_SOCKET_* */
#include <cutils/sockets.h>
@@ -146,48 +148,42 @@
return -1;
}
-/* reads a file, making sure it is terminated with \n \0 */
-void *read_file(const char *fn, unsigned *_sz)
-{
- char *data;
- int sz;
- int fd;
+bool read_file(const char* path, std::string* content) {
+ content->clear();
+
+ int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
+ if (fd == -1) {
+ return false;
+ }
+
+ // For security reasons, disallow world-writable
+ // or group-writable files.
struct stat sb;
-
- data = 0;
- fd = open(fn, O_RDONLY);
- if(fd < 0) return 0;
-
- // for security reasons, disallow world-writable
- // or group-writable files
- if (fstat(fd, &sb) < 0) {
- ERROR("fstat failed for '%s'\n", fn);
- goto oops;
+ if (fstat(fd, &sb) == -1) {
+ ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
+ return false;
}
if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
- ERROR("skipping insecure file '%s'\n", fn);
- goto oops;
+ ERROR("skipping insecure file '%s'\n", path);
+ return false;
}
- sz = lseek(fd, 0, SEEK_END);
- if(sz < 0) goto oops;
+ bool okay = android::base::ReadFdToString(fd, content);
+ TEMP_FAILURE_RETRY(close(fd));
+ if (okay) {
+ content->append("\n", 1);
+ }
+ return okay;
+}
- if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
-
- data = (char*) malloc(sz + 2);
- if(data == 0) goto oops;
-
- if(read(fd, data, sz) != sz) goto oops;
- close(fd);
- data[sz] = '\n';
- data[sz+1] = 0;
- if(_sz) *_sz = sz;
- return data;
-
-oops:
- close(fd);
- if(data != 0) free(data);
- return 0;
+int write_file(const char* path, const char* content) {
+ int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
+ if (fd == -1) {
+ return -errno;
+ }
+ int result = android::base::WriteStringToFd(content, fd) ? 0 : -errno;
+ TEMP_FAILURE_RETRY(close(fd));
+ return result;
}
#define MAX_MTD_PARTITIONS 16
@@ -207,7 +203,7 @@
ssize_t pmtdsize;
int r;
- fd = open("/proc/mtd", O_RDONLY);
+ fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
if (fd < 0)
return;
@@ -329,9 +325,9 @@
if (!s)
return;
- for (; *s; s++) {
+ while (*s) {
s += strspn(s, accept);
- if (*s) *s = '_';
+ if (*s) *s++ = '_';
}
}
@@ -404,74 +400,6 @@
exit(1);
}
-void get_hardware_name(char *hardware, unsigned int *revision)
-{
- const char *cpuinfo = "/proc/cpuinfo";
- char *data = NULL;
- size_t len = 0, limit = 1024;
- int fd, n;
- char *x, *hw, *rev;
-
- /* Hardware string was provided on kernel command line */
- if (hardware[0])
- return;
-
- fd = open(cpuinfo, O_RDONLY);
- if (fd < 0) return;
-
- for (;;) {
- x = realloc(data, limit);
- if (!x) {
- ERROR("Failed to allocate memory to read %s\n", cpuinfo);
- goto done;
- }
- data = x;
-
- n = read(fd, data + len, limit - len);
- if (n < 0) {
- ERROR("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno);
- goto done;
- }
- len += n;
-
- if (len < limit)
- break;
-
- /* We filled the buffer, so increase size and loop to read more */
- limit *= 2;
- }
-
- data[len] = 0;
- hw = strstr(data, "\nHardware");
- rev = strstr(data, "\nRevision");
-
- if (hw) {
- x = strstr(hw, ": ");
- if (x) {
- x += 2;
- n = 0;
- while (*x && *x != '\n') {
- if (!isspace(*x))
- hardware[n++] = tolower(*x);
- x++;
- if (n == 31) break;
- }
- hardware[n] = 0;
- }
- }
-
- if (rev) {
- x = strstr(rev, ": ");
- if (x) {
- *revision = strtoul(x + 2, 0, 16);
- }
- }
-
-done:
- close(fd);
- free(data);
-}
-
void import_kernel_cmdline(int in_qemu,
void (*import_kernel_nv)(char *name, int in_qemu))
{
@@ -479,7 +407,7 @@
char *ptr;
int fd;
- fd = open("/proc/cmdline", O_RDONLY);
+ fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
int n = read(fd, cmdline, sizeof(cmdline) - 1);
if (n < 0) n = 0;
diff --git a/init/util.h b/init/util.h
index 04b8129..e0b3c69 100644
--- a/init/util.h
+++ b/init/util.h
@@ -20,14 +20,19 @@
#include <sys/stat.h>
#include <sys/types.h>
+#include <string>
+
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
-static const char *coldboot_done = "/dev/.coldboot_done";
+#define COLDBOOT_DONE "/dev/.coldboot_done"
int mtd_name_to_number(const char *name);
int create_socket(const char *name, int type, mode_t perm,
uid_t uid, gid_t gid, const char *socketcon);
-void *read_file(const char *fn, unsigned *_sz);
+
+bool read_file(const char* path, std::string* content);
+int write_file(const char* path, const char* content);
+
time_t gettime(void);
unsigned int decode_uid(const char *s);
@@ -37,7 +42,6 @@
void remove_link(const char *oldpath, const char *newpath);
int wait_for_file(const char *filename, int timeout);
void open_devnull_stdio(void);
-void get_hardware_name(char *hardware, unsigned int *revision);
void import_kernel_cmdline(int in_qemu, void (*import_kernel_nv)(char *name, int in_qemu));
int make_dir(const char *path, mode_t mode);
int restorecon(const char *pathname);
diff --git a/init/util_test.cpp b/init/util_test.cpp
new file mode 100644
index 0000000..5b3ab50
--- /dev/null
+++ b/init/util_test.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 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 "util.h"
+
+#include <errno.h>
+#include <gtest/gtest.h>
+
+TEST(util, read_file_ENOENT) {
+ std::string s("hello");
+ errno = 0;
+ EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
+ EXPECT_EQ(ENOENT, errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(util, read_file_success) {
+ std::string s("hello");
+ EXPECT_TRUE(read_file("/proc/version", &s));
+ EXPECT_GT(s.length(), 6U);
+ EXPECT_EQ('\n', s[s.length() - 1]);
+ s[5] = 0;
+ EXPECT_STREQ("Linux", s.c_str());
+}
+
+TEST(util, decode_uid) {
+ EXPECT_EQ(0U, decode_uid("root"));
+ EXPECT_EQ(-1U, decode_uid("toot"));
+ EXPECT_EQ(123U, decode_uid("123"));
+}
diff --git a/init/watchdogd.c b/init/watchdogd.cpp
similarity index 96%
rename from init/watchdogd.c
rename to init/watchdogd.cpp
index fb53836..0790811 100644
--- a/init/watchdogd.c
+++ b/init/watchdogd.cpp
@@ -18,6 +18,7 @@
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <linux/watchdog.h>
@@ -47,7 +48,7 @@
timeout = interval + margin;
- fd = open(DEV_NAME, O_RDWR);
+ fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
if (fd < 0) {
ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
return 1;
diff --git a/libbacktrace/Android.build.mk b/libbacktrace/Android.build.mk
index 3c80cc2..7e1cd53 100644
--- a/libbacktrace/Android.build.mk
+++ b/libbacktrace/Android.build.mk
@@ -18,6 +18,7 @@
LOCAL_MODULE := $(module)
LOCAL_MODULE_TAGS := $(module_tag)
+LOCAL_MULTILIB := $($(module)_multilib)
LOCAL_ADDITIONAL_DEPENDENCIES := \
$(LOCAL_PATH)/Android.mk \
@@ -60,14 +61,16 @@
$($(module)_ldlibs_$(build_type)) \
ifeq ($(build_type),target)
- include external/stlport/libstlport.mk
-
include $(BUILD_$(build_target))
endif
ifeq ($(build_type),host)
# Only build if host builds are supported.
ifeq ($(build_host),true)
+ LOCAL_CFLAGS += -Wno-extern-c-compat
+ ifneq ($($(module)_libc++),)
+ include external/libcxx/libcxx.mk
+ endif
include $(BUILD_HOST_$(build_target))
endif
endif
diff --git a/libbacktrace/Android.mk b/libbacktrace/Android.mk
index 33af0a3..f3b28dd 100755
--- a/libbacktrace/Android.mk
+++ b/libbacktrace/Android.mk
@@ -44,20 +44,12 @@
libbacktrace_shared_libraries_target := \
libcutils \
- libgccdemangle \
-# To enable using libunwind on each arch, add it to this list.
-libunwind_architectures := arm arm64 x86 x86_64
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),$(libunwind_architectures)))
libbacktrace_src_files += \
UnwindCurrent.cpp \
UnwindMap.cpp \
UnwindPtrace.cpp \
-libbacktrace_c_includes := \
- external/libunwind/include \
-
libbacktrace_shared_libraries := \
libunwind \
libunwind-ptrace \
@@ -68,23 +60,9 @@
libbacktrace_static_libraries_host := \
libcutils \
-else
-libbacktrace_src_files += \
- Corkscrew.cpp \
-
-libbacktrace_c_includes := \
- system/core/libcorkscrew \
-
-libbacktrace_shared_libraries := \
- libcorkscrew \
-
-libbacktrace_shared_libraries_target += \
- libdl \
-
libbacktrace_ldlibs_host := \
- -ldl \
-
-endif
+ -lpthread \
+ -lrt \
module := libbacktrace
module_tag := optional
@@ -92,6 +70,7 @@
build_target := SHARED_LIBRARY
include $(LOCAL_PATH)/Android.build.mk
build_type := host
+libbacktrace_multilib := both
include $(LOCAL_PATH)/Android.build.mk
#-------------------------------------------------------------------------
@@ -118,26 +97,17 @@
-fno-builtin \
-O0 \
-g \
- -DGTEST_HAS_STD_STRING \
-
-ifneq ($(TARGET_ARCH),arm64)
-backtrace_test_cflags += -fstack-protector-all
-else
- $(info TODO: $(LOCAL_PATH)/Android.mk -fstack-protector not yet available for the AArch64 toolchain)
- common_cflags += -fno-stack-protector
-endif # arm64
backtrace_test_cflags_target := \
- -DGTEST_OS_LINUX_ANDROID \
+ -DENABLE_PSS_TESTS \
backtrace_test_src_files := \
backtrace_test.cpp \
+ GetPss.cpp \
thread_utils.c \
-backtrace_test_ldlibs := \
- -lpthread \
-
backtrace_test_ldlibs_host := \
+ -lpthread \
-lrt \
backtrace_test_shared_libraries := \
@@ -147,6 +117,9 @@
backtrace_test_shared_libraries_target := \
libcutils \
+backtrace_test_static_libraries_host := \
+ libcutils \
+
module := backtrace_test
module_tag := debug
build_type := target
@@ -168,6 +141,8 @@
LOCAL_SRC_FILES := \
BacktraceMap.cpp \
+LOCAL_MULTILIB := both
+
include $(BUILD_HOST_SHARED_LIBRARY)
endif # HOST_OS-darwin
diff --git a/libbacktrace/BacktraceImpl.cpp b/libbacktrace/BacktraceImpl.cpp
index 855810e..4650b6a 100644
--- a/libbacktrace/BacktraceImpl.cpp
+++ b/libbacktrace/BacktraceImpl.cpp
@@ -17,8 +17,10 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/param.h>
#include <sys/ptrace.h>
#include <sys/types.h>
+#include <ucontext.h>
#include <unistd.h>
#include <string>
@@ -27,6 +29,7 @@
#include <backtrace/BacktraceMap.h>
#include "BacktraceImpl.h"
+#include "BacktraceLog.h"
#include "thread_utils.h"
//-------------------------------------------------------------------------
@@ -54,8 +57,8 @@
}
}
-bool Backtrace::Unwind(size_t num_ignore_frames) {
- return impl_->Unwind(num_ignore_frames);
+bool Backtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
+ return impl_->Unwind(num_ignore_frames, ucontext);
}
extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
@@ -97,15 +100,15 @@
std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
const char* map_name;
- if (frame->map && !frame->map->name.empty()) {
- map_name = frame->map->name.c_str();
+ if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
+ map_name = frame->map.name.c_str();
} else {
map_name = "<unknown>";
}
uintptr_t relative_pc;
- if (frame->map) {
- relative_pc = frame->pc - frame->map->start;
+ if (BacktraceMap::IsValid(frame->map)) {
+ relative_pc = frame->pc - frame->map.start;
} else {
relative_pc = frame->pc;
}
@@ -126,8 +129,8 @@
return buf;
}
-const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
- return map_->Find(pc);
+void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
+ map_->FillIn(pc, map);
}
//-------------------------------------------------------------------------
@@ -145,8 +148,9 @@
return false;
}
- const backtrace_map_t* map = FindMap(ptr);
- if (map && map->flags & PROT_READ) {
+ backtrace_map_t map;
+ FillInMap(ptr, &map);
+ if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
*out_value = *reinterpret_cast<word_t*>(ptr);
return true;
} else {
@@ -156,6 +160,17 @@
}
}
+size_t BacktraceCurrent::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
+ backtrace_map_t map;
+ FillInMap(addr, &map);
+ if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
+ return 0;
+ }
+ bytes = MIN(map.end - addr, bytes);
+ memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
+ return bytes;
+}
+
//-------------------------------------------------------------------------
// BacktracePtrace functions.
//-------------------------------------------------------------------------
@@ -168,25 +183,88 @@
BacktracePtrace::~BacktracePtrace() {
}
-bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) {
- if (!VerifyReadWordArgs(ptr, out_value)) {
+#if !defined(__APPLE__)
+static bool PtraceRead(pid_t tid, uintptr_t addr, word_t* out_value) {
+ // ptrace() returns -1 and sets errno when the operation fails.
+ // To disambiguate -1 from a valid result, we clear errno beforehand.
+ errno = 0;
+ *out_value = ptrace(PTRACE_PEEKTEXT, tid, reinterpret_cast<void*>(addr), NULL);
+ if (*out_value == static_cast<word_t>(-1) && errno) {
+ BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
+ reinterpret_cast<void*>(addr), tid, strerror(errno));
return false;
}
+ return true;
+}
+#endif
+bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) {
#if defined(__APPLE__)
BACK_LOGW("MacOS does not support reading from another pid.");
return false;
#else
- // ptrace() returns -1 and sets errno when the operation fails.
- // To disambiguate -1 from a valid result, we clear errno beforehand.
- errno = 0;
- *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
- if (*out_value == static_cast<word_t>(-1) && errno) {
- BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
- reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
+ if (!VerifyReadWordArgs(ptr, out_value)) {
return false;
}
- return true;
+
+ backtrace_map_t map;
+ FillInMap(ptr, &map);
+ if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
+ return false;
+ }
+
+ return PtraceRead(Tid(), ptr, out_value);
+#endif
+}
+
+size_t BacktracePtrace::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
+#if defined(__APPLE__)
+ BACK_LOGW("MacOS does not support reading from another pid.");
+ return 0;
+#else
+ backtrace_map_t map;
+ FillInMap(addr, &map);
+ if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
+ return 0;
+ }
+
+ bytes = MIN(map.end - addr, bytes);
+ size_t bytes_read = 0;
+ word_t data_word;
+ size_t align_bytes = addr & (sizeof(word_t) - 1);
+ if (align_bytes != 0) {
+ if (!PtraceRead(Tid(), addr & ~(sizeof(word_t) - 1), &data_word)) {
+ return 0;
+ }
+ align_bytes = sizeof(word_t) - align_bytes;
+ memcpy(buffer, reinterpret_cast<uint8_t*>(&data_word) + sizeof(word_t) - align_bytes,
+ align_bytes);
+ addr += align_bytes;
+ buffer += align_bytes;
+ bytes -= align_bytes;
+ bytes_read += align_bytes;
+ }
+
+ size_t num_words = bytes / sizeof(word_t);
+ for (size_t i = 0; i < num_words; i++) {
+ if (!PtraceRead(Tid(), addr, &data_word)) {
+ return bytes_read;
+ }
+ memcpy(buffer, &data_word, sizeof(word_t));
+ buffer += sizeof(word_t);
+ addr += sizeof(word_t);
+ bytes_read += sizeof(word_t);
+ }
+
+ size_t left_over = bytes & (sizeof(word_t) - 1);
+ if (left_over) {
+ if (!PtraceRead(Tid(), addr, &data_word)) {
+ return bytes_read;
+ }
+ memcpy(buffer, &data_word, left_over);
+ bytes_read += left_over;
+ }
+ return bytes_read;
#endif
}
diff --git a/libbacktrace/BacktraceImpl.h b/libbacktrace/BacktraceImpl.h
index 48dd11c..18c3cb5 100755
--- a/libbacktrace/BacktraceImpl.h
+++ b/libbacktrace/BacktraceImpl.h
@@ -21,17 +21,12 @@
#include <backtrace/BacktraceMap.h>
#include <sys/types.h>
-#include <log/log.h>
-
-// Macro to log the function name along with the warning message.
-#define BACK_LOGW(format, ...) \
- ALOGW("%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__)
class BacktraceImpl {
public:
virtual ~BacktraceImpl() { }
- virtual bool Unwind(size_t num_ignore_frames) = 0;
+ virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext) = 0;
// The name returned is not demangled, Backtrace::GetFunctionName()
// takes care of demangling the name.
@@ -42,8 +37,8 @@
inline pid_t Pid() { return backtrace_obj_->Pid(); }
inline pid_t Tid() { return backtrace_obj_->Tid(); }
- inline const backtrace_map_t* FindMap(uintptr_t addr) {
- return backtrace_obj_->FindMap(addr);
+ inline void FillInMap(uintptr_t addr, backtrace_map_t* map) {
+ backtrace_obj_->FillInMap(addr, map);
}
inline std::string GetFunctionName(uintptr_t pc, uintptr_t* offset) {
return backtrace_obj_->GetFunctionName(pc, offset);
@@ -61,6 +56,8 @@
BacktraceCurrent(BacktraceImpl* impl, BacktraceMap* map);
virtual ~BacktraceCurrent();
+ size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes);
+
bool ReadWord(uintptr_t ptr, word_t* out_value);
};
@@ -69,6 +66,8 @@
BacktracePtrace(BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map);
virtual ~BacktracePtrace();
+ size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes);
+
bool ReadWord(uintptr_t ptr, word_t* out_value);
};
diff --git a/libbacktrace/BacktraceLog.h b/libbacktrace/BacktraceLog.h
new file mode 100755
index 0000000..1632ec2
--- /dev/null
+++ b/libbacktrace/BacktraceLog.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2014 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 _LIBBACKTRACE_BACKTRACE_LOG_H
+#define _LIBBACKTRACE_BACKTRACE_LOG_H
+
+#define LOG_TAG "libbacktrace"
+
+#include <log/log.h>
+
+// Macro to log the function name along with the warning message.
+#define BACK_LOGW(format, ...) \
+ ALOGW("%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__)
+
+#endif // _LIBBACKTRACE_BACKTRACE_LOG_H
diff --git a/libbacktrace/BacktraceMap.cpp b/libbacktrace/BacktraceMap.cpp
index 6eb290d..82a4085 100644
--- a/libbacktrace/BacktraceMap.cpp
+++ b/libbacktrace/BacktraceMap.cpp
@@ -37,14 +37,15 @@
BacktraceMap::~BacktraceMap() {
}
-const backtrace_map_t* BacktraceMap::Find(uintptr_t addr) {
+void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
for (BacktraceMap::const_iterator it = begin();
it != end(); ++it) {
if (addr >= it->start && addr < it->end) {
- return &*it;
+ *map = *it;
+ return;
}
}
- return NULL;
+ *map = {};
}
bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
@@ -108,11 +109,11 @@
#if defined(__APPLE__)
// cmd is guaranteed to always be big enough to hold this string.
- sprintf(cmd, "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
+ snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
FILE* fp = popen(cmd, "r");
#else
// path is guaranteed to always be big enough to hold this string.
- sprintf(path, "/proc/%d/maps", pid_);
+ snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
FILE* fp = fopen(path, "r");
#endif
if (fp == NULL) {
@@ -137,7 +138,7 @@
#if defined(__APPLE__)
// Corkscrew and libunwind don't compile on the mac, so create a generic
// map object.
-BacktraceMap* BacktraceMap::Create(pid_t pid) {
+BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
BacktraceMap* map = new BacktraceMap(pid);
if (!map->Build()) {
delete map;
diff --git a/libbacktrace/BacktraceThread.cpp b/libbacktrace/BacktraceThread.cpp
index 5ffe516..439cc3b 100644
--- a/libbacktrace/BacktraceThread.cpp
+++ b/libbacktrace/BacktraceThread.cpp
@@ -16,200 +16,212 @@
#include <errno.h>
#include <inttypes.h>
+#include <limits.h>
#include <pthread.h>
#include <signal.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/syscall.h>
+#include <sys/time.h>
#include <sys/types.h>
+#include <ucontext.h>
+#include <unistd.h>
#include <cutils/atomic.h>
+#include "BacktraceLog.h"
#include "BacktraceThread.h"
#include "thread_utils.h"
//-------------------------------------------------------------------------
// ThreadEntry implementation.
//-------------------------------------------------------------------------
-static ThreadEntry* g_list = NULL;
-static pthread_mutex_t g_entry_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
+ThreadEntry* ThreadEntry::list_ = NULL;
+pthread_mutex_t ThreadEntry::list_mutex_ = PTHREAD_MUTEX_INITIALIZER;
-ThreadEntry::ThreadEntry(
- BacktraceThreadInterface* intf, pid_t pid, pid_t tid, size_t num_ignore_frames)
- : thread_intf(intf), pid(pid), tid(tid), next(NULL), prev(NULL),
- state(STATE_WAITING), num_ignore_frames(num_ignore_frames) {
+// Assumes that ThreadEntry::list_mutex_ has already been locked before
+// creating a ThreadEntry object.
+ThreadEntry::ThreadEntry(pid_t pid, pid_t tid)
+ : pid_(pid), tid_(tid), ref_count_(1), mutex_(PTHREAD_MUTEX_INITIALIZER),
+ wait_mutex_(PTHREAD_MUTEX_INITIALIZER), wait_value_(0),
+ next_(ThreadEntry::list_), prev_(NULL) {
+ pthread_condattr_t attr;
+ pthread_condattr_init(&attr);
+ pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+ pthread_cond_init(&wait_cond_, &attr);
+
+ // Add ourselves to the list.
+ if (ThreadEntry::list_) {
+ ThreadEntry::list_->prev_ = this;
+ }
+ ThreadEntry::list_ = this;
}
-ThreadEntry::~ThreadEntry() {
- pthread_mutex_lock(&g_entry_mutex);
- if (g_list == this) {
- g_list = next;
+ThreadEntry* ThreadEntry::Get(pid_t pid, pid_t tid, bool create) {
+ pthread_mutex_lock(&ThreadEntry::list_mutex_);
+ ThreadEntry* entry = list_;
+ while (entry != NULL) {
+ if (entry->Match(pid, tid)) {
+ break;
+ }
+ entry = entry->next_;
+ }
+
+ if (!entry) {
+ if (create) {
+ entry = new ThreadEntry(pid, tid);
+ }
} else {
- if (next) {
- next->prev = prev;
- }
- prev->next = next;
+ entry->ref_count_++;
}
- pthread_mutex_unlock(&g_entry_mutex);
-
- next = NULL;
- prev = NULL;
-}
-
-ThreadEntry* ThreadEntry::AddThreadToUnwind(
- BacktraceThreadInterface* intf, pid_t pid, pid_t tid, size_t num_ignore_frames) {
- ThreadEntry* entry = new ThreadEntry(intf, pid, tid, num_ignore_frames);
-
- pthread_mutex_lock(&g_entry_mutex);
- ThreadEntry* cur_entry = g_list;
- while (cur_entry != NULL) {
- if (cur_entry->Match(pid, tid)) {
- // There is already an entry for this pid/tid, this is bad.
- BACK_LOGW("Entry for pid %d tid %d already exists.", pid, tid);
-
- pthread_mutex_unlock(&g_entry_mutex);
- return NULL;
- }
- cur_entry = cur_entry->next;
- }
-
- // Add the entry to the list.
- entry->next = g_list;
- if (g_list) {
- g_list->prev = entry;
- }
- g_list = entry;
- pthread_mutex_unlock(&g_entry_mutex);
+ pthread_mutex_unlock(&ThreadEntry::list_mutex_);
return entry;
}
+void ThreadEntry::Remove(ThreadEntry* entry) {
+ pthread_mutex_unlock(&entry->mutex_);
+
+ pthread_mutex_lock(&ThreadEntry::list_mutex_);
+ if (--entry->ref_count_ == 0) {
+ delete entry;
+ }
+ pthread_mutex_unlock(&ThreadEntry::list_mutex_);
+}
+
+// Assumes that ThreadEntry::list_mutex_ has already been locked before
+// deleting a ThreadEntry object.
+ThreadEntry::~ThreadEntry() {
+ if (list_ == this) {
+ list_ = next_;
+ } else {
+ if (next_) {
+ next_->prev_ = prev_;
+ }
+ prev_->next_ = next_;
+ }
+
+ next_ = NULL;
+ prev_ = NULL;
+
+ pthread_cond_destroy(&wait_cond_);
+}
+
+void ThreadEntry::Wait(int value) {
+ timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
+ BACK_LOGW("clock_gettime failed: %s", strerror(errno));
+ abort();
+ }
+ ts.tv_sec += 10;
+
+ pthread_mutex_lock(&wait_mutex_);
+ while (wait_value_ != value) {
+ int ret = pthread_cond_timedwait(&wait_cond_, &wait_mutex_, &ts);
+ if (ret != 0) {
+ BACK_LOGW("pthread_cond_timedwait failed: %s", strerror(ret));
+ break;
+ }
+ }
+ pthread_mutex_unlock(&wait_mutex_);
+}
+
+void ThreadEntry::Wake() {
+ pthread_mutex_lock(&wait_mutex_);
+ wait_value_++;
+ pthread_mutex_unlock(&wait_mutex_);
+
+ pthread_cond_signal(&wait_cond_);
+}
+
+void ThreadEntry::CopyUcontextFromSigcontext(void* sigcontext) {
+ ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(sigcontext);
+ // The only thing the unwinder cares about is the mcontext data.
+ memcpy(&ucontext_.uc_mcontext, &ucontext->uc_mcontext, sizeof(ucontext->uc_mcontext));
+}
+
//-------------------------------------------------------------------------
// BacktraceThread functions.
//-------------------------------------------------------------------------
-static void SignalHandler(int n __attribute__((unused)), siginfo_t* siginfo,
- void* sigcontext) {
- if (pthread_mutex_lock(&g_entry_mutex) == 0) {
- pid_t pid = getpid();
- pid_t tid = gettid();
- ThreadEntry* cur_entry = g_list;
- while (cur_entry) {
- if (cur_entry->Match(pid, tid)) {
- break;
- }
- cur_entry = cur_entry->next;
- }
- pthread_mutex_unlock(&g_entry_mutex);
- if (!cur_entry) {
- BACK_LOGW("Unable to find pid %d tid %d information", pid, tid);
- return;
- }
+static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
- if (android_atomic_acquire_cas(STATE_WAITING, STATE_DUMPING, &cur_entry->state) == 0) {
- cur_entry->thread_intf->ThreadUnwind(siginfo, sigcontext,
- cur_entry->num_ignore_frames);
- }
- android_atomic_release_store(STATE_DONE, &cur_entry->state);
+static void SignalHandler(int, siginfo_t*, void* sigcontext) {
+ ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
+ if (!entry) {
+ BACK_LOGW("Unable to find pid %d tid %d information", getpid(), gettid());
+ return;
}
+
+ entry->CopyUcontextFromSigcontext(sigcontext);
+
+ // Indicate the ucontext is now valid.
+ entry->Wake();
+
+ // Pause the thread until the unwind is complete. This avoids having
+ // the thread run ahead causing problems.
+ entry->Wait(2);
+
+ ThreadEntry::Remove(entry);
}
-BacktraceThread::BacktraceThread(
- BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid,
- BacktraceMap* map)
- : BacktraceCurrent(impl, map), thread_intf_(thread_intf) {
+BacktraceThread::BacktraceThread(BacktraceImpl* impl, pid_t tid, BacktraceMap* map)
+ : BacktraceCurrent(impl, map) {
tid_ = tid;
}
BacktraceThread::~BacktraceThread() {
}
-void BacktraceThread::FinishUnwind() {
- for (std::vector<backtrace_frame_data_t>::iterator it = frames_.begin();
- it != frames_.end(); ++it) {
- it->map = FindMap(it->pc);
-
- it->func_offset = 0;
- it->func_name = GetFunctionName(it->pc, &it->func_offset);
- }
-}
-
-bool BacktraceThread::TriggerUnwindOnThread(ThreadEntry* entry) {
- entry->state = STATE_WAITING;
-
- if (tgkill(Pid(), Tid(), SIGURG) != 0) {
- BACK_LOGW("tgkill failed %s", strerror(errno));
- return false;
- }
-
- // Allow up to ten seconds for the dump to start.
- int wait_millis = 10000;
- int32_t state;
- while (true) {
- state = android_atomic_acquire_load(&entry->state);
- if (state != STATE_WAITING) {
- break;
- }
- if (wait_millis--) {
- usleep(1000);
- } else {
- break;
- }
- }
-
- bool cancelled = false;
- if (state == STATE_WAITING) {
- if (android_atomic_acquire_cas(state, STATE_CANCEL, &entry->state) == 0) {
- BACK_LOGW("Cancelled dump of thread %d", entry->tid);
- state = STATE_CANCEL;
- cancelled = true;
- } else {
- state = android_atomic_acquire_load(&entry->state);
- }
- }
-
- // Wait for at most ten seconds for the cancel or dump to finish.
- wait_millis = 10000;
- while (android_atomic_acquire_load(&entry->state) != STATE_DONE) {
- if (wait_millis--) {
- usleep(1000);
- } else {
- BACK_LOGW("Didn't finish thread unwind in 60 seconds.");
- break;
- }
- }
- return !cancelled;
-}
-
-bool BacktraceThread::Unwind(size_t num_ignore_frames) {
- ThreadEntry* entry = ThreadEntry::AddThreadToUnwind(
- thread_intf_, Pid(), Tid(), num_ignore_frames);
- if (!entry) {
- return false;
+bool BacktraceThread::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
+ if (ucontext) {
+ // Unwind using an already existing ucontext.
+ return impl_->Unwind(num_ignore_frames, ucontext);
}
// Prevent multiple threads trying to set the trigger action on different
// threads at the same time.
- bool retval = false;
- if (pthread_mutex_lock(&g_sigaction_mutex) == 0) {
- struct sigaction act, oldact;
- memset(&act, 0, sizeof(act));
- act.sa_sigaction = SignalHandler;
- act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
- sigemptyset(&act.sa_mask);
- if (sigaction(SIGURG, &act, &oldact) == 0) {
- retval = TriggerUnwindOnThread(entry);
- sigaction(SIGURG, &oldact, NULL);
- } else {
- BACK_LOGW("sigaction failed %s", strerror(errno));
- }
+ if (pthread_mutex_lock(&g_sigaction_mutex) < 0) {
+ BACK_LOGW("sigaction failed: %s", strerror(errno));
+ return false;
+ }
+
+ ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
+ entry->Lock();
+
+ struct sigaction act, oldact;
+ memset(&act, 0, sizeof(act));
+ act.sa_sigaction = SignalHandler;
+ act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
+ sigemptyset(&act.sa_mask);
+ if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
+ BACK_LOGW("sigaction failed %s", strerror(errno));
+ entry->Unlock();
+ ThreadEntry::Remove(entry);
pthread_mutex_unlock(&g_sigaction_mutex);
- } else {
- BACK_LOGW("unable to acquire sigaction mutex.");
+ return false;
}
- if (retval) {
- FinishUnwind();
+ if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
+ BACK_LOGW("tgkill %d failed: %s", Tid(), strerror(errno));
+ sigaction(THREAD_SIGNAL, &oldact, NULL);
+ entry->Unlock();
+ ThreadEntry::Remove(entry);
+ pthread_mutex_unlock(&g_sigaction_mutex);
+ return false;
}
- delete entry;
- return retval;
+ // Wait for the thread to get the ucontext.
+ entry->Wait(1);
+
+ // After the thread has received the signal, allow other unwinders to
+ // continue.
+ sigaction(THREAD_SIGNAL, &oldact, NULL);
+ pthread_mutex_unlock(&g_sigaction_mutex);
+
+ bool unwind_done = impl_->Unwind(num_ignore_frames, entry->GetUcontext());
+
+ // Tell the signal handler to exit and release the entry.
+ entry->Wake();
+
+ return unwind_done;
}
diff --git a/libbacktrace/BacktraceThread.h b/libbacktrace/BacktraceThread.h
index 3412d58..99a8638 100644
--- a/libbacktrace/BacktraceThread.h
+++ b/libbacktrace/BacktraceThread.h
@@ -18,73 +18,75 @@
#define _LIBBACKTRACE_BACKTRACE_THREAD_H
#include <inttypes.h>
+#include <pthread.h>
+#include <signal.h>
+#include <string.h>
#include <sys/types.h>
+#include <ucontext.h>
#include "BacktraceImpl.h"
-enum state_e {
- STATE_WAITING = 0,
- STATE_DUMPING,
- STATE_DONE,
- STATE_CANCEL,
-};
+// The signal used to cause a thread to dump the stack.
+#if defined(__GLIBC__)
+// GLIBC reserves __SIGRTMIN signals, so use SIGRTMIN to avoid errors.
+#define THREAD_SIGNAL SIGRTMIN
+#else
+#define THREAD_SIGNAL (__SIGRTMIN+1)
+#endif
-class BacktraceThreadInterface;
+class ThreadEntry {
+public:
+ static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true);
-struct ThreadEntry {
- ThreadEntry(
- BacktraceThreadInterface* impl, pid_t pid, pid_t tid,
- size_t num_ignore_frames);
+ static void Remove(ThreadEntry* entry);
+
+ void Wake();
+
+ void Wait(int);
+
+ void CopyUcontextFromSigcontext(void*);
+
+ inline void Lock() {
+ pthread_mutex_lock(&mutex_);
+
+ // Always reset the wait value since this could be the first or nth
+ // time this entry is locked.
+ wait_value_ = 0;
+ }
+
+ inline void Unlock() {
+ pthread_mutex_unlock(&mutex_);
+ }
+
+ inline ucontext_t* GetUcontext() { return &ucontext_; }
+
+private:
+ ThreadEntry(pid_t pid, pid_t tid);
~ThreadEntry();
- bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid && chk_tid == tid); }
+ bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); }
- static ThreadEntry* AddThreadToUnwind(
- BacktraceThreadInterface* thread_intf, pid_t pid, pid_t tid,
- size_t num_ignored_frames);
+ pid_t pid_;
+ pid_t tid_;
+ int ref_count_;
+ pthread_mutex_t mutex_;
+ pthread_mutex_t wait_mutex_;
+ pthread_cond_t wait_cond_;
+ int wait_value_;
+ ThreadEntry* next_;
+ ThreadEntry* prev_;
+ ucontext_t ucontext_;
- BacktraceThreadInterface* thread_intf;
- pid_t pid;
- pid_t tid;
- ThreadEntry* next;
- ThreadEntry* prev;
- int32_t state;
- int num_ignore_frames;
-};
-
-// Interface class that does not contain any local storage, only defines
-// virtual functions to be defined by subclasses.
-class BacktraceThreadInterface {
-public:
- virtual ~BacktraceThreadInterface() { }
-
- virtual void ThreadUnwind(
- siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) = 0;
+ static ThreadEntry* list_;
+ static pthread_mutex_t list_mutex_;
};
class BacktraceThread : public BacktraceCurrent {
public:
- // impl and thread_intf should point to the same object, this allows
- // the compiler to catch if an implementation does not properly
- // subclass both.
- BacktraceThread(
- BacktraceImpl* impl, BacktraceThreadInterface* thread_intf, pid_t tid,
- BacktraceMap* map);
+ BacktraceThread(BacktraceImpl* impl, pid_t tid, BacktraceMap* map);
virtual ~BacktraceThread();
- virtual bool Unwind(size_t num_ignore_frames);
-
- virtual void ThreadUnwind(
- siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
- thread_intf_->ThreadUnwind(siginfo, sigcontext, num_ignore_frames);
- }
-
-private:
- virtual bool TriggerUnwindOnThread(ThreadEntry* entry);
-
- virtual void FinishUnwind();
-
- BacktraceThreadInterface* thread_intf_;
+ virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext);
};
#endif // _LIBBACKTRACE_BACKTRACE_THREAD_H
diff --git a/libbacktrace/Corkscrew.cpp b/libbacktrace/Corkscrew.cpp
deleted file mode 100644
index efeee2e..0000000
--- a/libbacktrace/Corkscrew.cpp
+++ /dev/null
@@ -1,251 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#define LOG_TAG "libbacktrace"
-
-#include <backtrace/Backtrace.h>
-
-#include <string.h>
-
-#include <backtrace-arch.h>
-#include <corkscrew/backtrace.h>
-
-#ifndef __USE_GNU
-#define __USE_GNU
-#endif
-#include <dlfcn.h>
-
-#include "Corkscrew.h"
-
-//-------------------------------------------------------------------------
-// CorkscrewMap functions.
-//-------------------------------------------------------------------------
-CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) {
-}
-
-CorkscrewMap::~CorkscrewMap() {
- if (map_info_) {
- free_map_info_list(map_info_);
- map_info_ = NULL;
- }
-}
-
-bool CorkscrewMap::Build() {
- map_info_ = load_map_info_list(pid_);
-
- // Use the information in map_info_ to construct the BacktraceMap data
- // rather than reparsing /proc/self/maps.
- map_info_t* cur_map = map_info_;
- while (cur_map) {
- backtrace_map_t map;
- map.start = cur_map->start;
- map.end = cur_map->end;
- map.flags = 0;
- if (cur_map->is_readable) {
- map.flags |= PROT_READ;
- }
- if (cur_map->is_writable) {
- map.flags |= PROT_WRITE;
- }
- if (cur_map->is_executable) {
- map.flags |= PROT_EXEC;
- }
- map.name = cur_map->name;
-
- // The maps are in descending order, but we want them in ascending order.
- maps_.push_front(map);
-
- cur_map = cur_map->next;
- }
- return map_info_ != NULL;
-}
-
-//-------------------------------------------------------------------------
-// CorkscrewCommon functions.
-//-------------------------------------------------------------------------
-bool CorkscrewCommon::GenerateFrameData(
- backtrace_frame_t* cork_frames, ssize_t num_frames) {
- if (num_frames < 0) {
- BACK_LOGW("libcorkscrew unwind failed.");
- return false;
- }
-
- std::vector<backtrace_frame_data_t>* frames = GetFrames();
- frames->resize(num_frames);
- size_t i = 0;
- for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
- it != frames->end(); ++it, ++i) {
- it->num = i;
- it->pc = cork_frames[i].absolute_pc;
- it->sp = cork_frames[i].stack_top;
- it->stack_size = cork_frames[i].stack_size;
- it->func_offset = 0;
-
- it->map = FindMap(it->pc);
- it->func_name = GetFunctionName(it->pc, &it->func_offset);
- }
- return true;
-}
-
-//-------------------------------------------------------------------------
-// CorkscrewCurrent functions.
-//-------------------------------------------------------------------------
-CorkscrewCurrent::CorkscrewCurrent() {
-}
-
-CorkscrewCurrent::~CorkscrewCurrent() {
-}
-
-bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
- backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
- ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
-
- return GenerateFrameData(frames, num_frames);
-}
-
-std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
- *offset = 0;
-
- Dl_info info;
- const backtrace_map_t* map = FindMap(pc);
- if (map) {
- if (dladdr((const void*)pc, &info)) {
- if (info.dli_sname) {
- *offset = pc - map->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
- return info.dli_sname;
- }
- } else {
- // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
- symbol_table_t* symbol_table = load_symbol_table(map->name.c_str());
- if (symbol_table) {
- // First check if we can find the symbol using a relative pc.
- std::string name;
- const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start);
- if (elf_symbol) {
- name = elf_symbol->name;
- *offset = pc - map->start - elf_symbol->start;
- } else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
- // Found the symbol using the absolute pc.
- name = elf_symbol->name;
- *offset = pc - elf_symbol->start;
- }
- free_symbol_table(symbol_table);
- return name;
- }
- }
- }
- return "";
-}
-
-//-------------------------------------------------------------------------
-// CorkscrewThread functions.
-//-------------------------------------------------------------------------
-CorkscrewThread::CorkscrewThread() {
-}
-
-CorkscrewThread::~CorkscrewThread() {
-}
-
-void CorkscrewThread::ThreadUnwind(
- siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
- backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES];
- CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap());
- ssize_t num_frames = unwind_backtrace_signal_arch(
- siginfo, sigcontext, map->GetMapInfo(), cork_frames,
- num_ignore_frames, MAX_BACKTRACE_FRAMES);
- if (num_frames > 0) {
- std::vector<backtrace_frame_data_t>* frames = GetFrames();
- frames->resize(num_frames);
- size_t i = 0;
- for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
- it != frames->end(); ++it, ++i) {
- it->num = i;
- it->pc = cork_frames[i].absolute_pc;
- it->sp = cork_frames[i].stack_top;
- it->stack_size = cork_frames[i].stack_size;
- it->map = NULL;
- it->func_offset = 0;
- }
- }
-}
-
-//-------------------------------------------------------------------------
-// CorkscrewPtrace functions.
-//-------------------------------------------------------------------------
-CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
-}
-
-CorkscrewPtrace::~CorkscrewPtrace() {
- if (ptrace_context_) {
- free_ptrace_context(ptrace_context_);
- ptrace_context_ = NULL;
- }
-}
-
-bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
- ptrace_context_ = load_ptrace_context(Tid());
-
- backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
- ssize_t num_frames = unwind_backtrace_ptrace(
- Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
-
- return GenerateFrameData(frames, num_frames);
-}
-
-std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
- // Get information about a different process.
- const map_info_t* map_info;
- const symbol_t* symbol;
- find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
- char* symbol_name = NULL;
- if (symbol) {
- if (map_info) {
- *offset = pc - map_info->start - symbol->start;
- }
- symbol_name = symbol->name;
- return symbol_name;
- }
-
- return "";
-}
-
-//-------------------------------------------------------------------------
-// C++ object creation functions.
-//-------------------------------------------------------------------------
-Backtrace* CreateCurrentObj(BacktraceMap* map) {
- return new BacktraceCurrent(new CorkscrewCurrent(), map);
-}
-
-Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
- return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map);
-}
-
-Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
- CorkscrewThread* thread_obj = new CorkscrewThread();
- return new BacktraceThread(thread_obj, thread_obj, tid, map);
-}
-
-//-------------------------------------------------------------------------
-// BacktraceMap create function.
-//-------------------------------------------------------------------------
-BacktraceMap* BacktraceMap::Create(pid_t pid) {
- BacktraceMap* map = new CorkscrewMap(pid);
- if (!map->Build()) {
- delete map;
- return NULL;
- }
- return map;
-}
diff --git a/libbacktrace/Corkscrew.h b/libbacktrace/Corkscrew.h
deleted file mode 100644
index 1633398..0000000
--- a/libbacktrace/Corkscrew.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2013 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 _LIBBACKTRACE_CORKSCREW_H
-#define _LIBBACKTRACE_CORKSCREW_H
-
-#include <inttypes.h>
-
-#include <string>
-
-#include <backtrace/Backtrace.h>
-#include <backtrace/BacktraceMap.h>
-
-#include <corkscrew/backtrace.h>
-
-#include "BacktraceImpl.h"
-#include "BacktraceThread.h"
-
-class CorkscrewMap : public BacktraceMap {
-public:
- CorkscrewMap(pid_t pid);
- virtual ~CorkscrewMap();
-
- virtual bool Build();
-
- map_info_t* GetMapInfo() { return map_info_; }
-
-private:
- map_info_t* map_info_;
-};
-
-class CorkscrewCommon : public BacktraceImpl {
-public:
- bool GenerateFrameData(backtrace_frame_t* cork_frames, ssize_t num_frames);
-};
-
-class CorkscrewCurrent : public CorkscrewCommon {
-public:
- CorkscrewCurrent();
- virtual ~CorkscrewCurrent();
-
- virtual bool Unwind(size_t num_ignore_threads);
-
- virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset);
-};
-
-class CorkscrewThread : public CorkscrewCurrent, public BacktraceThreadInterface {
-public:
- CorkscrewThread();
- virtual ~CorkscrewThread();
-
- virtual void ThreadUnwind(
- siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames);
-};
-
-class CorkscrewPtrace : public CorkscrewCommon {
-public:
- CorkscrewPtrace();
- virtual ~CorkscrewPtrace();
-
- virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset);
-
- virtual bool Unwind(size_t num_ignore_threads);
-
-private:
- ptrace_context_t* ptrace_context_;
-};
-
-#endif // _LIBBACKTRACE_CORKSCREW_H
diff --git a/libbacktrace/GetPss.cpp b/libbacktrace/GetPss.cpp
new file mode 100644
index 0000000..442383b
--- /dev/null
+++ b/libbacktrace/GetPss.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2014 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 <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+// This is an extremely simplified version of libpagemap.
+
+#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << (bits)) - 1))
+
+#define PAGEMAP_PRESENT(x) (_BITS(x, 63, 1))
+#define PAGEMAP_SWAPPED(x) (_BITS(x, 62, 1))
+#define PAGEMAP_SHIFT(x) (_BITS(x, 55, 6))
+#define PAGEMAP_PFN(x) (_BITS(x, 0, 55))
+#define PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50))
+#define PAGEMAP_SWAP_TYPE(x) (_BITS(x, 0, 5))
+
+static bool ReadData(int fd, unsigned long place, uint64_t *data) {
+ if (lseek(fd, place * sizeof(uint64_t), SEEK_SET) < 0) {
+ return false;
+ }
+ if (read(fd, (void*)data, sizeof(uint64_t)) != (ssize_t)sizeof(uint64_t)) {
+ return false;
+ }
+ return true;
+}
+
+size_t GetPssBytes() {
+ FILE* maps = fopen("/proc/self/maps", "r");
+ assert(maps != NULL);
+
+ int pagecount_fd = open("/proc/kpagecount", O_RDONLY);
+ assert(pagecount_fd >= 0);
+
+ int pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
+ assert(pagemap_fd >= 0);
+
+ char line[4096];
+ size_t total_pss = 0;
+ int pagesize = getpagesize();
+ while (fgets(line, sizeof(line), maps)) {
+ uintptr_t start, end;
+ if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " ", &start, &end) != 2) {
+ total_pss = 0;
+ break;
+ }
+ for (size_t page = start/pagesize; page < end/pagesize; page++) {
+ uint64_t data;
+ if (ReadData(pagemap_fd, page, &data)) {
+ if (PAGEMAP_PRESENT(data) && !PAGEMAP_SWAPPED(data)) {
+ uint64_t count;
+ if (ReadData(pagecount_fd, PAGEMAP_PFN(data), &count)) {
+ total_pss += (count >= 1) ? pagesize / count : 0;
+ }
+ }
+ }
+ }
+ }
+
+ fclose(maps);
+
+ close(pagecount_fd);
+ close(pagemap_fd);
+
+ return total_pss;
+}
diff --git a/adb/usb_vendors.h b/libbacktrace/GetPss.h
similarity index 73%
copy from adb/usb_vendors.h
copy to libbacktrace/GetPss.h
index cee23a1..787c33d 100644
--- a/adb/usb_vendors.h
+++ b/libbacktrace/GetPss.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2014 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.
@@ -14,12 +14,9 @@
* limitations under the License.
*/
-#ifndef __USB_VENDORS_H
-#define __USB_VENDORS_H
+#ifndef _LIBBACKTRACE_GET_PSS_H
+#define _LIBBACKTRACE_GET_PSS_H
-extern int vendorIds[];
-extern unsigned vendorIdCount;
+size_t GetPssBytes();
-void usb_vendors_init(void);
-
-#endif
+#endif // _LIBBACKTRACE_GET_PSS_H
diff --git a/libbacktrace/UnwindCurrent.cpp b/libbacktrace/UnwindCurrent.cpp
index 81e69bb..372555b 100755
--- a/libbacktrace/UnwindCurrent.cpp
+++ b/libbacktrace/UnwindCurrent.cpp
@@ -14,10 +14,8 @@
* limitations under the License.
*/
-#define LOG_TAG "libbacktrace"
-
-#include <sys/ucontext.h>
#include <sys/types.h>
+#include <ucontext.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
@@ -25,6 +23,8 @@
#define UNW_LOCAL_ONLY
#include <libunwind.h>
+#include "BacktraceLog.h"
+#include "BacktraceThread.h"
#include "UnwindCurrent.h"
#include "UnwindMap.h"
@@ -37,13 +37,43 @@
UnwindCurrent::~UnwindCurrent() {
}
-bool UnwindCurrent::Unwind(size_t num_ignore_frames) {
- int ret = unw_getcontext(&context_);
- if (ret < 0) {
- BACK_LOGW("unw_getcontext failed %d", ret);
- return false;
+bool UnwindCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
+ if (!ucontext) {
+ int ret = unw_getcontext(&context_);
+ if (ret < 0) {
+ BACK_LOGW("unw_getcontext failed %d", ret);
+ return false;
+ }
}
- return UnwindFromContext(num_ignore_frames, true);
+ else {
+ GetUnwContextFromUcontext(ucontext);
+ }
+ return UnwindFromContext(num_ignore_frames, false);
+}
+
+void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
+ unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);
+
+#if defined(__arm__)
+ unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
+ unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
+ unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
+ unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
+ unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
+ unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
+ unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
+ unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
+ unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
+ unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
+ unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
+ unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
+ unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
+ unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
+ unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
+ unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
+#else
+ unw_context->uc_mcontext = ucontext->uc_mcontext;
+#endif
}
std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
@@ -58,12 +88,14 @@
return "";
}
-bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) {
+bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool within_handler) {
// The cursor structure is pretty large, do not put it on the stack.
unw_cursor_t* cursor = new unw_cursor_t;
int ret = unw_init_local(cursor, &context_);
if (ret < 0) {
- BACK_LOGW("unw_init_local failed %d", ret);
+ if (!within_handler) {
+ BACK_LOGW("unw_init_local failed %d", ret);
+ }
delete cursor;
return false;
}
@@ -75,13 +107,17 @@
unw_word_t pc;
ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
if (ret < 0) {
- BACK_LOGW("Failed to read IP %d", ret);
+ if (!within_handler) {
+ BACK_LOGW("Failed to read IP %d", ret);
+ }
break;
}
unw_word_t sp;
ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
if (ret < 0) {
- BACK_LOGW("Failed to read SP %d", ret);
+ if (!within_handler) {
+ BACK_LOGW("Failed to read SP %d", ret);
+ }
break;
}
@@ -99,11 +135,10 @@
prev->stack_size = frame->sp - prev->sp;
}
- if (resolve) {
+ if (!within_handler) {
frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
- frame->map = FindMap(frame->pc);
+ FillInMap(frame->pc, &frame->map);
} else {
- frame->map = NULL;
frame->func_offset = 0;
}
num_frames++;
@@ -117,47 +152,6 @@
return true;
}
-void UnwindCurrent::ExtractContext(void* sigcontext) {
- unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_);
- const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext);
-
-#if defined(__arm__)
- context->regs[0] = uc->uc_mcontext.arm_r0;
- context->regs[1] = uc->uc_mcontext.arm_r1;
- context->regs[2] = uc->uc_mcontext.arm_r2;
- context->regs[3] = uc->uc_mcontext.arm_r3;
- context->regs[4] = uc->uc_mcontext.arm_r4;
- context->regs[5] = uc->uc_mcontext.arm_r5;
- context->regs[6] = uc->uc_mcontext.arm_r6;
- context->regs[7] = uc->uc_mcontext.arm_r7;
- context->regs[8] = uc->uc_mcontext.arm_r8;
- context->regs[9] = uc->uc_mcontext.arm_r9;
- context->regs[10] = uc->uc_mcontext.arm_r10;
- context->regs[11] = uc->uc_mcontext.arm_fp;
- context->regs[12] = uc->uc_mcontext.arm_ip;
- context->regs[13] = uc->uc_mcontext.arm_sp;
- context->regs[14] = uc->uc_mcontext.arm_lr;
- context->regs[15] = uc->uc_mcontext.arm_pc;
-#else
- context->uc_mcontext = uc->uc_mcontext;
-#endif
-}
-
-//-------------------------------------------------------------------------
-// UnwindThread functions.
-//-------------------------------------------------------------------------
-UnwindThread::UnwindThread() {
-}
-
-UnwindThread::~UnwindThread() {
-}
-
-void UnwindThread::ThreadUnwind(
- siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) {
- ExtractContext(sigcontext);
- UnwindFromContext(num_ignore_frames, false);
-}
-
//-------------------------------------------------------------------------
// C++ object creation function.
//-------------------------------------------------------------------------
@@ -166,6 +160,5 @@
}
Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
- UnwindThread* thread_obj = new UnwindThread();
- return new BacktraceThread(thread_obj, thread_obj, tid, map);
+ return new BacktraceThread(new UnwindCurrent(), tid, map);
}
diff --git a/libbacktrace/UnwindCurrent.h b/libbacktrace/UnwindCurrent.h
index acce110..2375e6e 100644
--- a/libbacktrace/UnwindCurrent.h
+++ b/libbacktrace/UnwindCurrent.h
@@ -20,7 +20,6 @@
#include <string>
#include "BacktraceImpl.h"
-#include "BacktraceThread.h"
#define UNW_LOCAL_ONLY
#include <libunwind.h>
@@ -30,25 +29,16 @@
UnwindCurrent();
virtual ~UnwindCurrent();
- virtual bool Unwind(size_t num_ignore_frames);
+ virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext);
virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset);
- bool UnwindFromContext(size_t num_ignore_frames, bool resolve);
+ bool UnwindFromContext(size_t num_ignore_frames, bool within_handler);
- void ExtractContext(void* sigcontext);
+ void GetUnwContextFromUcontext(const ucontext_t* context);
protected:
unw_context_t context_;
};
-class UnwindThread : public UnwindCurrent, public BacktraceThreadInterface {
-public:
- UnwindThread();
- virtual ~UnwindThread();
-
- virtual void ThreadUnwind(
- siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames);
-};
-
#endif // _LIBBACKTRACE_UNWIND_CURRENT_H
diff --git a/libbacktrace/UnwindMap.cpp b/libbacktrace/UnwindMap.cpp
index 8268db6..284a561 100644
--- a/libbacktrace/UnwindMap.cpp
+++ b/libbacktrace/UnwindMap.cpp
@@ -14,9 +14,8 @@
* limitations under the License.
*/
-#define LOG_TAG "libbacktrace"
-
#include <pthread.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
@@ -24,6 +23,7 @@
#include <libunwind.h>
+#include "BacktraceLog.h"
#include "UnwindMap.h"
//-------------------------------------------------------------------------
@@ -32,57 +32,21 @@
// only update the local address space once, and keep a reference count
// of maps using the same map cursor.
//-------------------------------------------------------------------------
-static pthread_mutex_t g_map_mutex = PTHREAD_MUTEX_INITIALIZER;
-static unw_map_cursor_t g_map_cursor;
-static int g_map_references = 0;
-
UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
- map_cursor_.map_list = NULL;
}
UnwindMap::~UnwindMap() {
- if (pid_ == getpid()) {
- pthread_mutex_lock(&g_map_mutex);
- if (--g_map_references == 0) {
- // Clear the local address space map.
- unw_map_local_set(NULL);
- unw_map_cursor_destroy(&map_cursor_);
- }
- pthread_mutex_unlock(&g_map_mutex);
- } else {
- unw_map_cursor_destroy(&map_cursor_);
- }
+ unw_map_cursor_destroy(&map_cursor_);
+ unw_map_cursor_clear(&map_cursor_);
}
-bool UnwindMap::Build() {
- bool return_value = true;
- if (pid_ == getpid()) {
- pthread_mutex_lock(&g_map_mutex);
- if (g_map_references == 0) {
- return_value = (unw_map_cursor_create(&map_cursor_, pid_) == 0);
- if (return_value) {
- // Set the local address space map to our new map.
- unw_map_local_set(&map_cursor_);
- g_map_references = 1;
- g_map_cursor = map_cursor_;
- }
- } else {
- g_map_references++;
- map_cursor_ = g_map_cursor;
- }
- pthread_mutex_unlock(&g_map_mutex);
- } else {
- return_value = (unw_map_cursor_create(&map_cursor_, pid_) == 0);
- }
-
- if (!return_value)
- return false;
-
+bool UnwindMap::GenerateMap() {
// Use the map_cursor information to construct the BacktraceMap data
// rather than reparsing /proc/self/maps.
unw_map_cursor_reset(&map_cursor_);
+
unw_map_t unw_map;
- while (unw_map_cursor_get(&map_cursor_, &unw_map)) {
+ while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
backtrace_map_t map;
map.start = unw_map.start;
@@ -97,11 +61,85 @@
return true;
}
+bool UnwindMap::Build() {
+ return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
+}
+
+UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
+}
+
+UnwindMapLocal::~UnwindMapLocal() {
+ if (map_created_) {
+ unw_map_local_destroy();
+ unw_map_cursor_clear(&map_cursor_);
+ }
+}
+
+bool UnwindMapLocal::GenerateMap() {
+ // It's possible for the map to be regenerated while this loop is occurring.
+ // If that happens, get the map again, but only try at most three times
+ // before giving up.
+ for (int i = 0; i < 3; i++) {
+ maps_.clear();
+
+ unw_map_local_cursor_get(&map_cursor_);
+
+ unw_map_t unw_map;
+ int ret;
+ while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
+ backtrace_map_t map;
+
+ map.start = unw_map.start;
+ map.end = unw_map.end;
+ map.flags = unw_map.flags;
+ map.name = unw_map.path;
+
+ free(unw_map.path);
+
+ // The maps are in descending order, but we want them in ascending order.
+ maps_.push_front(map);
+ }
+ // Check to see if the map changed while getting the data.
+ if (ret != -UNW_EINVAL) {
+ return true;
+ }
+ }
+
+ BACK_LOGW("Unable to generate the map.");
+ return false;
+}
+
+bool UnwindMapLocal::Build() {
+ return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
+}
+
+void UnwindMapLocal::FillIn(uintptr_t addr, backtrace_map_t* map) {
+ BacktraceMap::FillIn(addr, map);
+ if (!IsValid(*map)) {
+ // Check to see if the underlying map changed and regenerate the map
+ // if it did.
+ if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
+ if (GenerateMap()) {
+ BacktraceMap::FillIn(addr, map);
+ }
+ }
+ }
+}
+
//-------------------------------------------------------------------------
// BacktraceMap create function.
//-------------------------------------------------------------------------
-BacktraceMap* BacktraceMap::Create(pid_t pid) {
- BacktraceMap* map = new UnwindMap(pid);
+BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
+ BacktraceMap* map;
+
+ if (uncached) {
+ // Force use of the base class to parse the maps when this call is made.
+ map = new BacktraceMap(pid);
+ } else if (pid == getpid()) {
+ map = new UnwindMapLocal();
+ } else {
+ map = new UnwindMap(pid);
+ }
if (!map->Build()) {
delete map;
return NULL;
diff --git a/libbacktrace/UnwindMap.h b/libbacktrace/UnwindMap.h
index 5a874e8..be8855e 100644
--- a/libbacktrace/UnwindMap.h
+++ b/libbacktrace/UnwindMap.h
@@ -32,8 +32,25 @@
unw_map_cursor_t* GetMapCursor() { return &map_cursor_; }
-private:
+protected:
+ virtual bool GenerateMap();
+
unw_map_cursor_t map_cursor_;
};
+class UnwindMapLocal : public UnwindMap {
+public:
+ UnwindMapLocal();
+ virtual ~UnwindMapLocal();
+
+ virtual bool Build();
+
+ virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
+
+protected:
+ virtual bool GenerateMap();
+
+ bool map_created_;
+};
+
#endif // _LIBBACKTRACE_UNWIND_MAP_H
diff --git a/libbacktrace/UnwindPtrace.cpp b/libbacktrace/UnwindPtrace.cpp
index 732dae8..efe758b 100644
--- a/libbacktrace/UnwindPtrace.cpp
+++ b/libbacktrace/UnwindPtrace.cpp
@@ -14,17 +14,17 @@
* limitations under the License.
*/
-#define LOG_TAG "libbacktrace"
-
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
#include <sys/types.h>
#include <string.h>
+#include <ucontext.h>
#include <libunwind.h>
#include <libunwind-ptrace.h>
+#include "BacktraceLog.h"
#include "UnwindMap.h"
#include "UnwindPtrace.h"
@@ -46,7 +46,12 @@
}
}
-bool UnwindPtrace::Unwind(size_t num_ignore_frames) {
+bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
+ if (ucontext) {
+ BACK_LOGW("Unwinding from a specified context not supported yet.");
+ return false;
+ }
+
addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
if (!addr_space_) {
BACK_LOGW("unw_create_addr_space failed.");
@@ -101,7 +106,7 @@
frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
- frame->map = FindMap(frame->pc);
+ FillInMap(frame->pc, &frame->map);
num_frames++;
} else {
diff --git a/libbacktrace/UnwindPtrace.h b/libbacktrace/UnwindPtrace.h
index 1e82117..2fb7967 100644
--- a/libbacktrace/UnwindPtrace.h
+++ b/libbacktrace/UnwindPtrace.h
@@ -28,7 +28,7 @@
UnwindPtrace();
virtual ~UnwindPtrace();
- virtual bool Unwind(size_t num_ignore_frames);
+ virtual bool Unwind(size_t num_ignore_frames, ucontext_t* ucontext);
virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset);
diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp
index 23eaf92..b1e34bd 100644
--- a/libbacktrace/backtrace_test.cpp
+++ b/libbacktrace/backtrace_test.cpp
@@ -16,9 +16,10 @@
#include <dirent.h>
#include <errno.h>
+#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
-#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -30,11 +31,15 @@
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
-#include <UniquePtr.h>
+
+// For the THREAD_SIGNAL definition.
+#include "BacktraceThread.h"
#include <cutils/atomic.h>
#include <gtest/gtest.h>
+#include <algorithm>
+#include <memory>
#include <vector>
#include "thread_utils.h"
@@ -46,7 +51,7 @@
#define NS_PER_SEC 1000000000ULL
// Number of simultaneous dumping operations to perform.
-#define NUM_THREADS 20
+#define NUM_THREADS 40
// Number of simultaneous threads running in our forked process.
#define NUM_PTRACE_THREADS 5
@@ -55,6 +60,7 @@
pid_t tid;
int32_t state;
pthread_t threadId;
+ void* data;
};
struct dump_thread_t {
@@ -137,9 +143,9 @@
}
void VerifyLevelBacktrace(void*) {
- UniquePtr<Backtrace> backtrace(
+ std::unique_ptr<Backtrace> backtrace(
Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(backtrace.get() != NULL);
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyLevelDump(backtrace.get());
@@ -157,9 +163,9 @@
}
void VerifyMaxBacktrace(void*) {
- UniquePtr<Backtrace> backtrace(
+ std::unique_ptr<Backtrace> backtrace(
Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(backtrace.get() != NULL);
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyMaxDump(backtrace.get());
@@ -175,8 +181,8 @@
}
void VerifyThreadTest(pid_t tid, void (*VerifyFunc)(Backtrace*)) {
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), tid));
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyFunc(backtrace.get());
@@ -193,7 +199,7 @@
}
TEST(libbacktrace, local_trace) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, NULL), 0);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
}
void VerifyIgnoreFrames(
@@ -203,7 +209,7 @@
EXPECT_EQ(bt_all->NumFrames(), bt_ign2->NumFrames() + 2);
// Check all of the frames are the same > the current frame.
- bool check = (cur_proc == NULL);
+ bool check = (cur_proc == nullptr);
for (size_t i = 0; i < bt_ign2->NumFrames(); i++) {
if (check) {
EXPECT_EQ(bt_ign2->GetFrame(i)->pc, bt_ign1->GetFrame(i+1)->pc);
@@ -221,30 +227,30 @@
}
void VerifyLevelIgnoreFrames(void*) {
- UniquePtr<Backtrace> all(
+ std::unique_ptr<Backtrace> all(
Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(all.get() != NULL);
+ ASSERT_TRUE(all.get() != nullptr);
ASSERT_TRUE(all->Unwind(0));
- UniquePtr<Backtrace> ign1(
+ std::unique_ptr<Backtrace> ign1(
Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(ign1.get() != NULL);
+ ASSERT_TRUE(ign1.get() != nullptr);
ASSERT_TRUE(ign1->Unwind(1));
- UniquePtr<Backtrace> ign2(
+ std::unique_ptr<Backtrace> ign2(
Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(ign2.get() != NULL);
+ ASSERT_TRUE(ign2.get() != nullptr);
ASSERT_TRUE(ign2->Unwind(2));
VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), "VerifyLevelIgnoreFrames");
}
TEST(libbacktrace, local_trace_ignore_frames) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, NULL), 0);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelIgnoreFrames, nullptr), 0);
}
TEST(libbacktrace, local_max_trace) {
- ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, NULL), 0);
+ ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxBacktrace, nullptr), 0);
}
void VerifyProcTest(pid_t pid, pid_t tid, bool share_map,
@@ -264,13 +270,13 @@
// Wait for the process to get to a stopping point.
WaitForStop(ptrace_tid);
- UniquePtr<BacktraceMap> map;
+ std::unique_ptr<BacktraceMap> map;
if (share_map) {
map.reset(BacktraceMap::Create(pid));
}
- UniquePtr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, tid, map.get()));
ASSERT_TRUE(backtrace->Unwind(0));
- ASSERT_TRUE(backtrace.get() != NULL);
+ ASSERT_TRUE(backtrace.get() != nullptr);
if (ReadyFunc(backtrace.get())) {
VerifyFunc(backtrace.get());
verified = true;
@@ -286,8 +292,8 @@
TEST(libbacktrace, ptrace_trace) {
pid_t pid;
if ((pid = fork()) == 0) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
- exit(1);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
+ _exit(1);
}
VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyLevelDump);
@@ -299,8 +305,8 @@
TEST(libbacktrace, ptrace_trace_shared_map) {
pid_t pid;
if ((pid = fork()) == 0) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
- exit(1);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
+ _exit(1);
}
VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, true, ReadyLevelBacktrace, VerifyLevelDump);
@@ -313,8 +319,8 @@
TEST(libbacktrace, ptrace_max_trace) {
pid_t pid;
if ((pid = fork()) == 0) {
- ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, NULL, NULL), 0);
- exit(1);
+ ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, nullptr, nullptr), 0);
+ _exit(1);
}
VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyMaxBacktrace, VerifyMaxDump);
@@ -324,22 +330,22 @@
}
void VerifyProcessIgnoreFrames(Backtrace* bt_all) {
- UniquePtr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(ign1.get() != NULL);
+ std::unique_ptr<Backtrace> ign1(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
+ ASSERT_TRUE(ign1.get() != nullptr);
ASSERT_TRUE(ign1->Unwind(1));
- UniquePtr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(ign2.get() != NULL);
+ std::unique_ptr<Backtrace> ign2(Backtrace::Create(bt_all->Pid(), BACKTRACE_CURRENT_THREAD));
+ ASSERT_TRUE(ign2.get() != nullptr);
ASSERT_TRUE(ign2->Unwind(2));
- VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), NULL);
+ VerifyIgnoreFrames(bt_all, ign1.get(), ign2.get(), nullptr);
}
TEST(libbacktrace, ptrace_ignore_frames) {
pid_t pid;
if ((pid = fork()) == 0) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
- exit(1);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
+ _exit(1);
}
VerifyProcTest(pid, BACKTRACE_CURRENT_THREAD, false, ReadyLevelBacktrace, VerifyProcessIgnoreFrames);
@@ -350,8 +356,8 @@
// Create a process with multiple threads and dump all of the threads.
void* PtraceThreadLevelRun(void*) {
- EXPECT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
- return NULL;
+ EXPECT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
+ return nullptr;
}
void GetThreads(pid_t pid, std::vector<pid_t>* threads) {
@@ -360,9 +366,9 @@
snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
DIR* tasks_dir = opendir(task_path);
- ASSERT_TRUE(tasks_dir != NULL);
+ ASSERT_TRUE(tasks_dir != nullptr);
struct dirent* entry;
- while ((entry = readdir(tasks_dir)) != NULL) {
+ while ((entry = readdir(tasks_dir)) != nullptr) {
char* end;
pid_t tid = strtoul(entry->d_name, &end, 10);
if (*end == '\0') {
@@ -381,10 +387,10 @@
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_t thread;
- ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, NULL) == 0);
+ ASSERT_TRUE(pthread_create(&thread, &attr, PtraceThreadLevelRun, nullptr) == 0);
}
- ASSERT_NE(test_level_one(1, 2, 3, 4, NULL, NULL), 0);
- exit(1);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, nullptr, nullptr), 0);
+ _exit(1);
}
// Check to see that all of the threads are running before unwinding.
@@ -415,27 +421,27 @@
}
void VerifyLevelThread(void*) {
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyLevelDump(backtrace.get());
}
TEST(libbacktrace, thread_current_level) {
- ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, NULL), 0);
+ ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelThread, nullptr), 0);
}
void VerifyMaxThread(void*) {
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyMaxDump(backtrace.get());
}
TEST(libbacktrace, thread_current_max) {
- ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, NULL), 0);
+ ASSERT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, VerifyMaxThread, nullptr), 0);
}
void* ThreadLevelRun(void* data) {
@@ -443,7 +449,7 @@
thread->tid = gettid();
EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
- return NULL;
+ return nullptr;
}
TEST(libbacktrace, thread_level_trace) {
@@ -451,19 +457,25 @@
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- thread_t thread_data = { 0, 0, 0 };
+ thread_t thread_data = { 0, 0, 0, nullptr };
pthread_t thread;
ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
// Wait up to 2 seconds for the tid to be set.
ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
+ // Make sure that the thread signal used is not visible when compiled for
+ // the target.
+#if !defined(__GLIBC__)
+ ASSERT_LT(THREAD_SIGNAL, SIGRTMIN);
+#endif
+
// Save the current signal action and make sure it is restored afterwards.
struct sigaction cur_action;
- ASSERT_TRUE(sigaction(SIGURG, NULL, &cur_action) == 0);
+ ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &cur_action) == 0);
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyLevelDump(backtrace.get());
@@ -473,8 +485,15 @@
// Verify that the old action was restored.
struct sigaction new_action;
- ASSERT_TRUE(sigaction(SIGURG, NULL, &new_action) == 0);
+ ASSERT_TRUE(sigaction(THREAD_SIGNAL, nullptr, &new_action) == 0);
EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction);
+ // The SA_RESTORER flag gets set behind our back, so a direct comparison
+ // doesn't work unless we mask the value off. Mips doesn't have this
+ // flag, so skip this on that platform.
+#ifdef SA_RESTORER
+ cur_action.sa_flags &= ~SA_RESTORER;
+ new_action.sa_flags &= ~SA_RESTORER;
+#endif
EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags);
}
@@ -483,26 +502,26 @@
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- thread_t thread_data = { 0, 0, 0 };
+ thread_t thread_data = { 0, 0, 0, nullptr };
pthread_t thread;
ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0);
// Wait up to 2 seconds for the tid to be set.
ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
- UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
- ASSERT_TRUE(all.get() != NULL);
+ std::unique_ptr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(all.get() != nullptr);
ASSERT_TRUE(all->Unwind(0));
- UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
- ASSERT_TRUE(ign1.get() != NULL);
+ std::unique_ptr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(ign1.get() != nullptr);
ASSERT_TRUE(ign1->Unwind(1));
- UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
- ASSERT_TRUE(ign2.get() != NULL);
+ std::unique_ptr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(ign2.get() != nullptr);
ASSERT_TRUE(ign2->Unwind(2));
- VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL);
+ VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), nullptr);
// Tell the thread to exit its infinite loop.
android_atomic_acquire_store(0, &thread_data.state);
@@ -513,7 +532,7 @@
thread->tid = gettid();
EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
- return NULL;
+ return nullptr;
}
TEST(libbacktrace, thread_max_trace) {
@@ -521,15 +540,15 @@
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- thread_t thread_data = { 0, 0, 0 };
+ thread_t thread_data = { 0, 0, 0, nullptr };
pthread_t thread;
ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0);
// Wait for the tid to be set.
ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VerifyMaxDump(backtrace.get());
@@ -552,7 +571,7 @@
android_atomic_acquire_store(1, &dump->done);
- return NULL;
+ return nullptr;
}
TEST(libbacktrace, thread_multiple_dump) {
@@ -572,7 +591,7 @@
// Wait for tids to be set.
for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) {
- ASSERT_TRUE(WaitForNonZero(&it->state, 10));
+ ASSERT_TRUE(WaitForNonZero(&it->state, 30));
}
// Start all of the dumpers at once, they will spin until they are signalled
@@ -591,19 +610,62 @@
android_atomic_acquire_store(1, &dump_now);
for (size_t i = 0; i < NUM_THREADS; i++) {
- ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 10));
+ ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
// Tell the runner thread to exit its infinite loop.
android_atomic_acquire_store(0, &runners[i].state);
- ASSERT_TRUE(dumpers[i].backtrace != NULL);
+ ASSERT_TRUE(dumpers[i].backtrace != nullptr);
VerifyMaxDump(dumpers[i].backtrace);
delete dumpers[i].backtrace;
- dumpers[i].backtrace = NULL;
+ dumpers[i].backtrace = nullptr;
}
}
+TEST(libbacktrace, thread_multiple_dump_same_thread) {
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ thread_t runner;
+ runner.tid = 0;
+ runner.state = 0;
+ ASSERT_TRUE(pthread_create(&runner.threadId, &attr, ThreadMaxRun, &runner) == 0);
+
+ // Wait for tids to be set.
+ ASSERT_TRUE(WaitForNonZero(&runner.state, 30));
+
+ // Start all of the dumpers at once, they will spin until they are signalled
+ // to begin their dump run.
+ int32_t dump_now = 0;
+ // Dump the same thread NUM_THREADS simultaneously.
+ std::vector<dump_thread_t> dumpers(NUM_THREADS);
+ for (size_t i = 0; i < NUM_THREADS; i++) {
+ dumpers[i].thread.tid = runner.tid;
+ dumpers[i].thread.state = 0;
+ dumpers[i].done = 0;
+ dumpers[i].now = &dump_now;
+
+ ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0);
+ }
+
+ // Start all of the dumpers going at once.
+ android_atomic_acquire_store(1, &dump_now);
+
+ for (size_t i = 0; i < NUM_THREADS; i++) {
+ ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 30));
+
+ ASSERT_TRUE(dumpers[i].backtrace != nullptr);
+ VerifyMaxDump(dumpers[i].backtrace);
+
+ delete dumpers[i].backtrace;
+ dumpers[i].backtrace = nullptr;
+ }
+
+ // Tell the runner thread to exit its infinite loop.
+ android_atomic_acquire_store(0, &runner.state);
+}
+
// This test is for UnwindMaps that should share the same map cursor when
// multiple maps are created for the current process at the same time.
TEST(libbacktrace, simultaneous_maps) {
@@ -627,22 +689,36 @@
delete map3;
}
+TEST(libbacktrace, fillin_erases) {
+ BacktraceMap* back_map = BacktraceMap::Create(getpid());
+
+ backtrace_map_t map;
+
+ map.start = 1;
+ map.end = 3;
+ map.flags = 1;
+ map.name = "Initialized";
+ back_map->FillIn(0, &map);
+ delete back_map;
+
+ ASSERT_FALSE(BacktraceMap::IsValid(map));
+ ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
+ ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
+ ASSERT_EQ(0, map.flags);
+ ASSERT_EQ("", map.name);
+}
+
TEST(libbacktrace, format_test) {
- UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
- ASSERT_TRUE(backtrace.get() != NULL);
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
+ ASSERT_TRUE(backtrace.get() != nullptr);
backtrace_frame_data_t frame;
frame.num = 1;
frame.pc = 2;
frame.sp = 0;
frame.stack_size = 0;
- frame.map = NULL;
frame.func_offset = 0;
- backtrace_map_t map;
- map.start = 0;
- map.end = 0;
-
// Check no map set.
frame.num = 1;
#if defined(__LP64__)
@@ -653,8 +729,8 @@
backtrace->FormatFrameData(&frame));
// Check map name empty, but exists.
- frame.map = ↦
- map.start = 1;
+ frame.map.start = 1;
+ frame.map.end = 1;
#if defined(__LP64__)
EXPECT_EQ("#01 pc 0000000000000001 <unknown>",
#else
@@ -665,9 +741,9 @@
// Check relative pc is set and map name is set.
frame.pc = 0x12345679;
- frame.map = ↦
- map.name = "MapFake";
- map.start = 1;
+ frame.map.name = "MapFake";
+ frame.map.start = 1;
+ frame.map.end = 1;
#if defined(__LP64__)
EXPECT_EQ("#01 pc 0000000012345678 MapFake",
#else
@@ -693,3 +769,296 @@
#endif
backtrace->FormatFrameData(&frame));
}
+
+struct map_test_t {
+ uintptr_t start;
+ uintptr_t end;
+};
+
+bool map_sort(map_test_t i, map_test_t j) {
+ return i.start < j.start;
+}
+
+void VerifyMap(pid_t pid) {
+ char buffer[4096];
+ snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
+
+ FILE* map_file = fopen(buffer, "r");
+ ASSERT_TRUE(map_file != nullptr);
+ std::vector<map_test_t> test_maps;
+ while (fgets(buffer, sizeof(buffer), map_file)) {
+ map_test_t map;
+ ASSERT_EQ(2, sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " ", &map.start, &map.end));
+ test_maps.push_back(map);
+ }
+ fclose(map_file);
+ std::sort(test_maps.begin(), test_maps.end(), map_sort);
+
+ std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(pid));
+
+ // Basic test that verifies that the map is in the expected order.
+ std::vector<map_test_t>::const_iterator test_it = test_maps.begin();
+ for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
+ ASSERT_TRUE(test_it != test_maps.end());
+ ASSERT_EQ(test_it->start, it->start);
+ ASSERT_EQ(test_it->end, it->end);
+ ++test_it;
+ }
+ ASSERT_TRUE(test_it == test_maps.end());
+}
+
+TEST(libbacktrace, verify_map_remote) {
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ while (true) {
+ }
+ _exit(0);
+ }
+ ASSERT_LT(0, pid);
+
+ ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
+
+ // Wait for the process to get to a stopping point.
+ WaitForStop(pid);
+
+ // The maps should match exactly since the forked process has been paused.
+ VerifyMap(pid);
+
+ ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
+
+ kill(pid, SIGKILL);
+ ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
+}
+
+void* ThreadReadTest(void* data) {
+ thread_t* thread_data = reinterpret_cast<thread_t*>(data);
+
+ thread_data->tid = gettid();
+
+ // Create two map pages.
+ // Mark the second page as not-readable.
+ size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
+ uint8_t* memory;
+ if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
+ return reinterpret_cast<void*>(-1);
+ }
+
+ if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
+ return reinterpret_cast<void*>(-1);
+ }
+
+ // Set up a simple pattern in memory.
+ for (size_t i = 0; i < pagesize; i++) {
+ memory[i] = i;
+ }
+
+ thread_data->data = memory;
+
+ // Tell the caller it's okay to start reading memory.
+ android_atomic_acquire_store(1, &thread_data->state);
+
+ // Loop waiting for everything
+ while (thread_data->state) {
+ }
+
+ free(memory);
+
+ android_atomic_acquire_store(1, &thread_data->state);
+
+ return nullptr;
+}
+
+void RunReadTest(Backtrace* backtrace, uintptr_t read_addr) {
+ size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
+
+ // Create a page of data to use to do quick compares.
+ uint8_t* expected = new uint8_t[pagesize];
+ for (size_t i = 0; i < pagesize; i++) {
+ expected[i] = i;
+ }
+ uint8_t* data = new uint8_t[2*pagesize];
+ // Verify that we can only read one page worth of data.
+ size_t bytes_read = backtrace->Read(read_addr, data, 2 * pagesize);
+ ASSERT_EQ(pagesize, bytes_read);
+ ASSERT_TRUE(memcmp(data, expected, pagesize) == 0);
+
+ // Verify unaligned reads.
+ for (size_t i = 1; i < sizeof(word_t); i++) {
+ bytes_read = backtrace->Read(read_addr + i, data, 2 * sizeof(word_t));
+ ASSERT_EQ(2 * sizeof(word_t), bytes_read);
+ ASSERT_TRUE(memcmp(data, &expected[i], 2 * sizeof(word_t)) == 0)
+ << "Offset at " << i << " failed";
+ }
+ delete data;
+ delete expected;
+}
+
+TEST(libbacktrace, thread_read) {
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_t thread;
+ thread_t thread_data = { 0, 0, 0, nullptr };
+ ASSERT_TRUE(pthread_create(&thread, &attr, ThreadReadTest, &thread_data) == 0);
+
+ ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
+
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid));
+ ASSERT_TRUE(backtrace.get() != nullptr);
+
+ RunReadTest(backtrace.get(), reinterpret_cast<uintptr_t>(thread_data.data));
+
+ android_atomic_acquire_store(0, &thread_data.state);
+
+ ASSERT_TRUE(WaitForNonZero(&thread_data.state, 10));
+}
+
+volatile uintptr_t g_ready = 0;
+volatile uintptr_t g_addr = 0;
+
+void ForkedReadTest() {
+ // Create two map pages.
+ size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
+ uint8_t* memory;
+ if (posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2 * pagesize) != 0) {
+ perror("Failed to allocate memory\n");
+ exit(1);
+ }
+
+ // Mark the second page as not-readable.
+ if (mprotect(&memory[pagesize], pagesize, PROT_NONE) != 0) {
+ perror("Failed to mprotect memory\n");
+ exit(1);
+ }
+
+ // Set up a simple pattern in memory.
+ for (size_t i = 0; i < pagesize; i++) {
+ memory[i] = i;
+ }
+
+ g_addr = reinterpret_cast<uintptr_t>(memory);
+ g_ready = 1;
+
+ while (1) {
+ usleep(US_PER_MSEC);
+ }
+}
+
+TEST(libbacktrace, process_read) {
+ pid_t pid;
+ if ((pid = fork()) == 0) {
+ ForkedReadTest();
+ exit(0);
+ }
+ ASSERT_NE(-1, pid);
+
+ bool test_executed = false;
+ uint64_t start = NanoTime();
+ while (1) {
+ if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
+ WaitForStop(pid);
+
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(pid, pid));
+
+ uintptr_t read_addr;
+ size_t bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_ready),
+ reinterpret_cast<uint8_t*>(&read_addr),
+ sizeof(uintptr_t));
+ ASSERT_EQ(sizeof(uintptr_t), bytes_read);
+ if (read_addr) {
+ // The forked process is ready to be read.
+ bytes_read = backtrace->Read(reinterpret_cast<uintptr_t>(&g_addr),
+ reinterpret_cast<uint8_t*>(&read_addr),
+ sizeof(uintptr_t));
+ ASSERT_EQ(sizeof(uintptr_t), bytes_read);
+
+ RunReadTest(backtrace.get(), read_addr);
+
+ test_executed = true;
+ break;
+ }
+ ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
+ }
+ if ((NanoTime() - start) > 5 * NS_PER_SEC) {
+ break;
+ }
+ usleep(US_PER_MSEC);
+ }
+ kill(pid, SIGKILL);
+ ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
+
+ ASSERT_TRUE(test_executed);
+}
+
+#if defined(ENABLE_PSS_TESTS)
+#include "GetPss.h"
+
+#define MAX_LEAK_BYTES 32*1024UL
+
+void CheckForLeak(pid_t pid, pid_t tid) {
+ // Do a few runs to get the PSS stable.
+ for (size_t i = 0; i < 100; i++) {
+ Backtrace* backtrace = Backtrace::Create(pid, tid);
+ ASSERT_TRUE(backtrace != nullptr);
+ ASSERT_TRUE(backtrace->Unwind(0));
+ delete backtrace;
+ }
+ size_t stable_pss = GetPssBytes();
+
+ // Loop enough that even a small leak should be detectable.
+ for (size_t i = 0; i < 4096; i++) {
+ Backtrace* backtrace = Backtrace::Create(pid, tid);
+ ASSERT_TRUE(backtrace != nullptr);
+ ASSERT_TRUE(backtrace->Unwind(0));
+ delete backtrace;
+ }
+ size_t new_pss = GetPssBytes();
+ size_t abs_diff = (new_pss > stable_pss) ? new_pss - stable_pss : stable_pss - new_pss;
+ // As long as the new pss is within a certain amount, consider everything okay.
+ ASSERT_LE(abs_diff, MAX_LEAK_BYTES);
+}
+
+TEST(libbacktrace, check_for_leak_local) {
+ CheckForLeak(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD);
+}
+
+TEST(libbacktrace, check_for_leak_local_thread) {
+ thread_t thread_data = { 0, 0, 0, nullptr };
+ pthread_t thread;
+ ASSERT_TRUE(pthread_create(&thread, nullptr, ThreadLevelRun, &thread_data) == 0);
+
+ // Wait up to 2 seconds for the tid to be set.
+ ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2));
+
+ CheckForLeak(BACKTRACE_CURRENT_PROCESS, thread_data.tid);
+
+ // Tell the thread to exit its infinite loop.
+ android_atomic_acquire_store(0, &thread_data.state);
+
+ ASSERT_TRUE(pthread_join(thread, nullptr) == 0);
+}
+
+TEST(libbacktrace, check_for_leak_remote) {
+ pid_t pid;
+
+ if ((pid = fork()) == 0) {
+ while (true) {
+ }
+ _exit(0);
+ }
+ ASSERT_LT(0, pid);
+
+ ASSERT_TRUE(ptrace(PTRACE_ATTACH, pid, 0, 0) == 0);
+
+ // Wait for the process to get to a stopping point.
+ WaitForStop(pid);
+
+ CheckForLeak(pid, BACKTRACE_CURRENT_THREAD);
+
+ ASSERT_TRUE(ptrace(PTRACE_DETACH, pid, 0, 0) == 0);
+
+ kill(pid, SIGKILL);
+ ASSERT_EQ(waitpid(pid, nullptr, 0), pid);
+}
+#endif
diff --git a/libcorkscrew/Android.mk b/libcorkscrew/Android.mk
deleted file mode 100644
index 8f3b68c..0000000
--- a/libcorkscrew/Android.mk
+++ /dev/null
@@ -1,100 +0,0 @@
-# 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.
-
-LOCAL_PATH:= $(call my-dir)
-
-generic_src_files := \
- backtrace.c \
- backtrace-helper.c \
- demangle.c \
- map_info.c \
- ptrace.c \
- symbol_table.c
-
-arm_src_files := \
- arch-arm/backtrace-arm.c \
- arch-arm/ptrace-arm.c
-
-x86_src_files := \
- arch-x86/backtrace-x86.c \
- arch-x86/ptrace-x86.c
-
-ifneq ($(TARGET_IS_64_BIT),true)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := $(generic_src_files)
-
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_SRC_FILES += $(arm_src_files)
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
-endif
-ifeq ($(TARGET_ARCH),x86)
-LOCAL_SRC_FILES += $(x86_src_files)
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
-endif
-ifeq ($(TARGET_ARCH),mips)
-LOCAL_SRC_FILES += \
- arch-mips/backtrace-mips.c \
- arch-mips/ptrace-mips.c
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
-endif
-
-LOCAL_SHARED_LIBRARIES += libdl libcutils liblog libgccdemangle
-
-LOCAL_CFLAGS += -std=gnu99 -Werror -Wno-unused-parameter
-LOCAL_MODULE := libcorkscrew
-LOCAL_MODULE_TAGS := optional
-
-include $(BUILD_SHARED_LIBRARY)
-
-# Build test.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := test.cpp
-LOCAL_CFLAGS += -Werror -fno-inline-small-functions
-LOCAL_SHARED_LIBRARIES := libcorkscrew
-LOCAL_MODULE := libcorkscrew_test
-LOCAL_MODULE_TAGS := optional
-include $(BUILD_EXECUTABLE)
-
-endif # TARGET_IS_64_BIT == false
-
-
-ifeq ($(HOST_OS)-$(HOST_ARCH),linux-x86)
-
-# Build libcorkscrew.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES += $(generic_src_files) $(x86_src_files)
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
-LOCAL_STATIC_LIBRARIES += libcutils liblog
-LOCAL_LDLIBS += -ldl
-ifeq ($(HOST_OS),linux)
- LOCAL_SHARED_LIBRARIES += libgccdemangle # TODO: is this even needed on Linux?
- LOCAL_LDLIBS += -lrt
-endif
-LOCAL_CFLAGS += -std=gnu99 -Werror -Wno-unused-parameter
-LOCAL_MODULE := libcorkscrew
-LOCAL_MODULE_TAGS := optional
-include $(BUILD_HOST_SHARED_LIBRARY)
-
-# Build test.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := test.cpp
-LOCAL_CFLAGS += -Werror
-LOCAL_SHARED_LIBRARIES := libcorkscrew
-LOCAL_MODULE := libcorkscrew_test
-LOCAL_MODULE_TAGS := optional
-include $(BUILD_HOST_EXECUTABLE)
-
-endif # $(HOST_OS)-$(HOST_ARCH) == linux-x86
diff --git a/libcorkscrew/NOTICE b/libcorkscrew/NOTICE
deleted file mode 100644
index becc120..0000000
--- a/libcorkscrew/NOTICE
+++ /dev/null
@@ -1,190 +0,0 @@
-
- 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.
-
- 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.
-
-
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
diff --git a/libcorkscrew/arch-arm/backtrace-arm.c b/libcorkscrew/arch-arm/backtrace-arm.c
deleted file mode 100644
index 751efbf..0000000
--- a/libcorkscrew/arch-arm/backtrace-arm.c
+++ /dev/null
@@ -1,589 +0,0 @@
-/*
- * 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.
- */
-
-/*
- * Backtracing functions for ARM.
- *
- * This implementation uses the exception unwinding tables provided by
- * the compiler to unwind call frames. Refer to the ARM Exception Handling ABI
- * documentation (EHABI) for more details about what's going on here.
- *
- * An ELF binary may contain an EXIDX section that provides an index to
- * the exception handling table of each function, sorted by program
- * counter address.
- *
- * This implementation also supports unwinding other processes via ptrace().
- * In that case, the EXIDX section is found by reading the ELF section table
- * structures using ptrace().
- *
- * Because the tables are used for exception handling, it can happen that
- * a given function will not have an exception handling table. In particular,
- * exceptions are assumed to only ever be thrown at call sites. Therefore,
- * by definition leaf functions will not have exception handling tables.
- * This may make unwinding impossible in some cases although we can still get
- * some idea of the call stack by examining the PC and LR registers.
- *
- * As we are only interested in backtrace information, we do not need
- * to perform all of the work of unwinding such as restoring register
- * state and running cleanup functions. Unwinding is performed virtually on
- * an abstract machine context consisting of just the ARM core registers.
- * Furthermore, we do not run generic "personality functions" because
- * we may not be in a position to execute arbitrary code, especially if
- * we are running in a signal handler or using ptrace()!
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../backtrace-arch.h"
-#include "../backtrace-helper.h"
-#include "../ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-
-#include <stdlib.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <errno.h>
-#include <sys/ptrace.h>
-#include <elf.h>
-#include <cutils/log.h>
-
-#include <ucontext.h>
-
-/* Unwind state. */
-typedef struct {
- uint32_t gregs[16];
-} unwind_state_t;
-
-static const int R_SP = 13;
-static const int R_LR = 14;
-static const int R_PC = 15;
-
-/* Special EXIDX value that indicates that a frame cannot be unwound. */
-static const uint32_t EXIDX_CANTUNWIND = 1;
-
-/* Get the EXIDX section start and size for the module that contains a
- * given program counter address.
- *
- * When the executable is statically linked, the EXIDX section can be
- * accessed by querying the values of the __exidx_start and __exidx_end
- * symbols.
- *
- * When the executable is dynamically linked, the linker exports a function
- * called dl_unwind_find_exidx that obtains the EXIDX section for a given
- * absolute program counter address.
- *
- * Bionic exports a helpful function called __gnu_Unwind_Find_exidx that
- * handles both cases, so we use that here.
- */
-typedef long unsigned int* _Unwind_Ptr;
-extern _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr pc, int *pcount);
-
-static uintptr_t find_exidx(uintptr_t pc, size_t* out_exidx_size) {
- int count;
- uintptr_t start = (uintptr_t)__gnu_Unwind_Find_exidx((_Unwind_Ptr)pc, &count);
- *out_exidx_size = count;
- return start;
-}
-
-/* Transforms a 31-bit place-relative offset to an absolute address.
- * We assume the most significant bit is clear. */
-static uintptr_t prel_to_absolute(uintptr_t place, uint32_t prel_offset) {
- return place + (((int32_t)(prel_offset << 1)) >> 1);
-}
-
-static uintptr_t get_exception_handler(const memory_t* memory,
- const map_info_t* map_info_list, uintptr_t pc) {
- if (!pc) {
- ALOGV("get_exception_handler: pc is zero, no handler");
- return 0;
- }
-
- uintptr_t exidx_start;
- size_t exidx_size;
- const map_info_t* mi;
- if (memory->tid < 0) {
- mi = NULL;
- exidx_start = find_exidx(pc, &exidx_size);
- } else {
- mi = find_map_info(map_info_list, pc);
- if (mi && mi->data) {
- const map_info_data_t* data = (const map_info_data_t*)mi->data;
- exidx_start = data->exidx_start;
- exidx_size = data->exidx_size;
- } else {
- exidx_start = 0;
- exidx_size = 0;
- }
- }
-
- uintptr_t handler = 0;
- int32_t handler_index = -1;
- if (exidx_start) {
- uint32_t low = 0;
- uint32_t high = exidx_size;
- while (low < high) {
- uint32_t index = (low + high) / 2;
- uintptr_t entry = exidx_start + index * 8;
- uint32_t entry_prel_pc;
- ALOGV("XXX low=%u, high=%u, index=%u", low, high, index);
- if (!try_get_word(memory, entry, &entry_prel_pc)) {
- break;
- }
- uintptr_t entry_pc = prel_to_absolute(entry, entry_prel_pc);
- ALOGV("XXX entry_pc=0x%08x", entry_pc);
- if (pc < entry_pc) {
- high = index;
- continue;
- }
- if (index + 1 < exidx_size) {
- uintptr_t next_entry = entry + 8;
- uint32_t next_entry_prel_pc;
- if (!try_get_word(memory, next_entry, &next_entry_prel_pc)) {
- break;
- }
- uintptr_t next_entry_pc = prel_to_absolute(next_entry, next_entry_prel_pc);
- ALOGV("XXX next_entry_pc=0x%08x", next_entry_pc);
- if (pc >= next_entry_pc) {
- low = index + 1;
- continue;
- }
- }
-
- uintptr_t entry_handler_ptr = entry + 4;
- uint32_t entry_handler;
- if (!try_get_word(memory, entry_handler_ptr, &entry_handler)) {
- break;
- }
- if (entry_handler & (1L << 31)) {
- handler = entry_handler_ptr; // in-place handler data
- } else if (entry_handler != EXIDX_CANTUNWIND) {
- handler = prel_to_absolute(entry_handler_ptr, entry_handler);
- }
- handler_index = index;
- break;
- }
- }
- if (mi) {
- ALOGV("get_exception_handler: pc=0x%08x, module='%s', module_start=0x%08x, "
- "exidx_start=0x%08x, exidx_size=%d, handler=0x%08x, handler_index=%d",
- pc, mi->name, mi->start, exidx_start, exidx_size, handler, handler_index);
- } else {
- ALOGV("get_exception_handler: pc=0x%08x, "
- "exidx_start=0x%08x, exidx_size=%d, handler=0x%08x, handler_index=%d",
- pc, exidx_start, exidx_size, handler, handler_index);
- }
- return handler;
-}
-
-typedef struct {
- uintptr_t ptr;
- uint32_t word;
-} byte_stream_t;
-
-static bool try_next_byte(const memory_t* memory, byte_stream_t* stream, uint8_t* out_value) {
- uint8_t result;
- switch (stream->ptr & 3) {
- case 0:
- if (!try_get_word(memory, stream->ptr, &stream->word)) {
- *out_value = 0;
- return false;
- }
- *out_value = stream->word >> 24;
- break;
-
- case 1:
- *out_value = stream->word >> 16;
- break;
-
- case 2:
- *out_value = stream->word >> 8;
- break;
-
- default:
- *out_value = stream->word;
- break;
- }
-
- ALOGV("next_byte: ptr=0x%08x, value=0x%02x", stream->ptr, *out_value);
- stream->ptr += 1;
- return true;
-}
-
-static void set_reg(unwind_state_t* state, uint32_t reg, uint32_t value) {
- ALOGV("set_reg: reg=%d, value=0x%08x", reg, value);
- state->gregs[reg] = value;
-}
-
-static bool try_pop_registers(const memory_t* memory, unwind_state_t* state, uint32_t mask) {
- uint32_t sp = state->gregs[R_SP];
- bool sp_updated = false;
- for (int i = 0; i < 16; i++) {
- if (mask & (1 << i)) {
- uint32_t value;
- if (!try_get_word(memory, sp, &value)) {
- return false;
- }
- if (i == R_SP) {
- sp_updated = true;
- }
- set_reg(state, i, value);
- sp += 4;
- }
- }
- if (!sp_updated) {
- set_reg(state, R_SP, sp);
- }
- return true;
-}
-
-/* Executes a built-in personality routine as defined in the EHABI.
- * Returns true if unwinding should continue.
- *
- * The data for the built-in personality routines consists of a sequence
- * of unwinding instructions, followed by a sequence of scope descriptors,
- * each of which has a length and offset encoded using 16-bit or 32-bit
- * values.
- *
- * We only care about the unwinding instructions. They specify the
- * operations of an abstract machine whose purpose is to transform the
- * virtual register state (including the stack pointer) such that
- * the call frame is unwound and the PC register points to the call site.
- */
-static bool execute_personality_routine(const memory_t* memory,
- unwind_state_t* state, byte_stream_t* stream, int pr_index) {
- size_t size;
- switch (pr_index) {
- case 0: // Personality routine #0, short frame, descriptors have 16-bit scope.
- size = 3;
- break;
- case 1: // Personality routine #1, long frame, descriptors have 16-bit scope.
- case 2: { // Personality routine #2, long frame, descriptors have 32-bit scope.
- uint8_t size_byte;
- if (!try_next_byte(memory, stream, &size_byte)) {
- return false;
- }
- size = (uint32_t)size_byte * sizeof(uint32_t) + 2;
- break;
- }
- default: // Unknown personality routine. Stop here.
- return false;
- }
-
- bool pc_was_set = false;
- while (size--) {
- uint8_t op;
- if (!try_next_byte(memory, stream, &op)) {
- return false;
- }
- if ((op & 0xc0) == 0x00) {
- // "vsp = vsp + (xxxxxx << 2) + 4"
- set_reg(state, R_SP, state->gregs[R_SP] + ((op & 0x3f) << 2) + 4);
- } else if ((op & 0xc0) == 0x40) {
- // "vsp = vsp - (xxxxxx << 2) - 4"
- set_reg(state, R_SP, state->gregs[R_SP] - ((op & 0x3f) << 2) - 4);
- } else if ((op & 0xf0) == 0x80) {
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- uint32_t mask = (((uint32_t)op & 0x0f) << 12) | ((uint32_t)op2 << 4);
- if (mask) {
- // "Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}"
- if (!try_pop_registers(memory, state, mask)) {
- return false;
- }
- if (mask & (1 << R_PC)) {
- pc_was_set = true;
- }
- } else {
- // "Refuse to unwind"
- return false;
- }
- } else if ((op & 0xf0) == 0x90) {
- if (op != 0x9d && op != 0x9f) {
- // "Set vsp = r[nnnn]"
- set_reg(state, R_SP, state->gregs[op & 0x0f]);
- } else {
- // "Reserved as prefix for ARM register to register moves"
- // "Reserved as prefix for Intel Wireless MMX register to register moves"
- return false;
- }
- } else if ((op & 0xf8) == 0xa0) {
- // "Pop r4-r[4+nnn]"
- uint32_t mask = (0x0ff0 >> (7 - (op & 0x07))) & 0x0ff0;
- if (!try_pop_registers(memory, state, mask)) {
- return false;
- }
- } else if ((op & 0xf8) == 0xa8) {
- // "Pop r4-r[4+nnn], r14"
- uint32_t mask = ((0x0ff0 >> (7 - (op & 0x07))) & 0x0ff0) | 0x4000;
- if (!try_pop_registers(memory, state, mask)) {
- return false;
- }
- } else if (op == 0xb0) {
- // "Finish"
- break;
- } else if (op == 0xb1) {
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- if (op2 != 0x00 && (op2 & 0xf0) == 0x00) {
- // "Pop integer registers under mask {r3, r2, r1, r0}"
- if (!try_pop_registers(memory, state, op2)) {
- return false;
- }
- } else {
- // "Spare"
- return false;
- }
- } else if (op == 0xb2) {
- // "vsp = vsp + 0x204 + (uleb128 << 2)"
- uint32_t value = 0;
- uint32_t shift = 0;
- uint8_t op2;
- do {
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- value |= (op2 & 0x7f) << shift;
- shift += 7;
- } while (op2 & 0x80);
- set_reg(state, R_SP, state->gregs[R_SP] + (value << 2) + 0x204);
- } else if (op == 0xb3) {
- // "Pop VFP double-precision registers D[ssss]-D[ssss+cccc] saved (as if) by FSTMFDX"
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 12);
- } else if ((op & 0xf8) == 0xb8) {
- // "Pop VFP double-precision registers D[8]-D[8+nnn] saved (as if) by FSTMFDX"
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 12);
- } else if ((op & 0xf8) == 0xc0) {
- // "Intel Wireless MMX pop wR[10]-wR[10+nnn]"
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 8);
- } else if (op == 0xc6) {
- // "Intel Wireless MMX pop wR[ssss]-wR[ssss+cccc]"
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
- } else if (op == 0xc7) {
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- if (op2 != 0x00 && (op2 & 0xf0) == 0x00) {
- // "Intel Wireless MMX pop wCGR registers under mask {wCGR3,2,1,0}"
- set_reg(state, R_SP, state->gregs[R_SP] + __builtin_popcount(op2) * 4);
- } else {
- // "Spare"
- return false;
- }
- } else if (op == 0xc8) {
- // "Pop VFP double precision registers D[16+ssss]-D[16+ssss+cccc]
- // saved (as if) by FSTMFD"
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
- } else if (op == 0xc9) {
- // "Pop VFP double precision registers D[ssss]-D[ssss+cccc] saved (as if) by FSTMFDD"
- uint8_t op2;
- if (!(size--) || !try_next_byte(memory, stream, &op2)) {
- return false;
- }
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
- } else if ((op == 0xf8) == 0xd0) {
- // "Pop VFP double-precision registers D[8]-D[8+nnn] saved (as if) by FSTMFDD"
- set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 8);
- } else {
- // "Spare"
- return false;
- }
- }
- if (!pc_was_set) {
- set_reg(state, R_PC, state->gregs[R_LR]);
- }
- return true;
-}
-
-static bool try_get_half_word(const memory_t* memory, uint32_t pc, uint16_t* out_value) {
- uint32_t word;
- if (try_get_word(memory, pc & ~2, &word)) {
- *out_value = pc & 2 ? word >> 16 : word & 0xffff;
- return true;
- }
- return false;
-}
-
-uintptr_t rewind_pc_arch(const memory_t* memory, uintptr_t pc) {
- if (pc & 1) {
- /* Thumb mode - need to check whether the bl(x) has long offset or not.
- * Examples:
- *
- * arm blx in the middle of thumb:
- * 187ae: 2300 movs r3, #0
- * 187b0: f7fe ee1c blx 173ec
- * 187b4: 2c00 cmp r4, #0
- *
- * arm bl in the middle of thumb:
- * 187d8: 1c20 adds r0, r4, #0
- * 187da: f136 fd15 bl 14f208
- * 187de: 2800 cmp r0, #0
- *
- * pure thumb:
- * 18894: 189b adds r3, r3, r2
- * 18896: 4798 blx r3
- * 18898: b001 add sp, #4
- */
- uint16_t prev1, prev2;
- if (try_get_half_word(memory, pc - 5, &prev1)
- && ((prev1 & 0xf000) == 0xf000)
- && try_get_half_word(memory, pc - 3, &prev2)
- && ((prev2 & 0xe000) == 0xe000)) {
- pc -= 4; // long offset
- } else {
- pc -= 2;
- }
- } else {
- /* ARM mode, all instructions are 32bit. Yay! */
- pc -= 4;
- }
- return pc;
-}
-
-static ssize_t unwind_backtrace_common(const memory_t* memory,
- const map_info_t* map_info_list,
- unwind_state_t* state, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth) {
- size_t ignored_frames = 0;
- size_t returned_frames = 0;
-
- for (size_t index = 0; returned_frames < max_depth; index++) {
- uintptr_t pc = index ? rewind_pc_arch(memory, state->gregs[R_PC])
- : state->gregs[R_PC];
- backtrace_frame_t* frame = add_backtrace_entry(pc,
- backtrace, ignore_depth, max_depth, &ignored_frames, &returned_frames);
- if (frame) {
- frame->stack_top = state->gregs[R_SP];
- }
-
- uintptr_t handler = get_exception_handler(memory, map_info_list, pc);
- if (!handler) {
- // If there is no handler for the PC and this is the first frame,
- // then the program may have branched to an invalid address.
- // Try starting from the LR instead, otherwise stop unwinding.
- if (index == 0 && state->gregs[R_LR]
- && state->gregs[R_LR] != state->gregs[R_PC]) {
- set_reg(state, R_PC, state->gregs[R_LR]);
- continue;
- } else {
- break;
- }
- }
-
- byte_stream_t stream;
- stream.ptr = handler;
- uint8_t pr;
- if (!try_next_byte(memory, &stream, &pr)) {
- break;
- }
- if ((pr & 0xf0) != 0x80) {
- // The first word is a place-relative pointer to a generic personality
- // routine function. We don't support invoking such functions, so stop here.
- break;
- }
-
- // The first byte indicates the personality routine to execute.
- // Following bytes provide instructions to the personality routine.
- if (!execute_personality_routine(memory, state, &stream, pr & 0x0f)) {
- break;
- }
- if (frame && state->gregs[R_SP] > frame->stack_top) {
- frame->stack_size = state->gregs[R_SP] - frame->stack_top;
- }
- if (!state->gregs[R_PC]) {
- break;
- }
- }
-
- // Ran out of frames that we could unwind using handlers.
- // Add a final entry for the LR if it looks sane and call it good.
- if (returned_frames < max_depth
- && state->gregs[R_LR]
- && state->gregs[R_LR] != state->gregs[R_PC]
- && is_executable_map(map_info_list, state->gregs[R_LR])) {
- // We don't know where the stack for this extra frame starts so we
- // don't return any stack information for it.
- add_backtrace_entry(rewind_pc_arch(memory, state->gregs[R_LR]),
- backtrace, ignore_depth, max_depth, &ignored_frames, &returned_frames);
- }
- return returned_frames;
-}
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext,
- const map_info_t* map_info_list,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
- const ucontext_t* uc = (const ucontext_t*)sigcontext;
-
- unwind_state_t state;
-
- state.gregs[0] = uc->uc_mcontext.arm_r0;
- state.gregs[1] = uc->uc_mcontext.arm_r1;
- state.gregs[2] = uc->uc_mcontext.arm_r2;
- state.gregs[3] = uc->uc_mcontext.arm_r3;
- state.gregs[4] = uc->uc_mcontext.arm_r4;
- state.gregs[5] = uc->uc_mcontext.arm_r5;
- state.gregs[6] = uc->uc_mcontext.arm_r6;
- state.gregs[7] = uc->uc_mcontext.arm_r7;
- state.gregs[8] = uc->uc_mcontext.arm_r8;
- state.gregs[9] = uc->uc_mcontext.arm_r9;
- state.gregs[10] = uc->uc_mcontext.arm_r10;
- state.gregs[11] = uc->uc_mcontext.arm_fp;
- state.gregs[12] = uc->uc_mcontext.arm_ip;
- state.gregs[13] = uc->uc_mcontext.arm_sp;
- state.gregs[14] = uc->uc_mcontext.arm_lr;
- state.gregs[15] = uc->uc_mcontext.arm_pc;
-
- memory_t memory;
- init_memory(&memory, map_info_list);
- return unwind_backtrace_common(&memory, map_info_list, &state,
- backtrace, ignore_depth, max_depth);
-}
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
- struct pt_regs regs;
- if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) {
- return -1;
- }
-
- unwind_state_t state;
- for (int i = 0; i < 16; i++) {
- state.gregs[i] = regs.uregs[i];
- }
-
- memory_t memory;
- init_memory_ptrace(&memory, tid);
- return unwind_backtrace_common(&memory, context->map_info_list, &state,
- backtrace, ignore_depth, max_depth);
-}
diff --git a/libcorkscrew/arch-arm/ptrace-arm.c b/libcorkscrew/arch-arm/ptrace-arm.c
deleted file mode 100644
index a50844e..0000000
--- a/libcorkscrew/arch-arm/ptrace-arm.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../ptrace-arch.h"
-
-#include <elf.h>
-#include <cutils/log.h>
-
-#ifndef PT_ARM_EXIDX
-#define PT_ARM_EXIDX 0x70000001
-#endif
-
-static void load_exidx_header(pid_t pid, map_info_t* mi,
- uintptr_t* out_exidx_start, size_t* out_exidx_size) {
- uint32_t elf_phoff;
- uint32_t elf_phentsize_ehsize;
- uint32_t elf_shentsize_phnum;
- if (try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phoff), &elf_phoff)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_ehsize),
- &elf_phentsize_ehsize)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phnum),
- &elf_shentsize_phnum)) {
- uint32_t elf_phentsize = elf_phentsize_ehsize >> 16;
- uint32_t elf_phnum = elf_shentsize_phnum & 0xffff;
- for (uint32_t i = 0; i < elf_phnum; i++) {
- uintptr_t elf_phdr = mi->start + elf_phoff + i * elf_phentsize;
- uint32_t elf_phdr_type;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_type), &elf_phdr_type)) {
- break;
- }
- if (elf_phdr_type == PT_ARM_EXIDX) {
- uint32_t elf_phdr_offset;
- uint32_t elf_phdr_filesz;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_offset),
- &elf_phdr_offset)
- || !try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_filesz),
- &elf_phdr_filesz)) {
- break;
- }
- *out_exidx_start = mi->start + elf_phdr_offset;
- *out_exidx_size = elf_phdr_filesz / 8;
- ALOGV("Parsed EXIDX header info for %s: start=0x%08x, size=%d", mi->name,
- *out_exidx_start, *out_exidx_size);
- return;
- }
- }
- }
- *out_exidx_start = 0;
- *out_exidx_size = 0;
-}
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) {
- load_exidx_header(pid, mi, &data->exidx_start, &data->exidx_size);
-}
-
-void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data) {
-}
diff --git a/libcorkscrew/arch-mips/backtrace-mips.c b/libcorkscrew/arch-mips/backtrace-mips.c
deleted file mode 100644
index 832fb86..0000000
--- a/libcorkscrew/arch-mips/backtrace-mips.c
+++ /dev/null
@@ -1,901 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-/*
- * Backtracing functions for mips
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../backtrace-arch.h"
-#include "../backtrace-helper.h"
-#include "../ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-#include "dwarf.h"
-
-#include <stdlib.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/ptrace.h>
-#include <cutils/log.h>
-
-#include <sys/ucontext.h>
-
-/* For PTRACE_GETREGS */
-typedef struct {
- uint64_t regs[32];
- uint64_t lo;
- uint64_t hi;
- uint64_t epc;
- uint64_t badvaddr;
- uint64_t status;
- uint64_t cause;
-} user_regs_struct;
-
-enum {
- REG_ZERO = 0, REG_AT, REG_V0, REG_V1,
- REG_A0, REG_A1, REG_A2, REG_A3,
- REG_T0, REG_T1, REG_T2, REG_T3,
- REG_T4, REG_T5, REG_T6, REG_T7,
- REG_S0, REG_S1, REG_S2, REG_S3,
- REG_S4, REG_S5, REG_S6, REG_S7,
- REG_T8, REG_T9, REG_K0, REG_K1,
- REG_GP, REG_SP, REG_S8, REG_RA,
-};
-
-
-/* Unwind state. */
-typedef struct {
- uint32_t reg[DWARF_REGISTERS];
-} unwind_state_t;
-
-uintptr_t rewind_pc_arch(const memory_t* memory __attribute__((unused)), uintptr_t pc) {
- if (pc == 0)
- return pc;
- if ((pc & 1) == 0)
- return pc-8; /* jal/bal/jalr + branch delay slot */
- return pc;
-}
-
-/* Read byte through 4 byte cache. Usually we read byte by byte and updating cursor. */
-static bool try_get_byte(const memory_t* memory, uintptr_t ptr, uint8_t* out_value, uint32_t* cursor) {
- static uintptr_t lastptr;
- static uint32_t buf;
-
- ptr += *cursor;
-
- if (ptr < lastptr || lastptr + 3 < ptr) {
- lastptr = (ptr >> 2) << 2;
- if (!try_get_word(memory, lastptr, &buf)) {
- return false;
- }
- }
- *out_value = (uint8_t)((buf >> ((ptr & 3) * 8)) & 0xff);
- ++*cursor;
- return true;
-}
-
-/* Getting X bytes. 4 is maximum for now. */
-static bool try_get_xbytes(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint8_t bytes, uint32_t* cursor) {
- uint32_t data = 0;
- if (bytes > 4) {
- ALOGE("can't read more than 4 bytes, trying to read %d", bytes);
- return false;
- }
- for (int i = 0; i < bytes; i++) {
- uint8_t buf;
- if (!try_get_byte(memory, ptr, &buf, cursor)) {
- return false;
- }
- data |= (uint32_t)buf << (i * 8);
- }
- *out_value = data;
- return true;
-}
-
-/* Reads signed/unsigned LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_leb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor, bool sign_extend) {
- uint8_t buf = 0;
- uint32_t val = 0;
- uint8_t c = 0;
- do {
- if (!try_get_byte(memory, ptr, &buf, cursor)) {
- return false;
- }
- val |= ((uint32_t)buf & 0x7f) << (c * 7);
- c++;
- } while (buf & 0x80 && (c * 7) <= 32);
- if (c * 7 > 32) {
- ALOGE("%s: data exceeds expected 4 bytes maximum", __FUNCTION__);
- return false;
- }
- if (sign_extend) {
- if (buf & 0x40) {
- val |= ((uint32_t)-1 << (c * 7));
- }
- }
- *out_value = val;
- return true;
-}
-
-/* Reads signed LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_sleb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor) {
- return try_get_leb128(memory, ptr, out_value, cursor, true);
-}
-
-/* Reads unsigned LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_uleb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor) {
- return try_get_leb128(memory, ptr, out_value, cursor, false);
-}
-
-/* Getting data encoded by dwarf encodings. */
-static bool read_dwarf(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint8_t encoding, uint32_t* cursor) {
- uint32_t data = 0;
- bool issigned = true;
- uintptr_t addr = ptr + *cursor;
- /* Lower 4 bits is data type/size */
- /* TODO: add more encodings if it becomes necessary */
- switch (encoding & 0xf) {
- case DW_EH_PE_absptr:
- if (!try_get_xbytes(memory, ptr, &data, 4, cursor)) {
- return false;
- }
- *out_value = data;
- return true;
- case DW_EH_PE_udata4:
- issigned = false;
- case DW_EH_PE_sdata4:
- if (!try_get_xbytes(memory, ptr, &data, 4, cursor)) {
- return false;
- }
- break;
- default:
- ALOGE("unrecognized dwarf lower part encoding: 0x%x", encoding);
- return false;
- }
- /* Higher 4 bits is modifier */
- /* TODO: add more encodings if it becomes necessary */
- switch (encoding & 0xf0) {
- case 0:
- *out_value = data;
- break;
- case DW_EH_PE_pcrel:
- if (issigned) {
- *out_value = addr + (int32_t)data;
- } else {
- *out_value = addr + data;
- }
- break;
- /* Assuming ptr is correct base to calculate datarel */
- case DW_EH_PE_datarel:
- if (issigned) {
- *out_value = ptr + (int32_t)data;
- } else {
- *out_value = ptr + data;
- }
- break;
- default:
- ALOGE("unrecognized dwarf higher part encoding: 0x%x", encoding);
- return false;
- }
- return true;
-}
-
-/* Having PC find corresponding FDE by reading .eh_frame_hdr section data. */
-static uintptr_t find_fde(const memory_t* memory,
- const map_info_t* map_info_list, uintptr_t pc) {
- if (!pc) {
- ALOGV("find_fde: pc is zero, no eh_frame");
- return 0;
- }
- const map_info_t* mi = find_map_info(map_info_list, pc);
- if (!mi) {
- ALOGV("find_fde: no map info for pc:0x%x", pc);
- return 0;
- }
- const map_info_data_t* midata = mi->data;
- if (!midata) {
- ALOGV("find_fde: no eh_frame_hdr for map: start=0x%x, end=0x%x", mi->start, mi->end);
- return 0;
- }
-
- eh_frame_hdr_info_t eh_hdr_info;
- memset(&eh_hdr_info, 0, sizeof(eh_frame_hdr_info_t));
-
- /* Getting the first word of eh_frame_hdr:
- 1st byte is version;
- 2nd byte is encoding of pointer to eh_frames;
- 3rd byte is encoding of count of FDEs in lookup table;
- 4th byte is encoding of lookup table entries.
- */
- uintptr_t eh_frame_hdr = midata->eh_frame_hdr;
- uint32_t c = 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.version, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.eh_frame_ptr_enc, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.fde_count_enc, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.fde_table_enc, &c)) return 0;
-
- /* TODO: 3rd byte can be DW_EH_PE_omit, that means no lookup table available and we should
- try to parse eh_frame instead. Not sure how often it may occur, skipping now.
- */
- if (eh_hdr_info.version != 1) {
- ALOGV("find_fde: eh_frame_hdr version %d is not supported", eh_hdr_info.version);
- return 0;
- }
- /* Getting the data:
- 2nd word is eh_frame pointer (normally not used, because lookup table has all we need);
- 3rd word is count of FDEs in the lookup table;
- starting from 4 word there is FDE lookup table (pairs of PC and FDE pointer) sorted by PC;
- */
- if (!read_dwarf(memory, eh_frame_hdr, &eh_hdr_info.eh_frame_ptr, eh_hdr_info.eh_frame_ptr_enc, &c)) return 0;
- if (!read_dwarf(memory, eh_frame_hdr, &eh_hdr_info.fde_count, eh_hdr_info.fde_count_enc, &c)) return 0;
- ALOGV("find_fde: found %d FDEs", eh_hdr_info.fde_count);
-
- int32_t low = 0;
- int32_t high = eh_hdr_info.fde_count;
- uintptr_t start = 0;
- uintptr_t fde = 0;
- /* eh_frame_hdr + c points to lookup table at this point. */
- while (low <= high) {
- uint32_t mid = (high + low)/2;
- uint32_t entry = c + mid * 8;
- if (!read_dwarf(memory, eh_frame_hdr, &start, eh_hdr_info.fde_table_enc, &entry)) return 0;
- if (pc <= start) {
- high = mid - 1;
- } else {
- low = mid + 1;
- }
- }
- /* Value found is at high. */
- if (high < 0) {
- ALOGV("find_fde: pc %x is out of FDE bounds: %x", pc, start);
- return 0;
- }
- c += high * 8;
- if (!read_dwarf(memory, eh_frame_hdr, &start, eh_hdr_info.fde_table_enc, &c)) return 0;
- if (!read_dwarf(memory, eh_frame_hdr, &fde, eh_hdr_info.fde_table_enc, &c)) return 0;
- ALOGV("pc 0x%x, ENTRY %d: start=0x%x, fde=0x%x", pc, high, start, fde);
- return fde;
-}
-
-/* Execute single dwarf instruction and update dwarf state accordingly. */
-static bool execute_dwarf(const memory_t* memory, uintptr_t ptr, cie_info_t* cie_info,
- dwarf_state_t* dstate, uint32_t* cursor,
- dwarf_state_t* stack, uint8_t* stack_ptr) {
- uint8_t inst;
- uint8_t op = 0;
-
- if (!try_get_byte(memory, ptr, &inst, cursor)) {
- return false;
- }
- ALOGV("DW_CFA inst: 0x%x", inst);
-
- /* For some instructions upper 2 bits is opcode and lower 6 bits is operand. See dwarf-2.0 7.23. */
- if (inst & 0xc0) {
- op = inst & 0x3f;
- inst &= 0xc0;
- }
-
- switch ((dwarf_CFA)inst) {
- uint32_t reg = 0;
- uint32_t offset = 0;
- case DW_CFA_advance_loc:
- dstate->loc += op * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc: %d to 0x%x", op, dstate->loc);
- break;
- case DW_CFA_offset:
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- dstate->regs[op].rule = 'o';
- dstate->regs[op].value = offset * cie_info->data_align;
- ALOGV("DW_CFA_offset: r%d = o(%d)", op, dstate->regs[op].value);
- break;
- case DW_CFA_restore:
- dstate->regs[op].rule = stack->regs[op].rule;
- dstate->regs[op].value = stack->regs[op].value;
- ALOGV("DW_CFA_restore: r%d = %c(%d)", op, dstate->regs[op].rule, dstate->regs[op].value);
- break;
- case DW_CFA_nop:
- break;
- case DW_CFA_set_loc: // probably we don't have it on mips.
- if (!try_get_xbytes(memory, ptr, &offset, 4, cursor)) return false;
- if (offset < dstate->loc) {
- ALOGE("DW_CFA_set_loc: attempt to move location backward");
- return false;
- }
- dstate->loc = offset * cie_info->code_align;
- ALOGV("DW_CFA_set_loc: %d to 0x%x", offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc1:
- if (!try_get_byte(memory, ptr, (uint8_t*)&offset, cursor)) return false;
- dstate->loc += (uint8_t)offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc1: %d to 0x%x", (uint8_t)offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc2:
- if (!try_get_xbytes(memory, ptr, &offset, 2, cursor)) return false;
- dstate->loc += (uint16_t)offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc2: %d to 0x%x", (uint16_t)offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc4:
- if (!try_get_xbytes(memory, ptr, &offset, 4, cursor)) return false;
- dstate->loc += offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc4: %d to 0x%x", offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_offset_extended: // probably we don't have it on mips.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_offset_extended: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'o';
- dstate->regs[reg].value = offset * cie_info->data_align;
- ALOGV("DW_CFA_offset_extended: r%d = o(%d)", reg, dstate->regs[reg].value);
- break;
- case DW_CFA_restore_extended: // probably we don't have it on mips.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_restore_extended: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = stack->regs[reg].rule;
- dstate->regs[reg].value = stack->regs[reg].value;
- ALOGV("DW_CFA_restore: r%d = %c(%d)", reg, dstate->regs[reg].rule, dstate->regs[reg].value);
- break;
- case DW_CFA_undefined: // probably we don't have it on mips.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_undefined: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'u';
- dstate->regs[reg].value = 0;
- ALOGV("DW_CFA_undefined: r%d", reg);
- break;
- case DW_CFA_same_value: // probably we don't have it on mips.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_undefined: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 's';
- dstate->regs[reg].value = 0;
- ALOGV("DW_CFA_same_value: r%d", reg);
- break;
- case DW_CFA_register: // probably we don't have it on mips.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- /* that's new register actually, not offset */
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- if (reg >= DWARF_REGISTERS || offset >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_register: r%d or r%d exceeds supported number of registers (%d)", reg, offset, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'r';
- dstate->regs[reg].value = offset;
- ALOGV("DW_CFA_register: r%d = r(%d)", reg, dstate->regs[reg].value);
- break;
- case DW_CFA_remember_state:
- if (*stack_ptr == DWARF_STATES_STACK) {
- ALOGE("DW_CFA_remember_state: states stack overflow %d", *stack_ptr);
- return false;
- }
- stack[(*stack_ptr)++] = *dstate;
- ALOGV("DW_CFA_remember_state: stacktop moves to %d", *stack_ptr);
- break;
- case DW_CFA_restore_state:
- /* We have CIE state saved at 0 position. It's not supposed to be taken
- by DW_CFA_restore_state. */
- if (*stack_ptr == 1) {
- ALOGE("DW_CFA_restore_state: states stack is empty");
- return false;
- }
- /* Don't touch location on restore. */
- uintptr_t saveloc = dstate->loc;
- *dstate = stack[--*stack_ptr];
- dstate->loc = saveloc;
- ALOGV("DW_CFA_restore_state: stacktop moves to %d", *stack_ptr);
- break;
- case DW_CFA_def_cfa:
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- dstate->cfa_reg = reg;
- dstate->cfa_off = offset;
- ALOGV("DW_CFA_def_cfa: %x(r%d)", offset, reg);
- break;
- case DW_CFA_def_cfa_register:
- if (!try_get_uleb128(memory, ptr, ®, cursor)) {
- return false;
- }
- dstate->cfa_reg = reg;
- ALOGV("DW_CFA_def_cfa_register: r%d", reg);
- break;
- case DW_CFA_def_cfa_offset:
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) {
- return false;
- }
- dstate->cfa_off = offset;
- ALOGV("DW_CFA_def_cfa_offset: %x", offset);
- break;
- default:
- ALOGE("unrecognized DW_CFA_* instruction: 0x%x", inst);
- return false;
- }
- return true;
-}
-
-/* Restoring particular register value based on dwarf state. */
-static bool get_old_register_value(const memory_t* memory, uint32_t cfa,
- dwarf_state_t* dstate, uint8_t reg,
- unwind_state_t* state, unwind_state_t* newstate) {
- uint32_t addr;
- switch (dstate->regs[reg].rule) {
- case 0:
- /* We don't have dstate updated for this register, so assuming value kept the same.
- Normally we should look into state and return current value as the old one
- but we don't have all registers in state to handle this properly */
- ALOGV("get_old_register_value: value of r%d is the same", reg);
- // for SP if it's not updated by dwarf rule we assume it's equal to CFA
- // for PC if it's not updated by dwarf rule we assume it's equal to RA
- if (reg == DWARF_SP) {
- ALOGV("get_old_register_value: adjusting sp to CFA: 0x%x", cfa);
- newstate->reg[reg] = cfa;
- } else if (reg == DWARF_PC) {
- ALOGV("get_old_register_value: adjusting PC to RA: 0x%x", newstate->reg[DWARF_RA]);
- newstate->reg[reg] = newstate->reg[DWARF_RA];
- } else {
- newstate->reg[reg] = state->reg[reg];
- }
- break;
- case 'o':
- addr = cfa + (int32_t)dstate->regs[reg].value;
- if (!try_get_word(memory, addr, &newstate->reg[reg])) {
- ALOGE("get_old_register_value: can't read from 0x%x", addr);
- return false;
- }
- ALOGV("get_old_register_value: r%d at 0x%x is 0x%x", reg, addr, newstate->reg[reg]);
- break;
- case 'r':
- /* We don't have all registers in state so don't even try to look at 'r' */
- ALOGE("get_old_register_value: register lookup not implemented yet");
- break;
- default:
- ALOGE("get_old_register_value: unexpected rule:%c value:%d for register %d",
- dstate->regs[reg].rule, (int32_t)dstate->regs[reg].value, reg);
- return false;
- }
- return true;
-}
-
-/* Updaing state based on dwarf state. */
-static bool update_state(const memory_t* memory, unwind_state_t* state,
- dwarf_state_t* dstate) {
- unwind_state_t newstate;
- /* We can restore more registers here if we need them. Meanwile doing minimal work here. */
- /* Getting CFA. */
- uintptr_t cfa = 0;
- if (dstate->cfa_reg == DWARF_SP) {
- cfa = state->reg[DWARF_SP] + dstate->cfa_off;
- } else if (dstate->cfa_reg == DWARF_FP) {
- cfa = state->reg[DWARF_FP] + dstate->cfa_off;
- } else {
- ALOGE("update_state: unexpected CFA register: %d", dstate->cfa_reg);
- return false;
- }
- ALOGV("update_state: new CFA: 0x%x", cfa);
-
- /* Update registers. Order is important to allow RA to propagate to PC */
- /* Getting FP. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_FP, state, &newstate)) return false;
- /* Getting SP. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_SP, state, &newstate)) return false;
- /* Getting RA. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_RA, state, &newstate)) return false;
- /* Getting PC. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_PC, state, &newstate)) return false;
-
- ALOGV("update_state: PC: 0x%x; restore PC: 0x%x", state->reg[DWARF_PC], newstate.reg[DWARF_PC]);
- ALOGV("update_state: RA: 0x%x; restore RA: 0x%x", state->reg[DWARF_RA], newstate.reg[DWARF_RA]);
- ALOGV("update_state: FP: 0x%x; restore FP: 0x%x", state->reg[DWARF_FP], newstate.reg[DWARF_FP]);
- ALOGV("update_state: SP: 0x%x; restore SP: 0x%x", state->reg[DWARF_SP], newstate.reg[DWARF_SP]);
-
- if (newstate.reg[DWARF_PC] == 0)
- return false;
-
- /* End backtrace if registers do not change */
- if ((state->reg[DWARF_PC] == newstate.reg[DWARF_PC]) &&
- (state->reg[DWARF_RA] == newstate.reg[DWARF_RA]) &&
- (state->reg[DWARF_FP] == newstate.reg[DWARF_FP]) &&
- (state->reg[DWARF_SP] == newstate.reg[DWARF_SP]))
- return false;
-
- *state = newstate;
- return true;
-}
-
-/* Execute CIE and FDE instructions for FDE found with find_fde. */
-static bool execute_fde(const memory_t* memory,
- uintptr_t fde,
- unwind_state_t* state) {
- uint32_t fde_length = 0;
- uint32_t cie_length = 0;
- uintptr_t cie = 0;
- uintptr_t cie_offset = 0;
- cie_info_t cie_i;
- cie_info_t* cie_info = &cie_i;
- fde_info_t fde_i;
- fde_info_t* fde_info = &fde_i;
- dwarf_state_t dwarf_state;
- dwarf_state_t* dstate = &dwarf_state;
- dwarf_state_t stack[DWARF_STATES_STACK];
- uint8_t stack_ptr = 0;
-
- memset(dstate, 0, sizeof(dwarf_state_t));
- memset(cie_info, 0, sizeof(cie_info_t));
- memset(fde_info, 0, sizeof(fde_info_t));
-
- /* Read common CIE or FDE area:
- 1st word is length;
- 2nd word is ID: 0 for CIE, CIE pointer for FDE.
- */
- if (!try_get_word(memory, fde, &fde_length)) {
- return false;
- }
- if ((int32_t)fde_length == -1) {
- ALOGV("execute_fde: 64-bit dwarf detected, not implemented yet");
- return false;
- }
- if (!try_get_word(memory, fde + 4, &cie_offset)) {
- return false;
- }
- if (cie_offset == 0) {
- /* This is CIE. We shouldn't be here normally. */
- cie = fde;
- cie_length = fde_length;
- } else {
- /* Find CIE. */
- /* Positive cie_offset goes backward from current field. */
- cie = fde + 4 - cie_offset;
- if (!try_get_word(memory, cie, &cie_length)) {
- return false;
- }
- if ((int32_t)cie_length == -1) {
- ALOGV("execute_fde: 64-bit dwarf detected, not implemented yet");
- return false;
- }
- if (!try_get_word(memory, cie + 4, &cie_offset)) {
- return false;
- }
- if (cie_offset != 0) {
- ALOGV("execute_fde: can't find CIE");
- return false;
- }
- }
- ALOGV("execute_fde: FDE length: %d", fde_length);
- ALOGV("execute_fde: CIE pointer: %x", cie);
- ALOGV("execute_fde: CIE length: %d", cie_length);
-
- /* Read CIE:
- Augmentation independent:
- 1st byte is version;
- next x bytes is /0 terminated augmentation string;
- next x bytes is unsigned LEB128 encoded code alignment factor;
- next x bytes is signed LEB128 encoded data alignment factor;
- next 1 (CIE version 1) or x (CIE version 3 unsigned LEB128) bytes is return register column;
- Augmentation dependent:
- if 'z' next x bytes is unsigned LEB128 encoded augmentation data size;
- if 'L' next 1 byte is LSDA encoding;
- if 'R' next 1 byte is FDE encoding;
- if 'S' CIE represents signal handler stack frame;
- if 'P' next 1 byte is personality encoding folowed by personality function pointer;
- Next x bytes is CIE program.
- */
-
- uint32_t c = 8;
- if (!try_get_byte(memory, cie, &cie_info->version, &c)) {
- return false;
- }
- ALOGV("execute_fde: CIE version: %d", cie_info->version);
- uint8_t ch;
- do {
- if (!try_get_byte(memory, cie, &ch, &c)) {
- return false;
- }
- switch (ch) {
- case '\0': break;
- case 'z': cie_info->aug_z = 1; break;
- case 'L': cie_info->aug_L = 1; break;
- case 'R': cie_info->aug_R = 1; break;
- case 'S': cie_info->aug_S = 1; break;
- case 'P': cie_info->aug_P = 1; break;
- default:
- ALOGV("execute_fde: Unrecognized CIE augmentation char: '%c'", ch);
- return false;
- break;
- }
- } while (ch);
- if (!try_get_uleb128(memory, cie, &cie_info->code_align, &c)) {
- return false;
- }
- if (!try_get_sleb128(memory, cie, &cie_info->data_align, &c)) {
- return false;
- }
- if (cie_info->version >= 3) {
- if (!try_get_uleb128(memory, cie, &cie_info->reg, &c)) {
- return false;
- }
- } else {
- if (!try_get_byte(memory, cie, (uint8_t*)&cie_info->reg, &c)) {
- return false;
- }
- }
- ALOGV("execute_fde: CIE code alignment factor: %d", cie_info->code_align);
- ALOGV("execute_fde: CIE data alignment factor: %d", cie_info->data_align);
- if (cie_info->aug_z) {
- if (!try_get_uleb128(memory, cie, &cie_info->aug_z, &c)) {
- return false;
- }
- }
- if (cie_info->aug_L) {
- if (!try_get_byte(memory, cie, &cie_info->aug_L, &c)) {
- return false;
- }
- } else {
- /* Default encoding. */
- cie_info->aug_L = DW_EH_PE_absptr;
- }
- if (cie_info->aug_R) {
- if (!try_get_byte(memory, cie, &cie_info->aug_R, &c)) {
- return false;
- }
- } else {
- /* Default encoding. */
- cie_info->aug_R = DW_EH_PE_absptr;
- }
- if (cie_info->aug_P) {
- /* Get encoding of personality routine pointer. We don't use it now. */
- if (!try_get_byte(memory, cie, (uint8_t*)&cie_info->aug_P, &c)) {
- return false;
- }
- /* Get routine pointer. */
- if (!read_dwarf(memory, cie, &cie_info->aug_P, (uint8_t)cie_info->aug_P, &c)) {
- return false;
- }
- }
- /* CIE program. */
- /* Length field itself (4 bytes) is not included into length. */
- stack[0] = *dstate;
- stack_ptr = 1;
- while (c < cie_length + 4) {
- if (!execute_dwarf(memory, cie, cie_info, dstate, &c, stack, &stack_ptr)) {
- return false;
- }
- }
-
- /* We went directly to CIE. Normally it shouldn't occur. */
- if (cie == fde) return true;
-
- /* Go back to FDE. */
- c = 8;
- /* Read FDE:
- Augmentation independent:
- next x bytes (encoded as specified in CIE) is FDE starting address;
- next x bytes (encoded as specified in CIE) is FDE number of instructions covered;
- Augmentation dependent:
- if 'z' next x bytes is unsigned LEB128 encoded augmentation data size;
- if 'L' next x bytes is LSDA pointer (encoded as specified in CIE);
- Next x bytes is FDE program.
- */
- if (!read_dwarf(memory, fde, &fde_info->start, (uint8_t)cie_info->aug_R, &c)) {
- return false;
- }
- dstate->loc = fde_info->start;
- ALOGV("execute_fde: FDE start: %x", dstate->loc);
- if (!read_dwarf(memory, fde, &fde_info->length, 0, &c)) {
- return false;
- }
- ALOGV("execute_fde: FDE length: %x", fde_info->length);
- if (cie_info->aug_z) {
- if (!try_get_uleb128(memory, fde, &fde_info->aug_z, &c)) {
- return false;
- }
- }
- if (cie_info->aug_L && cie_info->aug_L != DW_EH_PE_omit) {
- if (!read_dwarf(memory, fde, &fde_info->aug_L, cie_info->aug_L, &c)) {
- return false;
- }
- }
- /* FDE program. */
- /* Length field itself (4 bytes) is not included into length. */
- /* Save CIE state as 0 element of stack. Used by DW_CFA_restore. */
- stack[0] = *dstate;
- stack_ptr = 1;
- while (c < fde_length + 4 && state->reg[DWARF_PC] >= dstate->loc) {
- if (!execute_dwarf(memory, fde, cie_info, dstate, &c, stack, &stack_ptr)) {
- return false;
- }
- ALOGV("PC: %x, LOC: %x", state->reg[DWARF_PC], dstate->loc);
- }
-
- return update_state(memory, state, dstate);
-}
-
-static bool heuristic_state_update(const memory_t* memory, unwind_state_t* state)
-{
- bool found_start = false;
- int maxcheck = 1024;
- int32_t stack_size = 0;
- int32_t ra_offset = 0;
- dwarf_state_t dwarf_state;
- dwarf_state_t* dstate = &dwarf_state;
-
- static struct {
- uint32_t insn;
- uint32_t mask;
- } frame0sig[] = {
- {0x3c1c0000, 0xffff0000}, /* lui gp,xxxx */
- {0x279c0000, 0xffff0000}, /* addiu gp,gp,xxxx */
- {0x039fe021, 0xffffffff}, /* addu gp,gp,ra */
- };
- const int nframe0sig = sizeof(frame0sig)/sizeof(frame0sig[0]);
- int f0 = nframe0sig;
- memset(dstate, 0, sizeof(dwarf_state_t));
-
- /* Search code backwards looking for function prologue */
- for (uint32_t pc = state->reg[DWARF_PC]-4; maxcheck-- > 0 && !found_start; pc -= 4) {
- uint32_t op;
- int32_t immediate;
-
- if (!try_get_word(memory, pc, &op))
- return false;
-
- // ALOGV("@0x%08x: 0x%08x\n", pc, op);
-
- // Check for frame 0 signature
- if ((op & frame0sig[f0].mask) == frame0sig[f0].insn) {
- if (f0 == 0)
- return false;
- f0--;
- }
- else {
- f0 = nframe0sig;
- }
-
- switch (op & 0xffff0000) {
- case 0x27bd0000: // addiu sp, imm
- // looking for stack being decremented
- immediate = (((int32_t)op) << 16) >> 16;
- if (immediate < 0) {
- stack_size = -immediate;
- ALOGV("@0x%08x: found stack adjustment=%d\n", pc, stack_size);
- }
- break;
- case 0x039f0000: // e021
-
- case 0xafbf0000: // sw ra, imm(sp)
- ra_offset = (((int32_t)op) << 16) >> 16;
- ALOGV("@0x%08x: found ra offset=%d\n", pc, ra_offset);
- break;
- case 0x3c1c0000: // lui gp
- ALOGV("@0x%08x: found function boundary", pc);
- found_start = true;
- break;
- default:
- break;
- }
- }
-
- dstate->cfa_reg = DWARF_SP;
- dstate->cfa_off = stack_size;
-
- if (ra_offset) {
- dstate->regs[DWARF_RA].rule = 'o';
- dstate->regs[DWARF_RA].value = -stack_size + ra_offset;
- }
-
- return update_state(memory, state, dstate);
-}
-
-static ssize_t unwind_backtrace_common(const memory_t* memory,
- const map_info_t* map_info_list,
- unwind_state_t* state, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth) {
-
- size_t ignored_frames = 0;
- size_t returned_frames = 0;
-
- ALOGV("Unwinding tid: %d", memory->tid);
- ALOGV("PC: %x", state->reg[DWARF_PC]);
- ALOGV("RA: %x", state->reg[DWARF_RA]);
- ALOGV("FP: %x", state->reg[DWARF_FP]);
- ALOGV("SP: %x", state->reg[DWARF_SP]);
-
- for (size_t index = 0; returned_frames < max_depth; index++) {
- uintptr_t fde = find_fde(memory, map_info_list, state->reg[DWARF_PC]);
- backtrace_frame_t* frame = add_backtrace_entry(
- index ? rewind_pc_arch(memory, state->reg[DWARF_PC]) : state->reg[DWARF_PC],
- backtrace, ignore_depth, max_depth,
- &ignored_frames, &returned_frames);
- uint32_t stack_top = state->reg[DWARF_SP];
-
- if (fde) {
- /* Use FDE to update state */
- if (!execute_fde(memory, fde, state))
- break;
- }
- else {
- /* FDE is not found, update state heuristically */
- if (!heuristic_state_update(memory, state))
- break;
- }
-
- if (frame) {
- frame->stack_top = stack_top;
- if (stack_top < state->reg[DWARF_SP]) {
- frame->stack_size = state->reg[DWARF_SP] - stack_top;
- }
- }
- ALOGV("Stack: 0x%x ... 0x%x - %d bytes", frame->stack_top, state->reg[DWARF_SP], frame->stack_size);
- }
- return returned_frames;
-}
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo __attribute__((unused)), void* sigcontext,
- const map_info_t* map_info_list,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
- const ucontext_t* uc = (const ucontext_t*)sigcontext;
-
- unwind_state_t state;
- state.reg[DWARF_PC] = uc->uc_mcontext.pc;
- state.reg[DWARF_RA] = uc->uc_mcontext.gregs[REG_RA];
- state.reg[DWARF_FP] = uc->uc_mcontext.gregs[REG_S8];
- state.reg[DWARF_SP] = uc->uc_mcontext.gregs[REG_SP];
-
- ALOGV("unwind_backtrace_signal_arch: "
- "ignore_depth=%d max_depth=%d pc=0x%08x sp=0x%08x ra=0x%08x\n",
- ignore_depth, max_depth, state.reg[DWARF_PC], state.reg[DWARF_SP], state.reg[DWARF_RA]);
-
- memory_t memory;
- init_memory(&memory, map_info_list);
- return unwind_backtrace_common(&memory, map_info_list,
- &state, backtrace, ignore_depth, max_depth);
-}
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-
- user_regs_struct regs;
- if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) {
- return -1;
- }
-
- unwind_state_t state;
- state.reg[DWARF_PC] = regs.epc;
- state.reg[DWARF_RA] = regs.regs[REG_RA];
- state.reg[DWARF_FP] = regs.regs[REG_S8];
- state.reg[DWARF_SP] = regs.regs[REG_SP];
-
- ALOGV("unwind_backtrace_ptrace_arch: "
- "ignore_depth=%d max_depth=%d pc=0x%08x sp=0x%08x ra=0x%08x\n",
- ignore_depth, max_depth, state.reg[DWARF_PC], state.reg[DWARF_SP], state.reg[DWARF_RA]);
-
- memory_t memory;
- init_memory_ptrace(&memory, tid);
- return unwind_backtrace_common(&memory, context->map_info_list,
- &state, backtrace, ignore_depth, max_depth);
-}
diff --git a/libcorkscrew/arch-mips/dwarf.h b/libcorkscrew/arch-mips/dwarf.h
deleted file mode 100644
index 8504ea0..0000000
--- a/libcorkscrew/arch-mips/dwarf.h
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-/*
- * Dwarf2 data encoding flags.
- */
-
-#define DW_EH_PE_absptr 0x00
-#define DW_EH_PE_omit 0xff
-#define DW_EH_PE_uleb128 0x01
-#define DW_EH_PE_udata2 0x02
-#define DW_EH_PE_udata4 0x03
-#define DW_EH_PE_udata8 0x04
-#define DW_EH_PE_sleb128 0x09
-#define DW_EH_PE_sdata2 0x0A
-#define DW_EH_PE_sdata4 0x0B
-#define DW_EH_PE_sdata8 0x0C
-#define DW_EH_PE_signed 0x08
-#define DW_EH_PE_pcrel 0x10
-#define DW_EH_PE_textrel 0x20
-#define DW_EH_PE_datarel 0x30
-#define DW_EH_PE_funcrel 0x40
-#define DW_EH_PE_aligned 0x50
-#define DW_EH_PE_indirect 0x80
-
-/*
- * Dwarf2 call frame instructions.
- */
-
-typedef enum {
- DW_CFA_advance_loc = 0x40,
- DW_CFA_offset = 0x80,
- DW_CFA_restore = 0xc0,
- DW_CFA_nop = 0x00,
- DW_CFA_set_loc = 0x01,
- DW_CFA_advance_loc1 = 0x02,
- DW_CFA_advance_loc2 = 0x03,
- DW_CFA_advance_loc4 = 0x04,
- DW_CFA_offset_extended = 0x05,
- DW_CFA_restore_extended = 0x06,
- DW_CFA_undefined = 0x07,
- DW_CFA_same_value = 0x08,
- DW_CFA_register = 0x09,
- DW_CFA_remember_state = 0x0a,
- DW_CFA_restore_state = 0x0b,
- DW_CFA_def_cfa = 0x0c,
- DW_CFA_def_cfa_register = 0x0d,
- DW_CFA_def_cfa_offset = 0x0e
-} dwarf_CFA;
-
-/*
- * eh_frame_hdr information.
-*/
-
-typedef struct {
- uint8_t version;
- uint8_t eh_frame_ptr_enc;
- uint8_t fde_count_enc;
- uint8_t fde_table_enc;
- uintptr_t eh_frame_ptr;
- uint32_t fde_count;
-} eh_frame_hdr_info_t;
-
-/*
- * CIE information.
-*/
-
-typedef struct {
- uint8_t version;
- uint32_t code_align;
- uint32_t data_align;
- uint32_t reg;
- uint32_t aug_z;
- uint8_t aug_L;
- uint8_t aug_R;
- uint8_t aug_S;
- uint32_t aug_P;
-} cie_info_t;
-
-/*
- * FDE information.
-*/
-
-typedef struct {
- uint32_t start;
- uint32_t length; // number of instructions covered by FDE
- uint32_t aug_z;
- uint32_t aug_L;
-} fde_info_t;
-
-/*
- * Dwarf state.
-*/
-
-/* Stack of states: required for DW_CFA_remember_state/DW_CFA_restore_state
- 30 should be enough */
-#define DWARF_STATES_STACK 30
-
-typedef struct {
- char rule; // rule: o - offset(value); r - register(value)
- uint32_t value; // value
-} reg_rule_t;
-
-/* Dwarf preserved number of registers for mips */
-typedef enum
- {
- UNW_MIPS_R0,
- UNW_MIPS_R1,
- UNW_MIPS_R2,
- UNW_MIPS_R3,
- UNW_MIPS_R4,
- UNW_MIPS_R5,
- UNW_MIPS_R6,
- UNW_MIPS_R7,
- UNW_MIPS_R8,
- UNW_MIPS_R9,
- UNW_MIPS_R10,
- UNW_MIPS_R11,
- UNW_MIPS_R12,
- UNW_MIPS_R13,
- UNW_MIPS_R14,
- UNW_MIPS_R15,
- UNW_MIPS_R16,
- UNW_MIPS_R17,
- UNW_MIPS_R18,
- UNW_MIPS_R19,
- UNW_MIPS_R20,
- UNW_MIPS_R21,
- UNW_MIPS_R22,
- UNW_MIPS_R23,
- UNW_MIPS_R24,
- UNW_MIPS_R25,
- UNW_MIPS_R26,
- UNW_MIPS_R27,
- UNW_MIPS_R28,
- UNW_MIPS_R29,
- UNW_MIPS_R30,
- UNW_MIPS_R31,
-
- UNW_MIPS_PC = 34,
-
- /* FIXME: Other registers! */
-
- /* For MIPS, the CFA is the value of SP (r29) at the call site in the
- previous frame. */
- UNW_MIPS_CFA,
-
- UNW_TDEP_LASTREG,
-
- UNW_TDEP_LAST_REG = UNW_MIPS_R31,
-
- UNW_TDEP_IP = UNW_MIPS_R31,
- UNW_TDEP_SP = UNW_MIPS_R29,
- UNW_TDEP_EH = UNW_MIPS_R0 /* FIXME. */
-
- }
-mips_regnum_t;
-
-#define DWARF_REGISTERS UNW_TDEP_LASTREG
-
-typedef struct {
- uintptr_t loc; // location (ip)
- uint8_t cfa_reg; // index of register where CFA location stored
- intptr_t cfa_off; // offset
- reg_rule_t regs[DWARF_REGISTERS]; // dwarf preserved registers for mips
-} dwarf_state_t;
-
-/* DWARF registers we are caring about. */
-
-
-#define DWARF_SP UNW_MIPS_R29
-#define DWARF_RA UNW_MIPS_R31
-#define DWARF_PC UNW_MIPS_PC
-#define DWARF_FP UNW_MIPS_CFA /* FIXME is this correct? */
diff --git a/libcorkscrew/arch-mips/ptrace-mips.c b/libcorkscrew/arch-mips/ptrace-mips.c
deleted file mode 100644
index ba3b60a..0000000
--- a/libcorkscrew/arch-mips/ptrace-mips.c
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../ptrace-arch.h"
-
-#include <stddef.h>
-#include <elf.h>
-#include <cutils/log.h>
-
-static void load_eh_frame_hdr(pid_t pid, map_info_t* mi, uintptr_t *eh_frame_hdr) {
- uint32_t elf_phoff;
- uint32_t elf_phentsize_ehsize;
- uint32_t elf_shentsize_phnum;
-
-
- try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phoff), &elf_phoff);
- ALOGV("reading 0x%08x elf_phoff:%x", mi->start + offsetof(Elf32_Ehdr, e_phoff), elf_phoff);
- try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_ehsize), &elf_phentsize_ehsize);
- ALOGV("reading 0x%08x elf_phentsize_ehsize:%x", mi->start + offsetof(Elf32_Ehdr, e_ehsize), elf_phentsize_ehsize);
- try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phnum), &elf_shentsize_phnum);
- ALOGV("reading 0x%08x elf_shentsize_phnum:%x", mi->start + offsetof(Elf32_Ehdr, e_phnum), elf_shentsize_phnum);
-
-
-
- if (try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phoff), &elf_phoff)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_ehsize),
- &elf_phentsize_ehsize)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phnum),
- &elf_shentsize_phnum)) {
- uint32_t elf_phentsize = elf_phentsize_ehsize >> 16;
- uint32_t elf_phnum = elf_shentsize_phnum & 0xffff;
- for (uint32_t i = 0; i < elf_phnum; i++) {
- uintptr_t elf_phdr = mi->start + elf_phoff + i * elf_phentsize;
- uint32_t elf_phdr_type;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_type), &elf_phdr_type)) {
- break;
- }
- if (elf_phdr_type == PT_GNU_EH_FRAME) {
- uint32_t elf_phdr_offset;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_offset),
- &elf_phdr_offset)) {
- break;
- }
- *eh_frame_hdr = mi->start + elf_phdr_offset;
- ALOGV("Parsed .eh_frame_hdr info for %s: start=0x%08x", mi->name, *eh_frame_hdr);
- return;
- }
- }
- }
- *eh_frame_hdr = 0;
-}
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) {
- ALOGV("load_ptrace_map_info_data_arch");
- load_eh_frame_hdr(pid, mi, &data->eh_frame_hdr);
-}
-
-void free_ptrace_map_info_data_arch(map_info_t* mi __attribute__((unused)),
- map_info_data_t* data __attribute__((unused))) {
- ALOGV("free_ptrace_map_info_data_arch");
-}
diff --git a/libcorkscrew/arch-x86/backtrace-x86.c b/libcorkscrew/arch-x86/backtrace-x86.c
deleted file mode 100755
index df486de..0000000
--- a/libcorkscrew/arch-x86/backtrace-x86.c
+++ /dev/null
@@ -1,823 +0,0 @@
-/*
- * 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.
- */
-
-/*
- * Backtracing functions for x86.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../backtrace-arch.h"
-#include "../backtrace-helper.h"
-#include "../ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-#include "dwarf.h"
-
-#include <stdlib.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/ptrace.h>
-#include <cutils/log.h>
-
-#if defined(__APPLE__)
-
-#define _XOPEN_SOURCE
-#include <ucontext.h>
-
-#else
-
-// glibc has its own renaming of the Linux kernel's structures.
-#define __USE_GNU // For REG_EBP, REG_ESP, and REG_EIP.
-#include <ucontext.h>
-
-#endif
-
-/* Unwind state. */
-typedef struct {
- uint32_t reg[DWARF_REGISTERS];
-} unwind_state_t;
-
-typedef struct {
- backtrace_frame_t* backtrace;
- size_t ignore_depth;
- size_t max_depth;
- size_t ignored_frames;
- size_t returned_frames;
- memory_t memory;
-} backtrace_state_t;
-
-uintptr_t rewind_pc_arch(const memory_t* memory __attribute__((unused)), uintptr_t pc) {
- /* TODO: x86 instructions are 1-16 bytes, to define exact size of previous instruction
- we have to disassemble from the function entry point up to pc.
- Returning pc-1 is probably enough for now, the only drawback is that
- it points somewhere between the first byte of instruction we are looking for and
- the first byte of the next instruction. */
-
- return pc-1;
- /* TODO: We should adjust that for the signal frames and return pc for them instead of pc-1.
- To recognize signal frames we should read cie_info property. */
-}
-
-/* Read byte through 4 byte cache. Usually we read byte by byte and updating cursor. */
-static bool try_get_byte(const memory_t* memory, uintptr_t ptr, uint8_t* out_value, uint32_t* cursor) {
- static uintptr_t lastptr;
- static uint32_t buf;
-
- ptr += *cursor;
-
- if (ptr < lastptr || lastptr + 3 < ptr) {
- lastptr = (ptr >> 2) << 2;
- if (!try_get_word(memory, lastptr, &buf)) {
- return false;
- }
- }
- *out_value = (uint8_t)((buf >> ((ptr & 3) * 8)) & 0xff);
- ++*cursor;
- return true;
-}
-
-/* Getting X bytes. 4 is maximum for now. */
-static bool try_get_xbytes(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint8_t bytes, uint32_t* cursor) {
- uint32_t data = 0;
- if (bytes > 4) {
- ALOGE("can't read more than 4 bytes, trying to read %d", bytes);
- return false;
- }
- for (int i = 0; i < bytes; i++) {
- uint8_t buf;
- if (!try_get_byte(memory, ptr, &buf, cursor)) {
- return false;
- }
- data |= (uint32_t)buf << (i * 8);
- }
- *out_value = data;
- return true;
-}
-
-/* Reads signed/unsigned LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_leb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor, bool sign_extend) {
- uint8_t buf = 0;
- uint32_t val = 0;
- uint8_t c = 0;
- do {
- if (!try_get_byte(memory, ptr, &buf, cursor)) {
- return false;
- }
- val |= ((uint32_t)buf & 0x7f) << (c * 7);
- c++;
- } while (buf & 0x80 && (c * 7) <= 32);
- if (c * 7 > 32) {
- ALOGE("%s: data exceeds expected 4 bytes maximum", __FUNCTION__);
- return false;
- }
- if (sign_extend) {
- if (buf & 0x40) {
- val |= ((uint32_t)-1 << (c * 7));
- }
- }
- *out_value = val;
- return true;
-}
-
-/* Reads signed LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_sleb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor) {
- return try_get_leb128(memory, ptr, out_value, cursor, true);
-}
-
-/* Reads unsigned LEB128 encoded data. From 1 to 4 bytes. */
-static bool try_get_uleb128(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint32_t* cursor) {
- return try_get_leb128(memory, ptr, out_value, cursor, false);
-}
-
-/* Getting data encoded by dwarf encodings. */
-static bool read_dwarf(const memory_t* memory, uintptr_t ptr, uint32_t* out_value, uint8_t encoding, uint32_t* cursor) {
- uint32_t data = 0;
- bool issigned = true;
- uintptr_t addr = ptr + *cursor;
- /* Lower 4 bits is data type/size */
- /* TODO: add more encodings if it becomes necessary */
- switch (encoding & 0xf) {
- case DW_EH_PE_absptr:
- if (!try_get_xbytes(memory, ptr, &data, 4, cursor)) {
- return false;
- }
- *out_value = data;
- return true;
- case DW_EH_PE_udata4:
- issigned = false;
- case DW_EH_PE_sdata4:
- if (!try_get_xbytes(memory, ptr, &data, 4, cursor)) {
- return false;
- }
- break;
- default:
- ALOGE("unrecognized dwarf lower part encoding: 0x%x", encoding);
- return false;
- }
- /* Higher 4 bits is modifier */
- /* TODO: add more encodings if it becomes necessary */
- switch (encoding & 0xf0) {
- case 0:
- *out_value = data;
- break;
- case DW_EH_PE_pcrel:
- if (issigned) {
- *out_value = addr + (int32_t)data;
- } else {
- *out_value = addr + data;
- }
- break;
- /* Assuming ptr is correct base to calculate datarel */
- case DW_EH_PE_datarel:
- if (issigned) {
- *out_value = ptr + (int32_t)data;
- } else {
- *out_value = ptr + data;
- }
- break;
- default:
- ALOGE("unrecognized dwarf higher part encoding: 0x%x", encoding);
- return false;
- }
- return true;
-}
-
-/* Having PC find corresponding FDE by reading .eh_frame_hdr section data. */
-static uintptr_t find_fde(const memory_t* memory,
- const map_info_t* map_info_list, uintptr_t pc) {
- if (!pc) {
- ALOGV("find_fde: pc is zero, no eh_frame");
- return 0;
- }
- const map_info_t* mi = find_map_info(map_info_list, pc);
- if (!mi) {
- ALOGV("find_fde: no map info for pc:0x%x", pc);
- return 0;
- }
- const map_info_data_t* midata = mi->data;
- if (!midata) {
- ALOGV("find_fde: no eh_frame_hdr for map: start=0x%x, end=0x%x", mi->start, mi->end);
- return 0;
- }
-
- eh_frame_hdr_info_t eh_hdr_info;
- memset(&eh_hdr_info, 0, sizeof(eh_frame_hdr_info_t));
-
- /* Getting the first word of eh_frame_hdr:
- 1st byte is version;
- 2nd byte is encoding of pointer to eh_frames;
- 3rd byte is encoding of count of FDEs in lookup table;
- 4th byte is encoding of lookup table entries.
- */
- uintptr_t eh_frame_hdr = midata->eh_frame_hdr;
- uint32_t c = 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.version, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.eh_frame_ptr_enc, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.fde_count_enc, &c)) return 0;
- if (!try_get_byte(memory, eh_frame_hdr, &eh_hdr_info.fde_table_enc, &c)) return 0;
-
- /* TODO: 3rd byte can be DW_EH_PE_omit, that means no lookup table available and we should
- try to parse eh_frame instead. Not sure how often it may occur, skipping now.
- */
- if (eh_hdr_info.version != 1) {
- ALOGV("find_fde: eh_frame_hdr version %d is not supported", eh_hdr_info.version);
- return 0;
- }
- /* Getting the data:
- 2nd word is eh_frame pointer (normally not used, because lookup table has all we need);
- 3rd word is count of FDEs in the lookup table;
- starting from 4 word there is FDE lookup table (pairs of PC and FDE pointer) sorted by PC;
- */
- if (!read_dwarf(memory, eh_frame_hdr, &eh_hdr_info.eh_frame_ptr, eh_hdr_info.eh_frame_ptr_enc, &c)) return 0;
- if (!read_dwarf(memory, eh_frame_hdr, &eh_hdr_info.fde_count, eh_hdr_info.fde_count_enc, &c)) return 0;
- ALOGV("find_fde: found %d FDEs", eh_hdr_info.fde_count);
-
- int32_t low = 0;
- int32_t high = eh_hdr_info.fde_count;
- uintptr_t start = 0;
- uintptr_t fde = 0;
- /* eh_frame_hdr + c points to lookup table at this point. */
- while (low <= high) {
- uint32_t mid = (high + low)/2;
- uint32_t entry = c + mid * 8;
- if (!read_dwarf(memory, eh_frame_hdr, &start, eh_hdr_info.fde_table_enc, &entry)) return 0;
- if (pc <= start) {
- high = mid - 1;
- } else {
- low = mid + 1;
- }
- }
- /* Value found is at high. */
- if (high < 0) {
- ALOGV("find_fde: pc %x is out of FDE bounds: %x", pc, start);
- return 0;
- }
- c += high * 8;
- if (!read_dwarf(memory, eh_frame_hdr, &start, eh_hdr_info.fde_table_enc, &c)) return 0;
- if (!read_dwarf(memory, eh_frame_hdr, &fde, eh_hdr_info.fde_table_enc, &c)) return 0;
- ALOGV("pc 0x%x, ENTRY %d: start=0x%x, fde=0x%x", pc, high, start, fde);
- return fde;
-}
-
-/* Execute single dwarf instruction and update dwarf state accordingly. */
-static bool execute_dwarf(const memory_t* memory, uintptr_t ptr, cie_info_t* cie_info,
- dwarf_state_t* dstate, uint32_t* cursor,
- dwarf_state_t* stack, uint8_t* stack_ptr) {
- uint8_t inst;
- uint8_t op = 0;
-
- if (!try_get_byte(memory, ptr, &inst, cursor)) {
- return false;
- }
- ALOGV("DW_CFA inst: 0x%x", inst);
-
- /* For some instructions upper 2 bits is opcode and lower 6 bits is operand. See dwarf-2.0 7.23. */
- if (inst & 0xc0) {
- op = inst & 0x3f;
- inst &= 0xc0;
- }
-
- switch ((dwarf_CFA)inst) {
- uint32_t reg = 0;
- uint32_t offset = 0;
- case DW_CFA_advance_loc:
- dstate->loc += op * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc: %d to 0x%x", op, dstate->loc);
- break;
- case DW_CFA_offset:
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- dstate->regs[op].rule = 'o';
- dstate->regs[op].value = offset * cie_info->data_align;
- ALOGV("DW_CFA_offset: r%d = o(%d)", op, dstate->regs[op].value);
- break;
- case DW_CFA_restore:
- dstate->regs[op].rule = stack->regs[op].rule;
- dstate->regs[op].value = stack->regs[op].value;
- ALOGV("DW_CFA_restore: r%d = %c(%d)", op, dstate->regs[op].rule, dstate->regs[op].value);
- break;
- case DW_CFA_nop:
- break;
- case DW_CFA_set_loc: // probably we don't have it on x86.
- if (!try_get_xbytes(memory, ptr, &offset, 4, cursor)) return false;
- if (offset < dstate->loc) {
- ALOGE("DW_CFA_set_loc: attempt to move location backward");
- return false;
- }
- dstate->loc = offset * cie_info->code_align;
- ALOGV("DW_CFA_set_loc: %d to 0x%x", offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc1:
- if (!try_get_byte(memory, ptr, (uint8_t*)&offset, cursor)) return false;
- dstate->loc += (uint8_t)offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc1: %d to 0x%x", (uint8_t)offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc2:
- if (!try_get_xbytes(memory, ptr, &offset, 2, cursor)) return false;
- dstate->loc += (uint16_t)offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc2: %d to 0x%x", (uint16_t)offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_advance_loc4:
- if (!try_get_xbytes(memory, ptr, &offset, 4, cursor)) return false;
- dstate->loc += offset * cie_info->code_align;
- ALOGV("DW_CFA_advance_loc4: %d to 0x%x", offset * cie_info->code_align, dstate->loc);
- break;
- case DW_CFA_offset_extended: // probably we don't have it on x86.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_offset_extended: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'o';
- dstate->regs[reg].value = offset * cie_info->data_align;
- ALOGV("DW_CFA_offset_extended: r%d = o(%d)", reg, dstate->regs[reg].value);
- break;
- case DW_CFA_restore_extended: // probably we don't have it on x86.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_restore_extended: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = stack->regs[reg].rule;
- dstate->regs[reg].value = stack->regs[reg].value;
- ALOGV("DW_CFA_restore: r%d = %c(%d)", reg, dstate->regs[reg].rule, dstate->regs[reg].value);
- break;
- case DW_CFA_undefined: // probably we don't have it on x86.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_undefined: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'u';
- dstate->regs[reg].value = 0;
- ALOGV("DW_CFA_undefined: r%d", reg);
- break;
- case DW_CFA_same_value: // probably we don't have it on x86.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (reg >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_undefined: r%d exceeds supported number of registers (%d)", reg, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 's';
- dstate->regs[reg].value = 0;
- ALOGV("DW_CFA_same_value: r%d", reg);
- break;
- case DW_CFA_register: // probably we don't have it on x86.
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- /* that's new register actually, not offset */
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- if (reg >= DWARF_REGISTERS || offset >= DWARF_REGISTERS) {
- ALOGE("DW_CFA_register: r%d or r%d exceeds supported number of registers (%d)", reg, offset, DWARF_REGISTERS);
- return false;
- }
- dstate->regs[reg].rule = 'r';
- dstate->regs[reg].value = offset;
- ALOGV("DW_CFA_register: r%d = r(%d)", reg, dstate->regs[reg].value);
- break;
- case DW_CFA_remember_state:
- if (*stack_ptr == DWARF_STATES_STACK) {
- ALOGE("DW_CFA_remember_state: states stack overflow %d", *stack_ptr);
- return false;
- }
- stack[(*stack_ptr)++] = *dstate;
- ALOGV("DW_CFA_remember_state: stacktop moves to %d", *stack_ptr);
- break;
- case DW_CFA_restore_state:
- /* We have CIE state saved at 0 position. It's not supposed to be taken
- by DW_CFA_restore_state. */
- if (*stack_ptr == 1) {
- ALOGE("DW_CFA_restore_state: states stack is empty");
- return false;
- }
- /* Don't touch location on restore. */
- uintptr_t saveloc = dstate->loc;
- *dstate = stack[--*stack_ptr];
- dstate->loc = saveloc;
- ALOGV("DW_CFA_restore_state: stacktop moves to %d", *stack_ptr);
- break;
- case DW_CFA_def_cfa:
- if (!try_get_uleb128(memory, ptr, ®, cursor)) return false;
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) return false;
- dstate->cfa_reg = reg;
- dstate->cfa_off = offset;
- ALOGV("DW_CFA_def_cfa: %x(r%d)", offset, reg);
- break;
- case DW_CFA_def_cfa_register:
- if (!try_get_uleb128(memory, ptr, ®, cursor)) {
- return false;
- }
- dstate->cfa_reg = reg;
- ALOGV("DW_CFA_def_cfa_register: r%d", reg);
- break;
- case DW_CFA_def_cfa_offset:
- if (!try_get_uleb128(memory, ptr, &offset, cursor)) {
- return false;
- }
- dstate->cfa_off = offset;
- ALOGV("DW_CFA_def_cfa_offset: %x", offset);
- break;
- default:
- ALOGE("unrecognized DW_CFA_* instruction: 0x%x", inst);
- return false;
- }
- return true;
-}
-
-/* Restoring particular register value based on dwarf state. */
-static bool get_old_register_value(const memory_t* memory, uint32_t cfa,
- dwarf_state_t* dstate, uint8_t reg,
- unwind_state_t* state, unwind_state_t* newstate) {
- uint32_t addr;
- switch (dstate->regs[reg].rule) {
- case 0:
- /* We don't have dstate updated for this register, so assuming value kept the same.
- Normally we should look into state and return current value as the old one
- but we don't have all registers in state to handle this properly */
- ALOGV("get_old_register_value: value of r%d is the same", reg);
- // for ESP if it's not updated by dwarf rule we assume it's equal to CFA
- if (reg == DWARF_ESP) {
- ALOGV("get_old_register_value: adjusting esp to CFA: 0x%x", cfa);
- newstate->reg[reg] = cfa;
- } else {
- newstate->reg[reg] = state->reg[reg];
- }
- break;
- case 'o':
- addr = cfa + (int32_t)dstate->regs[reg].value;
- if (!try_get_word(memory, addr, &newstate->reg[reg])) {
- ALOGE("get_old_register_value: can't read from 0x%x", addr);
- return false;
- }
- ALOGV("get_old_register_value: r%d at 0x%x is 0x%x", reg, addr, newstate->reg[reg]);
- break;
- case 'r':
- /* We don't have all registers in state so don't even try to look at 'r' */
- ALOGE("get_old_register_value: register lookup not implemented yet");
- break;
- default:
- ALOGE("get_old_register_value: unexpected rule:%c value:%d for register %d",
- dstate->regs[reg].rule, (int32_t)dstate->regs[reg].value, reg);
- return false;
- }
- return true;
-}
-
-/* Updaing state based on dwarf state. */
-static bool update_state(const memory_t* memory, unwind_state_t* state,
- dwarf_state_t* dstate) {
- unwind_state_t newstate;
- /* We can restore more registers here if we need them. Meanwile doing minimal work here. */
- /* Getting CFA. */
- uintptr_t cfa = 0;
- if (dstate->cfa_reg == DWARF_ESP) {
- cfa = state->reg[DWARF_ESP] + dstate->cfa_off;
- } else if (dstate->cfa_reg == DWARF_EBP) {
- cfa = state->reg[DWARF_EBP] + dstate->cfa_off;
- } else {
- ALOGE("update_state: unexpected CFA register: %d", dstate->cfa_reg);
- return false;
- }
- ALOGV("update_state: new CFA: 0x%x", cfa);
- /* Getting EIP. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_EIP, state, &newstate)) return false;
- /* Getting EBP. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_EBP, state, &newstate)) return false;
- /* Getting ESP. */
- if (!get_old_register_value(memory, cfa, dstate, DWARF_ESP, state, &newstate)) return false;
-
- ALOGV("update_state: IP: 0x%x; restore IP: 0x%x", state->reg[DWARF_EIP], newstate.reg[DWARF_EIP]);
- ALOGV("update_state: EBP: 0x%x; restore EBP: 0x%x", state->reg[DWARF_EBP], newstate.reg[DWARF_EBP]);
- ALOGV("update_state: ESP: 0x%x; restore ESP: 0x%x", state->reg[DWARF_ESP], newstate.reg[DWARF_ESP]);
- *state = newstate;
- return true;
-}
-
-/* Execute CIE and FDE instructions for FDE found with find_fde. */
-static bool execute_fde(const memory_t* memory,
- uintptr_t fde,
- unwind_state_t* state) {
- uint32_t fde_length = 0;
- uint32_t cie_length = 0;
- uintptr_t cie = 0;
- uintptr_t cie_offset = 0;
- cie_info_t cie_i;
- cie_info_t* cie_info = &cie_i;
- fde_info_t fde_i;
- fde_info_t* fde_info = &fde_i;
- dwarf_state_t dwarf_state;
- dwarf_state_t* dstate = &dwarf_state;
- dwarf_state_t stack[DWARF_STATES_STACK];
- uint8_t stack_ptr = 0;
-
- memset(dstate, 0, sizeof(dwarf_state_t));
- memset(cie_info, 0, sizeof(cie_info_t));
- memset(fde_info, 0, sizeof(fde_info_t));
-
- /* Read common CIE or FDE area:
- 1st word is length;
- 2nd word is ID: 0 for CIE, CIE pointer for FDE.
- */
- if (!try_get_word(memory, fde, &fde_length)) {
- return false;
- }
- if ((int32_t)fde_length == -1) {
- ALOGV("execute_fde: 64-bit dwarf detected, not implemented yet");
- return false;
- }
- if (!try_get_word(memory, fde + 4, &cie_offset)) {
- return false;
- }
- if (cie_offset == 0) {
- /* This is CIE. We shouldn't be here normally. */
- cie = fde;
- cie_length = fde_length;
- } else {
- /* Find CIE. */
- /* Positive cie_offset goes backward from current field. */
- cie = fde + 4 - cie_offset;
- if (!try_get_word(memory, cie, &cie_length)) {
- return false;
- }
- if ((int32_t)cie_length == -1) {
- ALOGV("execute_fde: 64-bit dwarf detected, not implemented yet");
- return false;
- }
- if (!try_get_word(memory, cie + 4, &cie_offset)) {
- return false;
- }
- if (cie_offset != 0) {
- ALOGV("execute_fde: can't find CIE");
- return false;
- }
- }
- ALOGV("execute_fde: FDE length: %d", fde_length);
- ALOGV("execute_fde: CIE pointer: %x", cie);
- ALOGV("execute_fde: CIE length: %d", cie_length);
-
- /* Read CIE:
- Augmentation independent:
- 1st byte is version;
- next x bytes is /0 terminated augmentation string;
- next x bytes is unsigned LEB128 encoded code alignment factor;
- next x bytes is signed LEB128 encoded data alignment factor;
- next 1 (CIE version 1) or x (CIE version 3 unsigned LEB128) bytes is return register column;
- Augmentation dependent:
- if 'z' next x bytes is unsigned LEB128 encoded augmentation data size;
- if 'L' next 1 byte is LSDA encoding;
- if 'R' next 1 byte is FDE encoding;
- if 'S' CIE represents signal handler stack frame;
- if 'P' next 1 byte is personality encoding folowed by personality function pointer;
- Next x bytes is CIE program.
- */
-
- uint32_t c = 8;
- if (!try_get_byte(memory, cie, &cie_info->version, &c)) {
- return false;
- }
- ALOGV("execute_fde: CIE version: %d", cie_info->version);
- uint8_t ch;
- do {
- if (!try_get_byte(memory, cie, &ch, &c)) {
- return false;
- }
- switch (ch) {
- case '\0': break;
- case 'z': cie_info->aug_z = 1; break;
- case 'L': cie_info->aug_L = 1; break;
- case 'R': cie_info->aug_R = 1; break;
- case 'S': cie_info->aug_S = 1; break;
- case 'P': cie_info->aug_P = 1; break;
- default:
- ALOGV("execute_fde: Unrecognized CIE augmentation char: '%c'", ch);
- return false;
- break;
- }
- } while (ch);
- if (!try_get_uleb128(memory, cie, &cie_info->code_align, &c)) {
- return false;
- }
- if (!try_get_sleb128(memory, cie, &cie_info->data_align, &c)) {
- return false;
- }
- if (cie_info->version >= 3) {
- if (!try_get_uleb128(memory, cie, &cie_info->reg, &c)) {
- return false;
- }
- } else {
- if (!try_get_byte(memory, cie, (uint8_t*)&cie_info->reg, &c)) {
- return false;
- }
- }
- ALOGV("execute_fde: CIE code alignment factor: %d", cie_info->code_align);
- ALOGV("execute_fde: CIE data alignment factor: %d", cie_info->data_align);
- if (cie_info->aug_z) {
- if (!try_get_uleb128(memory, cie, &cie_info->aug_z, &c)) {
- return false;
- }
- }
- if (cie_info->aug_L) {
- if (!try_get_byte(memory, cie, &cie_info->aug_L, &c)) {
- return false;
- }
- } else {
- /* Default encoding. */
- cie_info->aug_L = DW_EH_PE_absptr;
- }
- if (cie_info->aug_R) {
- if (!try_get_byte(memory, cie, &cie_info->aug_R, &c)) {
- return false;
- }
- } else {
- /* Default encoding. */
- cie_info->aug_R = DW_EH_PE_absptr;
- }
- if (cie_info->aug_P) {
- /* Get encoding of personality routine pointer. We don't use it now. */
- if (!try_get_byte(memory, cie, (uint8_t*)&cie_info->aug_P, &c)) {
- return false;
- }
- /* Get routine pointer. */
- if (!read_dwarf(memory, cie, &cie_info->aug_P, (uint8_t)cie_info->aug_P, &c)) {
- return false;
- }
- }
- /* CIE program. */
- /* Length field itself (4 bytes) is not included into length. */
- stack[0] = *dstate;
- stack_ptr = 1;
- while (c < cie_length + 4) {
- if (!execute_dwarf(memory, cie, cie_info, dstate, &c, stack, &stack_ptr)) {
- return false;
- }
- }
-
- /* We went directly to CIE. Normally it shouldn't occur. */
- if (cie == fde) return true;
-
- /* Go back to FDE. */
- c = 8;
- /* Read FDE:
- Augmentation independent:
- next x bytes (encoded as specified in CIE) is FDE starting address;
- next x bytes (encoded as specified in CIE) is FDE number of instructions covered;
- Augmentation dependent:
- if 'z' next x bytes is unsigned LEB128 encoded augmentation data size;
- if 'L' next x bytes is LSDA pointer (encoded as specified in CIE);
- Next x bytes is FDE program.
- */
- if (!read_dwarf(memory, fde, &fde_info->start, (uint8_t)cie_info->aug_R, &c)) {
- return false;
- }
- dstate->loc = fde_info->start;
- ALOGV("execute_fde: FDE start: %x", dstate->loc);
- if (!read_dwarf(memory, fde, &fde_info->length, 0, &c)) {
- return false;
- }
- ALOGV("execute_fde: FDE length: %x", fde_info->length);
- if (cie_info->aug_z) {
- if (!try_get_uleb128(memory, fde, &fde_info->aug_z, &c)) {
- return false;
- }
- }
- if (cie_info->aug_L && cie_info->aug_L != DW_EH_PE_omit) {
- if (!read_dwarf(memory, fde, &fde_info->aug_L, cie_info->aug_L, &c)) {
- return false;
- }
- }
- /* FDE program. */
- /* Length field itself (4 bytes) is not included into length. */
- /* Save CIE state as 0 element of stack. Used by DW_CFA_restore. */
- stack[0] = *dstate;
- stack_ptr = 1;
- while (c < fde_length + 4 && state->reg[DWARF_EIP] >= dstate->loc) {
- if (!execute_dwarf(memory, fde, cie_info, dstate, &c, stack, &stack_ptr)) {
- return false;
- }
- ALOGV("IP: %x, LOC: %x", state->reg[DWARF_EIP], dstate->loc);
- }
-
- return update_state(memory, state, dstate);
-}
-
-static ssize_t unwind_backtrace_common(const memory_t* memory,
- const map_info_t* map_info_list,
- unwind_state_t* state, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth) {
-
- size_t ignored_frames = 0;
- size_t returned_frames = 0;
-
- ALOGV("Unwinding tid: %d", memory->tid);
- ALOGV("IP: %x", state->reg[DWARF_EIP]);
- ALOGV("BP: %x", state->reg[DWARF_EBP]);
- ALOGV("SP: %x", state->reg[DWARF_ESP]);
-
- for (size_t index = 0; returned_frames < max_depth; index++) {
- uintptr_t fde = find_fde(memory, map_info_list, state->reg[DWARF_EIP]);
- /* FDE is not found, it may happen if stack is corrupted or calling wrong adress.
- Getting return address from stack.
- */
- if (!fde) {
- uint32_t ip;
- ALOGV("trying to restore registers from stack");
- if (!try_get_word(memory, state->reg[DWARF_EBP] + 4, &ip) ||
- ip == state->reg[DWARF_EIP]) {
- ALOGV("can't get IP from stack");
- break;
- }
- /* We've been able to get IP from stack so recording the frame before continue. */
- backtrace_frame_t* frame = add_backtrace_entry(
- index ? rewind_pc_arch(memory, state->reg[DWARF_EIP]) : state->reg[DWARF_EIP],
- backtrace, ignore_depth, max_depth,
- &ignored_frames, &returned_frames);
- state->reg[DWARF_EIP] = ip;
- state->reg[DWARF_ESP] = state->reg[DWARF_EBP] + 8;
- if (!try_get_word(memory, state->reg[DWARF_EBP], &state->reg[DWARF_EBP])) {
- ALOGV("can't get EBP from stack");
- break;
- }
- ALOGV("restore IP: %x", state->reg[DWARF_EIP]);
- ALOGV("restore BP: %x", state->reg[DWARF_EBP]);
- ALOGV("restore SP: %x", state->reg[DWARF_ESP]);
- continue;
- }
- backtrace_frame_t* frame = add_backtrace_entry(
- index ? rewind_pc_arch(memory, state->reg[DWARF_EIP]) : state->reg[DWARF_EIP],
- backtrace, ignore_depth, max_depth,
- &ignored_frames, &returned_frames);
-
- uint32_t stack_top = state->reg[DWARF_ESP];
-
- if (!execute_fde(memory, fde, state)) break;
-
- if (frame) {
- frame->stack_top = stack_top;
- if (stack_top < state->reg[DWARF_ESP]) {
- frame->stack_size = state->reg[DWARF_ESP] - stack_top;
- }
- }
- ALOGV("Stack: 0x%x ... 0x%x - %d bytes", frame->stack_top, state->reg[DWARF_ESP], frame->stack_size);
- }
- return returned_frames;
-}
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo __attribute__((unused)), void* sigcontext,
- const map_info_t* map_info_list,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
- const ucontext_t* uc = (const ucontext_t*)sigcontext;
-
- unwind_state_t state;
-#if defined(__APPLE__)
- state.reg[DWARF_EBP] = uc->uc_mcontext->__ss.__ebp;
- state.reg[DWARF_ESP] = uc->uc_mcontext->__ss.__esp;
- state.reg[DWARF_EIP] = uc->uc_mcontext->__ss.__eip;
-#else
- state.reg[DWARF_EBP] = uc->uc_mcontext.gregs[REG_EBP];
- state.reg[DWARF_ESP] = uc->uc_mcontext.gregs[REG_ESP];
- state.reg[DWARF_EIP] = uc->uc_mcontext.gregs[REG_EIP];
-#endif
-
- memory_t memory;
- init_memory(&memory, map_info_list);
- return unwind_backtrace_common(&memory, map_info_list,
- &state, backtrace, ignore_depth, max_depth);
-}
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-#if defined(__APPLE__)
- return -1;
-#else
- pt_regs_x86_t regs;
- if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) {
- return -1;
- }
-
- unwind_state_t state;
- state.reg[DWARF_EBP] = regs.ebp;
- state.reg[DWARF_EIP] = regs.eip;
- state.reg[DWARF_ESP] = regs.esp;
-
- memory_t memory;
- init_memory_ptrace(&memory, tid);
- return unwind_backtrace_common(&memory, context->map_info_list,
- &state, backtrace, ignore_depth, max_depth);
-#endif
-}
diff --git a/libcorkscrew/arch-x86/dwarf.h b/libcorkscrew/arch-x86/dwarf.h
deleted file mode 100755
index 962fc55..0000000
--- a/libcorkscrew/arch-x86/dwarf.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-/*
- * Dwarf2 data encoding flags.
- */
-
-#define DW_EH_PE_absptr 0x00
-#define DW_EH_PE_omit 0xff
-#define DW_EH_PE_uleb128 0x01
-#define DW_EH_PE_udata2 0x02
-#define DW_EH_PE_udata4 0x03
-#define DW_EH_PE_udata8 0x04
-#define DW_EH_PE_sleb128 0x09
-#define DW_EH_PE_sdata2 0x0A
-#define DW_EH_PE_sdata4 0x0B
-#define DW_EH_PE_sdata8 0x0C
-#define DW_EH_PE_signed 0x08
-#define DW_EH_PE_pcrel 0x10
-#define DW_EH_PE_textrel 0x20
-#define DW_EH_PE_datarel 0x30
-#define DW_EH_PE_funcrel 0x40
-#define DW_EH_PE_aligned 0x50
-#define DW_EH_PE_indirect 0x80
-
-/*
- * Dwarf2 call frame instructions.
- */
-
-typedef enum {
- DW_CFA_advance_loc = 0x40,
- DW_CFA_offset = 0x80,
- DW_CFA_restore = 0xc0,
- DW_CFA_nop = 0x00,
- DW_CFA_set_loc = 0x01,
- DW_CFA_advance_loc1 = 0x02,
- DW_CFA_advance_loc2 = 0x03,
- DW_CFA_advance_loc4 = 0x04,
- DW_CFA_offset_extended = 0x05,
- DW_CFA_restore_extended = 0x06,
- DW_CFA_undefined = 0x07,
- DW_CFA_same_value = 0x08,
- DW_CFA_register = 0x09,
- DW_CFA_remember_state = 0x0a,
- DW_CFA_restore_state = 0x0b,
- DW_CFA_def_cfa = 0x0c,
- DW_CFA_def_cfa_register = 0x0d,
- DW_CFA_def_cfa_offset = 0x0e
-} dwarf_CFA;
-
-/*
- * eh_frame_hdr information.
-*/
-
-typedef struct {
- uint8_t version;
- uint8_t eh_frame_ptr_enc;
- uint8_t fde_count_enc;
- uint8_t fde_table_enc;
- uintptr_t eh_frame_ptr;
- uint32_t fde_count;
-} eh_frame_hdr_info_t;
-
-/*
- * CIE information.
-*/
-
-typedef struct {
- uint8_t version;
- uint32_t code_align;
- uint32_t data_align;
- uint32_t reg;
- uint32_t aug_z;
- uint8_t aug_L;
- uint8_t aug_R;
- uint8_t aug_S;
- uint32_t aug_P;
-} cie_info_t;
-
-/*
- * FDE information.
-*/
-
-typedef struct {
- uint32_t start;
- uint32_t length; // number of instructions covered by FDE
- uint32_t aug_z;
- uint32_t aug_L;
-} fde_info_t;
-
-/*
- * Dwarf state.
-*/
-
-/* Stack of states: required for DW_CFA_remember_state/DW_CFA_restore_state
- 30 should be enough */
-#define DWARF_STATES_STACK 30
-
-typedef struct {
- char rule; // rule: o - offset(value); r - register(value)
- uint32_t value; // value
-} reg_rule_t;
-
-/* Dwarf preserved number of registers for x86. */
-
-#define DWARF_REGISTERS 17
-
-typedef struct {
- uintptr_t loc; // location (ip)
- uint8_t cfa_reg; // index of register where CFA location stored
- intptr_t cfa_off; // offset
- reg_rule_t regs[DWARF_REGISTERS]; // dwarf preserved registers for x86
-} dwarf_state_t;
-
-/* DWARF registers we are caring about. */
-
-#define DWARF_EAX 0
-#define DWARF_ECX 1
-#define DWARF_EDX 2
-#define DWARF_EBX 3
-#define DWARF_ESP 4
-#define DWARF_EBP 5
-#define DWARF_ESI 6
-#define DWARF_EDI 7
-#define DWARF_EIP 8
-
-
diff --git a/libcorkscrew/arch-x86/ptrace-x86.c b/libcorkscrew/arch-x86/ptrace-x86.c
deleted file mode 100755
index 9c49b93..0000000
--- a/libcorkscrew/arch-x86/ptrace-x86.c
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../ptrace-arch.h"
-
-#include <stddef.h>
-#include <elf.h>
-#include <cutils/log.h>
-
-static void load_eh_frame_hdr(pid_t pid, map_info_t* mi, uintptr_t *eh_frame_hdr) {
- uint32_t elf_phoff;
- uint32_t elf_phentsize_ehsize;
- uint32_t elf_shentsize_phnum;
- if (try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phoff), &elf_phoff)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_ehsize),
- &elf_phentsize_ehsize)
- && try_get_word_ptrace(pid, mi->start + offsetof(Elf32_Ehdr, e_phnum),
- &elf_shentsize_phnum)) {
- uint32_t elf_phentsize = elf_phentsize_ehsize >> 16;
- uint32_t elf_phnum = elf_shentsize_phnum & 0xffff;
- for (uint32_t i = 0; i < elf_phnum; i++) {
- uintptr_t elf_phdr = mi->start + elf_phoff + i * elf_phentsize;
- uint32_t elf_phdr_type;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_type), &elf_phdr_type)) {
- break;
- }
- if (elf_phdr_type == PT_GNU_EH_FRAME) {
- uint32_t elf_phdr_offset;
- if (!try_get_word_ptrace(pid, elf_phdr + offsetof(Elf32_Phdr, p_offset),
- &elf_phdr_offset)) {
- break;
- }
- *eh_frame_hdr = mi->start + elf_phdr_offset;
- ALOGV("Parsed .eh_frame_hdr info for %s: start=0x%08x", mi->name, *eh_frame_hdr);
- return;
- }
- }
- }
- *eh_frame_hdr = 0;
-}
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) {
- load_eh_frame_hdr(pid, mi, &data->eh_frame_hdr);
-}
-
-void free_ptrace_map_info_data_arch(map_info_t* mi __attribute__((unused)),
- map_info_data_t* data __attribute__((unused))) {
-}
diff --git a/libcorkscrew/backtrace-arch.h b/libcorkscrew/backtrace-arch.h
deleted file mode 100644
index a46f80b..0000000
--- a/libcorkscrew/backtrace-arch.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.
- */
-
-/* Architecture dependent functions. */
-
-#ifndef _CORKSCREW_BACKTRACE_ARCH_H
-#define _CORKSCREW_BACKTRACE_ARCH_H
-
-#include "ptrace-arch.h"
-#include <corkscrew/backtrace.h>
-
-#include <signal.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Rewind the program counter by one instruction. */
-uintptr_t rewind_pc_arch(const memory_t* memory, uintptr_t pc);
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext,
- const map_info_t* map_info_list,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_ARCH_H
diff --git a/libcorkscrew/backtrace-helper.c b/libcorkscrew/backtrace-helper.c
deleted file mode 100644
index bf9d3f3..0000000
--- a/libcorkscrew/backtrace-helper.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "backtrace-helper.h"
-
-#include <cutils/log.h>
-
-backtrace_frame_t* add_backtrace_entry(uintptr_t pc, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth,
- size_t* ignored_frames, size_t* returned_frames) {
- if (*ignored_frames < ignore_depth) {
- *ignored_frames += 1;
- return NULL;
- }
- if (*returned_frames >= max_depth) {
- return NULL;
- }
- backtrace_frame_t* frame = &backtrace[*returned_frames];
- frame->absolute_pc = pc;
- frame->stack_top = 0;
- frame->stack_size = 0;
- *returned_frames += 1;
- return frame;
-}
diff --git a/libcorkscrew/backtrace-helper.h b/libcorkscrew/backtrace-helper.h
deleted file mode 100644
index 4d8a874..0000000
--- a/libcorkscrew/backtrace-helper.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.
- */
-
-/* Backtrace helper functions. */
-
-#ifndef _CORKSCREW_BACKTRACE_HELPER_H
-#define _CORKSCREW_BACKTRACE_HELPER_H
-
-#include <corkscrew/backtrace.h>
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Add a program counter to a backtrace if it will fit.
- * Returns the newly added frame, or NULL if none.
- */
-backtrace_frame_t* add_backtrace_entry(uintptr_t pc,
- backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth,
- size_t* ignored_frames, size_t* returned_frames);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_HELPER_H
diff --git a/libcorkscrew/backtrace.c b/libcorkscrew/backtrace.c
deleted file mode 100644
index f1dd61d..0000000
--- a/libcorkscrew/backtrace.c
+++ /dev/null
@@ -1,335 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "backtrace-arch.h"
-#include "backtrace-helper.h"
-#include "ptrace-arch.h"
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-#include <corkscrew/ptrace.h>
-#include <corkscrew/demangle.h>
-
-#include <unistd.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
-#include <pthread.h>
-#include <unwind.h>
-#include <cutils/log.h>
-#include <cutils/atomic.h>
-
-#define __USE_GNU // For dladdr(3) in glibc.
-#include <dlfcn.h>
-
-#if defined(__BIONIC__)
-
-// Bionic implements and exports gettid but only implements tgkill.
-extern int tgkill(int tgid, int tid, int sig);
-
-#elif defined(__APPLE__)
-
-#include <sys/syscall.h>
-
-// Mac OS >= 10.6 has a system call equivalent to Linux's gettid().
-static pid_t gettid() {
- return syscall(SYS_thread_selfid);
-}
-
-#else
-
-// glibc doesn't implement or export either gettid or tgkill.
-
-#include <unistd.h>
-#include <sys/syscall.h>
-
-static pid_t gettid() {
- return syscall(__NR_gettid);
-}
-
-static int tgkill(int tgid, int tid, int sig) {
- return syscall(__NR_tgkill, tgid, tid, sig);
-}
-
-#endif
-
-typedef struct {
- backtrace_frame_t* backtrace;
- size_t ignore_depth;
- size_t max_depth;
- size_t ignored_frames;
- size_t returned_frames;
- memory_t memory;
-} backtrace_state_t;
-
-static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
- backtrace_state_t* state = (backtrace_state_t*)arg;
- uintptr_t pc = _Unwind_GetIP(context);
- if (pc) {
- // TODO: Get information about the stack layout from the _Unwind_Context.
- // This will require a new architecture-specific function to query
- // the appropriate registers. Current callers of unwind_backtrace
- // don't need this information, so we won't bother collecting it just yet.
- add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace,
- state->ignore_depth, state->max_depth,
- &state->ignored_frames, &state->returned_frames);
- }
- return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
-}
-
-ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
- ALOGV("Unwinding current thread %d.", gettid());
-
- map_info_t* milist = acquire_my_map_info_list();
-
- backtrace_state_t state;
- state.backtrace = backtrace;
- state.ignore_depth = ignore_depth;
- state.max_depth = max_depth;
- state.ignored_frames = 0;
- state.returned_frames = 0;
- init_memory(&state.memory, milist);
-
- _Unwind_Reason_Code rc = _Unwind_Backtrace(unwind_backtrace_callback, &state);
-
- release_my_map_info_list(milist);
-
- if (state.returned_frames) {
- return state.returned_frames;
- }
- return rc == _URC_END_OF_STACK ? 0 : -1;
-}
-
-#ifdef CORKSCREW_HAVE_ARCH
-static const int32_t STATE_DUMPING = -1;
-static const int32_t STATE_DONE = -2;
-static const int32_t STATE_CANCEL = -3;
-
-static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
-static volatile struct {
- int32_t tid_state;
- const map_info_t* map_info_list;
- backtrace_frame_t* backtrace;
- size_t ignore_depth;
- size_t max_depth;
- size_t returned_frames;
-} g_unwind_signal_state;
-
-static void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) {
- if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) {
- g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
- siginfo, sigcontext,
- g_unwind_signal_state.map_info_list,
- g_unwind_signal_state.backtrace,
- g_unwind_signal_state.ignore_depth,
- g_unwind_signal_state.max_depth);
- android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state);
- } else {
- ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.",
- gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state));
- }
-}
-#endif
-
-ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
- size_t ignore_depth, size_t max_depth) {
- if (tid == gettid()) {
- return unwind_backtrace(backtrace, ignore_depth + 1, max_depth);
- }
-
- ALOGV("Unwinding thread %d from thread %d.", tid, gettid());
-
- // TODO: there's no tgkill(2) on Mac OS, so we'd either need the
- // mach_port_t or the pthread_t rather than the tid.
-#if defined(CORKSCREW_HAVE_ARCH) && !defined(__APPLE__)
- struct sigaction act;
- struct sigaction oact;
- memset(&act, 0, sizeof(act));
- act.sa_sigaction = unwind_backtrace_thread_signal_handler;
- act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
- sigemptyset(&act.sa_mask);
-
- pthread_mutex_lock(&g_unwind_signal_mutex);
- map_info_t* milist = acquire_my_map_info_list();
-
- ssize_t frames = -1;
- if (!sigaction(SIGURG, &act, &oact)) {
- g_unwind_signal_state.map_info_list = milist;
- g_unwind_signal_state.backtrace = backtrace;
- g_unwind_signal_state.ignore_depth = ignore_depth;
- g_unwind_signal_state.max_depth = max_depth;
- g_unwind_signal_state.returned_frames = 0;
- android_atomic_release_store(tid, &g_unwind_signal_state.tid_state);
-
- // Signal the specific thread that we want to dump.
- int32_t tid_state = tid;
- if (tgkill(getpid(), tid, SIGURG)) {
- ALOGV("Failed to send SIGURG to thread %d.", tid);
- } else {
- // Wait for the other thread to start dumping the stack, or time out.
- int wait_millis = 250;
- for (;;) {
- tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
- if (tid_state != tid) {
- break;
- }
- if (wait_millis--) {
- ALOGV("Waiting for thread %d to start dumping the stack...", tid);
- usleep(1000);
- } else {
- ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid);
- break;
- }
- }
- }
-
- // Try to cancel the dump if it has not started yet.
- if (tid_state == tid) {
- if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) {
- ALOGV("Canceled thread %d stack dump.", tid);
- tid_state = STATE_CANCEL;
- } else {
- tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
- }
- }
-
- // Wait indefinitely for the dump to finish or be canceled.
- // We cannot apply a timeout here because the other thread is accessing state that
- // is owned by this thread, such as milist. It should not take very
- // long to take the dump once started.
- while (tid_state == STATE_DUMPING) {
- ALOGV("Waiting for thread %d to finish dumping the stack...", tid);
- usleep(1000);
- tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state);
- }
-
- if (tid_state == STATE_DONE) {
- frames = g_unwind_signal_state.returned_frames;
- }
-
- sigaction(SIGURG, &oact, NULL);
- }
-
- release_my_map_info_list(milist);
- pthread_mutex_unlock(&g_unwind_signal_mutex);
- return frames;
-#else
- return -1;
-#endif
-}
-
-ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
- backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-#ifdef CORKSCREW_HAVE_ARCH
- return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
-#else
- return -1;
-#endif
-}
-
-static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
- symbol->relative_pc = pc;
- symbol->relative_symbol_addr = 0;
- symbol->map_name = NULL;
- symbol->symbol_name = NULL;
- symbol->demangled_name = NULL;
-}
-
-void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
- backtrace_symbol_t* backtrace_symbols) {
- map_info_t* milist = acquire_my_map_info_list();
- for (size_t i = 0; i < frames; i++) {
- const backtrace_frame_t* frame = &backtrace[i];
- backtrace_symbol_t* symbol = &backtrace_symbols[i];
- init_backtrace_symbol(symbol, frame->absolute_pc);
-
- const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
- if (mi) {
- symbol->relative_pc = frame->absolute_pc - mi->start;
- if (mi->name[0]) {
- symbol->map_name = strdup(mi->name);
- }
- Dl_info info;
- if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
- symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
- - (uintptr_t)info.dli_fbase;
- symbol->symbol_name = strdup(info.dli_sname);
- symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
- }
- }
- }
- release_my_map_info_list(milist);
-}
-
-void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
- const backtrace_frame_t* backtrace, size_t frames,
- backtrace_symbol_t* backtrace_symbols) {
- for (size_t i = 0; i < frames; i++) {
- const backtrace_frame_t* frame = &backtrace[i];
- backtrace_symbol_t* symbol = &backtrace_symbols[i];
- init_backtrace_symbol(symbol, frame->absolute_pc);
-
- const map_info_t* mi;
- const symbol_t* s;
- find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
- if (mi) {
- symbol->relative_pc = frame->absolute_pc - mi->start;
- if (mi->name[0]) {
- symbol->map_name = strdup(mi->name);
- }
- }
- if (s) {
- symbol->relative_symbol_addr = s->start;
- symbol->symbol_name = strdup(s->name);
- symbol->demangled_name = demangle_symbol_name(symbol->symbol_name);
- }
- }
-}
-
-void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
- for (size_t i = 0; i < frames; i++) {
- backtrace_symbol_t* symbol = &backtrace_symbols[i];
- free(symbol->map_name);
- free(symbol->symbol_name);
- free(symbol->demangled_name);
- init_backtrace_symbol(symbol, 0);
- }
-}
-
-void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)),
- const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) {
- const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>";
- const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name;
- int fieldWidth = (bufferSize - 80) / 2;
- if (symbolName) {
- uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
- if (pc_offset) {
- snprintf(buffer, bufferSize, "#%02u pc %08x %.*s (%.*s+%u)",
- frameNumber, (unsigned int) symbol->relative_pc,
- fieldWidth, mapName, fieldWidth, symbolName, pc_offset);
- } else {
- snprintf(buffer, bufferSize, "#%02u pc %08x %.*s (%.*s)",
- frameNumber, (unsigned int) symbol->relative_pc,
- fieldWidth, mapName, fieldWidth, symbolName);
- }
- } else {
- snprintf(buffer, bufferSize, "#%02u pc %08x %.*s",
- frameNumber, (unsigned int) symbol->relative_pc,
- fieldWidth, mapName);
- }
-}
diff --git a/libcorkscrew/demangle.c b/libcorkscrew/demangle.c
deleted file mode 100644
index 30ab1b0..0000000
--- a/libcorkscrew/demangle.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/demangle.h>
-
-#include <cutils/log.h>
-
-extern char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
- int *status);
-
-char* demangle_symbol_name(const char* name) {
-#if defined(__APPLE__)
- // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
- if (name != NULL && name[0] != '_') {
- return NULL;
- }
-#endif
- // __cxa_demangle handles NULL by returning NULL
- return __cxa_demangle(name, 0, 0, 0);
-}
diff --git a/libcorkscrew/map_info.c b/libcorkscrew/map_info.c
deleted file mode 100644
index 93dffbf..0000000
--- a/libcorkscrew/map_info.c
+++ /dev/null
@@ -1,279 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/map_info.h>
-
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <cutils/log.h>
-#include <sys/time.h>
-
-#if defined(__APPLE__)
-
-// Mac OS vmmap(1) output:
-// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
-// 012345678901234567890123456789012345678901234567890123456789
-// 0 1 2 3 4 5
-static map_info_t* parse_vmmap_line(const char* line) {
- unsigned long int start;
- unsigned long int end;
- char permissions[4];
- int name_pos;
- if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n",
- &start, &end, permissions, &name_pos) != 3) {
- return NULL;
- }
-
- const char* name = line + name_pos;
- size_t name_len = strlen(name);
-
- map_info_t* mi = calloc(1, sizeof(map_info_t) + name_len);
- if (mi != NULL) {
- mi->start = start;
- mi->end = end;
- mi->is_readable = permissions[0] == 'r';
- mi->is_writable = permissions[1] == 'w';
- mi->is_executable = permissions[2] == 'x';
- mi->data = NULL;
- memcpy(mi->name, name, name_len);
- mi->name[name_len - 1] = '\0';
- ALOGV("Parsed map: start=0x%08x, end=0x%08x, "
- "is_readable=%d, is_writable=%d is_executable=%d, name=%s",
- mi->start, mi->end,
- mi->is_readable, mi->is_writable, mi->is_executable, mi->name);
- }
- return mi;
-}
-
-map_info_t* load_map_info_list(pid_t pid) {
- char cmd[1024];
- snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid);
- FILE* fp = popen(cmd, "r");
- if (fp == NULL) {
- return NULL;
- }
-
- char line[1024];
- map_info_t* milist = NULL;
- while (fgets(line, sizeof(line), fp) != NULL) {
- map_info_t* mi = parse_vmmap_line(line);
- if (mi != NULL) {
- mi->next = milist;
- milist = mi;
- }
- }
- pclose(fp);
- return milist;
-}
-
-#else
-
-// Linux /proc/<pid>/maps lines:
-// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
-// 012345678901234567890123456789012345678901234567890123456789
-// 0 1 2 3 4 5
-static map_info_t* parse_maps_line(const char* line)
-{
- unsigned long int start;
- unsigned long int end;
- char permissions[5];
- int name_pos;
- if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", &start, &end,
- permissions, &name_pos) != 3) {
- return NULL;
- }
-
- while (isspace(line[name_pos])) {
- name_pos += 1;
- }
- const char* name = line + name_pos;
- size_t name_len = strlen(name);
- if (name_len && name[name_len - 1] == '\n') {
- name_len -= 1;
- }
-
- map_info_t* mi = calloc(1, sizeof(map_info_t) + name_len + 1);
- if (mi) {
- mi->start = start;
- mi->end = end;
- mi->is_readable = strlen(permissions) == 4 && permissions[0] == 'r';
- mi->is_writable = strlen(permissions) == 4 && permissions[1] == 'w';
- mi->is_executable = strlen(permissions) == 4 && permissions[2] == 'x';
- mi->data = NULL;
- memcpy(mi->name, name, name_len);
- mi->name[name_len] = '\0';
- ALOGV("Parsed map: start=0x%08x, end=0x%08x, "
- "is_readable=%d, is_writable=%d, is_executable=%d, name=%s",
- mi->start, mi->end,
- mi->is_readable, mi->is_writable, mi->is_executable, mi->name);
- }
- return mi;
-}
-
-map_info_t* load_map_info_list(pid_t tid) {
- char path[PATH_MAX];
- char line[1024];
- FILE* fp;
- map_info_t* milist = NULL;
-
- snprintf(path, PATH_MAX, "/proc/%d/maps", tid);
- fp = fopen(path, "r");
- if (fp) {
- while(fgets(line, sizeof(line), fp)) {
- map_info_t* mi = parse_maps_line(line);
- if (mi) {
- mi->next = milist;
- milist = mi;
- }
- }
- fclose(fp);
- }
- return milist;
-}
-
-#endif
-
-void free_map_info_list(map_info_t* milist) {
- while (milist) {
- map_info_t* next = milist->next;
- free(milist);
- milist = next;
- }
-}
-
-const map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr) {
- const map_info_t* mi = milist;
- while (mi && !(addr >= mi->start && addr < mi->end)) {
- mi = mi->next;
- }
- return mi;
-}
-
-bool is_readable_map(const map_info_t* milist, uintptr_t addr) {
- const map_info_t* mi = find_map_info(milist, addr);
- return mi && mi->is_readable;
-}
-
-bool is_writable_map(const map_info_t* milist, uintptr_t addr) {
- const map_info_t* mi = find_map_info(milist, addr);
- return mi && mi->is_writable;
-}
-
-bool is_executable_map(const map_info_t* milist, uintptr_t addr) {
- const map_info_t* mi = find_map_info(milist, addr);
- return mi && mi->is_executable;
-}
-
-static pthread_mutex_t g_my_map_info_list_mutex = PTHREAD_MUTEX_INITIALIZER;
-static map_info_t* g_my_map_info_list = NULL;
-
-static const int64_t MAX_CACHE_AGE = 5 * 1000 * 1000000LL;
-
-typedef struct {
- uint32_t refs;
- int64_t timestamp;
-} my_map_info_data_t;
-
-static int64_t now_ns() {
-#if defined(HAVE_POSIX_CLOCKS)
- struct timespec t;
- t.tv_sec = t.tv_nsec = 0;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return t.tv_sec * 1000000000LL + t.tv_nsec;
-#else
- struct timeval t;
- gettimeofday(&t, NULL);
- return t.tv_sec * 1000000000LL + t.tv_usec * 1000LL;
-#endif
-}
-
-static void dec_ref(map_info_t* milist, my_map_info_data_t* data) {
- if (!--data->refs) {
- ALOGV("Freed my_map_info_list %p.", milist);
- free(data);
- free_map_info_list(milist);
- }
-}
-
-map_info_t* acquire_my_map_info_list() {
- pthread_mutex_lock(&g_my_map_info_list_mutex);
-
- int64_t time = now_ns();
- if (g_my_map_info_list != NULL) {
- my_map_info_data_t* data = (my_map_info_data_t*)g_my_map_info_list->data;
- int64_t age = time - data->timestamp;
- if (age >= MAX_CACHE_AGE) {
- ALOGV("Invalidated my_map_info_list %p, age=%lld.", g_my_map_info_list, age);
- dec_ref(g_my_map_info_list, data);
- g_my_map_info_list = NULL;
- } else {
- ALOGV("Reusing my_map_info_list %p, age=%lld.", g_my_map_info_list, age);
- }
- }
-
- if (g_my_map_info_list == NULL) {
- my_map_info_data_t* data = (my_map_info_data_t*)malloc(sizeof(my_map_info_data_t));
- g_my_map_info_list = load_map_info_list(getpid());
- if (g_my_map_info_list != NULL) {
- ALOGV("Loaded my_map_info_list %p.", g_my_map_info_list);
- g_my_map_info_list->data = data;
- data->refs = 1;
- data->timestamp = time;
- } else {
- free(data);
- }
- }
-
- map_info_t* milist = g_my_map_info_list;
- if (milist) {
- my_map_info_data_t* data = (my_map_info_data_t*)g_my_map_info_list->data;
- data->refs += 1;
- }
-
- pthread_mutex_unlock(&g_my_map_info_list_mutex);
- return milist;
-}
-
-void release_my_map_info_list(map_info_t* milist) {
- if (milist) {
- pthread_mutex_lock(&g_my_map_info_list_mutex);
-
- my_map_info_data_t* data = (my_map_info_data_t*)milist->data;
- dec_ref(milist, data);
-
- pthread_mutex_unlock(&g_my_map_info_list_mutex);
- }
-}
-
-void flush_my_map_info_list() {
- pthread_mutex_lock(&g_my_map_info_list_mutex);
-
- if (g_my_map_info_list != NULL) {
- my_map_info_data_t* data = (my_map_info_data_t*) g_my_map_info_list->data;
- dec_ref(g_my_map_info_list, data);
- g_my_map_info_list = NULL;
- }
-
- pthread_mutex_unlock(&g_my_map_info_list_mutex);
-}
diff --git a/libcorkscrew/ptrace-arch.h b/libcorkscrew/ptrace-arch.h
deleted file mode 100755
index 0bcff63..0000000
--- a/libcorkscrew/ptrace-arch.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.
- */
-
-/* Architecture dependent functions. */
-
-#ifndef _CORKSCREW_PTRACE_ARCH_H
-#define _CORKSCREW_PTRACE_ARCH_H
-
-#include <corkscrew/ptrace.h>
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Custom extra data we stuff into map_info_t structures as part
- * of our ptrace_context_t. */
-typedef struct {
-#ifdef __arm__
- uintptr_t exidx_start;
- size_t exidx_size;
-#elif __mips__
- uintptr_t eh_frame_hdr;
-#elif __i386__
- uintptr_t eh_frame_hdr;
-#endif
- symbol_table_t* symbol_table;
-} map_info_data_t;
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data);
-void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_PTRACE_ARCH_H
diff --git a/libcorkscrew/ptrace.c b/libcorkscrew/ptrace.c
deleted file mode 100644
index be58f7f..0000000
--- a/libcorkscrew/ptrace.c
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <sys/ptrace.h>
-#include <cutils/log.h>
-
-static const uint32_t ELF_MAGIC = 0x464C457f; // "ELF\0177"
-
-#ifndef PAGE_SIZE
-#define PAGE_SIZE 4096
-#endif
-
-#ifndef PAGE_MASK
-#define PAGE_MASK (~(PAGE_SIZE - 1))
-#endif
-
-void init_memory(memory_t* memory, const map_info_t* map_info_list) {
- memory->tid = -1;
- memory->map_info_list = map_info_list;
-}
-
-void init_memory_ptrace(memory_t* memory, pid_t tid) {
- memory->tid = tid;
- memory->map_info_list = NULL;
-}
-
-bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) {
- ALOGV("try_get_word: reading word at %p", (void*) ptr);
- if (ptr & 3) {
- ALOGV("try_get_word: invalid pointer %p", (void*) ptr);
- *out_value = 0xffffffffL;
- return false;
- }
- if (memory->tid < 0) {
- if (!is_readable_map(memory->map_info_list, ptr)) {
- ALOGV("try_get_word: pointer %p not in a readable map", (void*) ptr);
- *out_value = 0xffffffffL;
- return false;
- }
- *out_value = *(uint32_t*)ptr;
- return true;
- } else {
-#if defined(__APPLE__)
- ALOGV("no ptrace on Mac OS");
- return false;
-#else
- // ptrace() returns -1 and sets errno when the operation fails.
- // To disambiguate -1 from a valid result, we clear errno beforehand.
- errno = 0;
- *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL);
- if (*out_value == 0xffffffffL && errno) {
- ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, "
- "ptrace() errno=%d", ptr, memory->tid, errno);
- return false;
- }
- return true;
-#endif
- }
-}
-
-bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
- memory_t memory;
- init_memory_ptrace(&memory, tid);
- return try_get_word(&memory, ptr, out_value);
-}
-
-static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) {
- if (mi->is_executable && mi->is_readable) {
- uint32_t elf_magic;
- if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
- map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t));
- if (data) {
- mi->data = data;
- if (mi->name[0]) {
- data->symbol_table = load_symbol_table(mi->name);
- }
-#ifdef CORKSCREW_HAVE_ARCH
- load_ptrace_map_info_data_arch(pid, mi, data);
-#endif
- }
- }
- }
-}
-
-ptrace_context_t* load_ptrace_context(pid_t pid) {
- ptrace_context_t* context =
- (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t));
- if (context) {
- context->map_info_list = load_map_info_list(pid);
- for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
- load_ptrace_map_info_data(pid, mi);
- }
- }
- return context;
-}
-
-static void free_ptrace_map_info_data(map_info_t* mi) {
- map_info_data_t* data = (map_info_data_t*)mi->data;
- if (data) {
- if (data->symbol_table) {
- free_symbol_table(data->symbol_table);
- }
-#ifdef CORKSCREW_HAVE_ARCH
- free_ptrace_map_info_data_arch(mi, data);
-#endif
- free(data);
- mi->data = NULL;
- }
-}
-
-void free_ptrace_context(ptrace_context_t* context) {
- for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
- free_ptrace_map_info_data(mi);
- }
- free_map_info_list(context->map_info_list);
- free(context);
-}
-
-void find_symbol_ptrace(const ptrace_context_t* context,
- uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) {
- const map_info_t* mi = find_map_info(context->map_info_list, addr);
- const symbol_t* symbol = NULL;
- if (mi) {
- const map_info_data_t* data = (const map_info_data_t*)mi->data;
- if (data && data->symbol_table) {
- symbol = find_symbol(data->symbol_table, addr - mi->start);
- }
- }
- *out_map_info = mi;
- *out_symbol = symbol;
-}
diff --git a/libcorkscrew/symbol_table.c b/libcorkscrew/symbol_table.c
deleted file mode 100644
index 982ccc8..0000000
--- a/libcorkscrew/symbol_table.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/symbol_table.h>
-
-#include <stdbool.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <cutils/log.h>
-
-#if defined(__APPLE__)
-#else
-
-#include <elf.h>
-
-static bool is_elf(Elf32_Ehdr* e) {
- return (e->e_ident[EI_MAG0] == ELFMAG0 &&
- e->e_ident[EI_MAG1] == ELFMAG1 &&
- e->e_ident[EI_MAG2] == ELFMAG2 &&
- e->e_ident[EI_MAG3] == ELFMAG3);
-}
-
-#endif
-
-// Compare function for qsort
-static int qcompar(const void *a, const void *b) {
- const symbol_t* asym = (const symbol_t*)a;
- const symbol_t* bsym = (const symbol_t*)b;
- if (asym->start > bsym->start) return 1;
- if (asym->start < bsym->start) return -1;
- return 0;
-}
-
-// Compare function for bsearch
-static int bcompar(const void *key, const void *element) {
- uintptr_t addr = *(const uintptr_t*)key;
- const symbol_t* symbol = (const symbol_t*)element;
- if (addr < symbol->start) return -1;
- if (addr >= symbol->end) return 1;
- return 0;
-}
-
-symbol_table_t* load_symbol_table(const char *filename) {
- symbol_table_t* table = NULL;
-#if !defined(__APPLE__)
- ALOGV("Loading symbol table from '%s'.", filename);
-
- int fd = open(filename, O_RDONLY);
- if (fd < 0) {
- goto out;
- }
-
- struct stat sb;
- if (fstat(fd, &sb)) {
- goto out_close;
- }
-
- size_t length = sb.st_size;
- char* base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
- if (base == MAP_FAILED) {
- goto out_close;
- }
-
- // Parse the file header
- Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
- if (!is_elf(hdr)) {
- goto out_close;
- }
- Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
-
- // Search for the dynamic symbols section
- int sym_idx = -1;
- int dynsym_idx = -1;
- for (Elf32_Half i = 0; i < hdr->e_shnum; i++) {
- if (shdr[i].sh_type == SHT_SYMTAB) {
- sym_idx = i;
- }
- if (shdr[i].sh_type == SHT_DYNSYM) {
- dynsym_idx = i;
- }
- }
- if (dynsym_idx == -1 && sym_idx == -1) {
- goto out_unmap;
- }
-
- table = malloc(sizeof(symbol_table_t));
- if(!table) {
- goto out_unmap;
- }
- table->num_symbols = 0;
-
- Elf32_Sym *dynsyms = NULL;
- int dynnumsyms = 0;
- char *dynstr = NULL;
- if (dynsym_idx != -1) {
- dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
- dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
- int dynstr_idx = shdr[dynsym_idx].sh_link;
- dynstr = base + shdr[dynstr_idx].sh_offset;
- }
-
- Elf32_Sym *syms = NULL;
- int numsyms = 0;
- char *str = NULL;
- if (sym_idx != -1) {
- syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
- numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
- int str_idx = shdr[sym_idx].sh_link;
- str = base + shdr[str_idx].sh_offset;
- }
-
- int dynsymbol_count = 0;
- if (dynsym_idx != -1) {
- // Iterate through the dynamic symbol table, and count how many symbols
- // are actually defined
- for (int i = 0; i < dynnumsyms; i++) {
- if (dynsyms[i].st_shndx != SHN_UNDEF) {
- dynsymbol_count++;
- }
- }
- }
-
- size_t symbol_count = 0;
- if (sym_idx != -1) {
- // Iterate through the symbol table, and count how many symbols
- // are actually defined
- for (int i = 0; i < numsyms; i++) {
- if (syms[i].st_shndx != SHN_UNDEF
- && str[syms[i].st_name]
- && syms[i].st_value
- && syms[i].st_size) {
- symbol_count++;
- }
- }
- }
-
- // Now, create an entry in our symbol table structure for each symbol...
- table->num_symbols += symbol_count + dynsymbol_count;
- table->symbols = malloc(table->num_symbols * sizeof(symbol_t));
- if (!table->symbols) {
- free(table);
- table = NULL;
- goto out_unmap;
- }
-
- size_t symbol_index = 0;
- if (dynsym_idx != -1) {
- // ...and populate them
- for (int i = 0; i < dynnumsyms; i++) {
- if (dynsyms[i].st_shndx != SHN_UNDEF) {
- table->symbols[symbol_index].name = strdup(dynstr + dynsyms[i].st_name);
- table->symbols[symbol_index].start = dynsyms[i].st_value;
- table->symbols[symbol_index].end = dynsyms[i].st_value + dynsyms[i].st_size;
- ALOGV(" [%d] '%s' 0x%08x-0x%08x (DYNAMIC)",
- symbol_index, table->symbols[symbol_index].name,
- table->symbols[symbol_index].start, table->symbols[symbol_index].end);
- symbol_index += 1;
- }
- }
- }
-
- if (sym_idx != -1) {
- // ...and populate them
- for (int i = 0; i < numsyms; i++) {
- if (syms[i].st_shndx != SHN_UNDEF
- && str[syms[i].st_name]
- && syms[i].st_value
- && syms[i].st_size) {
- table->symbols[symbol_index].name = strdup(str + syms[i].st_name);
- table->symbols[symbol_index].start = syms[i].st_value;
- table->symbols[symbol_index].end = syms[i].st_value + syms[i].st_size;
- ALOGV(" [%d] '%s' 0x%08x-0x%08x",
- symbol_index, table->symbols[symbol_index].name,
- table->symbols[symbol_index].start, table->symbols[symbol_index].end);
- symbol_index += 1;
- }
- }
- }
-
- // Sort the symbol table entries, so they can be bsearched later
- qsort(table->symbols, table->num_symbols, sizeof(symbol_t), qcompar);
-
-out_unmap:
- munmap(base, length);
-
-out_close:
- close(fd);
-#endif
-
-out:
- return table;
-}
-
-void free_symbol_table(symbol_table_t* table) {
- if (table) {
- for (size_t i = 0; i < table->num_symbols; i++) {
- free(table->symbols[i].name);
- }
- free(table->symbols);
- free(table);
- }
-}
-
-const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr) {
- if (!table) return NULL;
- return (const symbol_t*)bsearch(&addr, table->symbols, table->num_symbols,
- sizeof(symbol_t), bcompar);
-}
diff --git a/libcorkscrew/test.cpp b/libcorkscrew/test.cpp
deleted file mode 100644
index 22dfa7d..0000000
--- a/libcorkscrew/test.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-#include <corkscrew/backtrace.h>
-#include <corkscrew/symbol_table.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-int do_backtrace(float /* just to test demangling */) {
- const size_t MAX_DEPTH = 32;
- backtrace_frame_t* frames = (backtrace_frame_t*) malloc(sizeof(backtrace_frame_t) * MAX_DEPTH);
- ssize_t frame_count = unwind_backtrace(frames, 0, MAX_DEPTH);
- fprintf(stderr, "frame_count=%d\n", (int) frame_count);
- if (frame_count <= 0) {
- return frame_count;
- }
-
- backtrace_symbol_t* backtrace_symbols = (backtrace_symbol_t*) malloc(sizeof(backtrace_symbol_t) * frame_count);
- get_backtrace_symbols(frames, frame_count, backtrace_symbols);
-
- for (size_t i = 0; i < (size_t) frame_count; ++i) {
- char line[MAX_BACKTRACE_LINE_LENGTH];
- format_backtrace_line(i, &frames[i], &backtrace_symbols[i],
- line, MAX_BACKTRACE_LINE_LENGTH);
- if (backtrace_symbols[i].symbol_name != NULL) {
- // get_backtrace_symbols found the symbol's name with dladdr(3).
- fprintf(stderr, " %s\n", line);
- } else {
- // We don't have a symbol. Maybe this is a static symbol, and
- // we can look it up?
- symbol_table_t* symbols = NULL;
- if (backtrace_symbols[i].map_name != NULL) {
- symbols = load_symbol_table(backtrace_symbols[i].map_name);
- }
- const symbol_t* symbol = NULL;
- if (symbols != NULL) {
- symbol = find_symbol(symbols, frames[i].absolute_pc);
- }
- if (symbol != NULL) {
- int offset = frames[i].absolute_pc - symbol->start;
- fprintf(stderr, " %s (%s%+d)\n", line, symbol->name, offset);
- } else {
- fprintf(stderr, " %s (\?\?\?)\n", line);
- }
- free_symbol_table(symbols);
- }
- }
-
- free_backtrace_symbols(backtrace_symbols, frame_count);
- free(backtrace_symbols);
- free(frames);
- return frame_count;
-}
-
-struct C {
- int g(int i);
-};
-
-__attribute__ ((noinline)) int C::g(int i) {
- if (i == 0) {
- return do_backtrace(0.1);
- }
- return g(i - 1);
-}
-
-extern "C" __attribute__ ((noinline)) int f() {
- C c;
- return c.g(5);
-}
-
-int main() {
- flush_my_map_info_list();
- f();
-
- flush_my_map_info_list();
- f();
-
- return 0;
-}
diff --git a/libctest/Android.mk b/libctest/Android.mk
deleted file mode 100644
index 815fabb..0000000
--- a/libctest/Android.mk
+++ /dev/null
@@ -1,7 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_MODULE:= libctest
-LOCAL_SRC_FILES := ctest.c
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/libctest/NOTICE b/libctest/NOTICE
deleted file mode 100644
index c5b1efa..0000000
--- a/libctest/NOTICE
+++ /dev/null
@@ -1,190 +0,0 @@
-
- Copyright (c) 2005-2008, 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.
-
- 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.
-
-
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
diff --git a/libctest/ctest.c b/libctest/ctest.c
deleted file mode 100644
index ee6331f..0000000
--- a/libctest/ctest.c
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * 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.
- */
-
-#include <assert.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <ctest/ctest.h>
-
-#define MAX_TESTS 255
-
-/** Semi-random number used to identify assertion errors. */
-#define ASSERTION_ERROR 42
-
-typedef void TestCase();
-
-/** A suite of tests. */
-typedef struct {
- int size;
- const char* testNames[MAX_TESTS];
- TestCase* tests[MAX_TESTS];
- int currentTest;
- FILE* out;
-} TestSuite;
-
-/** Gets the test suite. Creates it if necessary. */
-static TestSuite* getTestSuite() {
- static TestSuite* suite = NULL;
-
- if (suite != NULL) {
- return suite;
- }
-
- suite = calloc(1, sizeof(TestSuite));
- assert(suite != NULL);
-
- suite->out = tmpfile();
- assert(suite->out != NULL);
-
- return suite;
-}
-
-void addNamedTest(const char* name, TestCase* test) {
- TestSuite* testSuite = getTestSuite();
- assert(testSuite->size <= MAX_TESTS);
-
- int index = testSuite->size;
- testSuite->testNames[index] = name;
- testSuite->tests[index] = test;
-
- testSuite->size++;
-}
-
-/** Prints failures to stderr. */
-static void printFailures(int failures) {
- TestSuite* suite = getTestSuite();
-
- fprintf(stderr, "FAILURE! %d of %d tests failed. Failures:\n",
- failures, suite->size);
-
- // Copy test output to stdout.
- rewind(suite->out);
- char buffer[512];
- size_t read;
- while ((read = fread(buffer, sizeof(char), 512, suite->out)) > 0) {
- // TODO: Make sure we actually wrote 'read' bytes.
- fwrite(buffer, sizeof(char), read, stderr);
- }
-}
-
-/** Runs a single test case. */
-static int runCurrentTest() {
- TestSuite* suite = getTestSuite();
-
- pid_t pid = fork();
- if (pid == 0) {
- // Child process. Runs test case.
- suite->tests[suite->currentTest]();
-
- // Exit successfully.
- exit(0);
- } else if (pid < 0) {
- fprintf(stderr, "Fork failed.");
- exit(1);
- } else {
- // Parent process. Wait for child.
- int status;
- waitpid(pid, &status, 0);
-
- if (!WIFEXITED(status)) {
- return -1;
- }
-
- return WEXITSTATUS(status);
- }
-}
-
-void runTests() {
- TestSuite* suite = getTestSuite();
-
- int failures = 0;
- for (suite->currentTest = 0; suite->currentTest < suite->size;
- suite->currentTest++) {
- // Flush stdout before forking.
- fflush(stdout);
-
- int result = runCurrentTest();
-
- if (result != 0) {
- printf("X");
-
- failures++;
-
- // Handle errors other than assertions.
- if (result != ASSERTION_ERROR) {
- // TODO: Report file name.
- fprintf(suite->out, "Process failed: [%s] status: %d\n",
- suite->testNames[suite->currentTest], result);
- fflush(suite->out);
- }
- } else {
- printf(".");
- }
- }
-
- printf("\n");
-
- if (failures > 0) {
- printFailures(failures);
- } else {
- printf("SUCCESS! %d tests ran successfully.\n", suite->size);
- }
-}
-
-void assertTrueWithSource(int value, const char* file, int line, char* message) {
- if (!value) {
- TestSuite* suite = getTestSuite();
-
- fprintf(suite->out, "Assertion failed: [%s:%d] %s: %s\n", file, line,
- suite->testNames[suite->currentTest], message);
- fflush(suite->out);
-
- // Exit the process for this test case.
- exit(ASSERTION_ERROR);
- }
-}
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 93bccb0..2c5e351 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -16,26 +16,11 @@
LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
-ifeq ($(TARGET_CPU_SMP),true)
- targetSmpFlag := -DANDROID_SMP=1
-else
- targetSmpFlag := -DANDROID_SMP=0
-endif
-hostSmpFlag := -DANDROID_SMP=0
-
commonSources := \
hashmap.c \
atomic.c.arm \
native_handle.c \
- socket_inaddr_any_server.c \
- socket_local_client.c \
- socket_local_server.c \
- socket_loopback_client.c \
- socket_loopback_server.c \
- socket_network_client.c \
- sockets.c \
config_utils.c \
- cpu_info.c \
load_file.c \
open_memstream.c \
strdup16to8.c \
@@ -47,9 +32,6 @@
iosched_policy.c \
str_parms.c \
-commonHostSources := \
- ashmem-host.c
-
# some files must not be compiled when building against Mingw
# they correspond to features not used by our host development tools
# which are also hard or even impossible to port to native Win32
@@ -67,7 +49,18 @@
ifneq ($(WINDOWS_HOST_ONLY),1)
commonSources += \
fs.c \
- multiuser.c
+ multiuser.c \
+ socket_inaddr_any_server.c \
+ socket_local_client.c \
+ socket_local_server.c \
+ socket_loopback_client.c \
+ socket_loopback_server.c \
+ socket_network_client.c \
+ sockets.c \
+
+ commonHostSources += \
+ ashmem-host.c
+
endif
@@ -75,21 +68,28 @@
# ========================================================
LOCAL_MODULE := libcutils
LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) dlmalloc_stubs.c
-LOCAL_LDLIBS := -lpthread
LOCAL_STATIC_LIBRARIES := liblog
-LOCAL_CFLAGS += $(hostSmpFlag)
+ifneq ($(HOST_OS),windows)
+LOCAL_CFLAGS += -Werror
+endif
+LOCAL_MULTILIB := both
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_HOST_STATIC_LIBRARY)
-# Static library for host, 64-bit
+# Tests for host
# ========================================================
include $(CLEAR_VARS)
-LOCAL_MODULE := lib64cutils
-LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) dlmalloc_stubs.c
-LOCAL_LDLIBS := -lpthread
-LOCAL_STATIC_LIBRARIES := lib64log
-LOCAL_CFLAGS += $(hostSmpFlag) -m64
-include $(BUILD_HOST_STATIC_LIBRARY)
+LOCAL_MODULE := tst_str_parms
+LOCAL_CFLAGS += -DTEST_STR_PARMS
+ifneq ($(HOST_OS),windows)
+LOCAL_CFLAGS += -Werror
+endif
+LOCAL_SRC_FILES := str_parms.c hashmap.c memory.c
+LOCAL_STATIC_LIBRARIES := liblog
+LOCAL_MODULE_TAGS := optional
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_HOST_EXECUTABLE)
# Shared and static library for target
@@ -102,30 +102,51 @@
ashmem-dev.c \
debugger.c \
klog.c \
+ memory.c \
partition_utils.c \
properties.c \
qtaguid.c \
trace.c \
- uevent.c
+ uevent.c \
-ifeq ($(TARGET_ARCH),arm)
- LOCAL_SRC_FILES += arch-arm/memset32.S
-else # !arm
- ifeq ($(TARGET_ARCH),x86)
- LOCAL_CFLAGS += -DHAVE_MEMSET16 -DHAVE_MEMSET32
- LOCAL_SRC_FILES += arch-x86/android_memset16.S arch-x86/android_memset32.S memory.c
- else # !x86
- ifeq ($(TARGET_ARCH),mips)
- LOCAL_SRC_FILES += arch-mips/android_memset.c
- else # !mips
- LOCAL_SRC_FILES += memory.c
- endif # !mips
- endif # !x86
-endif # !arm
+LOCAL_SRC_FILES_arm += \
+ arch-arm/memset32.S \
+
+# arch-arm/memset32.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+
+LOCAL_SRC_FILES_arm64 += \
+ arch-arm64/android_memset.S \
+
+ifndef ARCH_MIPS_REV6
+LOCAL_SRC_FILES_mips += \
+ arch-mips/android_memset.c \
+
+LOCAL_CFLAGS_mips += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+endif
+
+# TODO: switch mips64 back to using arch-mips/android_memset.c
+LOCAL_SRC_FILES_mips64 += \
+# arch-mips/android_memset.c \
+
+LOCAL_SRC_FILES_x86 += \
+ arch-x86/android_memset16.S \
+ arch-x86/android_memset32.S \
+
+LOCAL_SRC_FILES_x86_64 += \
+ arch-x86_64/android_memset16.S \
+ arch-x86_64/android_memset32.S \
+
+LOCAL_CFLAGS_arm += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+LOCAL_CFLAGS_arm64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+#LOCAL_CFLAGS_mips64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+LOCAL_CFLAGS_x86 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+LOCAL_CFLAGS_x86_64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
LOCAL_C_INCLUDES := $(libcutils_c_includes)
LOCAL_STATIC_LIBRARIES := liblog
-LOCAL_CFLAGS += $(targetSmpFlag)
+LOCAL_CFLAGS += -Werror -std=gnu90
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
@@ -134,16 +155,18 @@
# liblog symbols present in libcutils.
LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog
LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_CFLAGS += $(targetSmpFlag)
+LOCAL_CFLAGS += -Werror
LOCAL_C_INCLUDES := $(libcutils_c_includes)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := tst_str_parms
-LOCAL_CFLAGS += -DTEST_STR_PARMS
+LOCAL_CFLAGS += -DTEST_STR_PARMS -Werror
LOCAL_SRC_FILES := str_parms.c hashmap.c memory.c
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_MODULE_TAGS := optional
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_EXECUTABLE)
include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/libcutils/android_reboot.c b/libcutils/android_reboot.c
index b7895fa..6ae23c1 100644
--- a/libcutils/android_reboot.c
+++ b/libcutils/android_reboot.c
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <mntent.h>
#include <stdio.h>
#include <string.h>
@@ -33,37 +34,21 @@
*/
static int remount_ro_done(void)
{
- FILE *f;
- char mount_dev[256];
- char mount_dir[256];
- char mount_type[256];
- char mount_opts[256];
- int mount_freq;
- int mount_passno;
- int match;
+ FILE* fp;
+ struct mntent* mentry;
int found_rw_fs = 0;
- f = fopen("/proc/mounts", "r");
- if (! f) {
- /* If we can't read /proc/mounts, just give up */
+ if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
+ /* If we can't read /proc/mounts, just give up. */
return 1;
}
-
- do {
- match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
- mount_dev, mount_dir, mount_type,
- mount_opts, &mount_freq, &mount_passno);
- mount_dev[255] = 0;
- mount_dir[255] = 0;
- mount_type[255] = 0;
- mount_opts[255] = 0;
- if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw")) {
+ while ((mentry = getmntent(fp)) != NULL) {
+ if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
found_rw_fs = 1;
break;
}
- } while (match != EOF);
-
- fclose(f);
+ }
+ endmntent(fp);
return !found_rw_fs;
}
@@ -104,7 +89,7 @@
}
-int android_reboot(int cmd, int flags UNUSED, char *arg)
+int android_reboot(int cmd, int flags UNUSED, const char *arg)
{
int ret;
diff --git a/libcutils/arch-arm/memset32.S b/libcutils/arch-arm/memset32.S
index 4697265..6efab9f 100644
--- a/libcutils/arch-arm/memset32.S
+++ b/libcutils/arch-arm/memset32.S
@@ -51,8 +51,10 @@
android_memset32:
.fnstart
- .save {lr}
+ .cfi_startproc
str lr, [sp, #-4]!
+ .cfi_def_cfa_offset 4
+ .cfi_rel_offset lr, 0
/* align the destination to a cache-line */
mov r12, r1
@@ -89,5 +91,8 @@
strmih lr, [r0], #2
ldr lr, [sp], #4
+ .cfi_def_cfa_offset 0
+ .cfi_restore lr
bx lr
+ .cfi_endproc
.fnend
diff --git a/libcutils/arch-arm64/android_memset.S b/libcutils/arch-arm64/android_memset.S
new file mode 100644
index 0000000..9a83a68
--- /dev/null
+++ b/libcutils/arch-arm64/android_memset.S
@@ -0,0 +1,211 @@
+/* Copyright (c) 2012, Linaro Limited
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the Linaro nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* Assumptions:
+ *
+ * ARMv8-a, AArch64
+ * Unaligned accesses
+ *
+ */
+
+/* By default we assume that the DC instruction can be used to zero
+ data blocks more efficiently. In some circumstances this might be
+ unsafe, for example in an asymmetric multiprocessor environment with
+ different DC clear lengths (neither the upper nor lower lengths are
+ safe to use). */
+
+#define dst x0
+#define count x2
+#define tmp1 x3
+#define tmp1w w3
+#define tmp2 x4
+#define tmp2w w4
+#define zva_len_x x5
+#define zva_len w5
+#define zva_bits_x x6
+
+#define A_l x1
+#define A_lw w1
+#define tmp3w w9
+
+#define ENTRY(f) \
+ .text; \
+ .globl f; \
+ .align 0; \
+ .type f, %function; \
+ f: \
+ .cfi_startproc \
+
+#define END(f) \
+ .cfi_endproc; \
+ .size f, .-f; \
+
+ENTRY(android_memset16)
+ ands A_lw, A_lw, #0xffff
+ b.eq .Lzero_mem
+ orr A_lw, A_lw, A_lw, lsl #16
+ b .Lexpand_to_64
+END(android_memset16)
+
+ENTRY(android_memset32)
+ cmp A_lw, #0
+ b.eq .Lzero_mem
+.Lexpand_to_64:
+ orr A_l, A_l, A_l, lsl #32
+.Ltail_maybe_long:
+ cmp count, #64
+ b.ge .Lnot_short
+.Ltail_maybe_tiny:
+ cmp count, #15
+ b.le .Ltail15tiny
+.Ltail63:
+ ands tmp1, count, #0x30
+ b.eq .Ltail15
+ add dst, dst, tmp1
+ cmp tmp1w, #0x20
+ b.eq 1f
+ b.lt 2f
+ stp A_l, A_l, [dst, #-48]
+1:
+ stp A_l, A_l, [dst, #-32]
+2:
+ stp A_l, A_l, [dst, #-16]
+
+.Ltail15:
+ and count, count, #15
+ add dst, dst, count
+ stp A_l, A_l, [dst, #-16] /* Repeat some/all of last store. */
+ ret
+
+.Ltail15tiny:
+ /* Set up to 15 bytes. Does not assume earlier memory
+ being set. */
+ tbz count, #3, 1f
+ str A_l, [dst], #8
+1:
+ tbz count, #2, 1f
+ str A_lw, [dst], #4
+1:
+ tbz count, #1, 1f
+ strh A_lw, [dst], #2
+1:
+ ret
+
+ /* Critical loop. Start at a new cache line boundary. Assuming
+ * 64 bytes per line, this ensures the entire loop is in one line. */
+ .p2align 6
+.Lnot_short:
+ neg tmp2, dst
+ ands tmp2, tmp2, #15
+ b.eq 2f
+ /* Bring DST to 128-bit (16-byte) alignment. We know that there's
+ * more than that to set, so we simply store 16 bytes and advance by
+ * the amount required to reach alignment. */
+ sub count, count, tmp2
+ stp A_l, A_l, [dst]
+ add dst, dst, tmp2
+ /* There may be less than 63 bytes to go now. */
+ cmp count, #63
+ b.le .Ltail63
+2:
+ sub dst, dst, #16 /* Pre-bias. */
+ sub count, count, #64
+1:
+ stp A_l, A_l, [dst, #16]
+ stp A_l, A_l, [dst, #32]
+ stp A_l, A_l, [dst, #48]
+ stp A_l, A_l, [dst, #64]!
+ subs count, count, #64
+ b.ge 1b
+ tst count, #0x3f
+ add dst, dst, #16
+ b.ne .Ltail63
+ ret
+
+ /* For zeroing memory, check to see if we can use the ZVA feature to
+ * zero entire 'cache' lines. */
+.Lzero_mem:
+ mov A_l, #0
+ cmp count, #63
+ b.le .Ltail_maybe_tiny
+ neg tmp2, dst
+ ands tmp2, tmp2, #15
+ b.eq 1f
+ sub count, count, tmp2
+ stp A_l, A_l, [dst]
+ add dst, dst, tmp2
+ cmp count, #63
+ b.le .Ltail63
+1:
+ /* For zeroing small amounts of memory, it's not worth setting up
+ * the line-clear code. */
+ cmp count, #128
+ b.lt .Lnot_short
+ mrs tmp1, dczid_el0
+ tbnz tmp1, #4, .Lnot_short
+ mov tmp3w, #4
+ and zva_len, tmp1w, #15 /* Safety: other bits reserved. */
+ lsl zva_len, tmp3w, zva_len
+
+.Lzero_by_line:
+ /* Compute how far we need to go to become suitably aligned. We're
+ * already at quad-word alignment. */
+ cmp count, zva_len_x
+ b.lt .Lnot_short /* Not enough to reach alignment. */
+ sub zva_bits_x, zva_len_x, #1
+ neg tmp2, dst
+ ands tmp2, tmp2, zva_bits_x
+ b.eq 1f /* Already aligned. */
+ /* Not aligned, check that there's enough to copy after alignment. */
+ sub tmp1, count, tmp2
+ cmp tmp1, #64
+ ccmp tmp1, zva_len_x, #8, ge /* NZCV=0b1000 */
+ b.lt .Lnot_short
+ /* We know that there's at least 64 bytes to zero and that it's safe
+ * to overrun by 64 bytes. */
+ mov count, tmp1
+2:
+ stp A_l, A_l, [dst]
+ stp A_l, A_l, [dst, #16]
+ stp A_l, A_l, [dst, #32]
+ subs tmp2, tmp2, #64
+ stp A_l, A_l, [dst, #48]
+ add dst, dst, #64
+ b.ge 2b
+ /* We've overrun a bit, so adjust dst downwards. */
+ add dst, dst, tmp2
+1:
+ sub count, count, zva_len_x
+3:
+ dc zva, dst
+ add dst, dst, zva_len_x
+ subs count, count, zva_len_x
+ b.ge 3b
+ ands count, count, zva_bits_x
+ b.ne .Ltail_maybe_long
+ ret
+END(android_memset32)
diff --git a/libcutils/arch-x86/android_memset16.S b/libcutils/arch-x86/android_memset16.S
old mode 100644
new mode 100755
index f8b79bd..cb2ff14
--- a/libcutils/arch-x86/android_memset16.S
+++ b/libcutils/arch-x86/android_memset16.S
@@ -13,13 +13,707 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-/*
- * Contributed by: Intel Corporation
- */
-# include "cache_wrapper.S"
-# undef __i686
-# define USE_AS_ANDROID
-# define sse2_memset16_atom android_memset16
-# include "sse2-memset16-atom.S"
+#include "cache.h"
+#ifndef MEMSET
+# define MEMSET android_memset16
+#endif
+
+#ifndef L
+# define L(label) .L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n) .p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc .cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc .cfi_endproc
+#endif
+
+#ifndef cfi_rel_offset
+# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
+#endif
+
+#ifndef cfi_restore
+# define cfi_restore(reg) .cfi_restore reg
+#endif
+
+#ifndef cfi_adjust_cfa_offset
+# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name) \
+ .type name, @function; \
+ .globl name; \
+ .p2align 4; \
+name: \
+ cfi_startproc
+#endif
+
+#ifndef END
+# define END(name) \
+ cfi_endproc; \
+ .size name, .-name
+#endif
+
+#define CFI_PUSH(REG) \
+ cfi_adjust_cfa_offset (4); \
+ cfi_rel_offset (REG, 0)
+
+#define CFI_POP(REG) \
+ cfi_adjust_cfa_offset (-4); \
+ cfi_restore (REG)
+
+#define PUSH(REG) pushl REG; CFI_PUSH (REG)
+#define POP(REG) popl REG; CFI_POP (REG)
+
+#ifdef USE_AS_BZERO16
+# define DEST PARMS
+# define LEN DEST+4
+# define SETRTNVAL
+#else
+# define DEST PARMS
+# define CHR DEST+4
+# define LEN CHR+4
+# define SETRTNVAL movl DEST(%esp), %eax
+#endif
+
+#if (defined SHARED || defined __PIC__)
+# define ENTRANCE PUSH (%ebx);
+# define RETURN_END POP (%ebx); ret
+# define RETURN RETURN_END; CFI_PUSH (%ebx)
+# define PARMS 8 /* Preserve EBX. */
+# define JMPTBL(I, B) I - B
+
+/* Load an entry in a jump table into EBX and branch to it. TABLE is a
+ jump table with relative offsets. */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
+ /* We first load PC into EBX. */ \
+ call __x86.get_pc_thunk.bx; \
+ /* Get the address of the jump table. */ \
+ add $(TABLE - .), %ebx; \
+ /* Get the entry and convert the relative offset to the \
+ absolute address. */ \
+ add (%ebx,%ecx,4), %ebx; \
+ /* We loaded the jump table and adjuested EDX. Go. */ \
+ jmp *%ebx
+
+ .section .gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
+ .globl __x86.get_pc_thunk.bx
+ .hidden __x86.get_pc_thunk.bx
+ ALIGN (4)
+ .type __x86.get_pc_thunk.bx,@function
+__x86.get_pc_thunk.bx:
+ movl (%esp), %ebx
+ ret
+#else
+# define ENTRANCE
+# define RETURN_END ret
+# define RETURN RETURN_END
+# define PARMS 4
+# define JMPTBL(I, B) I
+
+/* Branch to an entry in a jump table. TABLE is a jump table with
+ absolute offsets. */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
+ jmp *TABLE(,%ecx,4)
+#endif
+
+ .section .text.sse2,"ax",@progbits
+ ALIGN (4)
+ENTRY (MEMSET)
+ ENTRANCE
+
+ movl LEN(%esp), %ecx
+ shr $1, %ecx
+#ifdef USE_AS_BZERO16
+ xor %eax, %eax
+#else
+ movzwl CHR(%esp), %eax
+ mov %eax, %edx
+ shl $16, %eax
+ or %edx, %eax
+#endif
+ movl DEST(%esp), %edx
+ cmp $32, %ecx
+ jae L(32wordsormore)
+
+L(write_less32words):
+ lea (%edx, %ecx, 2), %edx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_less32words))
+
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_less32words):
+ .int JMPTBL (L(write_0words), L(table_less32words))
+ .int JMPTBL (L(write_1words), L(table_less32words))
+ .int JMPTBL (L(write_2words), L(table_less32words))
+ .int JMPTBL (L(write_3words), L(table_less32words))
+ .int JMPTBL (L(write_4words), L(table_less32words))
+ .int JMPTBL (L(write_5words), L(table_less32words))
+ .int JMPTBL (L(write_6words), L(table_less32words))
+ .int JMPTBL (L(write_7words), L(table_less32words))
+ .int JMPTBL (L(write_8words), L(table_less32words))
+ .int JMPTBL (L(write_9words), L(table_less32words))
+ .int JMPTBL (L(write_10words), L(table_less32words))
+ .int JMPTBL (L(write_11words), L(table_less32words))
+ .int JMPTBL (L(write_12words), L(table_less32words))
+ .int JMPTBL (L(write_13words), L(table_less32words))
+ .int JMPTBL (L(write_14words), L(table_less32words))
+ .int JMPTBL (L(write_15words), L(table_less32words))
+ .int JMPTBL (L(write_16words), L(table_less32words))
+ .int JMPTBL (L(write_17words), L(table_less32words))
+ .int JMPTBL (L(write_18words), L(table_less32words))
+ .int JMPTBL (L(write_19words), L(table_less32words))
+ .int JMPTBL (L(write_20words), L(table_less32words))
+ .int JMPTBL (L(write_21words), L(table_less32words))
+ .int JMPTBL (L(write_22words), L(table_less32words))
+ .int JMPTBL (L(write_23words), L(table_less32words))
+ .int JMPTBL (L(write_24words), L(table_less32words))
+ .int JMPTBL (L(write_25words), L(table_less32words))
+ .int JMPTBL (L(write_26words), L(table_less32words))
+ .int JMPTBL (L(write_27words), L(table_less32words))
+ .int JMPTBL (L(write_28words), L(table_less32words))
+ .int JMPTBL (L(write_29words), L(table_less32words))
+ .int JMPTBL (L(write_30words), L(table_less32words))
+ .int JMPTBL (L(write_31words), L(table_less32words))
+ .popsection
+
+ ALIGN (4)
+L(write_28words):
+ movl %eax, -56(%edx)
+ movl %eax, -52(%edx)
+L(write_24words):
+ movl %eax, -48(%edx)
+ movl %eax, -44(%edx)
+L(write_20words):
+ movl %eax, -40(%edx)
+ movl %eax, -36(%edx)
+L(write_16words):
+ movl %eax, -32(%edx)
+ movl %eax, -28(%edx)
+L(write_12words):
+ movl %eax, -24(%edx)
+ movl %eax, -20(%edx)
+L(write_8words):
+ movl %eax, -16(%edx)
+ movl %eax, -12(%edx)
+L(write_4words):
+ movl %eax, -8(%edx)
+ movl %eax, -4(%edx)
+L(write_0words):
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(write_29words):
+ movl %eax, -58(%edx)
+ movl %eax, -54(%edx)
+L(write_25words):
+ movl %eax, -50(%edx)
+ movl %eax, -46(%edx)
+L(write_21words):
+ movl %eax, -42(%edx)
+ movl %eax, -38(%edx)
+L(write_17words):
+ movl %eax, -34(%edx)
+ movl %eax, -30(%edx)
+L(write_13words):
+ movl %eax, -26(%edx)
+ movl %eax, -22(%edx)
+L(write_9words):
+ movl %eax, -18(%edx)
+ movl %eax, -14(%edx)
+L(write_5words):
+ movl %eax, -10(%edx)
+ movl %eax, -6(%edx)
+L(write_1words):
+ mov %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(write_30words):
+ movl %eax, -60(%edx)
+ movl %eax, -56(%edx)
+L(write_26words):
+ movl %eax, -52(%edx)
+ movl %eax, -48(%edx)
+L(write_22words):
+ movl %eax, -44(%edx)
+ movl %eax, -40(%edx)
+L(write_18words):
+ movl %eax, -36(%edx)
+ movl %eax, -32(%edx)
+L(write_14words):
+ movl %eax, -28(%edx)
+ movl %eax, -24(%edx)
+L(write_10words):
+ movl %eax, -20(%edx)
+ movl %eax, -16(%edx)
+L(write_6words):
+ movl %eax, -12(%edx)
+ movl %eax, -8(%edx)
+L(write_2words):
+ movl %eax, -4(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(write_31words):
+ movl %eax, -62(%edx)
+ movl %eax, -58(%edx)
+L(write_27words):
+ movl %eax, -54(%edx)
+ movl %eax, -50(%edx)
+L(write_23words):
+ movl %eax, -46(%edx)
+ movl %eax, -42(%edx)
+L(write_19words):
+ movl %eax, -38(%edx)
+ movl %eax, -34(%edx)
+L(write_15words):
+ movl %eax, -30(%edx)
+ movl %eax, -26(%edx)
+L(write_11words):
+ movl %eax, -22(%edx)
+ movl %eax, -18(%edx)
+L(write_7words):
+ movl %eax, -14(%edx)
+ movl %eax, -10(%edx)
+L(write_3words):
+ movl %eax, -6(%edx)
+ movw %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+
+L(32wordsormore):
+ shl $1, %ecx
+ test $0x01, %edx
+ jz L(aligned2bytes)
+ mov %eax, (%edx)
+ mov %eax, -4(%edx, %ecx)
+ sub $2, %ecx
+ add $1, %edx
+ rol $8, %eax
+L(aligned2bytes):
+#ifdef USE_AS_BZERO16
+ pxor %xmm0, %xmm0
+#else
+ movd %eax, %xmm0
+ pshufd $0, %xmm0, %xmm0
+#endif
+ testl $0xf, %edx
+ jz L(aligned_16)
+/* ECX > 32 and EDX is not 16 byte aligned. */
+L(not_aligned_16):
+ movdqu %xmm0, (%edx)
+ movl %edx, %eax
+ and $-16, %edx
+ add $16, %edx
+ sub %edx, %eax
+ add %eax, %ecx
+ movd %xmm0, %eax
+
+ ALIGN (4)
+L(aligned_16):
+ cmp $128, %ecx
+ jae L(128bytesormore)
+
+L(aligned_16_less128bytes):
+ add %ecx, %edx
+ shr $1, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ ALIGN (4)
+L(128bytesormore):
+#ifdef SHARED_CACHE_SIZE
+ PUSH (%ebx)
+ mov $SHARED_CACHE_SIZE, %ebx
+#else
+# if (defined SHARED || defined __PIC__)
+ call __x86.get_pc_thunk.bx
+ add $_GLOBAL_OFFSET_TABLE_, %ebx
+ mov __x86_shared_cache_size@GOTOFF(%ebx), %ebx
+# else
+ PUSH (%ebx)
+ mov __x86_shared_cache_size, %ebx
+# endif
+#endif
+ cmp %ebx, %ecx
+ jae L(128bytesormore_nt_start)
+
+
+#ifdef DATA_CACHE_SIZE
+ POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+ cmp $DATA_CACHE_SIZE, %ecx
+#else
+# if (defined SHARED || defined __PIC__)
+# define RESTORE_EBX_STATE
+ call __x86.get_pc_thunk.bx
+ add $_GLOBAL_OFFSET_TABLE_, %ebx
+ cmp __x86_data_cache_size@GOTOFF(%ebx), %ecx
+# else
+ POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+ cmp __x86_data_cache_size, %ecx
+# endif
+#endif
+
+ jae L(128bytes_L2_normal)
+ subl $128, %ecx
+L(128bytesormore_normal):
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ lea 128(%edx), %edx
+ jb L(128bytesless_normal)
+
+
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ lea 128(%edx), %edx
+ jae L(128bytesormore_normal)
+
+L(128bytesless_normal):
+ lea 128(%ecx), %ecx
+ add %ecx, %edx
+ shr $1, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ ALIGN (4)
+L(128bytes_L2_normal):
+ prefetcht0 0x380(%edx)
+ prefetcht0 0x3c0(%edx)
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movaps %xmm0, 0x10(%edx)
+ movaps %xmm0, 0x20(%edx)
+ movaps %xmm0, 0x30(%edx)
+ movaps %xmm0, 0x40(%edx)
+ movaps %xmm0, 0x50(%edx)
+ movaps %xmm0, 0x60(%edx)
+ movaps %xmm0, 0x70(%edx)
+ add $128, %edx
+ cmp $128, %ecx
+ jae L(128bytes_L2_normal)
+
+L(128bytesless_L2_normal):
+ add %ecx, %edx
+ shr $1, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ RESTORE_EBX_STATE
+L(128bytesormore_nt_start):
+ sub %ebx, %ecx
+ mov %ebx, %eax
+ and $0x7f, %eax
+ add %eax, %ecx
+ movd %xmm0, %eax
+ ALIGN (4)
+L(128bytesormore_shared_cache_loop):
+ prefetcht0 0x3c0(%edx)
+ prefetcht0 0x380(%edx)
+ sub $0x80, %ebx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ add $0x80, %edx
+ cmp $0x80, %ebx
+ jae L(128bytesormore_shared_cache_loop)
+ cmp $0x80, %ecx
+ jb L(shared_cache_loop_end)
+ ALIGN (4)
+L(128bytesormore_nt):
+ sub $0x80, %ecx
+ movntdq %xmm0, (%edx)
+ movntdq %xmm0, 0x10(%edx)
+ movntdq %xmm0, 0x20(%edx)
+ movntdq %xmm0, 0x30(%edx)
+ movntdq %xmm0, 0x40(%edx)
+ movntdq %xmm0, 0x50(%edx)
+ movntdq %xmm0, 0x60(%edx)
+ movntdq %xmm0, 0x70(%edx)
+ add $0x80, %edx
+ cmp $0x80, %ecx
+ jae L(128bytesormore_nt)
+ sfence
+L(shared_cache_loop_end):
+#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
+ POP (%ebx)
+#endif
+ add %ecx, %edx
+ shr $1, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_16_128bytes):
+ .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_2bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_6bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_10bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_14bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_18bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_22bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_26bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_30bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_34bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_38bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_42bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_46bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_50bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_54bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_58bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_62bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_66bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_70bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_74bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_78bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_82bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_86bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_90bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_94bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_98bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_102bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_106bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_110bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_114bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_118bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_122bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_126bytes), L(table_16_128bytes))
+ .popsection
+
+
+ ALIGN (4)
+L(aligned_16_112bytes):
+ movdqa %xmm0, -112(%edx)
+L(aligned_16_96bytes):
+ movdqa %xmm0, -96(%edx)
+L(aligned_16_80bytes):
+ movdqa %xmm0, -80(%edx)
+L(aligned_16_64bytes):
+ movdqa %xmm0, -64(%edx)
+L(aligned_16_48bytes):
+ movdqa %xmm0, -48(%edx)
+L(aligned_16_32bytes):
+ movdqa %xmm0, -32(%edx)
+L(aligned_16_16bytes):
+ movdqa %xmm0, -16(%edx)
+L(aligned_16_0bytes):
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_114bytes):
+ movdqa %xmm0, -114(%edx)
+L(aligned_16_98bytes):
+ movdqa %xmm0, -98(%edx)
+L(aligned_16_82bytes):
+ movdqa %xmm0, -82(%edx)
+L(aligned_16_66bytes):
+ movdqa %xmm0, -66(%edx)
+L(aligned_16_50bytes):
+ movdqa %xmm0, -50(%edx)
+L(aligned_16_34bytes):
+ movdqa %xmm0, -34(%edx)
+L(aligned_16_18bytes):
+ movdqa %xmm0, -18(%edx)
+L(aligned_16_2bytes):
+ movw %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(aligned_16_116bytes):
+ movdqa %xmm0, -116(%edx)
+L(aligned_16_100bytes):
+ movdqa %xmm0, -100(%edx)
+L(aligned_16_84bytes):
+ movdqa %xmm0, -84(%edx)
+L(aligned_16_68bytes):
+ movdqa %xmm0, -68(%edx)
+L(aligned_16_52bytes):
+ movdqa %xmm0, -52(%edx)
+L(aligned_16_36bytes):
+ movdqa %xmm0, -36(%edx)
+L(aligned_16_20bytes):
+ movdqa %xmm0, -20(%edx)
+L(aligned_16_4bytes):
+ movl %eax, -4(%edx)
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_118bytes):
+ movdqa %xmm0, -118(%edx)
+L(aligned_16_102bytes):
+ movdqa %xmm0, -102(%edx)
+L(aligned_16_86bytes):
+ movdqa %xmm0, -86(%edx)
+L(aligned_16_70bytes):
+ movdqa %xmm0, -70(%edx)
+L(aligned_16_54bytes):
+ movdqa %xmm0, -54(%edx)
+L(aligned_16_38bytes):
+ movdqa %xmm0, -38(%edx)
+L(aligned_16_22bytes):
+ movdqa %xmm0, -22(%edx)
+L(aligned_16_6bytes):
+ movl %eax, -6(%edx)
+ movw %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_120bytes):
+ movdqa %xmm0, -120(%edx)
+L(aligned_16_104bytes):
+ movdqa %xmm0, -104(%edx)
+L(aligned_16_88bytes):
+ movdqa %xmm0, -88(%edx)
+L(aligned_16_72bytes):
+ movdqa %xmm0, -72(%edx)
+L(aligned_16_56bytes):
+ movdqa %xmm0, -56(%edx)
+L(aligned_16_40bytes):
+ movdqa %xmm0, -40(%edx)
+L(aligned_16_24bytes):
+ movdqa %xmm0, -24(%edx)
+L(aligned_16_8bytes):
+ movq %xmm0, -8(%edx)
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_122bytes):
+ movdqa %xmm0, -122(%edx)
+L(aligned_16_106bytes):
+ movdqa %xmm0, -106(%edx)
+L(aligned_16_90bytes):
+ movdqa %xmm0, -90(%edx)
+L(aligned_16_74bytes):
+ movdqa %xmm0, -74(%edx)
+L(aligned_16_58bytes):
+ movdqa %xmm0, -58(%edx)
+L(aligned_16_42bytes):
+ movdqa %xmm0, -42(%edx)
+L(aligned_16_26bytes):
+ movdqa %xmm0, -26(%edx)
+L(aligned_16_10bytes):
+ movq %xmm0, -10(%edx)
+ movw %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_124bytes):
+ movdqa %xmm0, -124(%edx)
+L(aligned_16_108bytes):
+ movdqa %xmm0, -108(%edx)
+L(aligned_16_92bytes):
+ movdqa %xmm0, -92(%edx)
+L(aligned_16_76bytes):
+ movdqa %xmm0, -76(%edx)
+L(aligned_16_60bytes):
+ movdqa %xmm0, -60(%edx)
+L(aligned_16_44bytes):
+ movdqa %xmm0, -44(%edx)
+L(aligned_16_28bytes):
+ movdqa %xmm0, -28(%edx)
+L(aligned_16_12bytes):
+ movq %xmm0, -12(%edx)
+ movl %eax, -4(%edx)
+ SETRTNVAL
+ RETURN
+
+
+ ALIGN (4)
+L(aligned_16_126bytes):
+ movdqa %xmm0, -126(%edx)
+L(aligned_16_110bytes):
+ movdqa %xmm0, -110(%edx)
+L(aligned_16_94bytes):
+ movdqa %xmm0, -94(%edx)
+L(aligned_16_78bytes):
+ movdqa %xmm0, -78(%edx)
+L(aligned_16_62bytes):
+ movdqa %xmm0, -62(%edx)
+L(aligned_16_46bytes):
+ movdqa %xmm0, -46(%edx)
+L(aligned_16_30bytes):
+ movdqa %xmm0, -30(%edx)
+L(aligned_16_14bytes):
+ movq %xmm0, -14(%edx)
+ movl %eax, -6(%edx)
+ movw %ax, -2(%edx)
+ SETRTNVAL
+ RETURN
+
+END (MEMSET)
diff --git a/libcutils/arch-x86/android_memset32.S b/libcutils/arch-x86/android_memset32.S
old mode 100644
new mode 100755
index 6249fce..f4326dc
--- a/libcutils/arch-x86/android_memset32.S
+++ b/libcutils/arch-x86/android_memset32.S
@@ -13,13 +13,498 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-/*
- * Contributed by: Intel Corporation
- */
-# include "cache_wrapper.S"
-# undef __i686
-# define USE_AS_ANDROID
-# define sse2_memset32_atom android_memset32
-# include "sse2-memset32-atom.S"
+#include "cache.h"
+#ifndef MEMSET
+# define MEMSET android_memset32
+#endif
+
+#ifndef L
+# define L(label) .L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n) .p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc .cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc .cfi_endproc
+#endif
+
+#ifndef cfi_rel_offset
+# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
+#endif
+
+#ifndef cfi_restore
+# define cfi_restore(reg) .cfi_restore reg
+#endif
+
+#ifndef cfi_adjust_cfa_offset
+# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name) \
+ .type name, @function; \
+ .globl name; \
+ .p2align 4; \
+name: \
+ cfi_startproc
+#endif
+
+#ifndef END
+# define END(name) \
+ cfi_endproc; \
+ .size name, .-name
+#endif
+
+#define CFI_PUSH(REG) \
+ cfi_adjust_cfa_offset (4); \
+ cfi_rel_offset (REG, 0)
+
+#define CFI_POP(REG) \
+ cfi_adjust_cfa_offset (-4); \
+ cfi_restore (REG)
+
+#define PUSH(REG) pushl REG; CFI_PUSH (REG)
+#define POP(REG) popl REG; CFI_POP (REG)
+
+#ifdef USE_AS_BZERO32
+# define DEST PARMS
+# define LEN DEST+4
+# define SETRTNVAL
+#else
+# define DEST PARMS
+# define DWDS DEST+4
+# define LEN DWDS+4
+# define SETRTNVAL movl DEST(%esp), %eax
+#endif
+
+#if (defined SHARED || defined __PIC__)
+# define ENTRANCE PUSH (%ebx);
+# define RETURN_END POP (%ebx); ret
+# define RETURN RETURN_END; CFI_PUSH (%ebx)
+# define PARMS 8 /* Preserve EBX. */
+# define JMPTBL(I, B) I - B
+
+/* Load an entry in a jump table into EBX and branch to it. TABLE is a
+ jump table with relative offsets. */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
+ /* We first load PC into EBX. */ \
+ call __x86.get_pc_thunk.bx; \
+ /* Get the address of the jump table. */ \
+ add $(TABLE - .), %ebx; \
+ /* Get the entry and convert the relative offset to the \
+ absolute address. */ \
+ add (%ebx,%ecx,4), %ebx; \
+ /* We loaded the jump table and adjuested EDX. Go. */ \
+ jmp *%ebx
+
+ .section .gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
+ .globl __x86.get_pc_thunk.bx
+ .hidden __x86.get_pc_thunk.bx
+ ALIGN (4)
+ .type __x86.get_pc_thunk.bx,@function
+__x86.get_pc_thunk.bx:
+ movl (%esp), %ebx
+ ret
+#else
+# define ENTRANCE
+# define RETURN_END ret
+# define RETURN RETURN_END
+# define PARMS 4
+# define JMPTBL(I, B) I
+
+/* Branch to an entry in a jump table. TABLE is a jump table with
+ absolute offsets. */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
+ jmp *TABLE(,%ecx,4)
+#endif
+
+ .section .text.sse2,"ax",@progbits
+ ALIGN (4)
+ENTRY (MEMSET)
+ ENTRANCE
+
+ movl LEN(%esp), %ecx
+ shr $2, %ecx
+#ifdef USE_AS_BZERO32
+ xor %eax, %eax
+#else
+ mov DWDS(%esp), %eax
+ mov %eax, %edx
+#endif
+ movl DEST(%esp), %edx
+ cmp $16, %ecx
+ jae L(16dbwordsormore)
+
+L(write_less16dbwords):
+ lea (%edx, %ecx, 4), %edx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_less16dbwords))
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_less16dbwords):
+ .int JMPTBL (L(write_0dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_1dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_2dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_3dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_4dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_5dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_6dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_7dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_8dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_9dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_10dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_11dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_12dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_13dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_14dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_15dbwords), L(table_less16dbwords))
+ .popsection
+
+ ALIGN (4)
+L(write_15dbwords):
+ movl %eax, -60(%edx)
+L(write_14dbwords):
+ movl %eax, -56(%edx)
+L(write_13dbwords):
+ movl %eax, -52(%edx)
+L(write_12dbwords):
+ movl %eax, -48(%edx)
+L(write_11dbwords):
+ movl %eax, -44(%edx)
+L(write_10dbwords):
+ movl %eax, -40(%edx)
+L(write_9dbwords):
+ movl %eax, -36(%edx)
+L(write_8dbwords):
+ movl %eax, -32(%edx)
+L(write_7dbwords):
+ movl %eax, -28(%edx)
+L(write_6dbwords):
+ movl %eax, -24(%edx)
+L(write_5dbwords):
+ movl %eax, -20(%edx)
+L(write_4dbwords):
+ movl %eax, -16(%edx)
+L(write_3dbwords):
+ movl %eax, -12(%edx)
+L(write_2dbwords):
+ movl %eax, -8(%edx)
+L(write_1dbwords):
+ movl %eax, -4(%edx)
+L(write_0dbwords):
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(16dbwordsormore):
+ test $3, %edx
+ jz L(aligned4bytes)
+ mov %eax, (%edx)
+ mov %eax, -4(%edx, %ecx, 4)
+ sub $1, %ecx
+ rol $24, %eax
+ add $1, %edx
+ test $3, %edx
+ jz L(aligned4bytes)
+ ror $8, %eax
+ add $1, %edx
+ test $3, %edx
+ jz L(aligned4bytes)
+ ror $8, %eax
+ add $1, %edx
+L(aligned4bytes):
+ shl $2, %ecx
+
+#ifdef USE_AS_BZERO32
+ pxor %xmm0, %xmm0
+#else
+ movd %eax, %xmm0
+ pshufd $0, %xmm0, %xmm0
+#endif
+ testl $0xf, %edx
+ jz L(aligned_16)
+/* ECX > 32 and EDX is not 16 byte aligned. */
+L(not_aligned_16):
+ movdqu %xmm0, (%edx)
+ movl %edx, %eax
+ and $-16, %edx
+ add $16, %edx
+ sub %edx, %eax
+ add %eax, %ecx
+ movd %xmm0, %eax
+ ALIGN (4)
+L(aligned_16):
+ cmp $128, %ecx
+ jae L(128bytesormore)
+
+L(aligned_16_less128bytes):
+ add %ecx, %edx
+ shr $2, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ ALIGN (4)
+L(128bytesormore):
+#ifdef SHARED_CACHE_SIZE
+ PUSH (%ebx)
+ mov $SHARED_CACHE_SIZE, %ebx
+#else
+# if (defined SHARED || defined __PIC__)
+ call __x86.get_pc_thunk.bx
+ add $_GLOBAL_OFFSET_TABLE_, %ebx
+ mov __x86_shared_cache_size@GOTOFF(%ebx), %ebx
+# else
+ PUSH (%ebx)
+ mov __x86_shared_cache_size, %ebx
+# endif
+#endif
+ cmp %ebx, %ecx
+ jae L(128bytesormore_nt_start)
+
+#ifdef DATA_CACHE_SIZE
+ POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+ cmp $DATA_CACHE_SIZE, %ecx
+#else
+# if (defined SHARED || defined __PIC__)
+# define RESTORE_EBX_STATE
+ call __x86.get_pc_thunk.bx
+ add $_GLOBAL_OFFSET_TABLE_, %ebx
+ cmp __x86_data_cache_size@GOTOFF(%ebx), %ecx
+# else
+ POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+ cmp __x86_data_cache_size, %ecx
+# endif
+#endif
+
+ jae L(128bytes_L2_normal)
+ subl $128, %ecx
+L(128bytesormore_normal):
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ lea 128(%edx), %edx
+ jb L(128bytesless_normal)
+
+
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ lea 128(%edx), %edx
+ jae L(128bytesormore_normal)
+
+L(128bytesless_normal):
+ lea 128(%ecx), %ecx
+ add %ecx, %edx
+ shr $2, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ ALIGN (4)
+L(128bytes_L2_normal):
+ prefetcht0 0x380(%edx)
+ prefetcht0 0x3c0(%edx)
+ sub $128, %ecx
+ movdqa %xmm0, (%edx)
+ movaps %xmm0, 0x10(%edx)
+ movaps %xmm0, 0x20(%edx)
+ movaps %xmm0, 0x30(%edx)
+ movaps %xmm0, 0x40(%edx)
+ movaps %xmm0, 0x50(%edx)
+ movaps %xmm0, 0x60(%edx)
+ movaps %xmm0, 0x70(%edx)
+ add $128, %edx
+ cmp $128, %ecx
+ jae L(128bytes_L2_normal)
+
+L(128bytesless_L2_normal):
+ add %ecx, %edx
+ shr $2, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ RESTORE_EBX_STATE
+L(128bytesormore_nt_start):
+ sub %ebx, %ecx
+ mov %ebx, %eax
+ and $0x7f, %eax
+ add %eax, %ecx
+ movd %xmm0, %eax
+ ALIGN (4)
+L(128bytesormore_shared_cache_loop):
+ prefetcht0 0x3c0(%edx)
+ prefetcht0 0x380(%edx)
+ sub $0x80, %ebx
+ movdqa %xmm0, (%edx)
+ movdqa %xmm0, 0x10(%edx)
+ movdqa %xmm0, 0x20(%edx)
+ movdqa %xmm0, 0x30(%edx)
+ movdqa %xmm0, 0x40(%edx)
+ movdqa %xmm0, 0x50(%edx)
+ movdqa %xmm0, 0x60(%edx)
+ movdqa %xmm0, 0x70(%edx)
+ add $0x80, %edx
+ cmp $0x80, %ebx
+ jae L(128bytesormore_shared_cache_loop)
+ cmp $0x80, %ecx
+ jb L(shared_cache_loop_end)
+
+ ALIGN (4)
+L(128bytesormore_nt):
+ sub $0x80, %ecx
+ movntdq %xmm0, (%edx)
+ movntdq %xmm0, 0x10(%edx)
+ movntdq %xmm0, 0x20(%edx)
+ movntdq %xmm0, 0x30(%edx)
+ movntdq %xmm0, 0x40(%edx)
+ movntdq %xmm0, 0x50(%edx)
+ movntdq %xmm0, 0x60(%edx)
+ movntdq %xmm0, 0x70(%edx)
+ add $0x80, %edx
+ cmp $0x80, %ecx
+ jae L(128bytesormore_nt)
+ sfence
+L(shared_cache_loop_end):
+#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
+ POP (%ebx)
+#endif
+ add %ecx, %edx
+ shr $2, %ecx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_16_128bytes):
+ .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+ .popsection
+
+ ALIGN (4)
+L(aligned_16_112bytes):
+ movdqa %xmm0, -112(%edx)
+L(aligned_16_96bytes):
+ movdqa %xmm0, -96(%edx)
+L(aligned_16_80bytes):
+ movdqa %xmm0, -80(%edx)
+L(aligned_16_64bytes):
+ movdqa %xmm0, -64(%edx)
+L(aligned_16_48bytes):
+ movdqa %xmm0, -48(%edx)
+L(aligned_16_32bytes):
+ movdqa %xmm0, -32(%edx)
+L(aligned_16_16bytes):
+ movdqa %xmm0, -16(%edx)
+L(aligned_16_0bytes):
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(aligned_16_116bytes):
+ movdqa %xmm0, -116(%edx)
+L(aligned_16_100bytes):
+ movdqa %xmm0, -100(%edx)
+L(aligned_16_84bytes):
+ movdqa %xmm0, -84(%edx)
+L(aligned_16_68bytes):
+ movdqa %xmm0, -68(%edx)
+L(aligned_16_52bytes):
+ movdqa %xmm0, -52(%edx)
+L(aligned_16_36bytes):
+ movdqa %xmm0, -36(%edx)
+L(aligned_16_20bytes):
+ movdqa %xmm0, -20(%edx)
+L(aligned_16_4bytes):
+ movl %eax, -4(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(aligned_16_120bytes):
+ movdqa %xmm0, -120(%edx)
+L(aligned_16_104bytes):
+ movdqa %xmm0, -104(%edx)
+L(aligned_16_88bytes):
+ movdqa %xmm0, -88(%edx)
+L(aligned_16_72bytes):
+ movdqa %xmm0, -72(%edx)
+L(aligned_16_56bytes):
+ movdqa %xmm0, -56(%edx)
+L(aligned_16_40bytes):
+ movdqa %xmm0, -40(%edx)
+L(aligned_16_24bytes):
+ movdqa %xmm0, -24(%edx)
+L(aligned_16_8bytes):
+ movq %xmm0, -8(%edx)
+ SETRTNVAL
+ RETURN
+
+ ALIGN (4)
+L(aligned_16_124bytes):
+ movdqa %xmm0, -124(%edx)
+L(aligned_16_108bytes):
+ movdqa %xmm0, -108(%edx)
+L(aligned_16_92bytes):
+ movdqa %xmm0, -92(%edx)
+L(aligned_16_76bytes):
+ movdqa %xmm0, -76(%edx)
+L(aligned_16_60bytes):
+ movdqa %xmm0, -60(%edx)
+L(aligned_16_44bytes):
+ movdqa %xmm0, -44(%edx)
+L(aligned_16_28bytes):
+ movdqa %xmm0, -28(%edx)
+L(aligned_16_12bytes):
+ movq %xmm0, -12(%edx)
+ movl %eax, -4(%edx)
+ SETRTNVAL
+ RETURN
+
+END (MEMSET)
diff --git a/libcutils/arch-x86/cache_wrapper.S b/libcutils/arch-x86/cache.h
similarity index 69%
rename from libcutils/arch-x86/cache_wrapper.S
rename to libcutils/arch-x86/cache.h
index 508fdd3..1c22fea 100644
--- a/libcutils/arch-x86/cache_wrapper.S
+++ b/libcutils/arch-x86/cache.h
@@ -13,12 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-/*
- * Contributed by: Intel Corporation
- */
+#if defined(__slm__)
+/* Values are optimized for Silvermont */
+#define SHARED_CACHE_SIZE (1024*1024) /* Silvermont L2 Cache */
+#define DATA_CACHE_SIZE (24*1024) /* Silvermont L1 Data Cache */
+#else
/* Values are optimized for Atom */
-#define SHARED_CACHE_SIZE (512*1024) /* Atom L2 Cache */
-#define DATA_CACHE_SIZE (24*1024) /* Atom L1 Data Cache */
+#define SHARED_CACHE_SIZE (512*1024) /* Atom L2 Cache */
+#define DATA_CACHE_SIZE (24*1024) /* Atom L1 Data Cache */
+#endif
+
#define SHARED_CACHE_SIZE_HALF (SHARED_CACHE_SIZE / 2)
#define DATA_CACHE_SIZE_HALF (DATA_CACHE_SIZE / 2)
diff --git a/libcutils/arch-x86/sse2-memset16-atom.S b/libcutils/arch-x86/sse2-memset16-atom.S
deleted file mode 100755
index c2a762b..0000000
--- a/libcutils/arch-x86/sse2-memset16-atom.S
+++ /dev/null
@@ -1,722 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-/*
- * Contributed by: Intel Corporation
- */
-
-#ifndef L
-# define L(label) .L##label
-#endif
-
-#ifndef ALIGN
-# define ALIGN(n) .p2align n
-#endif
-
-#ifndef cfi_startproc
-# define cfi_startproc .cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc .cfi_endproc
-#endif
-
-#ifndef cfi_rel_offset
-# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
-#endif
-
-#ifndef cfi_restore
-# define cfi_restore(reg) .cfi_restore reg
-#endif
-
-#ifndef cfi_adjust_cfa_offset
-# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
-#endif
-
-#ifndef ENTRY
-# define ENTRY(name) \
- .type name, @function; \
- .globl name; \
- .p2align 4; \
-name: \
- cfi_startproc
-#endif
-
-#ifndef END
-# define END(name) \
- cfi_endproc; \
- .size name, .-name
-#endif
-
-#define CFI_PUSH(REG) \
- cfi_adjust_cfa_offset (4); \
- cfi_rel_offset (REG, 0)
-
-#define CFI_POP(REG) \
- cfi_adjust_cfa_offset (-4); \
- cfi_restore (REG)
-
-#define PUSH(REG) pushl REG; CFI_PUSH (REG)
-#define POP(REG) popl REG; CFI_POP (REG)
-
-#ifdef USE_AS_BZERO16
-# define DEST PARMS
-# define LEN DEST+4
-#else
-# define DEST PARMS
-# define CHR DEST+4
-# define LEN CHR+4
-#endif
-
-#if 1
-# define SETRTNVAL
-#else
-# define SETRTNVAL movl DEST(%esp), %eax
-#endif
-
-#if (defined SHARED || defined __PIC__)
-# define ENTRANCE PUSH (%ebx);
-# define RETURN_END POP (%ebx); ret
-# define RETURN RETURN_END; CFI_PUSH (%ebx)
-# define PARMS 8 /* Preserve EBX. */
-# define JMPTBL(I, B) I - B
-
-/* Load an entry in a jump table into EBX and branch to it. TABLE is a
- jump table with relative offsets. */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
- /* We first load PC into EBX. */ \
- call __i686.get_pc_thunk.bx; \
- /* Get the address of the jump table. */ \
- add $(TABLE - .), %ebx; \
- /* Get the entry and convert the relative offset to the \
- absolute address. */ \
- add (%ebx,%ecx,4), %ebx; \
- /* We loaded the jump table and adjuested EDX. Go. */ \
- jmp *%ebx
-
- .section .gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
- .globl __i686.get_pc_thunk.bx
- .hidden __i686.get_pc_thunk.bx
- ALIGN (4)
- .type __i686.get_pc_thunk.bx,@function
-__i686.get_pc_thunk.bx:
- movl (%esp), %ebx
- ret
-#else
-# define ENTRANCE
-# define RETURN_END ret
-# define RETURN RETURN_END
-# define PARMS 4
-# define JMPTBL(I, B) I
-
-/* Branch to an entry in a jump table. TABLE is a jump table with
- absolute offsets. */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
- jmp *TABLE(,%ecx,4)
-#endif
-
- .section .text.sse2,"ax",@progbits
- ALIGN (4)
-ENTRY (sse2_memset16_atom)
- ENTRANCE
-
- movl LEN(%esp), %ecx
-#ifdef USE_AS_ANDROID
- shr $1, %ecx
-#endif
-#ifdef USE_AS_BZERO16
- xor %eax, %eax
-#else
- movzwl CHR(%esp), %eax
- mov %eax, %edx
- shl $16, %eax
- or %edx, %eax
-#endif
- movl DEST(%esp), %edx
- cmp $32, %ecx
- jae L(32wordsormore)
-
-L(write_less32words):
- lea (%edx, %ecx, 2), %edx
- BRANCH_TO_JMPTBL_ENTRY (L(table_less32words))
-
-
- .pushsection .rodata.sse2,"a",@progbits
- ALIGN (2)
-L(table_less32words):
- .int JMPTBL (L(write_0words), L(table_less32words))
- .int JMPTBL (L(write_1words), L(table_less32words))
- .int JMPTBL (L(write_2words), L(table_less32words))
- .int JMPTBL (L(write_3words), L(table_less32words))
- .int JMPTBL (L(write_4words), L(table_less32words))
- .int JMPTBL (L(write_5words), L(table_less32words))
- .int JMPTBL (L(write_6words), L(table_less32words))
- .int JMPTBL (L(write_7words), L(table_less32words))
- .int JMPTBL (L(write_8words), L(table_less32words))
- .int JMPTBL (L(write_9words), L(table_less32words))
- .int JMPTBL (L(write_10words), L(table_less32words))
- .int JMPTBL (L(write_11words), L(table_less32words))
- .int JMPTBL (L(write_12words), L(table_less32words))
- .int JMPTBL (L(write_13words), L(table_less32words))
- .int JMPTBL (L(write_14words), L(table_less32words))
- .int JMPTBL (L(write_15words), L(table_less32words))
- .int JMPTBL (L(write_16words), L(table_less32words))
- .int JMPTBL (L(write_17words), L(table_less32words))
- .int JMPTBL (L(write_18words), L(table_less32words))
- .int JMPTBL (L(write_19words), L(table_less32words))
- .int JMPTBL (L(write_20words), L(table_less32words))
- .int JMPTBL (L(write_21words), L(table_less32words))
- .int JMPTBL (L(write_22words), L(table_less32words))
- .int JMPTBL (L(write_23words), L(table_less32words))
- .int JMPTBL (L(write_24words), L(table_less32words))
- .int JMPTBL (L(write_25words), L(table_less32words))
- .int JMPTBL (L(write_26words), L(table_less32words))
- .int JMPTBL (L(write_27words), L(table_less32words))
- .int JMPTBL (L(write_28words), L(table_less32words))
- .int JMPTBL (L(write_29words), L(table_less32words))
- .int JMPTBL (L(write_30words), L(table_less32words))
- .int JMPTBL (L(write_31words), L(table_less32words))
- .popsection
-
- ALIGN (4)
-L(write_28words):
- movl %eax, -56(%edx)
- movl %eax, -52(%edx)
-L(write_24words):
- movl %eax, -48(%edx)
- movl %eax, -44(%edx)
-L(write_20words):
- movl %eax, -40(%edx)
- movl %eax, -36(%edx)
-L(write_16words):
- movl %eax, -32(%edx)
- movl %eax, -28(%edx)
-L(write_12words):
- movl %eax, -24(%edx)
- movl %eax, -20(%edx)
-L(write_8words):
- movl %eax, -16(%edx)
- movl %eax, -12(%edx)
-L(write_4words):
- movl %eax, -8(%edx)
- movl %eax, -4(%edx)
-L(write_0words):
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(write_29words):
- movl %eax, -58(%edx)
- movl %eax, -54(%edx)
-L(write_25words):
- movl %eax, -50(%edx)
- movl %eax, -46(%edx)
-L(write_21words):
- movl %eax, -42(%edx)
- movl %eax, -38(%edx)
-L(write_17words):
- movl %eax, -34(%edx)
- movl %eax, -30(%edx)
-L(write_13words):
- movl %eax, -26(%edx)
- movl %eax, -22(%edx)
-L(write_9words):
- movl %eax, -18(%edx)
- movl %eax, -14(%edx)
-L(write_5words):
- movl %eax, -10(%edx)
- movl %eax, -6(%edx)
-L(write_1words):
- mov %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(write_30words):
- movl %eax, -60(%edx)
- movl %eax, -56(%edx)
-L(write_26words):
- movl %eax, -52(%edx)
- movl %eax, -48(%edx)
-L(write_22words):
- movl %eax, -44(%edx)
- movl %eax, -40(%edx)
-L(write_18words):
- movl %eax, -36(%edx)
- movl %eax, -32(%edx)
-L(write_14words):
- movl %eax, -28(%edx)
- movl %eax, -24(%edx)
-L(write_10words):
- movl %eax, -20(%edx)
- movl %eax, -16(%edx)
-L(write_6words):
- movl %eax, -12(%edx)
- movl %eax, -8(%edx)
-L(write_2words):
- movl %eax, -4(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(write_31words):
- movl %eax, -62(%edx)
- movl %eax, -58(%edx)
-L(write_27words):
- movl %eax, -54(%edx)
- movl %eax, -50(%edx)
-L(write_23words):
- movl %eax, -46(%edx)
- movl %eax, -42(%edx)
-L(write_19words):
- movl %eax, -38(%edx)
- movl %eax, -34(%edx)
-L(write_15words):
- movl %eax, -30(%edx)
- movl %eax, -26(%edx)
-L(write_11words):
- movl %eax, -22(%edx)
- movl %eax, -18(%edx)
-L(write_7words):
- movl %eax, -14(%edx)
- movl %eax, -10(%edx)
-L(write_3words):
- movl %eax, -6(%edx)
- movw %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-
-L(32wordsormore):
- shl $1, %ecx
- test $0x01, %edx
- jz L(aligned2bytes)
- mov %eax, (%edx)
- mov %eax, -4(%edx, %ecx)
- sub $2, %ecx
- add $1, %edx
- rol $8, %eax
-L(aligned2bytes):
-#ifdef USE_AS_BZERO16
- pxor %xmm0, %xmm0
-#else
- movd %eax, %xmm0
- pshufd $0, %xmm0, %xmm0
-#endif
- testl $0xf, %edx
- jz L(aligned_16)
-/* ECX > 32 and EDX is not 16 byte aligned. */
-L(not_aligned_16):
- movdqu %xmm0, (%edx)
- movl %edx, %eax
- and $-16, %edx
- add $16, %edx
- sub %edx, %eax
- add %eax, %ecx
- movd %xmm0, %eax
-
- ALIGN (4)
-L(aligned_16):
- cmp $128, %ecx
- jae L(128bytesormore)
-
-L(aligned_16_less128bytes):
- add %ecx, %edx
- shr $1, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- ALIGN (4)
-L(128bytesormore):
-#ifdef SHARED_CACHE_SIZE
- PUSH (%ebx)
- mov $SHARED_CACHE_SIZE, %ebx
-#else
-# if (defined SHARED || defined __PIC__)
- call __i686.get_pc_thunk.bx
- add $_GLOBAL_OFFSET_TABLE_, %ebx
- mov __x86_shared_cache_size@GOTOFF(%ebx), %ebx
-# else
- PUSH (%ebx)
- mov __x86_shared_cache_size, %ebx
-# endif
-#endif
- cmp %ebx, %ecx
- jae L(128bytesormore_nt_start)
-
-
-#ifdef DATA_CACHE_SIZE
- POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
- cmp $DATA_CACHE_SIZE, %ecx
-#else
-# if (defined SHARED || defined __PIC__)
-# define RESTORE_EBX_STATE
- call __i686.get_pc_thunk.bx
- add $_GLOBAL_OFFSET_TABLE_, %ebx
- cmp __x86_data_cache_size@GOTOFF(%ebx), %ecx
-# else
- POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
- cmp __x86_data_cache_size, %ecx
-# endif
-#endif
-
- jae L(128bytes_L2_normal)
- subl $128, %ecx
-L(128bytesormore_normal):
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- lea 128(%edx), %edx
- jb L(128bytesless_normal)
-
-
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- lea 128(%edx), %edx
- jae L(128bytesormore_normal)
-
-L(128bytesless_normal):
- lea 128(%ecx), %ecx
- add %ecx, %edx
- shr $1, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- ALIGN (4)
-L(128bytes_L2_normal):
- prefetcht0 0x380(%edx)
- prefetcht0 0x3c0(%edx)
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movaps %xmm0, 0x10(%edx)
- movaps %xmm0, 0x20(%edx)
- movaps %xmm0, 0x30(%edx)
- movaps %xmm0, 0x40(%edx)
- movaps %xmm0, 0x50(%edx)
- movaps %xmm0, 0x60(%edx)
- movaps %xmm0, 0x70(%edx)
- add $128, %edx
- cmp $128, %ecx
- jae L(128bytes_L2_normal)
-
-L(128bytesless_L2_normal):
- add %ecx, %edx
- shr $1, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- RESTORE_EBX_STATE
-L(128bytesormore_nt_start):
- sub %ebx, %ecx
- mov %ebx, %eax
- and $0x7f, %eax
- add %eax, %ecx
- movd %xmm0, %eax
- ALIGN (4)
-L(128bytesormore_shared_cache_loop):
- prefetcht0 0x3c0(%edx)
- prefetcht0 0x380(%edx)
- sub $0x80, %ebx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- add $0x80, %edx
- cmp $0x80, %ebx
- jae L(128bytesormore_shared_cache_loop)
- cmp $0x80, %ecx
- jb L(shared_cache_loop_end)
- ALIGN (4)
-L(128bytesormore_nt):
- sub $0x80, %ecx
- movntdq %xmm0, (%edx)
- movntdq %xmm0, 0x10(%edx)
- movntdq %xmm0, 0x20(%edx)
- movntdq %xmm0, 0x30(%edx)
- movntdq %xmm0, 0x40(%edx)
- movntdq %xmm0, 0x50(%edx)
- movntdq %xmm0, 0x60(%edx)
- movntdq %xmm0, 0x70(%edx)
- add $0x80, %edx
- cmp $0x80, %ecx
- jae L(128bytesormore_nt)
- sfence
-L(shared_cache_loop_end):
-#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
- POP (%ebx)
-#endif
- add %ecx, %edx
- shr $1, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-
- .pushsection .rodata.sse2,"a",@progbits
- ALIGN (2)
-L(table_16_128bytes):
- .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_2bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_6bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_10bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_14bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_18bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_22bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_26bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_30bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_34bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_38bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_42bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_46bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_50bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_54bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_58bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_62bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_66bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_70bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_74bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_78bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_82bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_86bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_90bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_94bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_98bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_102bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_106bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_110bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_114bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_118bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_122bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_126bytes), L(table_16_128bytes))
- .popsection
-
-
- ALIGN (4)
-L(aligned_16_112bytes):
- movdqa %xmm0, -112(%edx)
-L(aligned_16_96bytes):
- movdqa %xmm0, -96(%edx)
-L(aligned_16_80bytes):
- movdqa %xmm0, -80(%edx)
-L(aligned_16_64bytes):
- movdqa %xmm0, -64(%edx)
-L(aligned_16_48bytes):
- movdqa %xmm0, -48(%edx)
-L(aligned_16_32bytes):
- movdqa %xmm0, -32(%edx)
-L(aligned_16_16bytes):
- movdqa %xmm0, -16(%edx)
-L(aligned_16_0bytes):
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_114bytes):
- movdqa %xmm0, -114(%edx)
-L(aligned_16_98bytes):
- movdqa %xmm0, -98(%edx)
-L(aligned_16_82bytes):
- movdqa %xmm0, -82(%edx)
-L(aligned_16_66bytes):
- movdqa %xmm0, -66(%edx)
-L(aligned_16_50bytes):
- movdqa %xmm0, -50(%edx)
-L(aligned_16_34bytes):
- movdqa %xmm0, -34(%edx)
-L(aligned_16_18bytes):
- movdqa %xmm0, -18(%edx)
-L(aligned_16_2bytes):
- movw %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(aligned_16_116bytes):
- movdqa %xmm0, -116(%edx)
-L(aligned_16_100bytes):
- movdqa %xmm0, -100(%edx)
-L(aligned_16_84bytes):
- movdqa %xmm0, -84(%edx)
-L(aligned_16_68bytes):
- movdqa %xmm0, -68(%edx)
-L(aligned_16_52bytes):
- movdqa %xmm0, -52(%edx)
-L(aligned_16_36bytes):
- movdqa %xmm0, -36(%edx)
-L(aligned_16_20bytes):
- movdqa %xmm0, -20(%edx)
-L(aligned_16_4bytes):
- movl %eax, -4(%edx)
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_118bytes):
- movdqa %xmm0, -118(%edx)
-L(aligned_16_102bytes):
- movdqa %xmm0, -102(%edx)
-L(aligned_16_86bytes):
- movdqa %xmm0, -86(%edx)
-L(aligned_16_70bytes):
- movdqa %xmm0, -70(%edx)
-L(aligned_16_54bytes):
- movdqa %xmm0, -54(%edx)
-L(aligned_16_38bytes):
- movdqa %xmm0, -38(%edx)
-L(aligned_16_22bytes):
- movdqa %xmm0, -22(%edx)
-L(aligned_16_6bytes):
- movl %eax, -6(%edx)
- movw %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_120bytes):
- movdqa %xmm0, -120(%edx)
-L(aligned_16_104bytes):
- movdqa %xmm0, -104(%edx)
-L(aligned_16_88bytes):
- movdqa %xmm0, -88(%edx)
-L(aligned_16_72bytes):
- movdqa %xmm0, -72(%edx)
-L(aligned_16_56bytes):
- movdqa %xmm0, -56(%edx)
-L(aligned_16_40bytes):
- movdqa %xmm0, -40(%edx)
-L(aligned_16_24bytes):
- movdqa %xmm0, -24(%edx)
-L(aligned_16_8bytes):
- movq %xmm0, -8(%edx)
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_122bytes):
- movdqa %xmm0, -122(%edx)
-L(aligned_16_106bytes):
- movdqa %xmm0, -106(%edx)
-L(aligned_16_90bytes):
- movdqa %xmm0, -90(%edx)
-L(aligned_16_74bytes):
- movdqa %xmm0, -74(%edx)
-L(aligned_16_58bytes):
- movdqa %xmm0, -58(%edx)
-L(aligned_16_42bytes):
- movdqa %xmm0, -42(%edx)
-L(aligned_16_26bytes):
- movdqa %xmm0, -26(%edx)
-L(aligned_16_10bytes):
- movq %xmm0, -10(%edx)
- movw %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_124bytes):
- movdqa %xmm0, -124(%edx)
-L(aligned_16_108bytes):
- movdqa %xmm0, -108(%edx)
-L(aligned_16_92bytes):
- movdqa %xmm0, -92(%edx)
-L(aligned_16_76bytes):
- movdqa %xmm0, -76(%edx)
-L(aligned_16_60bytes):
- movdqa %xmm0, -60(%edx)
-L(aligned_16_44bytes):
- movdqa %xmm0, -44(%edx)
-L(aligned_16_28bytes):
- movdqa %xmm0, -28(%edx)
-L(aligned_16_12bytes):
- movq %xmm0, -12(%edx)
- movl %eax, -4(%edx)
- SETRTNVAL
- RETURN
-
-
- ALIGN (4)
-L(aligned_16_126bytes):
- movdqa %xmm0, -126(%edx)
-L(aligned_16_110bytes):
- movdqa %xmm0, -110(%edx)
-L(aligned_16_94bytes):
- movdqa %xmm0, -94(%edx)
-L(aligned_16_78bytes):
- movdqa %xmm0, -78(%edx)
-L(aligned_16_62bytes):
- movdqa %xmm0, -62(%edx)
-L(aligned_16_46bytes):
- movdqa %xmm0, -46(%edx)
-L(aligned_16_30bytes):
- movdqa %xmm0, -30(%edx)
-L(aligned_16_14bytes):
- movq %xmm0, -14(%edx)
- movl %eax, -6(%edx)
- movw %ax, -2(%edx)
- SETRTNVAL
- RETURN
-
-END (sse2_memset16_atom)
diff --git a/libcutils/arch-x86/sse2-memset32-atom.S b/libcutils/arch-x86/sse2-memset32-atom.S
deleted file mode 100755
index 05eb64f..0000000
--- a/libcutils/arch-x86/sse2-memset32-atom.S
+++ /dev/null
@@ -1,513 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-/*
- * Contributed by: Intel Corporation
- */
-
-#ifndef L
-# define L(label) .L##label
-#endif
-
-#ifndef ALIGN
-# define ALIGN(n) .p2align n
-#endif
-
-#ifndef cfi_startproc
-# define cfi_startproc .cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc .cfi_endproc
-#endif
-
-#ifndef cfi_rel_offset
-# define cfi_rel_offset(reg, off) .cfi_rel_offset reg, off
-#endif
-
-#ifndef cfi_restore
-# define cfi_restore(reg) .cfi_restore reg
-#endif
-
-#ifndef cfi_adjust_cfa_offset
-# define cfi_adjust_cfa_offset(off) .cfi_adjust_cfa_offset off
-#endif
-
-#ifndef ENTRY
-# define ENTRY(name) \
- .type name, @function; \
- .globl name; \
- .p2align 4; \
-name: \
- cfi_startproc
-#endif
-
-#ifndef END
-# define END(name) \
- cfi_endproc; \
- .size name, .-name
-#endif
-
-#define CFI_PUSH(REG) \
- cfi_adjust_cfa_offset (4); \
- cfi_rel_offset (REG, 0)
-
-#define CFI_POP(REG) \
- cfi_adjust_cfa_offset (-4); \
- cfi_restore (REG)
-
-#define PUSH(REG) pushl REG; CFI_PUSH (REG)
-#define POP(REG) popl REG; CFI_POP (REG)
-
-#ifdef USE_AS_BZERO32
-# define DEST PARMS
-# define LEN DEST+4
-#else
-# define DEST PARMS
-# define DWDS DEST+4
-# define LEN DWDS+4
-#endif
-
-#ifdef USE_AS_WMEMSET32
-# define SETRTNVAL movl DEST(%esp), %eax
-#else
-# define SETRTNVAL
-#endif
-
-#if (defined SHARED || defined __PIC__)
-# define ENTRANCE PUSH (%ebx);
-# define RETURN_END POP (%ebx); ret
-# define RETURN RETURN_END; CFI_PUSH (%ebx)
-# define PARMS 8 /* Preserve EBX. */
-# define JMPTBL(I, B) I - B
-
-/* Load an entry in a jump table into EBX and branch to it. TABLE is a
- jump table with relative offsets. */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
- /* We first load PC into EBX. */ \
- call __i686.get_pc_thunk.bx; \
- /* Get the address of the jump table. */ \
- add $(TABLE - .), %ebx; \
- /* Get the entry and convert the relative offset to the \
- absolute address. */ \
- add (%ebx,%ecx,4), %ebx; \
- /* We loaded the jump table and adjuested EDX. Go. */ \
- jmp *%ebx
-
- .section .gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
- .globl __i686.get_pc_thunk.bx
- .hidden __i686.get_pc_thunk.bx
- ALIGN (4)
- .type __i686.get_pc_thunk.bx,@function
-__i686.get_pc_thunk.bx:
- movl (%esp), %ebx
- ret
-#else
-# define ENTRANCE
-# define RETURN_END ret
-# define RETURN RETURN_END
-# define PARMS 4
-# define JMPTBL(I, B) I
-
-/* Branch to an entry in a jump table. TABLE is a jump table with
- absolute offsets. */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE) \
- jmp *TABLE(,%ecx,4)
-#endif
-
- .section .text.sse2,"ax",@progbits
- ALIGN (4)
-ENTRY (sse2_memset32_atom)
- ENTRANCE
-
- movl LEN(%esp), %ecx
-#ifdef USE_AS_ANDROID
- shr $2, %ecx
-#endif
-#ifdef USE_AS_BZERO32
- xor %eax, %eax
-#else
- mov DWDS(%esp), %eax
- mov %eax, %edx
-#endif
- movl DEST(%esp), %edx
- cmp $16, %ecx
- jae L(16dbwordsormore)
-
-L(write_less16dbwords):
- lea (%edx, %ecx, 4), %edx
- BRANCH_TO_JMPTBL_ENTRY (L(table_less16dbwords))
-
- .pushsection .rodata.sse2,"a",@progbits
- ALIGN (2)
-L(table_less16dbwords):
- .int JMPTBL (L(write_0dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_1dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_2dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_3dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_4dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_5dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_6dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_7dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_8dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_9dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_10dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_11dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_12dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_13dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_14dbwords), L(table_less16dbwords))
- .int JMPTBL (L(write_15dbwords), L(table_less16dbwords))
- .popsection
-
- ALIGN (4)
-L(write_15dbwords):
- movl %eax, -60(%edx)
-L(write_14dbwords):
- movl %eax, -56(%edx)
-L(write_13dbwords):
- movl %eax, -52(%edx)
-L(write_12dbwords):
- movl %eax, -48(%edx)
-L(write_11dbwords):
- movl %eax, -44(%edx)
-L(write_10dbwords):
- movl %eax, -40(%edx)
-L(write_9dbwords):
- movl %eax, -36(%edx)
-L(write_8dbwords):
- movl %eax, -32(%edx)
-L(write_7dbwords):
- movl %eax, -28(%edx)
-L(write_6dbwords):
- movl %eax, -24(%edx)
-L(write_5dbwords):
- movl %eax, -20(%edx)
-L(write_4dbwords):
- movl %eax, -16(%edx)
-L(write_3dbwords):
- movl %eax, -12(%edx)
-L(write_2dbwords):
- movl %eax, -8(%edx)
-L(write_1dbwords):
- movl %eax, -4(%edx)
-L(write_0dbwords):
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(16dbwordsormore):
- test $3, %edx
- jz L(aligned4bytes)
- mov %eax, (%edx)
- mov %eax, -4(%edx, %ecx, 4)
- sub $1, %ecx
- rol $24, %eax
- add $1, %edx
- test $3, %edx
- jz L(aligned4bytes)
- ror $8, %eax
- add $1, %edx
- test $3, %edx
- jz L(aligned4bytes)
- ror $8, %eax
- add $1, %edx
-L(aligned4bytes):
- shl $2, %ecx
-
-#ifdef USE_AS_BZERO32
- pxor %xmm0, %xmm0
-#else
- movd %eax, %xmm0
- pshufd $0, %xmm0, %xmm0
-#endif
- testl $0xf, %edx
- jz L(aligned_16)
-/* ECX > 32 and EDX is not 16 byte aligned. */
-L(not_aligned_16):
- movdqu %xmm0, (%edx)
- movl %edx, %eax
- and $-16, %edx
- add $16, %edx
- sub %edx, %eax
- add %eax, %ecx
- movd %xmm0, %eax
- ALIGN (4)
-L(aligned_16):
- cmp $128, %ecx
- jae L(128bytesormore)
-
-L(aligned_16_less128bytes):
- add %ecx, %edx
- shr $2, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- ALIGN (4)
-L(128bytesormore):
-#ifdef SHARED_CACHE_SIZE
- PUSH (%ebx)
- mov $SHARED_CACHE_SIZE, %ebx
-#else
-# if (defined SHARED || defined __PIC__)
- call __i686.get_pc_thunk.bx
- add $_GLOBAL_OFFSET_TABLE_, %ebx
- mov __x86_shared_cache_size@GOTOFF(%ebx), %ebx
-# else
- PUSH (%ebx)
- mov __x86_shared_cache_size, %ebx
-# endif
-#endif
- cmp %ebx, %ecx
- jae L(128bytesormore_nt_start)
-
-#ifdef DATA_CACHE_SIZE
- POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
- cmp $DATA_CACHE_SIZE, %ecx
-#else
-# if (defined SHARED || defined __PIC__)
-# define RESTORE_EBX_STATE
- call __i686.get_pc_thunk.bx
- add $_GLOBAL_OFFSET_TABLE_, %ebx
- cmp __x86_data_cache_size@GOTOFF(%ebx), %ecx
-# else
- POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
- cmp __x86_data_cache_size, %ecx
-# endif
-#endif
-
- jae L(128bytes_L2_normal)
- subl $128, %ecx
-L(128bytesormore_normal):
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- lea 128(%edx), %edx
- jb L(128bytesless_normal)
-
-
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- lea 128(%edx), %edx
- jae L(128bytesormore_normal)
-
-L(128bytesless_normal):
- lea 128(%ecx), %ecx
- add %ecx, %edx
- shr $2, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- ALIGN (4)
-L(128bytes_L2_normal):
- prefetcht0 0x380(%edx)
- prefetcht0 0x3c0(%edx)
- sub $128, %ecx
- movdqa %xmm0, (%edx)
- movaps %xmm0, 0x10(%edx)
- movaps %xmm0, 0x20(%edx)
- movaps %xmm0, 0x30(%edx)
- movaps %xmm0, 0x40(%edx)
- movaps %xmm0, 0x50(%edx)
- movaps %xmm0, 0x60(%edx)
- movaps %xmm0, 0x70(%edx)
- add $128, %edx
- cmp $128, %ecx
- jae L(128bytes_L2_normal)
-
-L(128bytesless_L2_normal):
- add %ecx, %edx
- shr $2, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- RESTORE_EBX_STATE
-L(128bytesormore_nt_start):
- sub %ebx, %ecx
- mov %ebx, %eax
- and $0x7f, %eax
- add %eax, %ecx
- movd %xmm0, %eax
- ALIGN (4)
-L(128bytesormore_shared_cache_loop):
- prefetcht0 0x3c0(%edx)
- prefetcht0 0x380(%edx)
- sub $0x80, %ebx
- movdqa %xmm0, (%edx)
- movdqa %xmm0, 0x10(%edx)
- movdqa %xmm0, 0x20(%edx)
- movdqa %xmm0, 0x30(%edx)
- movdqa %xmm0, 0x40(%edx)
- movdqa %xmm0, 0x50(%edx)
- movdqa %xmm0, 0x60(%edx)
- movdqa %xmm0, 0x70(%edx)
- add $0x80, %edx
- cmp $0x80, %ebx
- jae L(128bytesormore_shared_cache_loop)
- cmp $0x80, %ecx
- jb L(shared_cache_loop_end)
-
- ALIGN (4)
-L(128bytesormore_nt):
- sub $0x80, %ecx
- movntdq %xmm0, (%edx)
- movntdq %xmm0, 0x10(%edx)
- movntdq %xmm0, 0x20(%edx)
- movntdq %xmm0, 0x30(%edx)
- movntdq %xmm0, 0x40(%edx)
- movntdq %xmm0, 0x50(%edx)
- movntdq %xmm0, 0x60(%edx)
- movntdq %xmm0, 0x70(%edx)
- add $0x80, %edx
- cmp $0x80, %ecx
- jae L(128bytesormore_nt)
- sfence
-L(shared_cache_loop_end):
-#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
- POP (%ebx)
-#endif
- add %ecx, %edx
- shr $2, %ecx
- BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
- .pushsection .rodata.sse2,"a",@progbits
- ALIGN (2)
-L(table_16_128bytes):
- .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
- .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
- .popsection
-
- ALIGN (4)
-L(aligned_16_112bytes):
- movdqa %xmm0, -112(%edx)
-L(aligned_16_96bytes):
- movdqa %xmm0, -96(%edx)
-L(aligned_16_80bytes):
- movdqa %xmm0, -80(%edx)
-L(aligned_16_64bytes):
- movdqa %xmm0, -64(%edx)
-L(aligned_16_48bytes):
- movdqa %xmm0, -48(%edx)
-L(aligned_16_32bytes):
- movdqa %xmm0, -32(%edx)
-L(aligned_16_16bytes):
- movdqa %xmm0, -16(%edx)
-L(aligned_16_0bytes):
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(aligned_16_116bytes):
- movdqa %xmm0, -116(%edx)
-L(aligned_16_100bytes):
- movdqa %xmm0, -100(%edx)
-L(aligned_16_84bytes):
- movdqa %xmm0, -84(%edx)
-L(aligned_16_68bytes):
- movdqa %xmm0, -68(%edx)
-L(aligned_16_52bytes):
- movdqa %xmm0, -52(%edx)
-L(aligned_16_36bytes):
- movdqa %xmm0, -36(%edx)
-L(aligned_16_20bytes):
- movdqa %xmm0, -20(%edx)
-L(aligned_16_4bytes):
- movl %eax, -4(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(aligned_16_120bytes):
- movdqa %xmm0, -120(%edx)
-L(aligned_16_104bytes):
- movdqa %xmm0, -104(%edx)
-L(aligned_16_88bytes):
- movdqa %xmm0, -88(%edx)
-L(aligned_16_72bytes):
- movdqa %xmm0, -72(%edx)
-L(aligned_16_56bytes):
- movdqa %xmm0, -56(%edx)
-L(aligned_16_40bytes):
- movdqa %xmm0, -40(%edx)
-L(aligned_16_24bytes):
- movdqa %xmm0, -24(%edx)
-L(aligned_16_8bytes):
- movq %xmm0, -8(%edx)
- SETRTNVAL
- RETURN
-
- ALIGN (4)
-L(aligned_16_124bytes):
- movdqa %xmm0, -124(%edx)
-L(aligned_16_108bytes):
- movdqa %xmm0, -108(%edx)
-L(aligned_16_92bytes):
- movdqa %xmm0, -92(%edx)
-L(aligned_16_76bytes):
- movdqa %xmm0, -76(%edx)
-L(aligned_16_60bytes):
- movdqa %xmm0, -60(%edx)
-L(aligned_16_44bytes):
- movdqa %xmm0, -44(%edx)
-L(aligned_16_28bytes):
- movdqa %xmm0, -28(%edx)
-L(aligned_16_12bytes):
- movq %xmm0, -12(%edx)
- movl %eax, -4(%edx)
- SETRTNVAL
- RETURN
-
-END (sse2_memset32_atom)
diff --git a/libcutils/arch-x86_64/android_memset16.S b/libcutils/arch-x86_64/android_memset16.S
new file mode 100644
index 0000000..cb6d4a3
--- /dev/null
+++ b/libcutils/arch-x86_64/android_memset16.S
@@ -0,0 +1,565 @@
+/*
+ * Copyright (C) 2014 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 "cache.h"
+
+#ifndef MEMSET
+# define MEMSET android_memset16
+#endif
+
+#ifndef L
+# define L(label) .L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n) .p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc .cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc .cfi_endproc
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name) \
+ .type name, @function; \
+ .globl name; \
+ .p2align 4; \
+name: \
+ cfi_startproc
+#endif
+
+#ifndef END
+# define END(name) \
+ cfi_endproc; \
+ .size name, .-name
+#endif
+
+#define JMPTBL(I, B) I - B
+
+/* Branch to an entry in a jump table. TABLE is a jump table with
+ relative offsets. INDEX is a register contains the index into the
+ jump table. SCALE is the scale of INDEX. */
+#define BRANCH_TO_JMPTBL_ENTRY(TABLE, INDEX, SCALE) \
+ lea TABLE(%rip), %r11; \
+ movslq (%r11, INDEX, SCALE), INDEX; \
+ lea (%r11, INDEX), INDEX; \
+ jmp *INDEX
+
+ .section .text.sse2,"ax",@progbits
+ ALIGN (4)
+ENTRY (MEMSET) // Address in rdi
+ shr $1, %rdx // Count in rdx
+ movzwl %si, %ecx
+ /* Fill the whole ECX with pattern. */
+ shl $16, %esi
+ or %esi, %ecx // Pattern in ecx
+
+ cmp $32, %rdx
+ jae L(32wordsormore)
+
+L(write_less32words):
+ lea (%rdi, %rdx, 2), %rdi
+ BRANCH_TO_JMPTBL_ENTRY (L(table_less32words), %rdx, 4)
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_less32words):
+ .int JMPTBL (L(write_0words), L(table_less32words))
+ .int JMPTBL (L(write_1words), L(table_less32words))
+ .int JMPTBL (L(write_2words), L(table_less32words))
+ .int JMPTBL (L(write_3words), L(table_less32words))
+ .int JMPTBL (L(write_4words), L(table_less32words))
+ .int JMPTBL (L(write_5words), L(table_less32words))
+ .int JMPTBL (L(write_6words), L(table_less32words))
+ .int JMPTBL (L(write_7words), L(table_less32words))
+ .int JMPTBL (L(write_8words), L(table_less32words))
+ .int JMPTBL (L(write_9words), L(table_less32words))
+ .int JMPTBL (L(write_10words), L(table_less32words))
+ .int JMPTBL (L(write_11words), L(table_less32words))
+ .int JMPTBL (L(write_12words), L(table_less32words))
+ .int JMPTBL (L(write_13words), L(table_less32words))
+ .int JMPTBL (L(write_14words), L(table_less32words))
+ .int JMPTBL (L(write_15words), L(table_less32words))
+ .int JMPTBL (L(write_16words), L(table_less32words))
+ .int JMPTBL (L(write_17words), L(table_less32words))
+ .int JMPTBL (L(write_18words), L(table_less32words))
+ .int JMPTBL (L(write_19words), L(table_less32words))
+ .int JMPTBL (L(write_20words), L(table_less32words))
+ .int JMPTBL (L(write_21words), L(table_less32words))
+ .int JMPTBL (L(write_22words), L(table_less32words))
+ .int JMPTBL (L(write_23words), L(table_less32words))
+ .int JMPTBL (L(write_24words), L(table_less32words))
+ .int JMPTBL (L(write_25words), L(table_less32words))
+ .int JMPTBL (L(write_26words), L(table_less32words))
+ .int JMPTBL (L(write_27words), L(table_less32words))
+ .int JMPTBL (L(write_28words), L(table_less32words))
+ .int JMPTBL (L(write_29words), L(table_less32words))
+ .int JMPTBL (L(write_30words), L(table_less32words))
+ .int JMPTBL (L(write_31words), L(table_less32words))
+ .popsection
+
+ ALIGN (4)
+L(write_28words):
+ movl %ecx, -56(%rdi)
+ movl %ecx, -52(%rdi)
+L(write_24words):
+ movl %ecx, -48(%rdi)
+ movl %ecx, -44(%rdi)
+L(write_20words):
+ movl %ecx, -40(%rdi)
+ movl %ecx, -36(%rdi)
+L(write_16words):
+ movl %ecx, -32(%rdi)
+ movl %ecx, -28(%rdi)
+L(write_12words):
+ movl %ecx, -24(%rdi)
+ movl %ecx, -20(%rdi)
+L(write_8words):
+ movl %ecx, -16(%rdi)
+ movl %ecx, -12(%rdi)
+L(write_4words):
+ movl %ecx, -8(%rdi)
+ movl %ecx, -4(%rdi)
+L(write_0words):
+ ret
+
+ ALIGN (4)
+L(write_29words):
+ movl %ecx, -58(%rdi)
+ movl %ecx, -54(%rdi)
+L(write_25words):
+ movl %ecx, -50(%rdi)
+ movl %ecx, -46(%rdi)
+L(write_21words):
+ movl %ecx, -42(%rdi)
+ movl %ecx, -38(%rdi)
+L(write_17words):
+ movl %ecx, -34(%rdi)
+ movl %ecx, -30(%rdi)
+L(write_13words):
+ movl %ecx, -26(%rdi)
+ movl %ecx, -22(%rdi)
+L(write_9words):
+ movl %ecx, -18(%rdi)
+ movl %ecx, -14(%rdi)
+L(write_5words):
+ movl %ecx, -10(%rdi)
+ movl %ecx, -6(%rdi)
+L(write_1words):
+ mov %cx, -2(%rdi)
+ ret
+
+ ALIGN (4)
+L(write_30words):
+ movl %ecx, -60(%rdi)
+ movl %ecx, -56(%rdi)
+L(write_26words):
+ movl %ecx, -52(%rdi)
+ movl %ecx, -48(%rdi)
+L(write_22words):
+ movl %ecx, -44(%rdi)
+ movl %ecx, -40(%rdi)
+L(write_18words):
+ movl %ecx, -36(%rdi)
+ movl %ecx, -32(%rdi)
+L(write_14words):
+ movl %ecx, -28(%rdi)
+ movl %ecx, -24(%rdi)
+L(write_10words):
+ movl %ecx, -20(%rdi)
+ movl %ecx, -16(%rdi)
+L(write_6words):
+ movl %ecx, -12(%rdi)
+ movl %ecx, -8(%rdi)
+L(write_2words):
+ movl %ecx, -4(%rdi)
+ ret
+
+ ALIGN (4)
+L(write_31words):
+ movl %ecx, -62(%rdi)
+ movl %ecx, -58(%rdi)
+L(write_27words):
+ movl %ecx, -54(%rdi)
+ movl %ecx, -50(%rdi)
+L(write_23words):
+ movl %ecx, -46(%rdi)
+ movl %ecx, -42(%rdi)
+L(write_19words):
+ movl %ecx, -38(%rdi)
+ movl %ecx, -34(%rdi)
+L(write_15words):
+ movl %ecx, -30(%rdi)
+ movl %ecx, -26(%rdi)
+L(write_11words):
+ movl %ecx, -22(%rdi)
+ movl %ecx, -18(%rdi)
+L(write_7words):
+ movl %ecx, -14(%rdi)
+ movl %ecx, -10(%rdi)
+L(write_3words):
+ movl %ecx, -6(%rdi)
+ movw %cx, -2(%rdi)
+ ret
+
+ ALIGN (4)
+L(32wordsormore):
+ shl $1, %rdx
+ test $0x01, %edi
+ jz L(aligned2bytes)
+ mov %ecx, (%rdi)
+ mov %ecx, -4(%rdi, %rdx)
+ sub $2, %rdx
+ add $1, %rdi
+ rol $8, %ecx
+L(aligned2bytes):
+ /* Fill xmm0 with the pattern. */
+ movd %ecx, %xmm0
+ pshufd $0, %xmm0, %xmm0
+
+ testl $0xf, %edi
+ jz L(aligned_16)
+/* RDX > 32 and RDI is not 16 byte aligned. */
+ movdqu %xmm0, (%rdi)
+ mov %rdi, %rsi
+ and $-16, %rdi
+ add $16, %rdi
+ sub %rdi, %rsi
+ add %rsi, %rdx
+
+ ALIGN (4)
+L(aligned_16):
+ cmp $128, %rdx
+ jge L(128bytesormore)
+
+L(aligned_16_less128bytes):
+ add %rdx, %rdi
+ shr $1, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ ALIGN (4)
+L(128bytesormore):
+ cmp $SHARED_CACHE_SIZE, %rdx
+ jg L(128bytesormore_nt)
+
+L(128bytesormore_normal):
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jge L(128bytesormore_normal)
+
+L(128bytesless_normal):
+ add %rdx, %rdi
+ shr $1, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ ALIGN (4)
+L(128bytesormore_nt):
+ sub $128, %rdx
+ movntdq %xmm0, (%rdi)
+ movntdq %xmm0, 0x10(%rdi)
+ movntdq %xmm0, 0x20(%rdi)
+ movntdq %xmm0, 0x30(%rdi)
+ movntdq %xmm0, 0x40(%rdi)
+ movntdq %xmm0, 0x50(%rdi)
+ movntdq %xmm0, 0x60(%rdi)
+ movntdq %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jge L(128bytesormore_nt)
+
+ sfence
+ add %rdx, %rdi
+ shr $1, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_16_128bytes):
+ .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_2bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_6bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_10bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_14bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_18bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_22bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_26bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_30bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_34bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_38bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_42bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_46bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_50bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_54bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_58bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_62bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_66bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_70bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_74bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_78bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_82bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_86bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_90bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_94bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_98bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_102bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_106bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_110bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_114bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_118bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_122bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_126bytes), L(table_16_128bytes))
+ .popsection
+
+ ALIGN (4)
+L(aligned_16_112bytes):
+ movdqa %xmm0, -112(%rdi)
+L(aligned_16_96bytes):
+ movdqa %xmm0, -96(%rdi)
+L(aligned_16_80bytes):
+ movdqa %xmm0, -80(%rdi)
+L(aligned_16_64bytes):
+ movdqa %xmm0, -64(%rdi)
+L(aligned_16_48bytes):
+ movdqa %xmm0, -48(%rdi)
+L(aligned_16_32bytes):
+ movdqa %xmm0, -32(%rdi)
+L(aligned_16_16bytes):
+ movdqa %xmm0, -16(%rdi)
+L(aligned_16_0bytes):
+ ret
+
+ ALIGN (4)
+L(aligned_16_114bytes):
+ movdqa %xmm0, -114(%rdi)
+L(aligned_16_98bytes):
+ movdqa %xmm0, -98(%rdi)
+L(aligned_16_82bytes):
+ movdqa %xmm0, -82(%rdi)
+L(aligned_16_66bytes):
+ movdqa %xmm0, -66(%rdi)
+L(aligned_16_50bytes):
+ movdqa %xmm0, -50(%rdi)
+L(aligned_16_34bytes):
+ movdqa %xmm0, -34(%rdi)
+L(aligned_16_18bytes):
+ movdqa %xmm0, -18(%rdi)
+L(aligned_16_2bytes):
+ movw %cx, -2(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_116bytes):
+ movdqa %xmm0, -116(%rdi)
+L(aligned_16_100bytes):
+ movdqa %xmm0, -100(%rdi)
+L(aligned_16_84bytes):
+ movdqa %xmm0, -84(%rdi)
+L(aligned_16_68bytes):
+ movdqa %xmm0, -68(%rdi)
+L(aligned_16_52bytes):
+ movdqa %xmm0, -52(%rdi)
+L(aligned_16_36bytes):
+ movdqa %xmm0, -36(%rdi)
+L(aligned_16_20bytes):
+ movdqa %xmm0, -20(%rdi)
+L(aligned_16_4bytes):
+ movl %ecx, -4(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_118bytes):
+ movdqa %xmm0, -118(%rdi)
+L(aligned_16_102bytes):
+ movdqa %xmm0, -102(%rdi)
+L(aligned_16_86bytes):
+ movdqa %xmm0, -86(%rdi)
+L(aligned_16_70bytes):
+ movdqa %xmm0, -70(%rdi)
+L(aligned_16_54bytes):
+ movdqa %xmm0, -54(%rdi)
+L(aligned_16_38bytes):
+ movdqa %xmm0, -38(%rdi)
+L(aligned_16_22bytes):
+ movdqa %xmm0, -22(%rdi)
+L(aligned_16_6bytes):
+ movl %ecx, -6(%rdi)
+ movw %cx, -2(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_120bytes):
+ movdqa %xmm0, -120(%rdi)
+L(aligned_16_104bytes):
+ movdqa %xmm0, -104(%rdi)
+L(aligned_16_88bytes):
+ movdqa %xmm0, -88(%rdi)
+L(aligned_16_72bytes):
+ movdqa %xmm0, -72(%rdi)
+L(aligned_16_56bytes):
+ movdqa %xmm0, -56(%rdi)
+L(aligned_16_40bytes):
+ movdqa %xmm0, -40(%rdi)
+L(aligned_16_24bytes):
+ movdqa %xmm0, -24(%rdi)
+L(aligned_16_8bytes):
+ movq %xmm0, -8(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_122bytes):
+ movdqa %xmm0, -122(%rdi)
+L(aligned_16_106bytes):
+ movdqa %xmm0, -106(%rdi)
+L(aligned_16_90bytes):
+ movdqa %xmm0, -90(%rdi)
+L(aligned_16_74bytes):
+ movdqa %xmm0, -74(%rdi)
+L(aligned_16_58bytes):
+ movdqa %xmm0, -58(%rdi)
+L(aligned_16_42bytes):
+ movdqa %xmm0, -42(%rdi)
+L(aligned_16_26bytes):
+ movdqa %xmm0, -26(%rdi)
+L(aligned_16_10bytes):
+ movq %xmm0, -10(%rdi)
+ movw %cx, -2(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_124bytes):
+ movdqa %xmm0, -124(%rdi)
+L(aligned_16_108bytes):
+ movdqa %xmm0, -108(%rdi)
+L(aligned_16_92bytes):
+ movdqa %xmm0, -92(%rdi)
+L(aligned_16_76bytes):
+ movdqa %xmm0, -76(%rdi)
+L(aligned_16_60bytes):
+ movdqa %xmm0, -60(%rdi)
+L(aligned_16_44bytes):
+ movdqa %xmm0, -44(%rdi)
+L(aligned_16_28bytes):
+ movdqa %xmm0, -28(%rdi)
+L(aligned_16_12bytes):
+ movq %xmm0, -12(%rdi)
+ movl %ecx, -4(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_126bytes):
+ movdqa %xmm0, -126(%rdi)
+L(aligned_16_110bytes):
+ movdqa %xmm0, -110(%rdi)
+L(aligned_16_94bytes):
+ movdqa %xmm0, -94(%rdi)
+L(aligned_16_78bytes):
+ movdqa %xmm0, -78(%rdi)
+L(aligned_16_62bytes):
+ movdqa %xmm0, -62(%rdi)
+L(aligned_16_46bytes):
+ movdqa %xmm0, -46(%rdi)
+L(aligned_16_30bytes):
+ movdqa %xmm0, -30(%rdi)
+L(aligned_16_14bytes):
+ movq %xmm0, -14(%rdi)
+ movl %ecx, -6(%rdi)
+ movw %cx, -2(%rdi)
+ ret
+
+END (MEMSET)
diff --git a/libcutils/arch-x86_64/android_memset32.S b/libcutils/arch-x86_64/android_memset32.S
new file mode 100644
index 0000000..1514aa2
--- /dev/null
+++ b/libcutils/arch-x86_64/android_memset32.S
@@ -0,0 +1,373 @@
+/*
+ * Copyright (C) 2014 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 "cache.h"
+
+#ifndef MEMSET
+# define MEMSET android_memset32
+#endif
+
+#ifndef L
+# define L(label) .L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n) .p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc .cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc .cfi_endproc
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name) \
+ .type name, @function; \
+ .globl name; \
+ .p2align 4; \
+name: \
+ cfi_startproc
+#endif
+
+#ifndef END
+# define END(name) \
+ cfi_endproc; \
+ .size name, .-name
+#endif
+
+#define JMPTBL(I, B) I - B
+
+/* Branch to an entry in a jump table. TABLE is a jump table with
+ relative offsets. INDEX is a register contains the index into the
+ jump table. SCALE is the scale of INDEX. */
+#define BRANCH_TO_JMPTBL_ENTRY(TABLE, INDEX, SCALE) \
+ lea TABLE(%rip), %r11; \
+ movslq (%r11, INDEX, SCALE), INDEX; \
+ lea (%r11, INDEX), INDEX; \
+ jmp *INDEX
+
+ .section .text.sse2,"ax",@progbits
+ ALIGN (4)
+ENTRY (MEMSET) // Address in rdi
+ shr $2, %rdx // Count in rdx
+ movl %esi, %ecx // Pattern in ecx
+
+ cmp $16, %rdx
+ jae L(16dbwordsormore)
+
+L(write_less16dbwords):
+ lea (%rdi, %rdx, 4), %rdi
+ BRANCH_TO_JMPTBL_ENTRY (L(table_less16dbwords), %rdx, 4)
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_less16dbwords):
+ .int JMPTBL (L(write_0dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_1dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_2dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_3dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_4dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_5dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_6dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_7dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_8dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_9dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_10dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_11dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_12dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_13dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_14dbwords), L(table_less16dbwords))
+ .int JMPTBL (L(write_15dbwords), L(table_less16dbwords))
+ .popsection
+
+ ALIGN (4)
+L(write_15dbwords):
+ movl %ecx, -60(%rdi)
+L(write_14dbwords):
+ movl %ecx, -56(%rdi)
+L(write_13dbwords):
+ movl %ecx, -52(%rdi)
+L(write_12dbwords):
+ movl %ecx, -48(%rdi)
+L(write_11dbwords):
+ movl %ecx, -44(%rdi)
+L(write_10dbwords):
+ movl %ecx, -40(%rdi)
+L(write_9dbwords):
+ movl %ecx, -36(%rdi)
+L(write_8dbwords):
+ movl %ecx, -32(%rdi)
+L(write_7dbwords):
+ movl %ecx, -28(%rdi)
+L(write_6dbwords):
+ movl %ecx, -24(%rdi)
+L(write_5dbwords):
+ movl %ecx, -20(%rdi)
+L(write_4dbwords):
+ movl %ecx, -16(%rdi)
+L(write_3dbwords):
+ movl %ecx, -12(%rdi)
+L(write_2dbwords):
+ movl %ecx, -8(%rdi)
+L(write_1dbwords):
+ movl %ecx, -4(%rdi)
+L(write_0dbwords):
+ ret
+
+ ALIGN (4)
+L(16dbwordsormore):
+ test $3, %edi
+ jz L(aligned4bytes)
+ mov %ecx, (%rdi)
+ mov %ecx, -4(%rdi, %rdx, 4)
+ sub $1, %rdx
+ rol $24, %ecx
+ add $1, %rdi
+ test $3, %edi
+ jz L(aligned4bytes)
+ ror $8, %ecx
+ add $1, %rdi
+ test $3, %edi
+ jz L(aligned4bytes)
+ ror $8, %ecx
+ add $1, %rdi
+L(aligned4bytes):
+ shl $2, %rdx
+
+ /* Fill xmm0 with the pattern. */
+ movd %ecx, %xmm0
+ pshufd $0, %xmm0, %xmm0
+
+ testl $0xf, %edi
+ jz L(aligned_16)
+/* RDX > 32 and RDI is not 16 byte aligned. */
+ movdqu %xmm0, (%rdi)
+ mov %rdi, %rsi
+ and $-16, %rdi
+ add $16, %rdi
+ sub %rdi, %rsi
+ add %rsi, %rdx
+
+ ALIGN (4)
+L(aligned_16):
+ cmp $128, %rdx
+ jge L(128bytesormore)
+
+L(aligned_16_less128bytes):
+ add %rdx, %rdi
+ shr $2, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ ALIGN (4)
+L(128bytesormore):
+ cmp $SHARED_CACHE_SIZE, %rdx
+ jg L(128bytesormore_nt)
+
+L(128bytesormore_normal):
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jl L(128bytesless_normal)
+
+ sub $128, %rdx
+ movdqa %xmm0, (%rdi)
+ movdqa %xmm0, 0x10(%rdi)
+ movdqa %xmm0, 0x20(%rdi)
+ movdqa %xmm0, 0x30(%rdi)
+ movdqa %xmm0, 0x40(%rdi)
+ movdqa %xmm0, 0x50(%rdi)
+ movdqa %xmm0, 0x60(%rdi)
+ movdqa %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jge L(128bytesormore_normal)
+
+L(128bytesless_normal):
+ add %rdx, %rdi
+ shr $2, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ ALIGN (4)
+L(128bytesormore_nt):
+ sub $128, %rdx
+ movntdq %xmm0, (%rdi)
+ movntdq %xmm0, 0x10(%rdi)
+ movntdq %xmm0, 0x20(%rdi)
+ movntdq %xmm0, 0x30(%rdi)
+ movntdq %xmm0, 0x40(%rdi)
+ movntdq %xmm0, 0x50(%rdi)
+ movntdq %xmm0, 0x60(%rdi)
+ movntdq %xmm0, 0x70(%rdi)
+ lea 128(%rdi), %rdi
+ cmp $128, %rdx
+ jge L(128bytesormore_nt)
+
+ sfence
+ add %rdx, %rdi
+ shr $2, %rdx
+ BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes), %rdx, 4)
+
+ .pushsection .rodata.sse2,"a",@progbits
+ ALIGN (2)
+L(table_16_128bytes):
+ .int JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+ .int JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+ .popsection
+
+ ALIGN (4)
+L(aligned_16_112bytes):
+ movdqa %xmm0, -112(%rdi)
+L(aligned_16_96bytes):
+ movdqa %xmm0, -96(%rdi)
+L(aligned_16_80bytes):
+ movdqa %xmm0, -80(%rdi)
+L(aligned_16_64bytes):
+ movdqa %xmm0, -64(%rdi)
+L(aligned_16_48bytes):
+ movdqa %xmm0, -48(%rdi)
+L(aligned_16_32bytes):
+ movdqa %xmm0, -32(%rdi)
+L(aligned_16_16bytes):
+ movdqa %xmm0, -16(%rdi)
+L(aligned_16_0bytes):
+ ret
+
+ ALIGN (4)
+L(aligned_16_116bytes):
+ movdqa %xmm0, -116(%rdi)
+L(aligned_16_100bytes):
+ movdqa %xmm0, -100(%rdi)
+L(aligned_16_84bytes):
+ movdqa %xmm0, -84(%rdi)
+L(aligned_16_68bytes):
+ movdqa %xmm0, -68(%rdi)
+L(aligned_16_52bytes):
+ movdqa %xmm0, -52(%rdi)
+L(aligned_16_36bytes):
+ movdqa %xmm0, -36(%rdi)
+L(aligned_16_20bytes):
+ movdqa %xmm0, -20(%rdi)
+L(aligned_16_4bytes):
+ movl %ecx, -4(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_120bytes):
+ movdqa %xmm0, -120(%rdi)
+L(aligned_16_104bytes):
+ movdqa %xmm0, -104(%rdi)
+L(aligned_16_88bytes):
+ movdqa %xmm0, -88(%rdi)
+L(aligned_16_72bytes):
+ movdqa %xmm0, -72(%rdi)
+L(aligned_16_56bytes):
+ movdqa %xmm0, -56(%rdi)
+L(aligned_16_40bytes):
+ movdqa %xmm0, -40(%rdi)
+L(aligned_16_24bytes):
+ movdqa %xmm0, -24(%rdi)
+L(aligned_16_8bytes):
+ movq %xmm0, -8(%rdi)
+ ret
+
+ ALIGN (4)
+L(aligned_16_124bytes):
+ movdqa %xmm0, -124(%rdi)
+L(aligned_16_108bytes):
+ movdqa %xmm0, -108(%rdi)
+L(aligned_16_92bytes):
+ movdqa %xmm0, -92(%rdi)
+L(aligned_16_76bytes):
+ movdqa %xmm0, -76(%rdi)
+L(aligned_16_60bytes):
+ movdqa %xmm0, -60(%rdi)
+L(aligned_16_44bytes):
+ movdqa %xmm0, -44(%rdi)
+L(aligned_16_28bytes):
+ movdqa %xmm0, -28(%rdi)
+L(aligned_16_12bytes):
+ movq %xmm0, -12(%rdi)
+ movl %ecx, -4(%rdi)
+ ret
+
+END (MEMSET)
diff --git a/libnl_2/cache.c b/libcutils/arch-x86_64/cache.h
similarity index 62%
rename from libnl_2/cache.c
rename to libcutils/arch-x86_64/cache.h
index c21974d..f144309 100644
--- a/libnl_2/cache.c
+++ b/libcutils/arch-x86_64/cache.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011 The Android Open Source Project
+ * Copyright (C) 2014 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.
@@ -14,24 +14,9 @@
* limitations under the License.
*/
-/* NOTICE: This is a clean room re-implementation of libnl */
+/* Values are optimized for Silvermont */
+#define SHARED_CACHE_SIZE (1024*1024) /* Silvermont L2 Cache */
+#define DATA_CACHE_SIZE (24*1024) /* Silvermont L1 Data Cache */
-#include "netlink/cache.h"
-#include "netlink/object.h"
-
-void nl_cache_free(struct nl_cache *cache)
-{
-
-}
-
-void nl_cache_clear(struct nl_cache *cache)
-{
-
-}
-
-void nl_cache_remove(struct nl_object *obj)
-{
-
-}
-
-
+#define SHARED_CACHE_SIZE_HALF (SHARED_CACHE_SIZE / 2)
+#define DATA_CACHE_SIZE_HALF (DATA_CACHE_SIZE / 2)
diff --git a/libcutils/ashmem-host.c b/libcutils/ashmem-host.c
index f03e130..abc4f94 100644
--- a/libcutils/ashmem-host.c
+++ b/libcutils/ashmem-host.c
@@ -19,96 +19,66 @@
* an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
*/
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
#include <errno.h>
-#include <time.h>
+#include <fcntl.h>
#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
#include <cutils/ashmem.h>
+#include <utils/Compat.h>
-int ashmem_create_region(const char *ignored, size_t size)
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
+int ashmem_create_region(const char *ignored __unused, size_t size)
{
- static const char txt[] = "abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char name[64];
- unsigned int retries = 0;
- pid_t pid = getpid();
- int fd;
-
- srand(time(NULL) + pid);
-
-retry:
- /* not beautiful, its just wolf-like loop unrolling */
- snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c",
- pid,
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
- txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))]);
-
- /* open O_EXCL & O_CREAT: we are either the sole owner or we fail */
- fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fd == -1) {
- /* unlikely, but if we failed because `name' exists, retry */
- if (errno == EEXIST && ++retries < 6)
- goto retry;
- return -1;
- }
-
- /* truncate the file to `len' bytes */
- if (ftruncate(fd, size) == -1)
- goto error;
-
- if (unlink(name) == -1)
- goto error;
-
- return fd;
-error:
- close(fd);
- return -1;
+ char template[PATH_MAX];
+ snprintf(template, sizeof(template), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
+ int fd = mkstemp(template);
+ if (fd != -1 && TEMP_FAILURE_RETRY(ftruncate(fd, size)) != -1 && unlink(template) != -1) {
+ return fd;
+ }
+ close(fd);
+ return -1;
}
-int ashmem_set_prot_region(int fd, int prot)
+int ashmem_set_prot_region(int fd __unused, int prot __unused)
{
- return 0;
+ return 0;
}
-int ashmem_pin_region(int fd, size_t offset, size_t len)
+int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
{
- return ASHMEM_NOT_PURGED;
+ return ASHMEM_NOT_PURGED;
}
-int ashmem_unpin_region(int fd, size_t offset, size_t len)
+int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
{
- return ASHMEM_IS_UNPINNED;
+ return ASHMEM_IS_UNPINNED;
}
int ashmem_get_size_region(int fd)
{
- struct stat buf;
- int result;
+ struct stat buf;
+ int result = fstat(fd, &buf);
+ if (result == -1) {
+ return -1;
+ }
- result = fstat(fd, &buf);
- if (result == -1) {
- return -1;
- }
+ // Check if this is an "ashmem" region.
+ // TODO: This is very hacky, and can easily break. We need some reliable indicator.
+ if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
+ errno = ENOTTY;
+ return -1;
+ }
- // Check if this is an "ashmem" region.
- // TODO: This is very hacky, and can easily break. We need some reliable indicator.
- if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
- errno = ENOTTY;
- return -1;
- }
-
- return (int)buf.st_size; // TODO: care about overflow (> 2GB file)?
+ return buf.st_size;
}
diff --git a/libcutils/atomic.c b/libcutils/atomic.c
index 1484ef8..d34aa00 100644
--- a/libcutils/atomic.c
+++ b/libcutils/atomic.c
@@ -14,6 +14,13 @@
* limitations under the License.
*/
+/*
+ * Generate non-inlined versions of android_atomic functions.
+ * Nobody should be using these, but some binary blobs currently (late 2014)
+ * are.
+ * If you read this in 2015 or later, please try to delete this file.
+ */
+
#define ANDROID_ATOMIC_INLINE
-#include <cutils/atomic-inline.h>
+#include <cutils/atomic.h>
diff --git a/libcutils/cpu_info.c b/libcutils/cpu_info.c
deleted file mode 100644
index 23dda8a..0000000
--- a/libcutils/cpu_info.c
+++ /dev/null
@@ -1,83 +0,0 @@
-/* libs/cutils/cpu_info.c
-**
-** Copyright 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.
-*/
-
-#include <cutils/cpu_info.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-// we cache the serial number here.
-// this is also used as a fgets() line buffer when we are reading /proc/cpuinfo
-static char serial_number[100] = { 0 };
-
-extern const char* get_cpu_serial_number(void)
-{
- if (serial_number[0] == 0)
- {
- FILE* file;
- char* chp, *end;
- char* whitespace;
- int length;
-
- // read serial number from /proc/cpuinfo
- file = fopen("proc/cpuinfo", "r");
- if (! file)
- return NULL;
-
- while ((chp = fgets(serial_number, sizeof(serial_number), file)) != NULL)
- {
- // look for something like "Serial : 999206122a03591c"
-
- if (strncmp(chp, "Serial", 6) != 0)
- continue;
-
- chp = strchr(chp, ':');
- if (!chp)
- continue;
-
- // skip colon and whitespace
- while ( *(++chp) == ' ') {}
-
- // truncate trailing whitespace
- end = chp;
- while (*end && *end != ' ' && *end != '\t' && *end != '\n' && *end != '\r')
- ++end;
- *end = 0;
-
- whitespace = strchr(chp, ' ');
- if (whitespace)
- *whitespace = 0;
- whitespace = strchr(chp, '\t');
- if (whitespace)
- *whitespace = 0;
- whitespace = strchr(chp, '\r');
- if (whitespace)
- *whitespace = 0;
- whitespace = strchr(chp, '\n');
- if (whitespace)
- *whitespace = 0;
-
- // shift serial number to beginning of the buffer
- memmove(serial_number, chp, strlen(chp) + 1);
- break;
- }
-
- fclose(file);
- }
-
- return (serial_number[0] ? serial_number : NULL);
-}
diff --git a/libcutils/debugger.c b/libcutils/debugger.c
index 7d907fc..4558719 100644
--- a/libcutils/debugger.c
+++ b/libcutils/debugger.c
@@ -14,76 +14,116 @@
* limitations under the License.
*/
+#include <stdbool.h>
+#include <fcntl.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
#include <unistd.h>
#include <cutils/debugger.h>
#include <cutils/sockets.h>
-int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
- int s = socket_local_client(DEBUGGER_SOCKET_NAME,
- ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
- if (s < 0) {
- return -1;
+#define LOG_TAG "DEBUG"
+#include <log/log.h>
+
+static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) {
+ int result = 0;
+ if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) {
+ result = -1;
+ } else {
+ char ack;
+ if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) != 1) {
+ result = -1;
+ }
+ }
+ return result;
+}
+
+static int make_dump_request(debugger_action_t action, pid_t tid, int timeout_secs) {
+ debugger_msg_t msg;
+ memset(&msg, 0, sizeof(msg));
+ msg.tid = tid;
+ msg.action = action;
+
+ int sock_fd = socket_local_client(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
+ SOCK_STREAM | SOCK_CLOEXEC);
+ if (sock_fd < 0) {
+ return -1;
+ }
+
+ if (timeout_secs > 0) {
+ struct timeval tm;
+ tm.tv_sec = timeout_secs;
+ tm.tv_usec = 0;
+ if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tm, sizeof(tm)) == -1) {
+ ALOGE("WARNING: Cannot set receive timeout value on socket: %s", strerror(errno));
}
- debugger_msg_t msg;
- msg.tid = tid;
- msg.action = DEBUGGER_ACTION_DUMP_TOMBSTONE;
- msg.abort_msg_address = 0;
-
- int result = 0;
- if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
- result = -1;
- } else {
- char ack;
- if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
- result = -1;
- } else {
- if (pathbuf && pathlen) {
- ssize_t n = TEMP_FAILURE_RETRY(read(s, pathbuf, pathlen - 1));
- if (n <= 0) {
- result = -1;
- } else {
- pathbuf[n] = '\0';
- }
- }
- }
+ if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, &tm, sizeof(tm)) == -1) {
+ ALOGE("WARNING: Cannot set send timeout value on socket: %s", strerror(errno));
}
- TEMP_FAILURE_RETRY(close(s));
- return result;
+ }
+
+ if (send_request(sock_fd, &msg, sizeof(msg)) < 0) {
+ TEMP_FAILURE_RETRY(close(sock_fd));
+ return -1;
+ }
+
+ return sock_fd;
}
int dump_backtrace_to_file(pid_t tid, int fd) {
- int s = socket_local_client(DEBUGGER_SOCKET_NAME,
- ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
- if (s < 0) {
- return -1;
- }
+ return dump_backtrace_to_file_timeout(tid, fd, 0);
+}
- debugger_msg_t msg;
- msg.tid = tid;
- msg.action = DEBUGGER_ACTION_DUMP_BACKTRACE;
- msg.abort_msg_address = 0;
+int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs) {
+ int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_BACKTRACE, tid, timeout_secs);
+ if (sock_fd < 0) {
+ return -1;
+ }
- int result = 0;
- if (TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg))) != sizeof(msg)) {
- result = -1;
- } else {
- char ack;
- if (TEMP_FAILURE_RETRY(read(s, &ack, 1)) != 1) {
- result = -1;
- } else {
- char buffer[4096];
- ssize_t n;
- while ((n = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)))) > 0) {
- if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
- result = -1;
- break;
- }
- }
- }
+ /* Write the data read from the socket to the fd. */
+ int result = 0;
+ char buffer[1024];
+ ssize_t n;
+ while ((n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
+ if (TEMP_FAILURE_RETRY(write(fd, buffer, n)) != n) {
+ result = -1;
+ break;
}
- TEMP_FAILURE_RETRY(close(s));
- return result;
+ }
+ TEMP_FAILURE_RETRY(close(sock_fd));
+ return result;
+}
+
+int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen) {
+ return dump_tombstone_timeout(tid, pathbuf, pathlen, 0);
+}
+
+int dump_tombstone_timeout(pid_t tid, char* pathbuf, size_t pathlen, int timeout_secs) {
+ int sock_fd = make_dump_request(DEBUGGER_ACTION_DUMP_TOMBSTONE, tid, timeout_secs);
+ if (sock_fd < 0) {
+ return -1;
+ }
+
+ /* Read the tombstone file name. */
+ char buffer[100]; /* This is larger than the largest tombstone path. */
+ int result = 0;
+ ssize_t n = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer) - 1));
+ if (n <= 0) {
+ result = -1;
+ } else {
+ if (pathbuf && pathlen) {
+ if (n >= (ssize_t) pathlen) {
+ n = pathlen - 1;
+ }
+ buffer[n] = '\0';
+ memcpy(pathbuf, buffer, n + 1);
+ }
+ }
+ TEMP_FAILURE_RETRY(close(sock_fd));
+ return result;
}
diff --git a/libcutils/dlmalloc_stubs.c b/libcutils/dlmalloc_stubs.c
index c327a55..2db473d 100644
--- a/libcutils/dlmalloc_stubs.c
+++ b/libcutils/dlmalloc_stubs.c
@@ -14,21 +14,22 @@
* limitations under the License.
*/
-#include "../../../bionic/libc/bionic/dlmalloc.h"
-#include "cutils/log.h"
+#include "log/log.h"
+
+#define UNUSED __attribute__((__unused__))
/*
* Stubs for functions defined in bionic/libc/bionic/dlmalloc.c. These
* are used in host builds, as the host libc will not contain these
* functions.
*/
-void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*),
- void* arg)
+void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*) UNUSED,
+ void* arg UNUSED)
{
ALOGW("Called host unimplemented stub: dlmalloc_inspect_all");
}
-int dlmalloc_trim(size_t unused)
+int dlmalloc_trim(size_t unused UNUSED)
{
ALOGW("Called host unimplemented stub: dlmalloc_trim");
return 0;
diff --git a/libcutils/fs.c b/libcutils/fs.c
index 286a8eb..45c7add 100644
--- a/libcutils/fs.c
+++ b/libcutils/fs.c
@@ -212,7 +212,7 @@
/* Yay, segment is ready for us to step into */
int next_fd;
- if ((next_fd = openat(fd, segment, 0)) == -1) {
+ if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
res = -errno;
goto done_close;
diff --git a/libcutils/iosched_policy.c b/libcutils/iosched_policy.c
index 5d90a01..a6da9ca 100644
--- a/libcutils/iosched_policy.c
+++ b/libcutils/iosched_policy.c
@@ -1,5 +1,4 @@
/*
-**
** Copyright 2007-2014, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,50 +14,41 @@
** limitations under the License.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
#include <errno.h>
#include <fcntl.h>
-
-#ifdef HAVE_SCHED_H
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#include <cutils/iosched_policy.h>
#ifdef HAVE_ANDROID_OS
-/* #include <linux/ioprio.h> */
-extern int ioprio_set(int which, int who, int ioprio);
-extern int ioprio_get(int which, int who);
+#include <linux/ioprio.h>
+#include <sys/syscall.h>
+#define __android_unused
+#else
+#define __android_unused __attribute__((__unused__))
#endif
-enum {
- WHO_PROCESS = 1,
- WHO_PGRP,
- WHO_USER,
-};
-
-#define CLASS_SHIFT 13
-#define IOPRIO_NORM 4
-
-int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio) {
+int android_set_ioprio(int pid __android_unused, IoSchedClass clazz __android_unused, int ioprio __android_unused) {
#ifdef HAVE_ANDROID_OS
- if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) {
+ if (syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, ioprio | (clazz << IOPRIO_CLASS_SHIFT))) {
return -1;
}
#endif
return 0;
}
-int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio) {
+int android_get_ioprio(int pid __android_unused, IoSchedClass *clazz, int *ioprio) {
#ifdef HAVE_ANDROID_OS
int rc;
- if ((rc = ioprio_get(WHO_PROCESS, pid)) < 0) {
+ if ((rc = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid)) < 0) {
return -1;
}
- *clazz = (rc >> CLASS_SHIFT);
+ *clazz = (rc >> IOPRIO_CLASS_SHIFT);
*ioprio = (rc & 0xff);
#else
*clazz = IoSchedClass_NONE;
@@ -66,5 +56,3 @@
#endif
return 0;
}
-
-#endif /* HAVE_SCHED_H */
diff --git a/libcutils/klog.c b/libcutils/klog.c
index d69fb10..fbb7b72 100644
--- a/libcutils/klog.c
+++ b/libcutils/klog.c
@@ -28,6 +28,10 @@
static int klog_fd = -1;
static int klog_level = KLOG_DEFAULT_LEVEL;
+int klog_get_level(void) {
+ return klog_level;
+}
+
void klog_set_level(int level) {
klog_level = level;
}
@@ -49,18 +53,24 @@
#define LOG_BUF_MAX 512
-void klog_write(int level, const char *fmt, ...)
+void klog_vwrite(int level, const char *fmt, va_list ap)
{
char buf[LOG_BUF_MAX];
- va_list ap;
if (level > klog_level) return;
if (klog_fd < 0) klog_init();
if (klog_fd < 0) return;
- va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
buf[LOG_BUF_MAX - 1] = 0;
- va_end(ap);
+
write(klog_fd, buf, strlen(buf));
}
+
+void klog_write(int level, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ klog_vwrite(level, fmt, ap);
+ va_end(ap);
+}
diff --git a/libcutils/loghack.h b/libcutils/loghack.h
deleted file mode 100644
index 750cab0..0000000
--- a/libcutils/loghack.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.
- */
-
-/**
- * This is a temporary hack to enable logging from cutils.
- */
-
-#ifndef _CUTILS_LOGHACK_H
-#define _CUTILS_LOGHACK_H
-
-#ifdef HAVE_ANDROID_OS
-#include <cutils/log.h>
-#else
-#include <stdio.h>
-#define ALOG(level, ...) \
- ((void)printf("cutils:" level "/" LOG_TAG ": " __VA_ARGS__))
-#define ALOGV(...) ALOG("V", __VA_ARGS__)
-#define ALOGD(...) ALOG("D", __VA_ARGS__)
-#define ALOGI(...) ALOG("I", __VA_ARGS__)
-#define ALOGW(...) ALOG("W", __VA_ARGS__)
-#define ALOGE(...) ALOG("E", __VA_ARGS__)
-#define LOG_ALWAYS_FATAL(...) do { ALOGE(__VA_ARGS__); exit(1); } while (0)
-#endif
-
-#endif // _CUTILS_LOGHACK_H
diff --git a/libcutils/native_handle.c b/libcutils/native_handle.c
index 4089968..9a4a5bb 100644
--- a/libcutils/native_handle.c
+++ b/libcutils/native_handle.c
@@ -30,9 +30,11 @@
native_handle_t* h = malloc(
sizeof(native_handle_t) + sizeof(int)*(numFds+numInts));
- h->version = sizeof(native_handle_t);
- h->numFds = numFds;
- h->numInts = numInts;
+ if (h) {
+ h->version = sizeof(native_handle_t);
+ h->numFds = numFds;
+ h->numInts = numInts;
+ }
return h;
}
diff --git a/libcutils/open_memstream.c b/libcutils/open_memstream.c
index 5b4388a..9183266 100644
--- a/libcutils/open_memstream.c
+++ b/libcutils/open_memstream.c
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#ifndef HAVE_OPEN_MEMSTREAM
+#if defined(__APPLE__)
/*
* Implementation of the POSIX open_memstream() function, which Linux has
@@ -59,8 +59,6 @@
# define DBUG(x) ((void)0)
#endif
-#ifdef HAVE_FUNOPEN
-
/*
* Definition of a seekable, write-only memory stream.
*/
@@ -251,12 +249,6 @@
return fp;
}
-#else /*not HAVE_FUNOPEN*/
-FILE* open_memstream(char** bufp, size_t* sizep)
-{
- abort();
-}
-#endif /*HAVE_FUNOPEN*/
@@ -378,4 +370,4 @@
#endif
-#endif /*!HAVE_OPEN_MEMSTREAM*/
+#endif /* __APPLE__ */
diff --git a/libcutils/partition_utils.c b/libcutils/partition_utils.c
index 10539fa..823b162 100644
--- a/libcutils/partition_utils.c
+++ b/libcutils/partition_utils.c
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-#include <sys/types.h>
-#include <unistd.h>
#include <fcntl.h>
-#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mount.h> /* for BLKGETSIZE */
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
#include <cutils/properties.h>
static int only_one_char(char *buf, int len, char c)
@@ -39,7 +40,7 @@
int partition_wiped(char *source)
{
char buf[4096];
- int fd, ret, wiped;
+ int fd, ret;
if ((fd = open(source, O_RDONLY)) < 0) {
return 0;
diff --git a/libcutils/process_name.c b/libcutils/process_name.c
index a6ab951..cc931eb 100644
--- a/libcutils/process_name.c
+++ b/libcutils/process_name.c
@@ -14,25 +14,27 @@
* limitations under the License.
*/
+#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
#include <cutils/process_name.h>
#ifdef HAVE_ANDROID_OS
#include <cutils/properties.h>
#endif
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#if defined(HAVE_PRCTL)
-#include <sys/prctl.h>
-#endif
#define PROCESS_NAME_DEVICE "/sys/qemu_trace/process_name"
static const char* process_name = "unknown";
+#ifdef HAVE_ANDROID_OS
static int running_in_emulator = -1;
+#endif
void set_process_name(const char* new_name) {
#ifdef HAVE_ANDROID_OS
@@ -49,7 +51,7 @@
strcpy(copy, new_name);
process_name = (const char*) copy;
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
if (len < 16) {
prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0);
} else {
diff --git a/libcutils/properties.c b/libcutils/properties.c
index 28d8b2f..4e46e02 100644
--- a/libcutils/properties.c
+++ b/libcutils/properties.c
@@ -15,18 +15,94 @@
*/
#define LOG_TAG "properties"
+// #define LOG_NDEBUG 0
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include <unistd.h>
#include <cutils/sockets.h>
#include <errno.h>
#include <assert.h>
#include <cutils/properties.h>
-#include "loghack.h"
+#include <stdbool.h>
+#include <inttypes.h>
+#include <log/log.h>
-#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
+int8_t property_get_bool(const char *key, int8_t default_value) {
+ if (!key) {
+ return default_value;
+ }
+
+ int8_t result = default_value;
+ char buf[PROPERTY_VALUE_MAX] = {'\0',};
+
+ int len = property_get(key, buf, "");
+ if (len == 1) {
+ char ch = buf[0];
+ if (ch == '0' || ch == 'n') {
+ result = false;
+ } else if (ch == '1' || ch == 'y') {
+ result = true;
+ }
+ } else if (len > 1) {
+ if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
+ result = false;
+ } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
+ result = true;
+ }
+ }
+
+ return result;
+}
+
+// Convert string property to int (default if fails); return default value if out of bounds
+static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
+ intmax_t default_value) {
+ if (!key) {
+ return default_value;
+ }
+
+ intmax_t result = default_value;
+ char buf[PROPERTY_VALUE_MAX] = {'\0',};
+ char *end = NULL;
+
+ int len = property_get(key, buf, "");
+ if (len > 0) {
+ int tmp = errno;
+ errno = 0;
+
+ // Infer base automatically
+ result = strtoimax(buf, &end, /*base*/0);
+ if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
+ // Over or underflow
+ result = default_value;
+ ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
+ } else if (result < lower_bound || result > upper_bound) {
+ // Out of range of requested bounds
+ result = default_value;
+ ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
+ } else if (end == buf) {
+ // Numeric conversion failed
+ result = default_value;
+ ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed",
+ __FUNCTION__, key, default_value);
+ }
+
+ errno = tmp;
+ }
+
+ return result;
+}
+
+int64_t property_get_int64(const char *key, int64_t default_value) {
+ return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
+}
+
+int32_t property_get_int32(const char *key, int32_t default_value) {
+ return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
+}
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
@@ -44,10 +120,13 @@
if(len > 0) {
return len;
}
-
if(default_value) {
len = strlen(default_value);
- memcpy(value, default_value, len + 1);
+ if (len >= PROPERTY_VALUE_MAX) {
+ len = PROPERTY_VALUE_MAX - 1;
+ }
+ memcpy(value, default_value, len);
+ value[len] = '\0';
}
return len;
}
@@ -75,265 +154,3 @@
struct property_list_callback_data data = { propfn, cookie };
return __system_property_foreach(property_list_callback, &data);
}
-
-#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
-
-/*
- * The Linux simulator provides a "system property server" that uses IPC
- * to set/get/list properties. The file descriptor is shared by all
- * threads in the process, so we use a mutex to ensure that requests
- * from multiple threads don't get interleaved.
- */
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <pthread.h>
-
-static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
-static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
-static int gPropFd = -1;
-
-/*
- * Connect to the properties server.
- *
- * Returns the socket descriptor on success.
- */
-static int connectToServer(const char* fileName)
-{
- int sock = -1;
- int cc;
-
- struct sockaddr_un addr;
-
- sock = socket(AF_UNIX, SOCK_STREAM, 0);
- if (sock < 0) {
- ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
- return -1;
- }
-
- /* connect to socket; fails if file doesn't exist */
- strcpy(addr.sun_path, fileName); // max 108 bytes
- addr.sun_family = AF_UNIX;
- cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
- if (cc < 0) {
- // ENOENT means socket file doesn't exist
- // ECONNREFUSED means socket exists but nobody is listening
- //ALOGW("AF_UNIX connect failed for '%s': %s\n",
- // fileName, strerror(errno));
- close(sock);
- return -1;
- }
-
- return sock;
-}
-
-/*
- * Perform one-time initialization.
- */
-static void init(void)
-{
- assert(gPropFd == -1);
-
- gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
- if (gPropFd < 0) {
- //ALOGW("not connected to system property server\n");
- } else {
- //ALOGV("Connected to system property server\n");
- }
-}
-
-int property_get(const char *key, char *value, const char *default_value)
-{
- char sendBuf[1+PROPERTY_KEY_MAX];
- char recvBuf[1+PROPERTY_VALUE_MAX];
- int len = -1;
-
- //ALOGV("PROPERTY GET [%s]\n", key);
-
- pthread_once(&gInitOnce, init);
- if (gPropFd < 0) {
- /* this mimics the behavior of the device implementation */
- if (default_value != NULL) {
- strcpy(value, default_value);
- len = strlen(value);
- }
- return len;
- }
-
- if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
-
- memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
-
- sendBuf[0] = (char) kSystemPropertyGet;
- strcpy(sendBuf+1, key);
-
- pthread_mutex_lock(&gPropertyFdLock);
- if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
- pthread_mutex_unlock(&gPropertyFdLock);
- return -1;
- }
- if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
- pthread_mutex_unlock(&gPropertyFdLock);
- return -1;
- }
- pthread_mutex_unlock(&gPropertyFdLock);
-
- /* first byte is 0 if value not defined, 1 if found */
- if (recvBuf[0] == 0) {
- if (default_value != NULL) {
- strcpy(value, default_value);
- len = strlen(value);
- } else {
- /*
- * If the value isn't defined, hand back an empty string and
- * a zero length, rather than a failure. This seems wrong,
- * since you can't tell the difference between "undefined" and
- * "defined but empty", but it's what the device does.
- */
- value[0] = '\0';
- len = 0;
- }
- } else if (recvBuf[0] == 1) {
- strcpy(value, recvBuf+1);
- len = strlen(value);
- } else {
- ALOGE("Got strange response to property_get request (%d)\n",
- recvBuf[0]);
- assert(0);
- return -1;
- }
- //ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
- // recvBuf[0], default_value, len, key, value);
-
- return len;
-}
-
-
-int property_set(const char *key, const char *value)
-{
- char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
- char recvBuf[1];
- int result = -1;
-
- //ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
-
- pthread_once(&gInitOnce, init);
- if (gPropFd < 0)
- return -1;
-
- if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
- if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
-
- memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
-
- sendBuf[0] = (char) kSystemPropertySet;
- strcpy(sendBuf+1, key);
- strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
-
- pthread_mutex_lock(&gPropertyFdLock);
- if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
- pthread_mutex_unlock(&gPropertyFdLock);
- return -1;
- }
- if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
- pthread_mutex_unlock(&gPropertyFdLock);
- return -1;
- }
- pthread_mutex_unlock(&gPropertyFdLock);
-
- if (recvBuf[0] != 1)
- return -1;
- return 0;
-}
-
-int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
- void *cookie)
-{
- //ALOGV("PROPERTY LIST\n");
- pthread_once(&gInitOnce, init);
- if (gPropFd < 0)
- return -1;
-
- return 0;
-}
-
-#else
-
-/* SUPER-cheesy place-holder implementation for Win32 */
-
-#include <cutils/threads.h>
-
-static mutex_t env_lock = MUTEX_INITIALIZER;
-
-int property_get(const char *key, char *value, const char *default_value)
-{
- char ename[PROPERTY_KEY_MAX + 6];
- char *p;
- int len;
-
- len = strlen(key);
- if(len >= PROPERTY_KEY_MAX) return -1;
- memcpy(ename, "PROP_", 5);
- memcpy(ename + 5, key, len + 1);
-
- mutex_lock(&env_lock);
-
- p = getenv(ename);
- if(p == 0) p = "";
- len = strlen(p);
- if(len >= PROPERTY_VALUE_MAX) {
- len = PROPERTY_VALUE_MAX - 1;
- }
-
- if((len == 0) && default_value) {
- len = strlen(default_value);
- memcpy(value, default_value, len + 1);
- } else {
- memcpy(value, p, len);
- value[len] = 0;
- }
-
- mutex_unlock(&env_lock);
-
- return len;
-}
-
-
-int property_set(const char *key, const char *value)
-{
- char ename[PROPERTY_KEY_MAX + 6];
- char *p;
- int len;
- int r;
-
- if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
-
- len = strlen(key);
- if(len >= PROPERTY_KEY_MAX) return -1;
- memcpy(ename, "PROP_", 5);
- memcpy(ename + 5, key, len + 1);
-
- mutex_lock(&env_lock);
-#ifdef HAVE_MS_C_RUNTIME
- {
- char temp[256];
- snprintf( temp, sizeof(temp), "%s=%s", ename, value);
- putenv(temp);
- r = 0;
- }
-#else
- r = setenv(ename, value, 1);
-#endif
- mutex_unlock(&env_lock);
-
- return r;
-}
-
-int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
- void *cookie)
-{
- return 0;
-}
-
-#endif
diff --git a/libcutils/qtaguid.c b/libcutils/qtaguid.c
index 5bb8176..00e211c 100644
--- a/libcutils/qtaguid.c
+++ b/libcutils/qtaguid.c
@@ -1,5 +1,4 @@
-/* libcutils/qtaguid.c
-**
+/*
** Copyright 2011, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,14 +18,16 @@
#define LOG_TAG "qtaguid"
-#include <cutils/qtaguid.h>
-#include <cutils/log.h>
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
+#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
-#include <pthread.h>
+
+#include <cutils/qtaguid.h>
+#include <log/log.h>
static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl";
static const int CTRL_MAX_INPUT_LEN = 128;
@@ -103,13 +104,13 @@
pthread_once(&resTrackInitDone, qtaguid_resTrack);
- snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);
+ snprintf(lineBuf, sizeof(lineBuf), "t %d %" PRIu64 " %d", sockfd, kTag, uid);
- ALOGV("Tagging socket %d with tag %llx{%u,0} for uid %d", sockfd, kTag, tag, uid);
+ ALOGV("Tagging socket %d with tag %" PRIx64 "{%u,0} for uid %d", sockfd, kTag, tag, uid);
res = write_ctrl(lineBuf);
if (res < 0) {
- ALOGI("Tagging socket %d with tag %llx(%d) for uid %d failed errno=%d",
+ ALOGI("Tagging socket %d with tag %" PRIx64 "(%d) for uid %d failed errno=%d",
sockfd, kTag, tag, uid, res);
}
@@ -144,17 +145,17 @@
int qtaguid_deleteTagData(int tag, uid_t uid) {
char lineBuf[CTRL_MAX_INPUT_LEN];
- int fd, cnt = 0, res = 0;
+ int cnt = 0, res = 0;
uint64_t kTag = (uint64_t)tag << 32;
- ALOGV("Deleting tag data with tag %llx{%d,0} for uid %d", kTag, tag, uid);
+ ALOGV("Deleting tag data with tag %" PRIx64 "{%d,0} for uid %d", kTag, tag, uid);
pthread_once(&resTrackInitDone, qtaguid_resTrack);
- snprintf(lineBuf, sizeof(lineBuf), "d %llu %d", kTag, uid);
+ snprintf(lineBuf, sizeof(lineBuf), "d %" PRIu64 " %d", kTag, uid);
res = write_ctrl(lineBuf);
if (res < 0) {
- ALOGI("Deleteing tag data with tag %llx/%d for uid %d failed with cnt=%d errno=%d",
+ ALOGI("Deleting tag data with tag %" PRIx64 "/%d for uid %d failed with cnt=%d errno=%d",
kTag, tag, uid, cnt, errno);
}
@@ -162,8 +163,6 @@
}
int qtaguid_setPacifier(int on) {
- int param_fd;
- int res;
const char *value;
value = on ? "Y" : "N";
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index d20d217..dfc8777 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -1,6 +1,4 @@
-
-/* libs/cutils/sched_policy.c
-**
+/*
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,14 +16,17 @@
#define LOG_TAG "SchedPolicy"
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
#include <cutils/sched_policy.h>
-#include <cutils/log.h>
+#include <log/log.h>
+
+#define UNUSED __attribute__((__unused__))
/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
* Call this any place a SchedPolicy is used as an input parameter.
@@ -36,22 +37,19 @@
return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
}
-#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
+#if defined(HAVE_ANDROID_OS)
-#include <sched.h>
#include <pthread.h>
-
-#ifndef SCHED_NORMAL
- #define SCHED_NORMAL 0
-#endif
-
-#ifndef SCHED_BATCH
- #define SCHED_BATCH 3
-#endif
+#include <sched.h>
+#include <sys/prctl.h>
#define POLICY_DEBUG 0
-#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
+// This prctl is only available in Android kernels.
+#define PR_SET_TIMERSLACK_PID 41
+
+// timer slack value in nS enforced when the thread moves to background
+#define TIMER_SLACK_BG 40000000
static pthread_once_t the_once = PTHREAD_ONCE_INIT;
@@ -60,9 +58,6 @@
// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
static int bg_cgroup_fd = -1;
static int fg_cgroup_fd = -1;
-#if CAN_SET_SP_SYSTEM
-static int system_cgroup_fd = -1;
-#endif
/* Add tid to the scheduling group defined by the policy */
static int add_tid_to_cgroup(int tid, SchedPolicy policy)
@@ -78,11 +73,6 @@
case SP_AUDIO_SYS:
fd = fg_cgroup_fd;
break;
-#if CAN_SET_SP_SYSTEM
- case SP_SYSTEM:
- fd = system_cgroup_fd;
- break;
-#endif
default:
fd = -1;
break;
@@ -123,21 +113,13 @@
if (!access("/dev/cpuctl/tasks", F_OK)) {
__sys_supports_schedgroups = 1;
-#if CAN_SET_SP_SYSTEM
filename = "/dev/cpuctl/tasks";
- system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
- if (system_cgroup_fd < 0) {
- SLOGV("open of %s failed: %s\n", filename, strerror(errno));
- }
-#endif
-
- filename = "/dev/cpuctl/apps/tasks";
fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
if (fg_cgroup_fd < 0) {
SLOGE("open of %s failed: %s\n", filename, strerror(errno));
}
- filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
+ filename = "/dev/cpuctl/bg_non_interactive/tasks";
bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
if (bg_cgroup_fd < 0) {
SLOGE("open of %s failed: %s\n", filename, strerror(errno));
@@ -221,11 +203,9 @@
int get_sched_policy(int tid, SchedPolicy *policy)
{
-#ifdef HAVE_GETTID
if (tid == 0) {
tid = gettid();
}
-#endif
pthread_once(&the_once, __initialize);
if (__sys_supports_schedgroups) {
@@ -233,11 +213,9 @@
if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
return -1;
if (grpBuf[0] == '\0') {
- *policy = SP_SYSTEM;
- } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
- *policy = SP_BACKGROUND;
- } else if (!strcmp(grpBuf, "apps")) {
*policy = SP_FOREGROUND;
+ } else if (!strcmp(grpBuf, "bg_non_interactive")) {
+ *policy = SP_BACKGROUND;
} else {
errno = ERANGE;
return -1;
@@ -260,11 +238,9 @@
int set_sched_policy(int tid, SchedPolicy policy)
{
-#ifdef HAVE_GETTID
if (tid == 0) {
tid = gettid();
}
-#endif
policy = _policy(policy);
pthread_once(&the_once, __initialize);
@@ -324,6 +300,8 @@
¶m);
}
+ prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
+
return 0;
}
@@ -331,12 +309,12 @@
/* Stubs for non-Android targets. */
-int set_sched_policy(int tid, SchedPolicy policy)
+int set_sched_policy(int tid UNUSED, SchedPolicy policy UNUSED)
{
return 0;
}
-int get_sched_policy(int tid, SchedPolicy *policy)
+int get_sched_policy(int tid UNUSED, SchedPolicy *policy)
{
*policy = SP_SYSTEM_DEFAULT;
return 0;
diff --git a/libcutils/socket_inaddr_any_server.c b/libcutils/socket_inaddr_any_server.c
index 7d5dab4..6c849de 100644
--- a/libcutils/socket_inaddr_any_server.c
+++ b/libcutils/socket_inaddr_any_server.c
@@ -1,5 +1,4 @@
-/* libs/cutils/socket_inaddr_any_server.c
-**
+/*
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,13 +14,11 @@
** limitations under the License.
*/
-#include <cutils/sockets.h>
-
+#include <errno.h>
+#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <errno.h>
-#include <stddef.h>
#ifndef HAVE_WINSOCK
#include <sys/socket.h>
@@ -30,13 +27,14 @@
#include <netinet/in.h>
#endif
+#include <cutils/sockets.h>
+
#define LISTEN_BACKLOG 4
/* open listen() port on any interface */
int socket_inaddr_any_server(int port, int type)
{
struct sockaddr_in addr;
- size_t alen;
int s, n;
memset(&addr, 0, sizeof(addr));
@@ -48,7 +46,7 @@
if(s < 0) return -1;
n = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof(n));
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(s);
diff --git a/libcutils/socket_local_client.c b/libcutils/socket_local_client.c
index 5310516..7b42daa 100644
--- a/libcutils/socket_local_client.c
+++ b/libcutils/socket_local_client.c
@@ -14,13 +14,13 @@
* limitations under the License.
*/
-#include <cutils/sockets.h>
-
+#include <errno.h>
+#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <errno.h>
-#include <stddef.h>
+
+#include <cutils/sockets.h>
#ifdef HAVE_WINSOCK
@@ -52,7 +52,7 @@
switch (namespaceId) {
case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
-#ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
+#if defined(__linux__)
namelen = strlen(name);
// Test with length +1 for the *initial* '\0'.
@@ -67,7 +67,7 @@
p_addr->sun_path[0] = 0;
memcpy(p_addr->sun_path + 1, name, namelen);
-#else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#else
/* this OS doesn't have the Linux abstract namespace */
namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
@@ -79,7 +79,7 @@
strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
strcat(p_addr->sun_path, name);
-#endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#endif
break;
case ANDROID_SOCKET_NAMESPACE_RESERVED:
@@ -128,7 +128,6 @@
{
struct sockaddr_un addr;
socklen_t alen;
- size_t namelen;
int err;
err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
diff --git a/libcutils/socket_local_server.c b/libcutils/socket_local_server.c
index 4971b1b..60eb86b 100644
--- a/libcutils/socket_local_server.c
+++ b/libcutils/socket_local_server.c
@@ -43,6 +43,8 @@
#define LISTEN_BACKLOG 4
+/* Only the bottom bits are really the socket type; there are flags too. */
+#define SOCK_TYPE_MASK 0xf
/**
* Binds a pre-created socket(AF_LOCAL) 's' to 'name'
@@ -64,7 +66,7 @@
}
/* basically: if this is a filesystem path, unlink first */
-#ifndef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
+#if !defined(__linux__)
if (1) {
#else
if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED
@@ -107,7 +109,7 @@
return -1;
}
- if (type == SOCK_STREAM) {
+ if ((type & SOCK_TYPE_MASK) == SOCK_STREAM) {
int ret;
ret = listen(s, LISTEN_BACKLOG);
diff --git a/libcutils/socket_loopback_client.c b/libcutils/socket_loopback_client.c
index cb82c5e..9aed7b7 100644
--- a/libcutils/socket_loopback_client.c
+++ b/libcutils/socket_loopback_client.c
@@ -1,5 +1,4 @@
-/* libs/cutils/socket_loopback_client.c
-**
+/*
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,13 +14,11 @@
** limitations under the License.
*/
-#include <cutils/sockets.h>
-
+#include <errno.h>
+#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <errno.h>
-#include <stddef.h>
#ifndef HAVE_WINSOCK
#include <sys/socket.h>
@@ -30,6 +27,8 @@
#include <netinet/in.h>
#endif
+#include <cutils/sockets.h>
+
/* Connect to port on the loopback IP interface. type is
* SOCK_STREAM or SOCK_DGRAM.
* return is a file descriptor or -1 on error
@@ -37,7 +36,6 @@
int socket_loopback_client(int port, int type)
{
struct sockaddr_in addr;
- socklen_t alen;
int s;
memset(&addr, 0, sizeof(addr));
diff --git a/libcutils/socket_loopback_server.c b/libcutils/socket_loopback_server.c
index 3208488..71afce7 100644
--- a/libcutils/socket_loopback_server.c
+++ b/libcutils/socket_loopback_server.c
@@ -1,5 +1,4 @@
-/* libs/cutils/socket_loopback_server.c
-**
+/*
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,13 +14,11 @@
** limitations under the License.
*/
-#include <cutils/sockets.h>
-
+#include <errno.h>
+#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <errno.h>
-#include <stddef.h>
#define LISTEN_BACKLOG 4
@@ -32,11 +29,12 @@
#include <netinet/in.h>
#endif
+#include <cutils/sockets.h>
+
/* open listen() port on loopback interface */
int socket_loopback_server(int port, int type)
{
struct sockaddr_in addr;
- size_t alen;
int s, n;
memset(&addr, 0, sizeof(addr));
@@ -48,7 +46,7 @@
if(s < 0) return -1;
n = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof(n));
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(s);
diff --git a/libcutils/socket_network_client.c b/libcutils/socket_network_client.c
index a64006c..e0031ba 100644
--- a/libcutils/socket_network_client.c
+++ b/libcutils/socket_network_client.c
@@ -1,5 +1,4 @@
-/* libs/cutils/socket_network_client.c
-**
+/*
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,22 +14,20 @@
** limitations under the License.
*/
-#include <cutils/sockets.h>
-
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <errno.h>
-#include <stddef.h>
-#ifndef HAVE_WINSOCK
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
-#endif
+#include <cutils/sockets.h>
/* Connect to port on the IP interface. type is
* SOCK_STREAM or SOCK_DGRAM.
@@ -38,28 +35,91 @@
*/
int socket_network_client(const char *host, int port, int type)
{
+ return socket_network_client_timeout(host, port, type, 0);
+}
+
+/* Connect to port on the IP interface. type is SOCK_STREAM or SOCK_DGRAM.
+ * timeout in seconds return is a file descriptor or -1 on error
+ */
+int socket_network_client_timeout(const char *host, int port, int type, int timeout)
+{
struct hostent *hp;
struct sockaddr_in addr;
- socklen_t alen;
int s;
+ int flags = 0, error = 0, ret = 0;
+ fd_set rset, wset;
+ socklen_t len = sizeof(error);
+ struct timeval ts;
+
+ ts.tv_sec = timeout;
+ ts.tv_usec = 0;
hp = gethostbyname(host);
- if(hp == 0) return -1;
-
+ if (hp == 0) return -1;
+
memset(&addr, 0, sizeof(addr));
addr.sin_family = hp->h_addrtype;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
s = socket(hp->h_addrtype, type, 0);
- if(s < 0) return -1;
+ if (s < 0) return -1;
- if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ if ((flags = fcntl(s, F_GETFL, 0)) < 0) {
+ close(s);
+ return -1;
+ }
+
+ if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
+ close(s);
+ return -1;
+ }
+
+ if ((ret = connect(s, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
+ if (errno != EINPROGRESS) {
+ close(s);
+ return -1;
+ }
+ }
+
+ if (ret == 0)
+ goto done;
+
+ FD_ZERO(&rset);
+ FD_SET(s, &rset);
+ wset = rset;
+
+ if ((ret = select(s + 1, &rset, &wset, NULL, (timeout) ? &ts : NULL)) < 0) {
+ close(s);
+ return -1;
+ }
+ if (ret == 0) { // we had a timeout
+ errno = ETIMEDOUT;
+ close(s);
+ return -1;
+ }
+
+ if (FD_ISSET(s, &rset) || FD_ISSET(s, &wset)) {
+ if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
+ close(s);
+ return -1;
+ }
+ } else {
+ close(s);
+ return -1;
+ }
+
+ if (error) { // check if we had a socket error
+ errno = error;
+ close(s);
+ return -1;
+ }
+
+done:
+ if (fcntl(s, F_SETFL, flags) < 0) {
close(s);
return -1;
}
return s;
-
}
-
diff --git a/libcutils/sockets.c b/libcutils/sockets.c
index b5a1b3d..15ede2b 100644
--- a/libcutils/sockets.c
+++ b/libcutils/sockets.c
@@ -14,15 +14,18 @@
* limitations under the License.
*/
-#include <cutils/log.h>
#include <cutils/sockets.h>
+#include <log/log.h>
#ifdef HAVE_ANDROID_OS
/* For the socket trust (credentials) check */
#include <private/android_filesystem_config.h>
+#define __android_unused
+#else
+#define __android_unused __attribute__((__unused__))
#endif
-bool socket_peer_is_trusted(int fd)
+bool socket_peer_is_trusted(int fd __android_unused)
{
#ifdef HAVE_ANDROID_OS
struct ucred cr;
diff --git a/libcutils/str_parms.c b/libcutils/str_parms.c
index 7cfbcb3..dfe8c4b 100644
--- a/libcutils/str_parms.c
+++ b/libcutils/str_parms.c
@@ -25,10 +25,9 @@
#include <string.h>
#include <cutils/hashmap.h>
-#include <cutils/log.h>
#include <cutils/memory.h>
-
#include <cutils/str_parms.h>
+#include <log/log.h>
#define UNUSED __attribute__((unused))
@@ -194,23 +193,46 @@
int str_parms_add_str(struct str_parms *str_parms, const char *key,
const char *value)
{
- void *old_val;
- void *tmp_key;
- void *tmp_val;
+ void *tmp_key = NULL;
+ void *tmp_val = NULL;
+ void *old_val = NULL;
+
+ // strdup and hashmapPut both set errno on failure.
+ // Set errno to 0 so we can recognize whether anything went wrong.
+ int saved_errno = errno;
+ errno = 0;
tmp_key = strdup(key);
- tmp_val = strdup(value);
- old_val = hashmapPut(str_parms->map, tmp_key, tmp_val);
-
- if (old_val) {
- free(old_val);
- free(tmp_key);
- } else if (errno == ENOMEM) {
- free(tmp_key);
- free(tmp_val);
- return -ENOMEM;
+ if (tmp_key == NULL) {
+ goto clean_up;
}
- return 0;
+
+ tmp_val = strdup(value);
+ if (tmp_val == NULL) {
+ goto clean_up;
+ }
+
+ old_val = hashmapPut(str_parms->map, tmp_key, tmp_val);
+ if (old_val == NULL) {
+ // Did hashmapPut fail?
+ if (errno == ENOMEM) {
+ goto clean_up;
+ }
+ // For new keys, hashmap takes ownership of tmp_key and tmp_val.
+ tmp_key = tmp_val = NULL;
+ } else {
+ // For existing keys, hashmap takes ownership of tmp_val.
+ // (It also gives up ownership of old_val.)
+ tmp_val = NULL;
+ }
+
+clean_up:
+ free(tmp_key);
+ free(tmp_val);
+ free(old_val);
+ int result = -errno;
+ errno = saved_errno;
+ return result;
}
int str_parms_add_int(struct str_parms *str_parms, const char *key, int value)
@@ -240,6 +262,10 @@
return ret;
}
+int str_parms_has_key(struct str_parms *str_parms, const char *key) {
+ return hashmapGet(str_parms->map, (void *)key) != NULL;
+}
+
int str_parms_get_str(struct str_parms *str_parms, const char *key, char *val,
int len)
{
@@ -337,7 +363,6 @@
{
struct str_parms *str_parms;
char *out_str;
- int ret;
str_parms = str_parms_create_str(str);
str_parms_add_str(str_parms, "dude", "woah");
@@ -352,8 +377,6 @@
int main(void)
{
- struct str_parms *str_parms;
-
test_str_parms_str("");
test_str_parms_str(";");
test_str_parms_str("=");
@@ -370,6 +393,15 @@
test_str_parms_str("foo=bar;baz=bat;");
test_str_parms_str("foo=bar;baz=bat;foo=bar");
+ // hashmapPut reports errors by setting errno to ENOMEM.
+ // Test that we're not confused by running in an environment where this is already true.
+ errno = ENOMEM;
+ test_str_parms_str("foo=bar;baz=");
+ if (errno != ENOMEM) {
+ abort();
+ }
+ test_str_parms_str("foo=bar;baz=");
+
return 0;
}
#endif
diff --git a/libcutils/tests/Android.mk b/libcutils/tests/Android.mk
index 6571161..5a54698 100644
--- a/libcutils/tests/Android.mk
+++ b/libcutils/tests/Android.mk
@@ -1 +1,52 @@
-include $(all-subdir-makefiles)
+# Copyright (C) 2014 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)
+
+test_src_files := \
+ MemsetTest.cpp \
+ PropertiesTest.cpp \
+
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_MODULE := libcutils_test
+LOCAL_SRC_FILES := $(test_src_files)
+LOCAL_SHARED_LIBRARIES := \
+ libcutils \
+ liblog \
+ libutils \
+
+LOCAL_MULTILIB := both
+LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+include $(BUILD_NATIVE_TEST)
+
+# The static libcutils tests cannot be built when using libc++ because there are
+# multiple symbol definition errors between libc++ and libgcc. b/18389856
+#include $(CLEAR_VARS)
+#LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+#LOCAL_MODULE := libcutils_test_static
+#LOCAL_FORCE_STATIC_EXECUTABLE := true
+#LOCAL_SRC_FILES := $(test_src_files)
+#LOCAL_STATIC_LIBRARIES := \
+# libc \
+# libcutils \
+# liblog \
+# libutils \
+
+#LOCAL_CXX_STL := stlport_static
+#LOCAL_MULTILIB := both
+#LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+#LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+#include $(BUILD_NATIVE_TEST)
diff --git a/libcutils/tests/MemsetTest.cpp b/libcutils/tests/MemsetTest.cpp
new file mode 100644
index 0000000..45efc51
--- /dev/null
+++ b/libcutils/tests/MemsetTest.cpp
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2014 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 <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include <cutils/memory.h>
+#include <gtest/gtest.h>
+
+#define FENCEPOST_LENGTH 8
+
+#define MAX_TEST_SIZE (64*1024)
+// Choose values that have no repeating byte values.
+#define MEMSET16_PATTERN 0xb139
+#define MEMSET32_PATTERN 0x48193a27
+
+enum test_e {
+ MEMSET16 = 0,
+ MEMSET32,
+};
+
+static int g_memset16_aligns[][2] = {
+ { 2, 0 },
+ { 4, 0 },
+ { 8, 0 },
+ { 16, 0 },
+ { 32, 0 },
+ { 64, 0 },
+ { 128, 0 },
+
+ { 4, 2 },
+
+ { 8, 2 },
+ { 8, 4 },
+ { 8, 6 },
+
+ { 128, 2 },
+ { 128, 4 },
+ { 128, 6 },
+ { 128, 8 },
+ { 128, 10 },
+ { 128, 12 },
+ { 128, 14 },
+ { 128, 16 },
+};
+
+static int g_memset32_aligns[][2] = {
+ { 4, 0 },
+ { 8, 0 },
+ { 16, 0 },
+ { 32, 0 },
+ { 64, 0 },
+ { 128, 0 },
+
+ { 8, 4 },
+
+ { 128, 4 },
+ { 128, 8 },
+ { 128, 12 },
+ { 128, 16 },
+};
+
+static size_t GetIncrement(size_t len, size_t min_incr) {
+ if (len >= 4096) {
+ return 1024;
+ } else if (len >= 1024) {
+ return 256;
+ }
+ return min_incr;
+}
+
+// Return a pointer into the current buffer with the specified alignment.
+static void *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) {
+ uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
+ if (alignment > 0) {
+ // When setting the alignment, set it to exactly the alignment chosen.
+ // The pointer returned will be guaranteed not to be aligned to anything
+ // more than that.
+ ptr += alignment - (ptr & (alignment - 1));
+ ptr |= alignment | or_mask;
+ }
+
+ return reinterpret_cast<void*>(ptr);
+}
+
+static void SetFencepost(uint8_t *buffer) {
+ for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
+ buffer[i] = 0xde;
+ buffer[i+1] = 0xad;
+ }
+}
+
+static void VerifyFencepost(uint8_t *buffer) {
+ for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
+ if (buffer[i] != 0xde || buffer[i+1] != 0xad) {
+ uint8_t expected_value;
+ if (buffer[i] == 0xde) {
+ i++;
+ expected_value = 0xad;
+ } else {
+ expected_value = 0xde;
+ }
+ ASSERT_EQ(expected_value, buffer[i]);
+ }
+ }
+}
+
+void RunMemsetTests(test_e test_type, uint32_t value, int align[][2], size_t num_aligns) {
+ size_t min_incr = 4;
+ if (test_type == MEMSET16) {
+ min_incr = 2;
+ value |= value << 16;
+ }
+ uint32_t* expected_buf = new uint32_t[MAX_TEST_SIZE/sizeof(uint32_t)];
+ for (size_t i = 0; i < MAX_TEST_SIZE/sizeof(uint32_t); i++) {
+ expected_buf[i] = value;
+ }
+
+ // Allocate one large buffer with lots of extra space so that we can
+ // guarantee that all possible alignments will fit.
+ uint8_t *buf = new uint8_t[3*MAX_TEST_SIZE];
+ uint8_t *buf_align;
+ for (size_t i = 0; i < num_aligns; i++) {
+ size_t incr = min_incr;
+ for (size_t len = incr; len <= MAX_TEST_SIZE; len += incr) {
+ incr = GetIncrement(len, min_incr);
+
+ buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr(
+ buf+FENCEPOST_LENGTH, align[i][0], align[i][1]));
+
+ SetFencepost(&buf_align[-FENCEPOST_LENGTH]);
+ SetFencepost(&buf_align[len]);
+
+ memset(buf_align, 0xff, len);
+ if (test_type == MEMSET16) {
+ android_memset16(reinterpret_cast<uint16_t*>(buf_align), value, len);
+ } else {
+ android_memset32(reinterpret_cast<uint32_t*>(buf_align), value, len);
+ }
+ ASSERT_EQ(0, memcmp(expected_buf, buf_align, len))
+ << "Failed size " << len << " align " << align[i][0] << " " << align[i][1] << "\n";
+
+ VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
+ VerifyFencepost(&buf_align[len]);
+ }
+ }
+ delete expected_buf;
+ delete buf;
+}
+
+TEST(libcutils, android_memset16_non_zero) {
+ RunMemsetTests(MEMSET16, MEMSET16_PATTERN, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
+}
+
+TEST(libcutils, android_memset16_zero) {
+ RunMemsetTests(MEMSET16, 0, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
+}
+
+TEST(libcutils, android_memset32_non_zero) {
+ RunMemsetTests(MEMSET32, MEMSET32_PATTERN, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
+}
+
+TEST(libcutils, android_memset32_zero) {
+ RunMemsetTests(MEMSET32, 0, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
+}
diff --git a/libcutils/tests/PropertiesTest.cpp b/libcutils/tests/PropertiesTest.cpp
new file mode 100644
index 0000000..659821c
--- /dev/null
+++ b/libcutils/tests/PropertiesTest.cpp
@@ -0,0 +1,309 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#define LOG_TAG "Properties_test"
+#include <utils/Log.h>
+#include <gtest/gtest.h>
+
+#include <cutils/properties.h>
+#include <limits.h>
+#include <string>
+#include <sstream>
+#include <iostream>
+
+namespace android {
+
+#define STRINGIFY_INNER(x) #x
+#define STRINGIFY(x) STRINGIFY_INNER(x)
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
+#define ASSERT_OK(x) ASSERT_EQ(0, (x))
+#define EXPECT_OK(x) EXPECT_EQ(0, (x))
+
+#define PROPERTY_TEST_KEY "libcutils.test.key"
+#define PROPERTY_TEST_VALUE_DEFAULT "<<<default_value>>>"
+
+template <typename T>
+static std::string HexString(T value) {
+ std::stringstream ss;
+ ss << "0x" << std::hex << std::uppercase << value;
+ return ss.str();
+}
+
+template <typename T>
+static ::testing::AssertionResult AssertEqualHex(const char *mExpr,
+ const char *nExpr,
+ T m,
+ T n) {
+ if (m == n) {
+ return ::testing::AssertionSuccess();
+ }
+
+ return ::testing::AssertionFailure()
+ << mExpr << " and " << nExpr << " (expected: " << HexString(m) <<
+ ", actual: " << HexString(n) << ") are not equal";
+}
+
+class PropertiesTest : public testing::Test {
+public:
+ PropertiesTest() : mValue() {}
+protected:
+ virtual void SetUp() {
+ EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
+ }
+
+ virtual void TearDown() {
+ EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
+ }
+
+ char mValue[PROPERTY_VALUE_MAX];
+
+ template <typename T>
+ static std::string ToString(T value) {
+ std::stringstream ss;
+ ss << value;
+
+ return ss.str();
+ }
+
+ // Return length of property read; value is written into mValue
+ int SetAndGetProperty(const char* value, const char* defaultValue = PROPERTY_TEST_VALUE_DEFAULT) {
+ EXPECT_OK(property_set(PROPERTY_TEST_KEY, value)) << "value: '" << value << "'";
+ return property_get(PROPERTY_TEST_KEY, mValue, defaultValue);
+ }
+
+ void ResetValue(unsigned char c = 0xFF) {
+ for (size_t i = 0; i < ARRAY_SIZE(mValue); ++i) {
+ mValue[i] = (char) c;
+ }
+ }
+};
+
+TEST_F(PropertiesTest, SetString) {
+
+ // Null key -> unsuccessful set
+ {
+ // Null key -> fails
+ EXPECT_GT(0, property_set(/*key*/NULL, PROPERTY_TEST_VALUE_DEFAULT));
+ }
+
+ // Null value -> returns default value
+ {
+ // Null value -> OK , and it clears the value
+ EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
+ ResetValue();
+
+ // Since the value is null, default value will be returned
+ int len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
+ EXPECT_EQ(strlen(PROPERTY_TEST_VALUE_DEFAULT), len);
+ EXPECT_STREQ(PROPERTY_TEST_VALUE_DEFAULT, mValue);
+ }
+
+ // Trivial case => get returns what was set
+ {
+ int len = SetAndGetProperty("hello_world");
+ EXPECT_EQ(strlen("hello_world"), len) << "hello_world key";
+ EXPECT_STREQ("hello_world", mValue);
+ ResetValue();
+ }
+
+ // Set to empty string => get returns default always
+ {
+ const char* EMPTY_STRING_DEFAULT = "EMPTY_STRING";
+ int len = SetAndGetProperty("", EMPTY_STRING_DEFAULT);
+ EXPECT_EQ(strlen(EMPTY_STRING_DEFAULT), len) << "empty key";
+ EXPECT_STREQ(EMPTY_STRING_DEFAULT, mValue);
+ ResetValue();
+ }
+
+ // Set to max length => get returns what was set
+ {
+ std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
+
+ int len = SetAndGetProperty(maxLengthString.c_str());
+ EXPECT_EQ(PROPERTY_VALUE_MAX-1, len) << "max length key";
+ EXPECT_STREQ(maxLengthString.c_str(), mValue);
+ ResetValue();
+ }
+
+ // Set to max length + 1 => set fails
+ {
+ const char* VALID_TEST_VALUE = "VALID_VALUE";
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, VALID_TEST_VALUE));
+
+ std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
+
+ // Expect that the value set fails since it's too long
+ EXPECT_GT(0, property_set(PROPERTY_TEST_KEY, oneLongerString.c_str()));
+ int len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
+
+ EXPECT_EQ(strlen(VALID_TEST_VALUE), len) << "set should've failed";
+ EXPECT_STREQ(VALID_TEST_VALUE, mValue);
+ ResetValue();
+ }
+}
+
+TEST_F(PropertiesTest, GetString) {
+
+ // Try to use a default value that's too long => set fails
+ {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
+
+ std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
+ std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
+
+ // Expect that the value is truncated since it's too long (by 1)
+ int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
+ EXPECT_EQ(PROPERTY_VALUE_MAX-1, len);
+ EXPECT_STREQ(maxLengthString.c_str(), mValue);
+ ResetValue();
+ }
+}
+
+TEST_F(PropertiesTest, GetBool) {
+ /**
+ * TRUE
+ */
+ const char *valuesTrue[] = { "1", "true", "y", "yes", "on", };
+ for (size_t i = 0; i < ARRAY_SIZE(valuesTrue); ++i) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesTrue[i]));
+ bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false);
+ EXPECT_TRUE(val) << "Property should've been TRUE for value: '" << valuesTrue[i] << "'";
+ }
+
+ /**
+ * FALSE
+ */
+ const char *valuesFalse[] = { "0", "false", "n", "no", "off", };
+ for (size_t i = 0; i < ARRAY_SIZE(valuesFalse); ++i) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesFalse[i]));
+ bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true);
+ EXPECT_FALSE(val) << "Property shoud've been FALSE For string value: '" << valuesFalse[i] << "'";
+ }
+
+ /**
+ * NEITHER
+ */
+ const char *valuesNeither[] = { "x0", "x1", "2", "-2", "True", "False", "garbage", "", " ",
+ "+1", " 1 ", " true", " true ", " y ", " yes", "yes ",
+ "+0", "-0", "00", " 00 ", " false", "false ",
+ };
+ for (size_t i = 0; i < ARRAY_SIZE(valuesNeither); ++i) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesNeither[i]));
+
+ // The default value should always be used
+ bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true);
+ EXPECT_TRUE(val) << "Property should've been NEITHER (true) for string value: '" << valuesNeither[i] << "'";
+
+ val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false);
+ EXPECT_FALSE(val) << "Property should've been NEITHER (false) for string value: '" << valuesNeither[i] << "'";
+ }
+}
+
+TEST_F(PropertiesTest, GetInt64) {
+ const int64_t DEFAULT_VALUE = INT64_C(0xDEADBEEFBEEFDEAD);
+
+ const std::string longMaxString = ToString(INT64_MAX);
+ const std::string longStringOverflow = longMaxString + "0";
+
+ const std::string longMinString = ToString(INT64_MIN);
+ const std::string longStringUnderflow = longMinString + "0";
+
+ const char* setValues[] = {
+ // base 10
+ "1", "2", "12345", "-1", "-2", "-12345",
+ // base 16
+ "0xFF", "0x0FF", "0xC0FFEE",
+ // base 8
+ "0", "01234", "07",
+ // corner cases
+ " 2", "2 ", "+0", "-0", " +0 ", longMaxString.c_str(), longMinString.c_str(),
+ // failing cases
+ NULL, "", " ", " ", "hello", " true ", "y",
+ longStringOverflow.c_str(), longStringUnderflow.c_str(),
+ };
+
+ int64_t getValues[] = {
+ // base 10
+ 1, 2, 12345, -1, -2, -12345,
+ // base 16
+ 0xFF, 0x0FF, 0xC0FFEE,
+ // base 8
+ 0, 01234, 07,
+ // corner cases
+ 2, 2, 0, 0, 0, INT64_MAX, INT64_MIN,
+ // failing cases
+ DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE,
+ DEFAULT_VALUE, DEFAULT_VALUE,
+ };
+
+ ASSERT_EQ(ARRAY_SIZE(setValues), ARRAY_SIZE(getValues));
+
+ for (size_t i = 0; i < ARRAY_SIZE(setValues); ++i) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i]));
+
+ int64_t val = property_get_int64(PROPERTY_TEST_KEY, DEFAULT_VALUE);
+ EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'";
+ }
+}
+
+TEST_F(PropertiesTest, GetInt32) {
+ const int32_t DEFAULT_VALUE = INT32_C(0xDEADBEEF);
+
+ const std::string intMaxString = ToString(INT32_MAX);
+ const std::string intStringOverflow = intMaxString + "0";
+
+ const std::string intMinString = ToString(INT32_MIN);
+ const std::string intStringUnderflow = intMinString + "0";
+
+ const char* setValues[] = {
+ // base 10
+ "1", "2", "12345", "-1", "-2", "-12345",
+ // base 16
+ "0xFF", "0x0FF", "0xC0FFEE", "0Xf00",
+ // base 8
+ "0", "01234", "07",
+ // corner cases
+ " 2", "2 ", "+0", "-0", " +0 ", intMaxString.c_str(), intMinString.c_str(),
+ // failing cases
+ NULL, "", " ", " ", "hello", " true ", "y",
+ intStringOverflow.c_str(), intStringUnderflow.c_str(),
+ };
+
+ int32_t getValues[] = {
+ // base 10
+ 1, 2, 12345, -1, -2, -12345,
+ // base 16
+ 0xFF, 0x0FF, 0xC0FFEE, 0Xf00,
+ // base 8
+ 0, 01234, 07,
+ // corner cases
+ 2, 2, 0, 0, 0, INT32_MAX, INT32_MIN,
+ // failing cases
+ DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE,
+ DEFAULT_VALUE, DEFAULT_VALUE,
+ };
+
+ ASSERT_EQ(ARRAY_SIZE(setValues), ARRAY_SIZE(getValues));
+
+ for (size_t i = 0; i < ARRAY_SIZE(setValues); ++i) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i]));
+
+ int32_t val = property_get_int32(PROPERTY_TEST_KEY, DEFAULT_VALUE);
+ EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'";
+ }
+}
+
+} // namespace android
diff --git a/libcutils/tests/memset_mips/Android.mk b/libcutils/tests/memset_mips/Android.mk
deleted file mode 100644
index c22fca9..0000000
--- a/libcutils/tests/memset_mips/Android.mk
+++ /dev/null
@@ -1,23 +0,0 @@
-# Copyright 2012 The Android Open Source Project
-
-ifeq ($(TARGET_ARCH),mips)
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
- test_memset.c \
- android_memset_dumb.S \
- android_memset_test.S \
- memset_cmips.S \
- memset_omips.S
-
-LOCAL_MODULE:= test_memset
-
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-LOCAL_STATIC_LIBRARIES := libcutils libc
-LOCAL_MODULE_TAGS := tests
-
-include $(BUILD_EXECUTABLE)
-
-endif
diff --git a/libcutils/tests/memset_mips/android_memset_dumb.S b/libcutils/tests/memset_mips/android_memset_dumb.S
deleted file mode 100644
index c8a1a37..0000000
--- a/libcutils/tests/memset_mips/android_memset_dumb.S
+++ /dev/null
@@ -1,36 +0,0 @@
- .global android_memset16_dumb
- .type android_memset16_dumb, @function
-android_memset16_dumb:
- .ent android_memset16_dumb
-
- .set noreorder
- beqz $a2,9f
- srl $a2,1
-
-1: sh $a1,($a0)
- subu $a2,1
- bnez $a2,1b
- addu $a0,2
- .set reorder
-
-9: j $ra
- .end android_memset16_dumb
- .size android_memset16_dumb,.-android_memset16_dumb
-
- .global android_memset32_dumb
- .type android_memset32_dumb, @function
-android_memset32_dumb:
- .ent android_memset32_dumb
- .set noreorder
- beqz $a2,9f
- srl $a2,2
-
-1: sw $a1,($a0)
- subu $a2,1
- bnez $a2,1b
- addu $a0,4
- .set reorder
-
-9: j $ra
- .end android_memset32_dumb
- .size android_memset32_dumb,.-android_memset32_dumb
diff --git a/libcutils/tests/memset_mips/android_memset_test.S b/libcutils/tests/memset_mips/android_memset_test.S
deleted file mode 100644
index e918843..0000000
--- a/libcutils/tests/memset_mips/android_memset_test.S
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * Copyright (C) 2006 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.
- */
-
-#ifdef NDEBUG
-#define DBG #
-#else
-#define DBG
-#endif
-
- .text
- .align
-
- /*
- * Optimized memset16 for MIPS
- *
- * void android_memset16_test(uint16_t* dst, uint16_t value, size_t size);
- *
- */
-
- .global android_memset16_test
- .type android_memset16_test, @function
-android_memset16_test:
- .ent android_memset16_test
- .set noreorder
-
- /* Check parameters */
-DBG andi $t0,$a0,1 /* $a0 must be halfword aligned */
-DBG tne $t0
-DBG lui $t1,0xffff /* $a1 must be 16bits */
-DBG and $t1,$a1
-DBG tne $t1
-DBG andi $t2,$a2,1 /* $a2 must be even */
-DBG tne $t2
-
-#if (__mips==32) && (__mips_isa_rev>=2)
- ins $a2,$0,0,1
-#else
- li $t0,~1
- and $a2,$t0
-#endif
-
- move $t8,$ra
- blez $a2,9f /* Anything to do? */
- andi $t0,$a0,2 /* Check dst alignment */
- /* Expand value to 32 bits and check destination alignment */
-#if (__mips==32) && (__mips_isa_rev>=2)
- beqz $t0,.Laligned32 /* dst is 32 bit aligned */
- ins $a1,$a1,16,16
-#else
- sll $t2,$a1,16
- beqz $t0,.Laligned32 /* dst is 32 bit aligned */
- or $a1,$t2
-#endif
- sh $a1,($a0) /* do one halfword to get aligned */
- subu $a2,2
- addu $a0,2
-
-.Laligned32:
- and $t1,$a2,63 /* is there enough left to do a full 64 byte loop? */
- beq $a2,$t1,1f
- subu $t2,$a2,$t1 /* $t2 is the number of bytes to do in loop64 */
- addu $t3,$a0,$t2 /* $t3 is the end marker for loop64 */
- subu $a2,$t2
-.Lloop64:
- addu $a0,64
- sw $a1,-64($a0)
- sw $a1,-60($a0)
- sw $a1,-56($a0)
- sw $a1,-52($a0)
- sw $a1,-48($a0)
- sw $a1,-44($a0)
- sw $a1,-40($a0)
- sw $a1,-36($a0)
- sw $a1,-32($a0)
- sw $a1,-28($a0)
- sw $a1,-24($a0)
- sw $a1,-20($a0)
- sw $a1,-16($a0)
- sw $a1,-12($a0)
- sw $a1,-8($a0)
- bne $a0,$t3,.Lloop64
- sw $a1,-4($a0)
-
- /* Do the last 0..62 bytes */
-1: li $t0,64+12
- andi $t1,$a2,0x3c /* $t1 how many bytes to store using sw */
- bal 1f
- subu $t0,$t1 /* 64+12-$t0 is offset to jump from 1f */
-1: addu $ra,$t0
- j $ra
- subu $a2,$t1
-2: sw $a1,60($a0)
- sw $a1,56($a0)
- sw $a1,52($a0)
- sw $a1,48($a0)
- sw $a1,44($a0)
- sw $a1,40($a0)
- sw $a1,36($a0)
- sw $a1,32($a0)
- sw $a1,28($a0)
- sw $a1,24($a0)
- sw $a1,20($a0)
- sw $a1,16($a0)
- sw $a1,12($a0)
- sw $a1,8($a0)
- sw $a1,4($a0)
- sw $a1,0($a0)
-
- beqz $a2,9f
- addu $a0,$t1
- sh $a1,($a0)
-
-9: j $t8
- nop
- .end android_memset16_test
- .size android_memset16_test,.-android_memset16_test
-
- /*
- * Optimized memset32 for MIPS
- *
- * void android_memset32_test(uint32_t* dst, uint32_t value, size_t size);
- *
- */
- .global android_memset32_test
- .type android_memset32_test, @function
-android_memset32_test:
- .ent android_memset32_test
- .set noreorder
-
- /* Check parameters */
-DBG andi $t0,$a0,3 /* $a0 must be word aligned */
-DBG tne $t0
-DBG andi $t2,$a2,3 /* $a2 must be a multiple of 4 bytes */
-DBG tne $t2
-
- b .Laligned32
- move $t8,$ra
- .end android_memset32_test
- .size android_memset32_test,.-android_memset32_test
diff --git a/libcutils/tests/memset_mips/memset_cmips.S b/libcutils/tests/memset_mips/memset_cmips.S
deleted file mode 100644
index f8f3a91..0000000
--- a/libcutils/tests/memset_mips/memset_cmips.S
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (c) 2009
- * MIPS Technologies, Inc., California.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/************************************************************************
- *
- * memset.S, version "64h" with 1 cache line horizon for "pref 30" and 14 nops
- * Version: "043009"
- *
- ************************************************************************/
-
-
-/************************************************************************
- * Include files
- ************************************************************************/
-
-#include "machine/asm.h"
-
-/*
- * This routine could be optimized for MIPS64. The current code only
- * uses MIPS32 instructions.
- */
-
-#if defined(__MIPSEB__)
-# define SWHI swl /* high part is left in big-endian */
-#endif
-
-#if defined(__MIPSEL__)
-# define SWHI swr /* high part is right in little-endian */
-#endif
-
-#if !(defined(XGPROF) || defined(XPROF))
-#undef SETUP_GP
-#define SETUP_GP
-#endif
-
-LEAF(memset_cmips,0)
-
- .set noreorder
- .set noat
-
- addu t0,a0,a2 # t0 is the "past the end" address
- slti AT,a2,4 # is a2 less than 4?
- bne AT,zero,.Llast4 # if yes, go to last4
- move v0,a0 # memset returns the dst pointer
-
- beq a1,zero,.Lset0
- subu v1,zero,a0
-
- # smear byte into 32 bit word
-#if (__mips==32) && (__mips_isa_rev>=2)
- ins a1, a1, 8, 8 # Replicate fill byte into half-word.
- ins a1, a1, 16, 16 # Replicate fill byte into word.
-#else
- and a1,0xff
- sll AT,a1,8
- or a1,AT
- sll AT,a1,16
- or a1,AT
-#endif
-
-.Lset0: andi v1,v1,0x3 # word-unaligned address?
- beq v1,zero,.Laligned # v1 is the unalignment count
- subu a2,a2,v1
- SWHI a1,0(a0)
- addu a0,a0,v1
-
-# Here we have the "word-aligned" a0 (until the "last4")
-.Laligned:
- andi t8,a2,0x3f # any 64-byte chunks?
- # t8 is the byte count past 64-byte chunks
- beq a2,t8,.Lchk8w # when a2==t8, no 64-byte chunks
- # There will be at most 1 32-byte chunk then
- subu a3,a2,t8 # subtract from a2 the reminder
- # Here a3 counts bytes in 16w chunks
- addu a3,a0,a3 # Now a3 is the final dst after 64-byte chunks
-
-# Find out, if there are any 64-byte chunks after which will be still at least
-# 96 bytes left. The value "96" is calculated as needed buffer for
-# "pref 30,64(a0)" prefetch, which can be used as "pref 30,0(a0)" after
-# incrementing "a0" by 64.
-# For "a2" below 160 there will be no such "pref 30 safe" 64-byte chunk.
-#
- sltiu v1,a2,160
- bgtz v1,.Lloop16w_nopref30 # skip "pref 30,0(a0)"
- subu t7,a2,96 # subtract "pref 30 unsafe" region
- # below we have at least 1 64-byte chunk which is "pref 30 safe"
- andi t6,t7,0x3f # t6 is past "64-byte safe chunks" reminder
- subu t5,t7,t6 # subtract from t7 the reminder
- # Here t5 counts bytes in 16w "safe" chunks
- addu t4,a0,t5 # Now t4 is the dst after 64-byte "safe" chunks
-
-# Don't use "pref 30,0(a0)" for a0 in a "middle" of a cache line
-# pref 30,0(a0)
-# Here we are in the region, where it is safe to use "pref 30,64(a0)"
-.Lloop16w:
- addiu a0,a0,64
- pref 30,-32(a0) # continue setting up the dest, addr 64-32
- sw a1,-64(a0)
- sw a1,-60(a0)
- sw a1,-56(a0)
- sw a1,-52(a0)
- sw a1,-48(a0)
- sw a1,-44(a0)
- sw a1,-40(a0)
- sw a1,-36(a0)
- nop
- nop # the extra nop instructions help to balance
- nop # cycles needed for "store" + "fill" + "evict"
- nop # For 64byte store there are needed 8 fill
- nop # and 8 evict cycles, i.e. at least 32 instr.
- nop
- nop
- pref 30,0(a0) # continue setting up the dest, addr 64-0
- sw a1,-32(a0)
- sw a1,-28(a0)
- sw a1,-24(a0)
- sw a1,-20(a0)
- sw a1,-16(a0)
- sw a1,-12(a0)
- sw a1,-8(a0)
- sw a1,-4(a0)
- nop
- nop
- nop
- nop # NOTE: adding 14 nop-s instead of 12 nop-s
- nop # gives better results for "fast" memory
- nop
- bne a0,t4,.Lloop16w
- nop
-
- beq a0,a3,.Lchk8w # maybe no more 64-byte chunks?
- nop # this "delayed slot" is useless ...
-
-.Lloop16w_nopref30: # there could be up to 3 "64-byte nopref30" chunks
- addiu a0,a0,64
- sw a1,-64(a0)
- sw a1,-60(a0)
- sw a1,-56(a0)
- sw a1,-52(a0)
- sw a1,-48(a0)
- sw a1,-44(a0)
- sw a1,-40(a0)
- sw a1,-36(a0)
- sw a1,-32(a0)
- sw a1,-28(a0)
- sw a1,-24(a0)
- sw a1,-20(a0)
- sw a1,-16(a0)
- sw a1,-12(a0)
- sw a1,-8(a0)
- bne a0,a3,.Lloop16w_nopref30
- sw a1,-4(a0)
-
-.Lchk8w: # t8 here is the byte count past 64-byte chunks
-
- andi t7,t8,0x1f # is there a 32-byte chunk?
- # the t7 is the reminder count past 32-bytes
- beq t8,t7,.Lchk1w # when t8==t7, no 32-byte chunk
- move a2,t7
-
- sw a1,0(a0)
- sw a1,4(a0)
- sw a1,8(a0)
- sw a1,12(a0)
- sw a1,16(a0)
- sw a1,20(a0)
- sw a1,24(a0)
- sw a1,28(a0)
- addiu a0,a0,32
-
-.Lchk1w:
- andi t8,a2,0x3 # now t8 is the reminder past 1w chunks
- beq a2,t8,.Llast4
- subu a3,a2,t8 # a3 is the count of bytes in 1w chunks
- addu a3,a0,a3 # now a3 is the dst address past the 1w chunks
-
-# copying in words (4-byte chunks)
-.LwordCopy_loop:
- addiu a0,a0,4
- bne a0,a3,.LwordCopy_loop
- sw a1,-4(a0)
-
-.Llast4:beq a0,t0,.Llast4e
-.Llast4l:addiu a0,a0,1
- bne a0,t0,.Llast4l
- sb a1,-1(a0)
-
-.Llast4e:
- j ra
- nop
-
- .set at
- .set reorder
-
-END(memset_cmips)
-
-
-/************************************************************************
- * Implementation : Static functions
- ************************************************************************/
-
diff --git a/libcutils/tests/memset_mips/memset_omips.S b/libcutils/tests/memset_mips/memset_omips.S
deleted file mode 100644
index 4c47001..0000000
--- a/libcutils/tests/memset_mips/memset_omips.S
+++ /dev/null
@@ -1,90 +0,0 @@
-/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
- Contributed by Hartvig Ekner <hartvige@mips.com>, 2002.
-
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
-
-/* void *memset_omips(void *s, int c, size_t n). */
-
-#include "machine/asm.h"
-
-#ifdef __mips64
-#error mips32 code being compiled for mips64!
-#endif
-
-#if defined(__MIPSEB__)
-#error big-endian is not supported in Broadcom MIPS Android platform
-# define SWHI swl /* high part is left in big-endian */
-#else
-# define SWHI swr /* high part is right in little-endian */
-#endif
-
-LEAF (memset_omips,0)
- .set noreorder
-
- slti t1, a2, 8 # Less than 8?
- bne t1, zero, .Llast8
- move v0, a0 # Setup exit value before too late
-
- beq a1, zero, .Lueven # If zero pattern, no need to extend
- andi a1, 0xff # Avoid problems with bogus arguments
- sll t0, a1, 8
- or a1, t0
- sll t0, a1, 16
- or a1, t0 # a1 is now pattern in full word
-
-.Lueven:
- subu t0, zero, a0 # Unaligned address?
- andi t0, 0x3
- beq t0, zero, .Lchkw
- subu a2, t0
- SWHI a1, 0(a0) # Yes, handle first unaligned part
- addu a0, t0 # Now both a0 and a2 are updated
-
-.Lchkw:
- andi t0, a2, 0x7 # Enough left for one loop iteration?
- beq t0, a2, .Lchkl
- subu a3, a2, t0
- addu a3, a0 # a3 is last loop address +1
- move a2, t0 # a2 is now # of bytes left after loop
-.Lloopw:
- addiu a0, 8 # Handle 2 words pr. iteration
- sw a1, -8(a0)
- bne a0, a3, .Lloopw
- sw a1, -4(a0)
-
-.Lchkl:
- andi t0, a2, 0x4 # Check if there is at least a full
- beq t0, zero, .Llast8 # word remaining after the loop
- subu a2, t0
- sw a1, 0(a0) # Yes...
- addiu a0, 4
-
-.Llast8:
- blez a2, .Lexit # Handle last 8 bytes (if cnt>0)
- addu a3, a2, a0 # a3 is last address +1
-.Llst8l:
- addiu a0, 1
- bne a0, a3, .Llst8l
- sb a1, -1(a0)
-.Lexit:
- j ra # Bye, bye
- nop
-
- .set reorder
-END (memset_omips)
-
-
diff --git a/libcutils/tests/memset_mips/test_memset.c b/libcutils/tests/memset_mips/test_memset.c
deleted file mode 100644
index 9705c65..0000000
--- a/libcutils/tests/memset_mips/test_memset.c
+++ /dev/null
@@ -1,235 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <cutils/memory.h>
-#include <time.h>
-
-/*
- * All systems must implement or emulate the rdhwr instruction to read
- * the userlocal register. Systems that emulate also return teh count register
- * when accessing register $2 so this should work on most systems
- */
-#define USE_RDHWR
-
-#ifdef USE_RDHWR
-#define UNITS "cycles"
-#define SCALE 2 /* Most CPU's */
-static inline uint32_t
-get_count(void)
-{
- uint32_t res;
- asm volatile (".set push; .set mips32r2; rdhwr %[res],$2; .set pop" : [res] "=r" (res) : : "memory");
- return res;
-}
-#else
-#define UNITS "ns"
-#define SCALE 1
-static inline uint32_t
-get_count(void)
-{
- struct timespec now;
- uint32_t res;
- clock_gettime(CLOCK_REALTIME, &now);
- res = (uint32_t)(now.tv_sec * 1000000000LL + now.tv_nsec);
- // printf ("now=%d.%09d res=%d\n", (int)now.tv_sec, (int)now.tv_nsec, res);
- return res;
-}
-#endif
-
-uint32_t overhead;
-void
-measure_overhead(void)
-{
- int i;
- uint32_t start, stop, delta;
- for (i = 0; i < 32; i++) {
- start = get_count();
- stop = get_count();
- delta = stop - start;
- if (overhead == 0 || delta < overhead)
- overhead = delta;
- }
- printf("overhead is %d"UNITS"\n", overhead);
-}
-
-uint32_t
-timeone(void (*fn)(), void *d, uint32_t val, uint32_t bytes)
-{
- uint32_t start, stop, delta;
- start = get_count();
- (*fn)(d, val, bytes);
- stop = get_count();
- delta = stop - start - overhead;
- // printf ("start=0x%08x stop=0x%08x delta=0x%08x\n", start, stop, delta);
- return delta * SCALE;
-}
-
-/* define VERIFY to check that memset only touches the bytes it's supposed to */
-/*#define VERIFY*/
-
-/*
- * Using a big arena means that memset will most likely miss in the cache
- * NB Enabling verification effectively warms up the cache...
- */
-#define ARENASIZE 0x1000000
-#ifdef VERIFY
-char arena[ARENASIZE+8]; /* Allow space for guard words */
-#else
-char arena[ARENASIZE];
-#endif
-
-void
-testone(char *tag, void (*fn)(), int trials, int minbytes, int maxbytes, int size, int threshold)
-{
- int offset;
- void *d;
- void *p;
- uint32_t v, notv = 0;
- uint32_t n;
- int i, units;
- int totalunits = 0, totalbytes = 0, samples = 0;
-
- /* Reset RNG to ensure each test uses same random values */
- srand(0); /* FIXME should be able to use some other seed than 0 */
-
- for (i = 0; i < trials; i++) {
- n = minbytes + (rand() % (maxbytes-minbytes)); /* How many bytes to do */
- offset = ((rand() % (ARENASIZE-n))); /* Where to start */
-
-#ifdef VERIFY
- offset += 4; /* Allow space for guard word at beginning */
-#endif
- v = rand();
-
- /* Adjust alignment and sizes based on transfer size */
- switch (size) {
- case 1:
- v &= 0xff;
- notv = ~v & 0xff;
- break;
- case 2:
- v &= 0xffff;
- notv = ~v & 0xffff;
- offset &= ~1;
- n &= ~1;
- break;
- case 4:
- notv = ~v;
- offset &= ~3;
- n &= ~3;
- break;
- }
-
- d = &arena[offset];
-
-#ifdef VERIFY
- /* Initialise the area and guard words */
- for (p = &arena[offset-4]; p < (void *)&arena[offset+n+4]; p = (void *)((uint32_t)p + size)) {
- if (size == 1)
- *(uint8_t *)p = notv;
- else if (size == 2)
- *(uint16_t *)p = notv;
- else if (size == 4)
- *(uint32_t *)p = notv;
- }
-#endif
- units = timeone(fn, d, v, n);
-#ifdef VERIFY
- /* Check the area and guard words */
- for (p = &arena[offset-4]; p < (void *)&arena[offset+n+4]; p = (void *)((uint32_t)p + size)) {
- uint32_t got = 0;
- if (size == 1)
- got = *(uint8_t *)p;
- else if (size == 2)
- got = *(uint16_t *)p;
- else if (size == 4)
- got = *(uint32_t *)p;
- if (p < (void *)&arena[offset]) {
- if (got != notv)
- printf ("%s: verify failure: preguard:%p d=%p v=%08x got=%08x n=%d\n", tag, p, d, v, got, n);
- }
- else if (p < (void *)&arena[offset+n]) {
- if (got != v)
- printf ("%s: verify failure: arena:%p d=%p v=%08x got=%08x n=%d\n", tag, p, d, v, n);
- }
- else {
- if (got != notv)
- printf ("%s: verify failure: postguard:%p d=%p v=%08x got=%08x n=%d\n", tag, p, d, v, n);
- }
- }
-#endif
-
- /* If the cycle count looks reasonable include it in the statistics */
- if (units < threshold) {
- totalbytes += n;
- totalunits += units;
- samples++;
- }
- }
-
- printf("%s: samples=%d avglen=%d avg" UNITS "=%d bp"UNITS"=%g\n",
- tag, samples, totalbytes/samples, totalunits/samples, (double)totalbytes/(double)totalunits);
-}
-
-extern void android_memset32_dumb(uint32_t* dst, uint32_t value, size_t size);
-extern void android_memset16_dumb(uint32_t* dst, uint16_t value, size_t size);
-extern void android_memset32_test(uint32_t* dst, uint32_t value, size_t size);
-extern void android_memset16_test(uint32_t* dst, uint16_t value, size_t size);
-extern void memset_cmips(void* dst, int value, size_t size);
-extern void memset_omips(void* dst, int value, size_t size);
-
-int
-main(int argc, char **argv)
-{
- int i;
- struct {
- char *type;
- int trials;
- int minbytes, maxbytes;
- } *pp, params[] = {
- {"small", 10000, 0, 64},
- {"medium", 10000, 64, 512},
- {"large", 10000, 512, 1280},
- {"varied", 10000, 0, 1280},
- };
-#define NPARAMS (sizeof(params)/sizeof(params[0]))
- struct {
- char *name;
- void (*fn)();
- int size;
- } *fp, functions[] = {
- {"dmemset16", (void (*)())android_memset16_dumb, 2},
- {"tmemset16", (void (*)())android_memset16_test, 2},
- {"lmemset16", (void (*)())android_memset16, 2},
-
- {"dmemset32", (void (*)())android_memset32_dumb, 4},
- {"tmemset32", (void (*)())android_memset32_test, 4},
- {"lmemset32", (void (*)())android_memset32, 4},
-
- {"cmemset", (void (*)())memset_cmips, 1},
- {"omemset", (void (*)())memset_omips, 1},
- {"lmemset", (void (*)())memset, 1},
- };
-#define NFUNCTIONS (sizeof(functions)/sizeof(functions[0]))
- char tag[40];
- int threshold;
-
- measure_overhead();
-
- /* Warm up the page cache */
- memset(arena, 0xff, ARENASIZE); /* use 0xff now to avoid COW later */
-
- for (fp = functions; fp < &functions[NFUNCTIONS]; fp++) {
- (fp->fn)(arena, 0xffffffff, ARENASIZE); /* one call to get the code into Icache */
- for (pp = params; pp < ¶ms[NPARAMS]; pp++) {
- sprintf(tag, "%10s: %7s %4d-%4d", fp->name, pp->type, pp->minbytes, pp->maxbytes);
-
- /* Set the cycle threshold */
- threshold = pp->maxbytes * 4 * 10; /* reasonable for cycles and ns */
- testone(tag, fp->fn, pp->trials, pp->minbytes, pp->maxbytes, fp->size, threshold);
- }
- printf ("\n");
- }
-
- return 0;
-}
diff --git a/libcutils/threads.c b/libcutils/threads.c
index 42cc928..ca600b3 100644
--- a/libcutils/threads.c
+++ b/libcutils/threads.c
@@ -1,33 +1,31 @@
-/* libs/cutils/threads.c
-**
+/*
** 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
+** 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
+** 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
+** 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 <cutils/threads.h>
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
void* thread_store_get( thread_store_t* store )
{
- const pthread_key_t k = store->tls;
-
if (!store->has_tls)
return NULL;
return pthread_getspecific( store->tls );
}
-
-extern void thread_store_set( thread_store_t* store,
+
+extern void thread_store_set( thread_store_t* store,
void* value,
thread_store_destruct_t destroy)
{
@@ -44,14 +42,12 @@
pthread_setspecific( store->tls, value );
}
-#endif
-
-#ifdef HAVE_WIN32_THREADS
+#else /* !defined(_WIN32) */
void* thread_store_get( thread_store_t* store )
{
if (!store->has_tls)
return NULL;
-
+
return (void*) TlsGetValue( store->tls );
}
@@ -67,7 +63,7 @@
} else while (store->lock_init != -2) {
Sleep(10); /* 10ms */
}
-
+
EnterCriticalSection( &store->lock );
if (!store->has_tls) {
store->tls = TlsAlloc();
@@ -78,7 +74,7 @@
store->has_tls = 1;
}
LeaveCriticalSection( &store->lock );
-
+
TlsSetValue( store->tls, value );
}
-#endif
+#endif /* !defined(_WIN32) */
diff --git a/libcutils/trace.c b/libcutils/trace.c
index 9754a44..4396625 100644
--- a/libcutils/trace.c
+++ b/libcutils/trace.c
@@ -28,7 +28,14 @@
#include <cutils/trace.h>
#define LOG_TAG "cutils-trace"
-#include <cutils/log.h>
+#include <log/log.h>
+
+/**
+ * Maximum size of a message that can be logged to the trace buffer.
+ * Note this message includes a tag, the pid, and the string given as the name.
+ * Names should be kept short to get the most use of the trace buffer.
+ */
+#define ATRACE_MESSAGE_LENGTH 1024
volatile int32_t atrace_is_ready = 0;
int atrace_marker_fd = -1;
@@ -86,7 +93,6 @@
static bool atrace_is_app_tracing_enabled()
{
bool sys_debuggable = false;
- bool proc_debuggable = false;
char value[PROPERTY_VALUE_MAX];
bool result = false;
@@ -184,3 +190,53 @@
{
pthread_once(&atrace_once_control, atrace_init_once);
}
+
+void atrace_begin_body(const char* name)
+{
+ char buf[ATRACE_MESSAGE_LENGTH];
+ size_t len;
+
+ len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
+ write(atrace_marker_fd, buf, len);
+}
+
+
+void atrace_async_begin_body(const char* name, int32_t cookie)
+{
+ char buf[ATRACE_MESSAGE_LENGTH];
+ size_t len;
+
+ len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
+ getpid(), name, cookie);
+ write(atrace_marker_fd, buf, len);
+}
+
+void atrace_async_end_body(const char* name, int32_t cookie)
+{
+ char buf[ATRACE_MESSAGE_LENGTH];
+ size_t len;
+
+ len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
+ getpid(), name, cookie);
+ write(atrace_marker_fd, buf, len);
+}
+
+void atrace_int_body(const char* name, int32_t value)
+{
+ char buf[ATRACE_MESSAGE_LENGTH];
+ size_t len;
+
+ len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
+ getpid(), name, value);
+ write(atrace_marker_fd, buf, len);
+}
+
+void atrace_int64_body(const char* name, int64_t value)
+{
+ char buf[ATRACE_MESSAGE_LENGTH];
+ size_t len;
+
+ len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
+ getpid(), name, value);
+ write(atrace_marker_fd, buf, len);
+}
diff --git a/libcutils/uevent.c b/libcutils/uevent.c
index 97a81e3..de5d227 100644
--- a/libcutils/uevent.c
+++ b/libcutils/uevent.c
@@ -31,12 +31,12 @@
*/
ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
{
- uid_t user = -1;
- return uevent_kernel_multicast_uid_recv(socket, buffer, length, &user);
+ uid_t uid = -1;
+ return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid);
}
/**
- * Like the above, but passes a uid_t in by reference. In the event that this
+ * Like the above, but passes a uid_t in by pointer. In the event that this
* fails due to a bad uid check, the uid_t will be set to the uid of the
* socket's peer.
*
@@ -44,8 +44,12 @@
* returns -1, sets errno to EIO, and sets "user" to the UID associated with the
* message. If the peer UID cannot be determined, "user" is set to -1."
*/
-ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer,
- size_t length, uid_t *user)
+ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid)
+{
+ return uevent_kernel_recv(socket, buffer, length, true, uid);
+}
+
+ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid)
{
struct iovec iov = { buffer, length };
struct sockaddr_nl addr;
@@ -60,7 +64,7 @@
0,
};
- *user = -1;
+ *uid = -1;
ssize_t n = recvmsg(socket, &hdr, 0);
if (n <= 0) {
return n;
@@ -73,14 +77,18 @@
}
struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg);
- *user = cred->uid;
+ *uid = cred->uid;
if (cred->uid != 0) {
/* ignoring netlink message from non-root user */
goto out;
}
- if (addr.nl_groups == 0 || addr.nl_pid != 0) {
- /* ignoring non-kernel or unicast netlink message */
+ if (addr.nl_pid != 0) {
+ /* ignore non-kernel */
+ goto out;
+ }
+ if (require_group && addr.nl_groups == 0) {
+ /* ignore unicast messages when requested */
goto out;
}
@@ -104,7 +112,7 @@
addr.nl_pid = getpid();
addr.nl_groups = 0xffffffff;
- s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
+ s = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
if(s < 0)
return -1;
diff --git a/libdiskconfig/Android.mk b/libdiskconfig/Android.mk
index b5d83fa..624e385 100644
--- a/libdiskconfig/Android.mk
+++ b/libdiskconfig/Android.mk
@@ -12,6 +12,7 @@
LOCAL_MODULE := libdiskconfig
LOCAL_MODULE_TAGS := optional
LOCAL_SYSTEM_SHARED_LIBRARIES := libcutils liblog libc
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
ifeq ($(HOST_OS),linux)
diff --git a/libdiskconfig/config_mbr.c b/libdiskconfig/config_mbr.c
index 7641b29..7b6ca1c 100644
--- a/libdiskconfig/config_mbr.c
+++ b/libdiskconfig/config_mbr.c
@@ -208,6 +208,26 @@
}
+static struct write_list *
+mk_mbr_sig()
+{
+ struct write_list *item;
+ if (!(item = alloc_wl(sizeof(uint16_t)))) {
+ ALOGE("Unable to allocate memory for MBR signature.");
+ return NULL;
+ }
+
+ {
+ /* DO NOT DEREFERENCE */
+ struct pc_boot_record *mbr = (void *)PC_MBR_DISK_OFFSET;
+ /* grab the offset in mbr where to write mbr signature. */
+ item->offset = (loff_t)((uintptr_t)((uint8_t *)(&mbr->mbr_sig)));
+ }
+
+ *((uint16_t*)item->data) = PC_BIOS_BOOT_SIG;
+ return item;
+}
+
struct write_list *
config_mbr(struct disk_info *dinfo)
{
@@ -276,6 +296,13 @@
wlist_add(&wr_list, temp_wr);
}
+ if ((temp_wr = mk_mbr_sig()))
+ wlist_add(&wr_list, temp_wr);
+ else {
+ ALOGE("Cannot set MBR signature");
+ goto fail;
+ }
+
return wr_list;
nospace:
diff --git a/libdiskconfig/diskconfig.c b/libdiskconfig/diskconfig.c
index 6fd81b7..1167d4b 100644
--- a/libdiskconfig/diskconfig.c
+++ b/libdiskconfig/diskconfig.c
@@ -30,7 +30,7 @@
#include <linux/fs.h>
#include <cutils/config_utils.h>
-#include <cutils/log.h>
+#include <log/log.h>
#include <diskconfig/diskconfig.h>
@@ -337,7 +337,7 @@
}
#if 1
- ALOGV("Device/file %s: size=%llu bytes, num_lba=%u, sect_size=%d",
+ ALOGV("Device/file %s: size=%" PRIu64 " bytes, num_lba=%u, sect_size=%d",
dinfo->device, disk_size, dinfo->num_lba, dinfo->sect_size);
#endif
diff --git a/libdiskconfig/diskutils.c b/libdiskconfig/diskutils.c
index e325735..5d0ee62 100644
--- a/libdiskconfig/diskutils.c
+++ b/libdiskconfig/diskutils.c
@@ -19,13 +19,14 @@
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
-#include <cutils/log.h>
+#include <log/log.h>
#include <diskconfig/diskconfig.h>
@@ -35,12 +36,12 @@
int dst_fd = -1;
int src_fd = -1;
uint8_t buffer[2048];
- int nr_bytes;
- int tmp;
+ ssize_t nr_bytes;
+ ssize_t tmp;
int done = 0;
uint64_t total = 0;
- ALOGI("Writing RAW image '%s' to '%s' (offset=%llu)", src, dst, offset);
+ ALOGI("Writing RAW image '%s' to '%s' (offset=%llu)", src, dst, (unsigned long long)offset);
if ((src_fd = open(src, O_RDONLY)) < 0) {
ALOGE("Could not open %s for reading (errno=%d).", src, errno);
goto fail;
@@ -53,7 +54,7 @@
}
if (lseek64(dst_fd, offset, SEEK_SET) != offset) {
- ALOGE("Could not seek to offset %lld in %s.", offset, dst);
+ ALOGE("Could not seek to offset %lld in %s.", (long long)offset, dst);
goto fail;
}
}
@@ -101,7 +102,7 @@
if (dst_fd >= 0)
fsync(dst_fd);
- ALOGI("Wrote %llu bytes to %s @ %lld", total, dst, offset);
+ ALOGI("Wrote %" PRIu64 " bytes to %s @ %lld", total, dst, (long long)offset);
close(src_fd);
if (dst_fd >= 0)
diff --git a/libdiskconfig/write_lst.c b/libdiskconfig/write_lst.c
index 826ef7a..90b1c82 100644
--- a/libdiskconfig/write_lst.c
+++ b/libdiskconfig/write_lst.c
@@ -71,18 +71,18 @@
{
for(; lst; lst = lst->next) {
if (lseek64(fd, lst->offset, SEEK_SET) != (loff_t)lst->offset) {
- ALOGE("Cannot seek to the specified position (%lld).", lst->offset);
+ ALOGE("Cannot seek to the specified position (%lld).", (long long)lst->offset);
goto fail;
}
if (!test) {
if (write(fd, lst->data, lst->len) != (int)lst->len) {
ALOGE("Failed writing %u bytes at position %lld.", lst->len,
- lst->offset);
+ (long long)lst->offset);
goto fail;
}
} else
- ALOGI("Would write %d bytes @ offset %lld.", lst->len, lst->offset);
+ ALOGI("Would write %d bytes @ offset %lld.", lst->len, (long long)lst->offset);
}
return 0;
diff --git a/libion/Android.mk b/libion/Android.mk
index e5d495b..6562cd3 100644
--- a/libion/Android.mk
+++ b/libion/Android.mk
@@ -1,4 +1,4 @@
-LOCAL_PATH:= $(call my-dir)
+LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := ion.c
@@ -7,6 +7,7 @@
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
@@ -15,6 +16,7 @@
LOCAL_MODULE_TAGS := optional tests
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/libion/ion.c b/libion/ion.c
index 80bdc2a..d1984bd 100644
--- a/libion/ion.c
+++ b/libion/ion.c
@@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
+#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
@@ -90,6 +91,7 @@
int flags, off_t offset, unsigned char **ptr, int *map_fd)
{
int ret;
+ unsigned char *tmp_ptr;
struct ion_fd_data data = {
.handle = handle,
};
@@ -102,22 +104,22 @@
ret = ion_ioctl(fd, ION_IOC_MAP, &data);
if (ret < 0)
return ret;
- *map_fd = data.fd;
- if (*map_fd < 0) {
+ if (data.fd < 0) {
ALOGE("map ioctl returned negative fd\n");
return -EINVAL;
}
- *ptr = mmap(NULL, length, prot, flags, *map_fd, offset);
- if (*ptr == MAP_FAILED) {
+ tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
+ if (tmp_ptr == MAP_FAILED) {
ALOGE("mmap failed: %s\n", strerror(errno));
return -errno;
}
+ *map_fd = data.fd;
+ *ptr = tmp_ptr;
return ret;
}
int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
{
- int map_fd;
int ret;
struct ion_fd_data data = {
.handle = handle,
@@ -129,11 +131,11 @@
ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
if (ret < 0)
return ret;
- *share_fd = data.fd;
- if (*share_fd < 0) {
+ if (data.fd < 0) {
ALOGE("share ioctl returned negative fd\n");
return -EINVAL;
}
+ *share_fd = data.fd;
return ret;
}
diff --git a/libion/ion_test.c b/libion/ion_test.c
index 8872282..b7d5583 100644
--- a/libion/ion_test.c
+++ b/libion/ion_test.c
@@ -164,8 +164,9 @@
printf("master->master? [%10s]\n", ptr);
if (recvmsg(sd[0], &msg, 0) < 0)
perror("master recv 1");
+ close(fd);
+ _exit(0);
} else {
- struct msghdr msg;
struct cmsghdr *cmsg;
char* ptr;
int fd, recv_fd;
@@ -205,6 +206,7 @@
strcpy(ptr, "child");
printf("child sending msg 2\n");
sendmsg(sd[1], &child_msg, 0);
+ close(fd);
}
}
diff --git a/libion/tests/Android.mk b/libion/tests/Android.mk
index 8dc7f9d..abf527a 100644
--- a/libion/tests/Android.mk
+++ b/libion/tests/Android.mk
@@ -21,7 +21,6 @@
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CFLAGS += -g -Wall -Werror -std=gnu++11 -Wno-missing-field-initializers
LOCAL_SHARED_LIBRARIES += libion
-LOCAL_STATIC_LIBRARIES += libgtest_main
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../kernel-headers
LOCAL_SRC_FILES := \
ion_test_fixture.cpp \
diff --git a/liblog/Android.mk b/liblog/Android.mk
index 0d6c970..70aff83 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -16,67 +16,77 @@
LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
+# This is what we want to do:
+# liblog_cflags := $(shell \
+# sed -n \
+# 's/^\([0-9]*\)[ \t]*liblog[ \t].*/-DLIBLOG_LOG_TAG=\1/p' \
+# $(LOCAL_PATH)/event.logtags)
+# so make sure we do not regret hard-coding it as follows:
+liblog_cflags := -DLIBLOG_LOG_TAG=1005
+
+ifneq ($(TARGET_USES_LOGD),false)
liblog_sources := logd_write.c
+else
+liblog_sources := logd_write_kern.c
+endif
# some files must not be compiled when building against Mingw
# they correspond to features not used by our host development tools
# which are also hard or even impossible to port to native Win32
-WITH_MINGW :=
-ifeq ($(HOST_OS),windows)
- ifeq ($(strip $(USE_CYGWIN)),)
- WITH_MINGW := true
- endif
-endif
-# USE_MINGW is defined when we build against Mingw on Linux
-ifneq ($(strip $(USE_MINGW)),)
- WITH_MINGW := true
-endif
-ifndef WITH_MINGW
+ifeq ($(strip $(USE_MINGW)),)
liblog_sources += \
- logprint.c \
event_tag_map.c
else
liblog_sources += \
uio.c
endif
-liblog_host_sources := $(liblog_sources) fake_log_device.c
-liblog_target_sources = $(liblog_sources) log_read.c
+liblog_host_sources := $(liblog_sources) fake_log_device.c event.logtags
+liblog_target_sources := $(liblog_sources) log_time.cpp log_is_loggable.c
+ifeq ($(strip $(USE_MINGW)),)
+liblog_target_sources += logprint.c
+endif
+ifneq ($(TARGET_USES_LOGD),false)
+liblog_target_sources += log_read.c
+else
+liblog_target_sources += log_read_kern.c
+endif
# Shared and static library for host
# ========================================================
LOCAL_MODULE := liblog
LOCAL_SRC_FILES := $(liblog_host_sources)
-LOCAL_LDLIBS := -lpthread
-LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1
+LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1 -Werror $(liblog_cflags)
+LOCAL_MULTILIB := both
include $(BUILD_HOST_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := liblog
LOCAL_WHOLE_STATIC_LIBRARIES := liblog
+ifeq ($(strip $(HOST_OS)),linux)
+LOCAL_LDLIBS := -lrt
+endif
+LOCAL_MULTILIB := both
include $(BUILD_HOST_SHARED_LIBRARY)
-# Static library for host, 64-bit
-# ========================================================
-include $(CLEAR_VARS)
-LOCAL_MODULE := lib64log
-LOCAL_SRC_FILES := $(liblog_host_sources)
-LOCAL_LDLIBS := -lpthread
-LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1 -m64
-include $(BUILD_HOST_STATIC_LIBRARY)
-
# Shared and static library for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_MODULE := liblog
LOCAL_SRC_FILES := $(liblog_target_sources)
+LOCAL_CFLAGS := -Werror $(liblog_cflags)
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := liblog
LOCAL_WHOLE_STATIC_LIBRARIES := liblog
+LOCAL_CFLAGS := -Werror $(liblog_cflags)
+
+# TODO: This is to work around b/19059885. Remove after root cause is fixed
+LOCAL_LDFLAGS_arm := -Wl,--hash-style=sysv
+
include $(BUILD_SHARED_LIBRARY)
include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/liblog/README b/liblog/README
index d7472e4..f29ac04 100644
--- a/liblog/README
+++ b/liblog/README
@@ -111,24 +111,56 @@
ger_list_alloc, calling in turn the android_logger_open for each log
id. Each entry can be retrieved with android_logger_list_read. The
log(s) can be closed with android_logger_list_free. The logs should be
- opened with an O_RDONLY mode. O_NDELAY mode will report when the log
- reading is done with an EAGAIN error return code, otherwise the
- android_logger_list_read call will block for new entries.
+ opened with an ANDROID_LOG_RDONLY mode. ANDROID_LOG_NONBLOCK mode
+ will report when the log reading is done with an EAGAIN error return
+ code, otherwise the android_logger_list_read call will block for new
+ entries.
+
+ The ANDROID_LOG_PSTORE mode flag to the android_logger_open is used to
+ switch from the active logs to the persistent logs from before the last
+ reboot.
The value returned by android_logger_open can be used as a parameter to
the android_logger_clear function to empty the sub-log. It is recom‐
- mended to only open log O_WRONLY.
+ mended to only open log ANDROID_LOG_WRONLY in that case.
The value returned by android_logger_open can be used as a parameter to
the android_logger_get_log_(size|readable_size|version) to retrieve the
sub-log maximum size, readable size and log buffer format protocol ver‐
sion respectively. android_logger_get_id returns the id that was used
- when opening the sub-log. It is recommended to open the log O_RDONLY
- in these cases.
+ when opening the sub-log. It is recommended to open the log
+ ANDROID_LOG_RDONLY in these cases.
+
+ERRORS
+ If messages fail, a negative error code will be returned to the caller.
+
+ The -ENOTCONN return code indicates that the logger daemon is stopped.
+
+ The -EBADF return code indicates that the log access point can not be
+ opened, or the log buffer id is out of range.
+
+ For the -EAGAIN return code, this means that the logging message was
+ temporarily backed-up either because of Denial Of Service (DOS) logging
+ pressure from some chatty application or service in the Android system,
+ or if too small of a value is set in /proc/sys/net/unix/max_dgram_qlen.
+ To aid in diagnosing the occurence of this, a binary event from liblog
+ will be sent to the log daemon once a new message can get through
+ indicating how many messages were dropped as a result. Please take
+ action to resolve the structural problems at the source.
+
+ It is generally not advised for the caller to retry the -EAGAIN return
+ code as this will only make the problem(s) worse and cause your
+ application to temporarily drop to the logger daemon priority, BATCH
+ scheduling policy and background task cgroup. If you require a group of
+ messages to be passed atomically, merge them into one message with
+ embedded newlines to the maximum length LOGGER_ENTRY_MAX_PAYLOAD.
+
+ Other return codes from writing operation can be returned. Since the
+ library retries on EINTR, -EINTR should never be returned.
SEE ALSO
syslogd(8)
- 17 Dec 2013 LIBLOG(3)
+ 24 Jan 2014 LIBLOG(3)
diff --git a/liblog/event.logtags b/liblog/event.logtags
new file mode 100644
index 0000000..72ecab1
--- /dev/null
+++ b/liblog/event.logtags
@@ -0,0 +1,36 @@
+# The entries in this file map a sparse set of log tag numbers to tag names.
+# This is installed on the device, in /system/etc, and parsed by logcat.
+#
+# Tag numbers are decimal integers, from 0 to 2^31. (Let's leave the
+# negative values alone for now.)
+#
+# Tag names are one or more ASCII letters and numbers or underscores, i.e.
+# "[A-Z][a-z][0-9]_". Do not include spaces or punctuation (the former
+# impacts log readability, the latter makes regex searches more annoying).
+#
+# Tag numbers and names are separated by whitespace. Blank lines and lines
+# starting with '#' are ignored.
+#
+# Optionally, after the tag names can be put a description for the value(s)
+# of the tag. Description are in the format
+# (<name>|data type[|data unit])
+# Multiple values are separated by commas.
+#
+# The data type is a number from the following values:
+# 1: int
+# 2: long
+# 3: string
+# 4: list
+#
+# The data unit is a number taken from the following list:
+# 1: Number of objects
+# 2: Number of bytes
+# 3: Number of milliseconds
+# 4: Number of allocations
+# 5: Id
+# 6: Percent
+# Default value for data of type int/long is 2 (bytes).
+#
+# TODO: generate ".java" and ".h" files with integer constants from this file.
+
+1005 liblog (dropped|1)
diff --git a/liblog/event_tag_map.c b/liblog/event_tag_map.c
index f3d1e2f..bea99aa 100644
--- a/liblog/event_tag_map.c
+++ b/liblog/event_tag_map.c
@@ -13,15 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <log/event_tag_map.h>
-#include <log/log.h>
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
-#include <fcntl.h>
#include <sys/mman.h>
-#include <errno.h>
-#include <assert.h>
+
+#include <log/event_tag_map.h>
+#include <log/log.h>
#define OUT_TAG "EventTagMap"
@@ -52,7 +53,6 @@
static int parseMapLines(EventTagMap* map);
static int scanTagLine(char** pData, EventTag* tag, int lineNum);
static int sortTags(EventTagMap* map);
-static void dumpTags(const EventTagMap* map);
/*
@@ -185,8 +185,6 @@
*/
static int processFile(EventTagMap* map)
{
- EventTag* tagArray = NULL;
-
/* get a tag count */
map->numTags = countMapLines(map);
if (map->numTags < 0)
@@ -422,17 +420,3 @@
return 0;
}
-
-/*
- * Dump the tag array for debugging.
- */
-static void dumpTags(const EventTagMap* map)
-{
- int i;
-
- for (i = 0; i < map->numTags; i++) {
- const EventTag* tag = &map->tagArray[i];
- printf(" %3d: %6d '%s'\n", i, tag->tagIndex, tag->tagStr);
- }
-}
-
diff --git a/liblog/fake_log_device.c b/liblog/fake_log_device.c
index da83a85..8a8ece2 100644
--- a/liblog/fake_log_device.c
+++ b/liblog/fake_log_device.c
@@ -21,18 +21,22 @@
*/
#include "fake_log_device.h"
-#include <log/logd.h>
-
-#include <stdlib.h>
-#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
-#ifdef HAVE_PTHREADS
+#include <log/logd.h>
+
+#if !defined(_WIN32)
#include <pthread.h>
#endif
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
#define kMaxTagLen 16 /* from the long-dead utils/Log.cpp */
#define kTagSetSize 16 /* arbitrary */
@@ -84,7 +88,7 @@
} LogState;
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
/*
* Locking. Since we're emulating a device, we need to be prepared
* to have multiple callers at the same time. This lock is used
@@ -102,10 +106,10 @@
{
pthread_mutex_unlock(&fakeLogDeviceLock);
}
-#else // !HAVE_PTHREADS
+#else // !defined(_WIN32)
#define lock() ((void)0)
#define unlock() ((void)0)
-#endif // !HAVE_PTHREADS
+#endif // !defined(_WIN32)
/*
@@ -316,9 +320,9 @@
return priorityStrings[idx];
}
-#ifndef HAVE_WRITEV
+#if defined(_WIN32)
/*
- * Some platforms like WIN32 do not have writev().
+ * WIN32 does not have writev().
* Make up something to replace it.
*/
static ssize_t fake_writev(int fd, const struct iovec *iov, int iovcnt) {
@@ -348,7 +352,7 @@
static void showLog(LogState *state,
int logPrio, const char* tag, const char* msg)
{
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
struct tm tmBuf;
#endif
struct tm* ptm;
@@ -373,7 +377,7 @@
* in the time stamp. Don't use forward slashes, parenthesis,
* brackets, asterisks, or other special chars here.
*/
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
ptm = localtime_r(&when, &tmBuf);
#else
ptm = localtime(&when);
@@ -613,7 +617,7 @@
/*
* Open a log output device and return a fake fd.
*/
-static int logOpen(const char* pathName, int flags)
+static int logOpen(const char* pathName, int flags __unused)
{
LogState *logState;
int fd = -1;
@@ -685,3 +689,9 @@
/* Assume that open() was called first. */
return redirectWritev(fd, vector, count);
}
+
+int __android_log_is_loggable(int prio, const char *tag __unused, int def)
+{
+ int logLevel = def;
+ return logLevel >= 0 && prio >= logLevel;
+}
diff --git a/liblog/log_is_loggable.c b/liblog/log_is_loggable.c
new file mode 100644
index 0000000..df67123
--- /dev/null
+++ b/liblog/log_is_loggable.c
@@ -0,0 +1,69 @@
+/*
+** Copyright 2014, 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 <ctype.h>
+#include <string.h>
+#include <sys/system_properties.h>
+
+#include <android/log.h>
+
+static int __android_log_level(const char *tag, int def)
+{
+ char buf[PROP_VALUE_MAX];
+
+ if (!tag || !*tag) {
+ return def;
+ }
+ {
+ static const char log_namespace[] = "log.tag.";
+ char key[sizeof(log_namespace) + strlen(tag)];
+
+ strcpy(key, log_namespace);
+ strcpy(key + sizeof(log_namespace) - 1, tag);
+
+ if (__system_property_get(key + 8, buf) <= 0) {
+ buf[0] = '\0';
+ }
+ }
+ switch (toupper(buf[0])) {
+ case 'V': return ANDROID_LOG_VERBOSE;
+ case 'D': return ANDROID_LOG_DEBUG;
+ case 'I': return ANDROID_LOG_INFO;
+ case 'W': return ANDROID_LOG_WARN;
+ case 'E': return ANDROID_LOG_ERROR;
+ case 'F': /* FALLTHRU */ /* Not officially supported */
+ case 'A': return ANDROID_LOG_FATAL;
+ case 'S': return -1; /* ANDROID_LOG_SUPPRESS */
+ }
+ return def;
+}
+
+int __android_log_is_loggable(int prio, const char *tag, int def)
+{
+ static char user;
+ int logLevel;
+
+ if (user == 0) {
+ char buf[PROP_VALUE_MAX];
+ if (__system_property_get("ro.build.type", buf) <= 0) {
+ buf[0] = '\0';
+ }
+ user = strcmp(buf, "user") ? -1 : 1;
+ }
+
+ logLevel = (user == 1) ? def : __android_log_level(tag, def);
+ return logLevel >= 0 && prio >= logLevel;
+}
diff --git a/liblog/log_read.c b/liblog/log_read.c
index 47aa711..5364e4f 100644
--- a/liblog/log_read.c
+++ b/liblog/log_read.c
@@ -14,37 +14,187 @@
** limitations under the License.
*/
-#define _GNU_SOURCE /* asprintf for x86 host */
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <poll.h>
-#include <string.h>
-#include <stdio.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stddef.h>
+#define NOMINMAX /* for windows to suppress definition of min in stdlib.h */
#include <stdlib.h>
+#include <string.h>
+#include <sys/cdefs.h>
+#include <unistd.h>
+
#include <cutils/list.h>
+#include <cutils/sockets.h>
#include <log/log.h>
#include <log/logger.h>
+#include <private/android_filesystem_config.h>
+#include <private/android_logger.h>
-#include <sys/ioctl.h>
+/* branchless on many architectures. */
+#define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
-#define __LOGGERIO 0xAE
+#if (defined(USE_MINGW) || defined(HAVE_WINSOCK))
+#define WEAK static
+#else
+#define WEAK __attribute__((weak))
+#endif
+#ifndef __unused
+#define __unused __attribute__((unused))
+#endif
-#define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */
-#define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */
-#define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */
-#define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */
-#define LOGGER_GET_VERSION _IO(__LOGGERIO, 5) /* abi version */
-#define LOGGER_SET_VERSION _IO(__LOGGERIO, 6) /* abi version */
+/* Private copy of ../libcutils/socket_local_client.c prevent library loops */
-typedef char bool;
-#define false (const bool)0
-#define true (const bool)1
+#ifdef HAVE_WINSOCK
-#define LOG_FILE_DIR "/dev/log/"
+int WEAK socket_local_client(const char *name, int namespaceId, int type)
+{
+ errno = ENOSYS;
+ return -ENOSYS;
+}
-/* timeout in milliseconds */
-#define LOG_TIMEOUT_FLUSH 5
-#define LOG_TIMEOUT_NEVER -1
+#else /* !HAVE_WINSOCK */
+
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/select.h>
+#include <sys/types.h>
+
+/* Private copy of ../libcutils/socket_local.h prevent library loops */
+#define FILESYSTEM_SOCKET_PREFIX "/tmp/"
+#define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/"
+/* End of ../libcutils/socket_local.h */
+
+#define LISTEN_BACKLOG 4
+
+/* Documented in header file. */
+int WEAK socket_make_sockaddr_un(const char *name, int namespaceId,
+ struct sockaddr_un *p_addr, socklen_t *alen)
+{
+ memset (p_addr, 0, sizeof (*p_addr));
+ size_t namelen;
+
+ switch (namespaceId) {
+ case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
+#if defined(__linux__)
+ namelen = strlen(name);
+
+ /* Test with length +1 for the *initial* '\0'. */
+ if ((namelen + 1) > sizeof(p_addr->sun_path)) {
+ goto error;
+ }
+
+ /*
+ * Note: The path in this case is *not* supposed to be
+ * '\0'-terminated. ("man 7 unix" for the gory details.)
+ */
+
+ p_addr->sun_path[0] = 0;
+ memcpy(p_addr->sun_path + 1, name, namelen);
+#else
+ /* this OS doesn't have the Linux abstract namespace */
+
+ namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
+ /* unix_path_max appears to be missing on linux */
+ if (namelen > sizeof(*p_addr)
+ - offsetof(struct sockaddr_un, sun_path) - 1) {
+ goto error;
+ }
+
+ strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
+ strcat(p_addr->sun_path, name);
+#endif
+ break;
+
+ case ANDROID_SOCKET_NAMESPACE_RESERVED:
+ namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
+ /* unix_path_max appears to be missing on linux */
+ if (namelen > sizeof(*p_addr)
+ - offsetof(struct sockaddr_un, sun_path) - 1) {
+ goto error;
+ }
+
+ strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
+ strcat(p_addr->sun_path, name);
+ break;
+
+ case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
+ namelen = strlen(name);
+ /* unix_path_max appears to be missing on linux */
+ if (namelen > sizeof(*p_addr)
+ - offsetof(struct sockaddr_un, sun_path) - 1) {
+ goto error;
+ }
+
+ strcpy(p_addr->sun_path, name);
+ break;
+
+ default:
+ /* invalid namespace id */
+ return -1;
+ }
+
+ p_addr->sun_family = AF_LOCAL;
+ *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
+ return 0;
+error:
+ return -1;
+}
+
+/**
+ * connect to peer named "name" on fd
+ * returns same fd or -1 on error.
+ * fd is not closed on error. that's your job.
+ *
+ * Used by AndroidSocketImpl
+ */
+int WEAK socket_local_client_connect(int fd, const char *name, int namespaceId,
+ int type __unused)
+{
+ struct sockaddr_un addr;
+ socklen_t alen;
+ int err;
+
+ err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
+
+ if (err < 0) {
+ goto error;
+ }
+
+ if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
+ goto error;
+ }
+
+ return fd;
+
+error:
+ return -1;
+}
+
+/**
+ * connect to peer named "name"
+ * returns fd or -1 on error
+ */
+int WEAK socket_local_client(const char *name, int namespaceId, int type)
+{
+ int s;
+
+ s = socket(AF_LOCAL, type, 0);
+ if(s < 0) return -1;
+
+ if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
+ close(s);
+ return -1;
+ }
+
+ return s;
+}
+
+#endif /* !HAVE_WINSOCK */
+/* End of ../libcutils/socket_local_client.c */
#define logger_for_each(logger, logger_list) \
for (logger = node_to_item((logger_list)->node.next, struct logger, node); \
@@ -56,47 +206,21 @@
[LOG_ID_MAIN] = "main",
[LOG_ID_RADIO] = "radio",
[LOG_ID_EVENTS] = "events",
- [LOG_ID_SYSTEM] = "system"
+ [LOG_ID_SYSTEM] = "system",
+ [LOG_ID_CRASH] = "crash",
};
-const char *android_log_id_to_name(log_id_t log_id) {
+const char *android_log_id_to_name(log_id_t log_id)
+{
if (log_id >= LOG_ID_MAX) {
log_id = LOG_ID_MAIN;
}
return LOG_NAME[log_id];
}
-static int accessmode(int mode)
+log_id_t android_name_to_log_id(const char *logName)
{
- if ((mode & O_ACCMODE) == O_WRONLY) {
- return W_OK;
- }
- if ((mode & O_ACCMODE) == O_RDWR) {
- return R_OK | W_OK;
- }
- return R_OK;
-}
-
-/* repeated fragment */
-static int check_allocate_accessible(char **n, const char *b, int mode)
-{
- *n = NULL;
-
- if (!b) {
- return -EINVAL;
- }
-
- asprintf(n, LOG_FILE_DIR "%s", b);
- if (!*n) {
- return -1;
- }
-
- return access(*n, accessmode(mode));
-}
-
-log_id_t android_name_to_log_id(const char *logName) {
const char *b;
- char *n;
int ret;
if (!logName) {
@@ -109,12 +233,6 @@
++b;
}
- ret = check_allocate_accessible(&n, b, O_RDONLY);
- free(n);
- if (ret) {
- return ret;
- }
-
for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
const char *l = LOG_NAME[ret];
if (l && !strcmp(b, l)) {
@@ -128,27 +246,15 @@
struct listnode node;
int mode;
unsigned int tail;
+ log_time start;
pid_t pid;
- unsigned int queued_lines;
- int timeout_ms;
- int error;
- bool flush;
- bool valid_entry; /* valiant(?) effort to deal with memory starvation */
- struct log_msg entry;
-};
-
-struct log_list {
- struct listnode node;
- struct log_msg entry; /* Truncated to event->len() + 1 to save space */
+ int sock;
};
struct logger {
struct listnode node;
struct logger_list *top;
- int fd;
log_id_t id;
- short *revents;
- struct listnode log_list;
};
/* android_logger_alloc unimplemented, no use case */
@@ -159,91 +265,263 @@
return;
}
- while (!list_empty(&logger->log_list)) {
- struct log_list *entry = node_to_item(
- list_head(&logger->log_list), struct log_list, node);
- list_remove(&entry->node);
- free(entry);
- if (logger->top->queued_lines) {
- logger->top->queued_lines--;
- }
- }
-
- if (logger->fd >= 0) {
- close(logger->fd);
- }
-
list_remove(&logger->node);
free(logger);
}
+/* android_logger_alloc unimplemented, no use case */
+
+/* method for getting the associated sublog id */
log_id_t android_logger_get_id(struct logger *logger)
{
return logger->id;
}
/* worker for sending the command to the logger */
-static int logger_ioctl(struct logger *logger, int cmd, int mode)
+static ssize_t send_log_msg(struct logger *logger,
+ const char *msg, char *buf, size_t buf_size)
{
- char *n;
- int f, ret;
-
- if (!logger || !logger->top) {
- return -EFAULT;
+ ssize_t ret;
+ size_t len;
+ char *cp;
+ int errno_save = 0;
+ int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM);
+ if (sock < 0) {
+ return sock;
}
- if (((mode & O_ACCMODE) == O_RDWR)
- || (((mode ^ logger->top->mode) & O_ACCMODE) == 0)) {
- return ioctl(logger->fd, cmd);
+ if (msg) {
+ snprintf(buf, buf_size, msg, logger ? logger->id : (unsigned) -1);
}
- /* We go here if android_logger_list_open got mode wrong for this ioctl */
- ret = check_allocate_accessible(&n, android_log_id_to_name(logger->id), mode);
- if (ret) {
- free(n);
+ len = strlen(buf) + 1;
+ ret = TEMP_FAILURE_RETRY(write(sock, buf, len));
+ if (ret <= 0) {
+ goto done;
+ }
+
+ len = buf_size;
+ cp = buf;
+ while ((ret = TEMP_FAILURE_RETRY(read(sock, cp, len))) > 0) {
+ struct pollfd p;
+
+ if (((size_t)ret == len) || (buf_size < PAGE_SIZE)) {
+ break;
+ }
+
+ len -= ret;
+ cp += ret;
+
+ memset(&p, 0, sizeof(p));
+ p.fd = sock;
+ p.events = POLLIN;
+
+ /* Give other side 20ms to refill pipe */
+ ret = TEMP_FAILURE_RETRY(poll(&p, 1, 20));
+
+ if (ret <= 0) {
+ break;
+ }
+
+ if (!(p.revents & POLLIN)) {
+ ret = 0;
+ break;
+ }
+ }
+
+ if (ret >= 0) {
+ ret += buf_size - len;
+ }
+
+done:
+ if ((ret == -1) && errno) {
+ errno_save = errno;
+ }
+ close(sock);
+ if (errno_save) {
+ errno = errno_save;
+ }
+ return ret;
+}
+
+static int check_log_success(char *buf, ssize_t ret)
+{
+ if (ret < 0) {
return ret;
}
- f = open(n, mode);
- free(n);
- if (f < 0) {
- return f;
+ if (strncmp(buf, "success", 7)) {
+ errno = EINVAL;
+ return -1;
}
- ret = ioctl(f, cmd);
- close (f);
+ return 0;
+}
- return ret;
+/* Determine the credentials of the caller */
+static bool uid_has_log_permission(uid_t uid)
+{
+ return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT);
+}
+
+static uid_t get_best_effective_uid()
+{
+ uid_t euid;
+ uid_t uid;
+ gid_t gid;
+ ssize_t i;
+ static uid_t last_uid = (uid_t) -1;
+
+ if (last_uid != (uid_t) -1) {
+ return last_uid;
+ }
+ uid = getuid();
+ if (uid_has_log_permission(uid)) {
+ return last_uid = uid;
+ }
+ euid = geteuid();
+ if (uid_has_log_permission(euid)) {
+ return last_uid = euid;
+ }
+ gid = getgid();
+ if (uid_has_log_permission(gid)) {
+ return last_uid = gid;
+ }
+ gid = getegid();
+ if (uid_has_log_permission(gid)) {
+ return last_uid = gid;
+ }
+ i = getgroups((size_t) 0, NULL);
+ if (i > 0) {
+ gid_t list[i];
+
+ getgroups(i, list);
+ while (--i >= 0) {
+ if (uid_has_log_permission(list[i])) {
+ return last_uid = list[i];
+ }
+ }
+ }
+ return last_uid = uid;
}
int android_logger_clear(struct logger *logger)
{
- return logger_ioctl(logger, LOGGER_FLUSH_LOG, O_WRONLY);
+ char buf[512];
+
+ if (logger->top->mode & ANDROID_LOG_PSTORE) {
+ if (uid_has_log_permission(get_best_effective_uid())) {
+ return unlink("/sys/fs/pstore/pmsg-ramoops-0");
+ }
+ errno = EPERM;
+ return -1;
+ }
+ return check_log_success(buf,
+ send_log_msg(logger, "clear %d", buf, sizeof(buf)));
}
/* returns the total size of the log's ring buffer */
-int android_logger_get_log_size(struct logger *logger)
+long android_logger_get_log_size(struct logger *logger)
{
- return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, O_RDWR);
+ char buf[512];
+
+ ssize_t ret = send_log_msg(logger, "getLogSize %d", buf, sizeof(buf));
+ if (ret < 0) {
+ return ret;
+ }
+
+ if ((buf[0] < '0') || ('9' < buf[0])) {
+ return -1;
+ }
+
+ return atol(buf);
+}
+
+int android_logger_set_log_size(struct logger *logger, unsigned long size)
+{
+ char buf[512];
+
+ snprintf(buf, sizeof(buf), "setLogSize %d %lu",
+ logger ? logger->id : (unsigned) -1, size);
+
+ return check_log_success(buf, send_log_msg(NULL, NULL, buf, sizeof(buf)));
}
/*
* returns the readable size of the log's ring buffer (that is, amount of the
* log consumed)
*/
-int android_logger_get_log_readable_size(struct logger *logger)
+long android_logger_get_log_readable_size(struct logger *logger)
{
- return logger_ioctl(logger, LOGGER_GET_LOG_LEN, O_RDONLY);
+ char buf[512];
+
+ ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf));
+ if (ret < 0) {
+ return ret;
+ }
+
+ if ((buf[0] < '0') || ('9' < buf[0])) {
+ return -1;
+ }
+
+ return atol(buf);
}
/*
* returns the logger version
*/
-int android_logger_get_log_version(struct logger *logger)
+int android_logger_get_log_version(struct logger *logger __unused)
{
- int ret = logger_ioctl(logger, LOGGER_GET_VERSION, O_RDWR);
- return (ret < 0) ? 1 : ret;
+ return 3;
+}
+
+/*
+ * returns statistics
+ */
+ssize_t android_logger_get_statistics(struct logger_list *logger_list,
+ char *buf, size_t len)
+{
+ struct logger *logger;
+ char *cp = buf;
+ size_t remaining = len;
+ size_t n;
+
+ n = snprintf(cp, remaining, "getStatistics");
+ n = min(n, remaining);
+ remaining -= n;
+ cp += n;
+
+ logger_for_each(logger, logger_list) {
+ n = snprintf(cp, remaining, " %d", logger->id);
+ n = min(n, remaining);
+ remaining -= n;
+ cp += n;
+ }
+ return send_log_msg(NULL, NULL, buf, len);
+}
+
+ssize_t android_logger_get_prune_list(struct logger_list *logger_list __unused,
+ char *buf, size_t len)
+{
+ return send_log_msg(NULL, "getPruneList", buf, len);
+}
+
+int android_logger_set_prune_list(struct logger_list *logger_list __unused,
+ char *buf, size_t len)
+{
+ const char cmd[] = "setPruneList ";
+ const size_t cmdlen = sizeof(cmd) - 1;
+
+ if (strlen(buf) > (len - cmdlen)) {
+ return -ENOMEM; /* KISS */
+ }
+ memmove(buf + cmdlen, buf, len - cmdlen);
+ buf[len - 1] = '\0';
+ memcpy(buf, cmd, cmdlen);
+
+ return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
}
struct logger_list *android_logger_list_alloc(int mode,
@@ -256,10 +534,36 @@
if (!logger_list) {
return NULL;
}
+
list_init(&logger_list->node);
logger_list->mode = mode;
+ logger_list->start.tv_sec = 0;
+ logger_list->start.tv_nsec = 0;
logger_list->tail = tail;
logger_list->pid = pid;
+ logger_list->sock = -1;
+
+ return logger_list;
+}
+
+struct logger_list *android_logger_list_alloc_time(int mode,
+ log_time start,
+ pid_t pid)
+{
+ struct logger_list *logger_list;
+
+ logger_list = calloc(1, sizeof(*logger_list));
+ if (!logger_list) {
+ return NULL;
+ }
+
+ list_init(&logger_list->node);
+ logger_list->mode = mode;
+ logger_list->start = start;
+ logger_list->tail = 0;
+ logger_list->pid = pid;
+ logger_list->sock = -1;
+
return logger_list;
}
@@ -270,9 +574,7 @@
struct logger *android_logger_open(struct logger_list *logger_list,
log_id_t id)
{
- struct listnode *node;
struct logger *logger;
- char *n;
if (!logger_list || (id >= LOG_ID_MAX)) {
goto err;
@@ -289,28 +591,11 @@
goto err;
}
- if (check_allocate_accessible(&n, android_log_id_to_name(id),
- logger_list->mode)) {
- goto err_name;
- }
-
- logger->fd = open(n, logger_list->mode);
- if (logger->fd < 0) {
- goto err_name;
- }
-
- free(n);
logger->id = id;
- list_init(&logger->log_list);
list_add_tail(&logger_list->node, &logger->node);
logger->top = logger_list;
- logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
goto ok;
-err_name:
- free(n);
-err_logger:
- free(logger);
err:
logger = NULL;
ok:
@@ -336,315 +621,260 @@
return logger_list;
}
-/* prevent memory starvation when backfilling */
-static unsigned int queue_threshold(struct logger_list *logger_list)
+static int android_logger_list_read_pstore(struct logger_list *logger_list,
+ struct log_msg *log_msg)
{
- return (logger_list->tail < 64) ? 64 : logger_list->tail;
-}
+ ssize_t ret;
+ off_t current, next;
+ uid_t uid;
+ struct logger *logger;
+ struct __attribute__((__packed__)) {
+ android_pmsg_log_header_t p;
+ android_log_header_t l;
+ } buf;
+ static uint8_t preread_count;
-static bool low_queue(struct listnode *node)
-{
- /* low is considered less than 2 */
- return list_head(node) == list_tail(node);
-}
+ memset(log_msg, 0, sizeof(*log_msg));
-/* Flush queues in sequential order, one at a time */
-static int android_logger_list_flush(struct logger_list *logger_list,
- struct log_msg *log_msg)
-{
- int ret = 0;
- struct log_list *firstentry = NULL;
+ if (logger_list->sock < 0) {
+ int fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY);
- while ((ret == 0)
- && (logger_list->flush
- || (logger_list->queued_lines > logger_list->tail))) {
- struct logger *logger;
+ if (fd < 0) {
+ return -errno;
+ }
+ logger_list->sock = fd;
+ preread_count = 0;
+ }
- /* Merge sort */
- bool at_least_one_is_low = false;
- struct logger *firstlogger = NULL;
- firstentry = NULL;
+ ret = 0;
+ while(1) {
+ if (preread_count < sizeof(buf)) {
+ ret = TEMP_FAILURE_RETRY(read(logger_list->sock,
+ &buf.p.magic + preread_count,
+ sizeof(buf) - preread_count));
+ if (ret < 0) {
+ return -errno;
+ }
+ preread_count += ret;
+ }
+ if (preread_count != sizeof(buf)) {
+ return preread_count ? -EIO : -EAGAIN;
+ }
+ if ((buf.p.magic != LOGGER_MAGIC)
+ || (buf.p.len <= sizeof(buf))
+ || (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD))
+ || (buf.l.id >= LOG_ID_MAX)
+ || (buf.l.realtime.tv_nsec >= NS_PER_SEC)) {
+ do {
+ memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
+ } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
+ continue;
+ }
+ preread_count = 0;
logger_for_each(logger, logger_list) {
- struct listnode *node;
- struct log_list *oldest = NULL;
-
- /* kernel logger channels not necessarily time-sort order */
- list_for_each(node, &logger->log_list) {
- struct log_list *entry = node_to_item(node,
- struct log_list, node);
- if (!oldest
- || (entry->entry.entry.sec < oldest->entry.entry.sec)
- || ((entry->entry.entry.sec == oldest->entry.entry.sec)
- && (entry->entry.entry.nsec < oldest->entry.entry.nsec))) {
- oldest = entry;
- }
- }
-
- if (!oldest) {
- at_least_one_is_low = true;
+ if (buf.l.id != logger->id) {
continue;
- } else if (low_queue(&logger->log_list)) {
- at_least_one_is_low = true;
}
- if (!firstentry
- || (oldest->entry.entry.sec < firstentry->entry.entry.sec)
- || ((oldest->entry.entry.sec == firstentry->entry.entry.sec)
- && (oldest->entry.entry.nsec < firstentry->entry.entry.nsec))) {
- firstentry = oldest;
- firstlogger = logger;
+ if ((logger_list->start.tv_sec || logger_list->start.tv_nsec)
+ && ((logger_list->start.tv_sec > buf.l.realtime.tv_sec)
+ || ((logger_list->start.tv_sec == buf.l.realtime.tv_sec)
+ && (logger_list->start.tv_nsec > buf.l.realtime.tv_nsec)))) {
+ break;
}
- }
- if (!firstentry) {
- break;
- }
-
- /* when trimming list, tries to keep one entry behind in each bucket */
- if (!logger_list->flush
- && at_least_one_is_low
- && (logger_list->queued_lines < queue_threshold(logger_list))) {
- break;
- }
-
- /* within tail?, send! */
- if ((logger_list->tail == 0)
- || (logger_list->queued_lines <= logger_list->tail)) {
- ret = firstentry->entry.entry.hdr_size;
- if (!ret) {
- ret = sizeof(firstentry->entry.entry_v1);
+ if (logger_list->pid && (logger_list->pid != buf.p.pid)) {
+ break;
}
- ret += firstentry->entry.entry.len;
- memcpy(log_msg->buf, firstentry->entry.buf, ret + 1);
- log_msg->extra.id = firstlogger->id;
+ uid = get_best_effective_uid();
+ if (!uid_has_log_permission(uid) && (uid != buf.p.uid)) {
+ break;
+ }
+
+ ret = TEMP_FAILURE_RETRY(read(logger_list->sock,
+ log_msg->entry_v3.msg,
+ buf.p.len - sizeof(buf)));
+ if (ret < 0) {
+ return -errno;
+ }
+ if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
+ return -EIO;
+ }
+
+ log_msg->entry_v3.len = buf.p.len - sizeof(buf);
+ log_msg->entry_v3.hdr_size = sizeof(log_msg->entry_v3);
+ log_msg->entry_v3.pid = buf.p.pid;
+ log_msg->entry_v3.tid = buf.l.tid;
+ log_msg->entry_v3.sec = buf.l.realtime.tv_sec;
+ log_msg->entry_v3.nsec = buf.l.realtime.tv_nsec;
+ log_msg->entry_v3.lid = buf.l.id;
+
+ return ret;
}
- /* next entry */
- list_remove(&firstentry->node);
- free(firstentry);
- if (logger_list->queued_lines) {
- logger_list->queued_lines--;
+ current = TEMP_FAILURE_RETRY(lseek(logger_list->sock,
+ (off_t)0, SEEK_CUR));
+ if (current < 0) {
+ return -errno;
+ }
+ next = TEMP_FAILURE_RETRY(lseek(logger_list->sock,
+ (off_t)(buf.p.len - sizeof(buf)),
+ SEEK_CUR));
+ if (next < 0) {
+ return -errno;
+ }
+ if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
+ return -EIO;
}
}
+}
- /* Flushed the list, no longer in tail mode for continuing content */
- if (logger_list->flush && !firstentry) {
- logger_list->tail = 0;
- }
- return ret;
+static void caught_signal(int signum __unused)
+{
}
/* Read from the selected logs */
int android_logger_list_read(struct logger_list *logger_list,
struct log_msg *log_msg)
{
+ int ret, e;
struct logger *logger;
- nfds_t nfds;
- struct pollfd *p, *pollfds = NULL;
- int error = 0, ret = 0;
-
- memset(log_msg, 0, sizeof(struct log_msg));
+ struct sigaction ignore;
+ struct sigaction old_sigaction;
+ unsigned int old_alarm = 0;
if (!logger_list) {
- return -ENODEV;
+ return -EINVAL;
}
- if (!(accessmode(logger_list->mode) & R_OK)) {
- logger_list->error = EPERM;
- goto done;
+ if (logger_list->mode & ANDROID_LOG_PSTORE) {
+ return android_logger_list_read_pstore(logger_list, log_msg);
}
- nfds = 0;
- logger_for_each(logger, logger_list) {
- ++nfds;
- }
- if (nfds <= 0) {
- error = ENODEV;
- goto done;
+ if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ memset(&ignore, 0, sizeof(ignore));
+ ignore.sa_handler = caught_signal;
+ sigemptyset(&ignore.sa_mask);
}
- /* Do we have anything to offer from the buffer or state? */
- if (logger_list->valid_entry) { /* implies we are also in a flush state */
- goto flush;
- }
+ if (logger_list->sock < 0) {
+ char buffer[256], *cp, c;
- ret = android_logger_list_flush(logger_list, log_msg);
- if (ret) {
- goto done;
- }
-
- if (logger_list->error) { /* implies we are also in a flush state */
- goto done;
- }
-
- /* Lets start grinding on metal */
- pollfds = calloc(nfds, sizeof(struct pollfd));
- if (!pollfds) {
- error = ENOMEM;
- goto flush;
- }
-
- p = pollfds;
- logger_for_each(logger, logger_list) {
- p->fd = logger->fd;
- p->events = POLLIN;
- logger->revents = &p->revents;
- ++p;
- }
-
- while (!ret && !error) {
- int result;
-
- /* If we oversleep it's ok, i.e. ignore EINTR. */
- result = TEMP_FAILURE_RETRY(
- poll(pollfds, nfds, logger_list->timeout_ms));
-
- if (result <= 0) {
- if (result) {
- error = errno;
- } else if (logger_list->mode & O_NDELAY) {
- error = EAGAIN;
- } else {
- logger_list->timeout_ms = LOG_TIMEOUT_NEVER;
+ int sock = socket_local_client("logdr",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_SEQPACKET);
+ if (sock < 0) {
+ if ((sock == -1) && errno) {
+ return -errno;
}
-
- logger_list->flush = true;
- goto try_flush;
+ return sock;
}
- logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
+ strcpy(buffer,
+ (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream");
+ cp = buffer + strlen(buffer);
- /* Anti starvation */
- if (!logger_list->flush
- && (logger_list->queued_lines > (queue_threshold(logger_list) / 2))) {
- /* Any queues with input pending that is low? */
- bool starving = false;
- logger_for_each(logger, logger_list) {
- if ((*(logger->revents) & POLLIN)
- && low_queue(&logger->log_list)) {
- starving = true;
- break;
- }
- }
-
- /* pushback on any queues that are not low */
- if (starving) {
- logger_for_each(logger, logger_list) {
- if ((*(logger->revents) & POLLIN)
- && !low_queue(&logger->log_list)) {
- *(logger->revents) &= ~POLLIN;
- }
- }
- }
- }
-
+ strcpy(cp, " lids");
+ cp += 5;
+ c = '=';
+ int remaining = sizeof(buffer) - (cp - buffer);
logger_for_each(logger, logger_list) {
- unsigned int hdr_size;
- struct log_list *entry;
+ ret = snprintf(cp, remaining, "%c%u", c, logger->id);
+ ret = min(ret, remaining);
+ remaining -= ret;
+ cp += ret;
+ c = ',';
+ }
- if (!(*(logger->revents) & POLLIN)) {
- continue;
+ if (logger_list->tail) {
+ ret = snprintf(cp, remaining, " tail=%u", logger_list->tail);
+ ret = min(ret, remaining);
+ remaining -= ret;
+ cp += ret;
+ }
+
+ if (logger_list->start.tv_sec || logger_list->start.tv_nsec) {
+ ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32,
+ logger_list->start.tv_sec,
+ logger_list->start.tv_nsec);
+ ret = min(ret, remaining);
+ remaining -= ret;
+ cp += ret;
+ }
+
+ if (logger_list->pid) {
+ ret = snprintf(cp, remaining, " pid=%u", logger_list->pid);
+ ret = min(ret, remaining);
+ remaining -= ret;
+ cp += ret;
+ }
+
+ if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ /* Deal with an unresponsive logd */
+ sigaction(SIGALRM, &ignore, &old_sigaction);
+ old_alarm = alarm(30);
+ }
+ ret = write(sock, buffer, cp - buffer);
+ e = errno;
+ if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ if (e == EINTR) {
+ e = ETIMEDOUT;
}
-
- memset(logger_list->entry.buf, 0, sizeof(struct log_msg));
- /* NOTE: driver guarantees we read exactly one full entry */
- result = read(logger->fd, logger_list->entry.buf,
- LOGGER_ENTRY_MAX_LEN);
- if (result <= 0) {
- if (!result) {
- error = EIO;
- } else if (errno != EINTR) {
- error = errno;
- }
- continue;
- }
-
- if (logger_list->pid
- && (logger_list->pid != logger_list->entry.entry.pid)) {
- continue;
- }
-
- hdr_size = logger_list->entry.entry.hdr_size;
- if (!hdr_size) {
- hdr_size = sizeof(logger_list->entry.entry_v1);
- }
-
- if ((hdr_size > sizeof(struct log_msg))
- || (logger_list->entry.entry.len
- > sizeof(logger_list->entry.buf) - hdr_size)
- || (logger_list->entry.entry.len != result - hdr_size)) {
- error = EINVAL;
- continue;
- }
-
- logger_list->entry.extra.id = logger->id;
-
- /* speedup: If not tail, and only one list, send directly */
- if (!logger_list->tail
- && (list_head(&logger_list->node)
- == list_tail(&logger_list->node))) {
- ret = result;
- memcpy(log_msg->buf, logger_list->entry.buf, result + 1);
- log_msg->extra.id = logger->id;
- break;
- }
-
- entry = malloc(sizeof(*entry) - sizeof(entry->entry) + result + 1);
-
- if (!entry) {
- logger_list->valid_entry = true;
- error = ENOMEM;
- break;
- }
-
- logger_list->queued_lines++;
-
- memcpy(entry->entry.buf, logger_list->entry.buf, result);
- entry->entry.buf[result] = '\0';
- list_add_tail(&logger->log_list, &entry->node);
+ alarm(old_alarm);
+ sigaction(SIGALRM, &old_sigaction, NULL);
}
if (ret <= 0) {
-try_flush:
- ret = android_logger_list_flush(logger_list, log_msg);
- }
- }
-
- free(pollfds);
-
-flush:
- if (error) {
- logger_list->flush = true;
- }
-
- if (ret <= 0) {
- ret = android_logger_list_flush(logger_list, log_msg);
-
- if (!ret && logger_list->valid_entry) {
- ret = logger_list->entry.entry.hdr_size;
- if (!ret) {
- ret = sizeof(logger_list->entry.entry_v1);
+ close(sock);
+ if ((ret == -1) && e) {
+ return -e;
}
- ret += logger_list->entry.entry.len;
-
- memcpy(log_msg->buf, logger_list->entry.buf,
- sizeof(struct log_msg));
- logger_list->valid_entry = false;
+ if (ret == 0) {
+ return -EIO;
+ }
+ return ret;
}
+
+ logger_list->sock = sock;
}
-done:
- if (logger_list->error) {
- error = logger_list->error;
- }
- if (error) {
- logger_list->error = error;
- if (!ret) {
- ret = -error;
+ ret = 0;
+ while(1) {
+ memset(log_msg, 0, sizeof(*log_msg));
+
+ if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ /* particularily useful if tombstone is reporting for logd */
+ sigaction(SIGALRM, &ignore, &old_sigaction);
+ old_alarm = alarm(30);
+ }
+ /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */
+ ret = recv(logger_list->sock, log_msg, LOGGER_ENTRY_MAX_LEN, 0);
+ e = errno;
+ if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ if ((ret == 0) || (e == EINTR)) {
+ e = EAGAIN;
+ ret = -1;
+ }
+ alarm(old_alarm);
+ sigaction(SIGALRM, &old_sigaction, NULL);
+ }
+
+ if (ret <= 0) {
+ if ((ret == -1) && e) {
+ return -e;
+ }
+ return ret;
+ }
+
+ logger_for_each(logger, logger_list) {
+ if (log_msg->entry.lid == logger->id) {
+ return ret;
+ }
}
}
+ /* NOTREACH */
return ret;
}
@@ -661,5 +891,9 @@
android_logger_free(logger);
}
+ if (logger_list->sock >= 0) {
+ close (logger_list->sock);
+ }
+
free(logger_list);
}
diff --git a/liblog/log_read_kern.c b/liblog/log_read_kern.c
new file mode 100644
index 0000000..bdc7b18
--- /dev/null
+++ b/liblog/log_read_kern.c
@@ -0,0 +1,741 @@
+/*
+** Copyright 2013-2014, 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.
+*/
+
+#define _GNU_SOURCE /* asprintf for x86 host */
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/cdefs.h>
+#include <sys/ioctl.h>
+
+#include <cutils/list.h>
+#include <log/log.h>
+#include <log/logger.h>
+
+#define __LOGGERIO 0xAE
+
+#define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */
+#define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */
+#define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */
+#define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */
+#define LOGGER_GET_VERSION _IO(__LOGGERIO, 5) /* abi version */
+#define LOGGER_SET_VERSION _IO(__LOGGERIO, 6) /* abi version */
+
+typedef char bool;
+#define false (const bool)0
+#define true (const bool)1
+
+#define LOG_FILE_DIR "/dev/log/"
+
+/* timeout in milliseconds */
+#define LOG_TIMEOUT_FLUSH 5
+#define LOG_TIMEOUT_NEVER -1
+
+#define logger_for_each(logger, logger_list) \
+ for (logger = node_to_item((logger_list)->node.next, struct logger, node); \
+ logger != node_to_item(&(logger_list)->node, struct logger, node); \
+ logger = node_to_item((logger)->node.next, struct logger, node))
+
+#ifndef __unused
+#define __unused __attribute__((unused))
+#endif
+
+/* In the future, we would like to make this list extensible */
+static const char *LOG_NAME[LOG_ID_MAX] = {
+ [LOG_ID_MAIN] = "main",
+ [LOG_ID_RADIO] = "radio",
+ [LOG_ID_EVENTS] = "events",
+ [LOG_ID_SYSTEM] = "system",
+ [LOG_ID_CRASH] = "crash"
+};
+
+const char *android_log_id_to_name(log_id_t log_id)
+{
+ if (log_id >= LOG_ID_MAX) {
+ log_id = LOG_ID_MAIN;
+ }
+ return LOG_NAME[log_id];
+}
+
+static int accessmode(int mode)
+{
+ if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_WRONLY) {
+ return W_OK;
+ }
+ if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR) {
+ return R_OK | W_OK;
+ }
+ return R_OK;
+}
+
+/* repeated fragment */
+static int check_allocate_accessible(char **n, const char *b, int mode)
+{
+ *n = NULL;
+
+ if (!b) {
+ return -EINVAL;
+ }
+
+ asprintf(n, LOG_FILE_DIR "%s", b);
+ if (!*n) {
+ return -1;
+ }
+
+ return access(*n, accessmode(mode));
+}
+
+log_id_t android_name_to_log_id(const char *logName)
+{
+ const char *b;
+ char *n;
+ int ret;
+
+ if (!logName) {
+ return -1; /* NB: log_id_t is unsigned */
+ }
+ b = strrchr(logName, '/');
+ if (!b) {
+ b = logName;
+ } else {
+ ++b;
+ }
+
+ ret = check_allocate_accessible(&n, b, ANDROID_LOG_RDONLY);
+ free(n);
+ if (ret) {
+ return ret;
+ }
+
+ for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
+ const char *l = LOG_NAME[ret];
+ if (l && !strcmp(b, l)) {
+ return ret;
+ }
+ }
+ return -1; /* should never happen */
+}
+
+struct logger_list {
+ struct listnode node;
+ int mode;
+ unsigned int tail;
+ pid_t pid;
+ unsigned int queued_lines;
+ int timeout_ms;
+ int error;
+ bool flush;
+ bool valid_entry; /* valiant(?) effort to deal with memory starvation */
+ struct log_msg entry;
+};
+
+struct log_list {
+ struct listnode node;
+ struct log_msg entry; /* Truncated to event->len() + 1 to save space */
+};
+
+struct logger {
+ struct listnode node;
+ struct logger_list *top;
+ int fd;
+ log_id_t id;
+ short *revents;
+ struct listnode log_list;
+};
+
+/* android_logger_alloc unimplemented, no use case */
+/* android_logger_free not exported */
+static void android_logger_free(struct logger *logger)
+{
+ if (!logger) {
+ return;
+ }
+
+ while (!list_empty(&logger->log_list)) {
+ struct log_list *entry = node_to_item(
+ list_head(&logger->log_list), struct log_list, node);
+ list_remove(&entry->node);
+ free(entry);
+ if (logger->top->queued_lines) {
+ logger->top->queued_lines--;
+ }
+ }
+
+ if (logger->fd >= 0) {
+ close(logger->fd);
+ }
+
+ list_remove(&logger->node);
+
+ free(logger);
+}
+
+log_id_t android_logger_get_id(struct logger *logger)
+{
+ return logger->id;
+}
+
+/* worker for sending the command to the logger */
+static int logger_ioctl(struct logger *logger, int cmd, int mode)
+{
+ char *n;
+ int f, ret;
+
+ if (!logger || !logger->top) {
+ return -EFAULT;
+ }
+
+ if (((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR)
+ || (((mode ^ logger->top->mode) & ANDROID_LOG_ACCMODE) == 0)) {
+ return ioctl(logger->fd, cmd);
+ }
+
+ /* We go here if android_logger_list_open got mode wrong for this ioctl */
+ ret = check_allocate_accessible(&n, android_log_id_to_name(logger->id), mode);
+ if (ret) {
+ free(n);
+ return ret;
+ }
+
+ f = open(n, mode);
+ free(n);
+ if (f < 0) {
+ return f;
+ }
+
+ ret = ioctl(f, cmd);
+ close (f);
+
+ return ret;
+}
+
+int android_logger_clear(struct logger *logger)
+{
+ return logger_ioctl(logger, LOGGER_FLUSH_LOG, ANDROID_LOG_WRONLY);
+}
+
+/* returns the total size of the log's ring buffer */
+long android_logger_get_log_size(struct logger *logger)
+{
+ return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, ANDROID_LOG_RDWR);
+}
+
+int android_logger_set_log_size(struct logger *logger __unused,
+ unsigned long size __unused)
+{
+ return -ENOTSUP;
+}
+
+/*
+ * returns the readable size of the log's ring buffer (that is, amount of the
+ * log consumed)
+ */
+long android_logger_get_log_readable_size(struct logger *logger)
+{
+ return logger_ioctl(logger, LOGGER_GET_LOG_LEN, ANDROID_LOG_RDONLY);
+}
+
+/*
+ * returns the logger version
+ */
+int android_logger_get_log_version(struct logger *logger)
+{
+ int ret = logger_ioctl(logger, LOGGER_GET_VERSION, ANDROID_LOG_RDWR);
+ return (ret < 0) ? 1 : ret;
+}
+
+/*
+ * returns statistics
+ */
+static const char unsupported[] = "18\nNot Supported\n\f";
+
+ssize_t android_logger_get_statistics(struct logger_list *logger_list __unused,
+ char *buf, size_t len)
+{
+ strncpy(buf, unsupported, len);
+ return -ENOTSUP;
+}
+
+ssize_t android_logger_get_prune_list(struct logger_list *logger_list __unused,
+ char *buf, size_t len)
+{
+ strncpy(buf, unsupported, len);
+ return -ENOTSUP;
+}
+
+int android_logger_set_prune_list(struct logger_list *logger_list __unused,
+ char *buf, size_t len)
+{
+ static const char unsupported_error[] = "Unsupported";
+ strncpy(buf, unsupported, len);
+ return -ENOTSUP;
+}
+
+struct logger_list *android_logger_list_alloc(int mode,
+ unsigned int tail,
+ pid_t pid)
+{
+ struct logger_list *logger_list;
+
+ logger_list = calloc(1, sizeof(*logger_list));
+ if (!logger_list) {
+ return NULL;
+ }
+ list_init(&logger_list->node);
+ logger_list->mode = mode;
+ logger_list->tail = tail;
+ logger_list->pid = pid;
+ return logger_list;
+}
+
+struct logger_list *android_logger_list_alloc_time(int mode,
+ log_time start __unused,
+ pid_t pid)
+{
+ return android_logger_list_alloc(mode, 0, pid);
+}
+
+/* android_logger_list_register unimplemented, no use case */
+/* android_logger_list_unregister unimplemented, no use case */
+
+/* Open the named log and add it to the logger list */
+struct logger *android_logger_open(struct logger_list *logger_list,
+ log_id_t id)
+{
+ struct listnode *node;
+ struct logger *logger;
+ char *n;
+
+ if (!logger_list || (id >= LOG_ID_MAX)) {
+ goto err;
+ }
+
+ logger_for_each(logger, logger_list) {
+ if (logger->id == id) {
+ goto ok;
+ }
+ }
+
+ logger = calloc(1, sizeof(*logger));
+ if (!logger) {
+ goto err;
+ }
+
+ if (check_allocate_accessible(&n, android_log_id_to_name(id),
+ logger_list->mode)) {
+ goto err_name;
+ }
+
+ logger->fd = open(n, logger_list->mode & (ANDROID_LOG_ACCMODE | ANDROID_LOG_NONBLOCK));
+ if (logger->fd < 0) {
+ goto err_name;
+ }
+
+ free(n);
+ logger->id = id;
+ list_init(&logger->log_list);
+ list_add_tail(&logger_list->node, &logger->node);
+ logger->top = logger_list;
+ logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
+ goto ok;
+
+err_name:
+ free(n);
+err_logger:
+ free(logger);
+err:
+ logger = NULL;
+ok:
+ return logger;
+}
+
+/* Open the single named log and make it part of a new logger list */
+struct logger_list *android_logger_list_open(log_id_t id,
+ int mode,
+ unsigned int tail,
+ pid_t pid)
+{
+ struct logger_list *logger_list = android_logger_list_alloc(mode, tail, pid);
+ if (!logger_list) {
+ return NULL;
+ }
+
+ if (!android_logger_open(logger_list, id)) {
+ android_logger_list_free(logger_list);
+ return NULL;
+ }
+
+ return logger_list;
+}
+
+/* prevent memory starvation when backfilling */
+static unsigned int queue_threshold(struct logger_list *logger_list)
+{
+ return (logger_list->tail < 64) ? 64 : logger_list->tail;
+}
+
+static bool low_queue(struct listnode *node)
+{
+ /* low is considered less than 2 */
+ return list_head(node) == list_tail(node);
+}
+
+/* Flush queues in sequential order, one at a time */
+static int android_logger_list_flush(struct logger_list *logger_list,
+ struct log_msg *log_msg)
+{
+ int ret = 0;
+ struct log_list *firstentry = NULL;
+
+ while ((ret == 0)
+ && (logger_list->flush
+ || (logger_list->queued_lines > logger_list->tail))) {
+ struct logger *logger;
+
+ /* Merge sort */
+ bool at_least_one_is_low = false;
+ struct logger *firstlogger = NULL;
+ firstentry = NULL;
+
+ logger_for_each(logger, logger_list) {
+ struct listnode *node;
+ struct log_list *oldest = NULL;
+
+ /* kernel logger channels not necessarily time-sort order */
+ list_for_each(node, &logger->log_list) {
+ struct log_list *entry = node_to_item(node,
+ struct log_list, node);
+ if (!oldest
+ || (entry->entry.entry.sec < oldest->entry.entry.sec)
+ || ((entry->entry.entry.sec == oldest->entry.entry.sec)
+ && (entry->entry.entry.nsec < oldest->entry.entry.nsec))) {
+ oldest = entry;
+ }
+ }
+
+ if (!oldest) {
+ at_least_one_is_low = true;
+ continue;
+ } else if (low_queue(&logger->log_list)) {
+ at_least_one_is_low = true;
+ }
+
+ if (!firstentry
+ || (oldest->entry.entry.sec < firstentry->entry.entry.sec)
+ || ((oldest->entry.entry.sec == firstentry->entry.entry.sec)
+ && (oldest->entry.entry.nsec < firstentry->entry.entry.nsec))) {
+ firstentry = oldest;
+ firstlogger = logger;
+ }
+ }
+
+ if (!firstentry) {
+ break;
+ }
+
+ /* when trimming list, tries to keep one entry behind in each bucket */
+ if (!logger_list->flush
+ && at_least_one_is_low
+ && (logger_list->queued_lines < queue_threshold(logger_list))) {
+ break;
+ }
+
+ /* within tail?, send! */
+ if ((logger_list->tail == 0)
+ || (logger_list->queued_lines <= logger_list->tail)) {
+ int diff;
+ ret = firstentry->entry.entry.hdr_size;
+ if (!ret) {
+ ret = sizeof(firstentry->entry.entry_v1);
+ }
+
+ /* Promote entry to v3 format */
+ memcpy(log_msg->buf, firstentry->entry.buf, ret);
+ diff = sizeof(firstentry->entry.entry_v3) - ret;
+ if (diff < 0) {
+ diff = 0;
+ } else if (diff > 0) {
+ memset(log_msg->buf + ret, 0, diff);
+ }
+ memcpy(log_msg->buf + ret + diff, firstentry->entry.buf + ret,
+ firstentry->entry.entry.len + 1);
+ ret += diff;
+ log_msg->entry.hdr_size = ret;
+ log_msg->entry.lid = firstlogger->id;
+
+ ret += firstentry->entry.entry.len;
+ }
+
+ /* next entry */
+ list_remove(&firstentry->node);
+ free(firstentry);
+ if (logger_list->queued_lines) {
+ logger_list->queued_lines--;
+ }
+ }
+
+ /* Flushed the list, no longer in tail mode for continuing content */
+ if (logger_list->flush && !firstentry) {
+ logger_list->tail = 0;
+ }
+ return ret;
+}
+
+/* Read from the selected logs */
+int android_logger_list_read(struct logger_list *logger_list,
+ struct log_msg *log_msg)
+{
+ struct logger *logger;
+ nfds_t nfds;
+ struct pollfd *p, *pollfds = NULL;
+ int error = 0, ret = 0;
+
+ memset(log_msg, 0, sizeof(struct log_msg));
+
+ if (!logger_list) {
+ return -ENODEV;
+ }
+
+ if (!(accessmode(logger_list->mode) & R_OK)) {
+ logger_list->error = EPERM;
+ goto done;
+ }
+
+ nfds = 0;
+ logger_for_each(logger, logger_list) {
+ ++nfds;
+ }
+ if (nfds <= 0) {
+ error = ENODEV;
+ goto done;
+ }
+
+ /* Do we have anything to offer from the buffer or state? */
+ if (logger_list->valid_entry) { /* implies we are also in a flush state */
+ goto flush;
+ }
+
+ ret = android_logger_list_flush(logger_list, log_msg);
+ if (ret) {
+ goto done;
+ }
+
+ if (logger_list->error) { /* implies we are also in a flush state */
+ goto done;
+ }
+
+ /* Lets start grinding on metal */
+ pollfds = calloc(nfds, sizeof(struct pollfd));
+ if (!pollfds) {
+ error = ENOMEM;
+ goto flush;
+ }
+
+ p = pollfds;
+ logger_for_each(logger, logger_list) {
+ p->fd = logger->fd;
+ p->events = POLLIN;
+ logger->revents = &p->revents;
+ ++p;
+ }
+
+ while (!ret && !error) {
+ int result;
+
+ /* If we oversleep it's ok, i.e. ignore EINTR. */
+ result = TEMP_FAILURE_RETRY(
+ poll(pollfds, nfds, logger_list->timeout_ms));
+
+ if (result <= 0) {
+ if (result) {
+ error = errno;
+ } else if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
+ error = EAGAIN;
+ } else {
+ logger_list->timeout_ms = LOG_TIMEOUT_NEVER;
+ }
+
+ logger_list->flush = true;
+ goto try_flush;
+ }
+
+ logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
+
+ /* Anti starvation */
+ if (!logger_list->flush
+ && (logger_list->queued_lines > (queue_threshold(logger_list) / 2))) {
+ /* Any queues with input pending that is low? */
+ bool starving = false;
+ logger_for_each(logger, logger_list) {
+ if ((*(logger->revents) & POLLIN)
+ && low_queue(&logger->log_list)) {
+ starving = true;
+ break;
+ }
+ }
+
+ /* pushback on any queues that are not low */
+ if (starving) {
+ logger_for_each(logger, logger_list) {
+ if ((*(logger->revents) & POLLIN)
+ && !low_queue(&logger->log_list)) {
+ *(logger->revents) &= ~POLLIN;
+ }
+ }
+ }
+ }
+
+ logger_for_each(logger, logger_list) {
+ unsigned int hdr_size;
+ struct log_list *entry;
+ int diff;
+
+ if (!(*(logger->revents) & POLLIN)) {
+ continue;
+ }
+
+ memset(logger_list->entry.buf, 0, sizeof(struct log_msg));
+ /* NOTE: driver guarantees we read exactly one full entry */
+ result = read(logger->fd, logger_list->entry.buf,
+ LOGGER_ENTRY_MAX_LEN);
+ if (result <= 0) {
+ if (!result) {
+ error = EIO;
+ } else if (errno != EINTR) {
+ error = errno;
+ }
+ continue;
+ }
+
+ if (logger_list->pid
+ && (logger_list->pid != logger_list->entry.entry.pid)) {
+ continue;
+ }
+
+ hdr_size = logger_list->entry.entry.hdr_size;
+ if (!hdr_size) {
+ hdr_size = sizeof(logger_list->entry.entry_v1);
+ }
+
+ if ((hdr_size > sizeof(struct log_msg))
+ || (logger_list->entry.entry.len
+ > sizeof(logger_list->entry.buf) - hdr_size)
+ || (logger_list->entry.entry.len != result - hdr_size)) {
+ error = EINVAL;
+ continue;
+ }
+
+ /* Promote entry to v3 format */
+ diff = sizeof(logger_list->entry.entry_v3) - hdr_size;
+ if (diff > 0) {
+ if (logger_list->entry.entry.len
+ > sizeof(logger_list->entry.buf) - hdr_size - diff) {
+ error = EINVAL;
+ continue;
+ }
+ result += diff;
+ memmove(logger_list->entry.buf + hdr_size + diff,
+ logger_list->entry.buf + hdr_size,
+ logger_list->entry.entry.len + 1);
+ memset(logger_list->entry.buf + hdr_size, 0, diff);
+ logger_list->entry.entry.hdr_size = hdr_size + diff;
+ }
+ logger_list->entry.entry.lid = logger->id;
+
+ /* speedup: If not tail, and only one list, send directly */
+ if (!logger_list->tail
+ && (list_head(&logger_list->node)
+ == list_tail(&logger_list->node))) {
+ ret = result;
+ memcpy(log_msg->buf, logger_list->entry.buf, result + 1);
+ break;
+ }
+
+ entry = malloc(sizeof(*entry) - sizeof(entry->entry) + result + 1);
+
+ if (!entry) {
+ logger_list->valid_entry = true;
+ error = ENOMEM;
+ break;
+ }
+
+ logger_list->queued_lines++;
+
+ memcpy(entry->entry.buf, logger_list->entry.buf, result);
+ entry->entry.buf[result] = '\0';
+ list_add_tail(&logger->log_list, &entry->node);
+ }
+
+ if (ret <= 0) {
+try_flush:
+ ret = android_logger_list_flush(logger_list, log_msg);
+ }
+ }
+
+ free(pollfds);
+
+flush:
+ if (error) {
+ logger_list->flush = true;
+ }
+
+ if (ret <= 0) {
+ ret = android_logger_list_flush(logger_list, log_msg);
+
+ if (!ret && logger_list->valid_entry) {
+ ret = logger_list->entry.entry.hdr_size;
+ if (!ret) {
+ ret = sizeof(logger_list->entry.entry_v1);
+ }
+ ret += logger_list->entry.entry.len;
+
+ memcpy(log_msg->buf, logger_list->entry.buf,
+ sizeof(struct log_msg));
+ logger_list->valid_entry = false;
+ }
+ }
+
+done:
+ if (logger_list->error) {
+ error = logger_list->error;
+ }
+ if (error) {
+ logger_list->error = error;
+ if (!ret) {
+ ret = -error;
+ }
+ }
+ return ret;
+}
+
+/* Close all the logs */
+void android_logger_list_free(struct logger_list *logger_list)
+{
+ if (logger_list == NULL) {
+ return;
+ }
+
+ while (!list_empty(&logger_list->node)) {
+ struct listnode *node = list_head(&logger_list->node);
+ struct logger *logger = node_to_item(node, struct logger, node);
+ android_logger_free(logger);
+ }
+
+ free(logger_list);
+}
diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp
new file mode 100644
index 0000000..50742df
--- /dev/null
+++ b/liblog/log_time.cpp
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2014 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 <ctype.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/cdefs.h>
+
+#include <log/log_read.h>
+
+const char log_time::default_format[] = "%m-%d %H:%M:%S.%3q";
+const timespec log_time::EPOCH = { 0, 0 };
+
+// Add %#q for fractional seconds to standard strptime function
+
+char *log_time::strptime(const char *s, const char *format) {
+ time_t now;
+#ifdef __linux__
+ *this = log_time(CLOCK_REALTIME);
+ now = tv_sec;
+#else
+ time(&now);
+ tv_sec = now;
+ tv_nsec = 0;
+#endif
+
+ struct tm *ptm;
+#if !defined(_WIN32)
+ struct tm tmBuf;
+ ptm = localtime_r(&now, &tmBuf);
+#else
+ ptm = localtime(&now);
+#endif
+
+ char fmt[strlen(format) + 1];
+ strcpy(fmt, format);
+
+ char *ret = const_cast<char *> (s);
+ char *cp;
+ for (char *f = cp = fmt; ; ++cp) {
+ if (!*cp) {
+ if (f != cp) {
+ ret = ::strptime(ret, f, ptm);
+ }
+ break;
+ }
+ if (*cp != '%') {
+ continue;
+ }
+ char *e = cp;
+ ++e;
+#if (defined(__BIONIC__))
+ if (*e == 's') {
+ *cp = '\0';
+ if (*f) {
+ ret = ::strptime(ret, f, ptm);
+ if (!ret) {
+ break;
+ }
+ }
+ tv_sec = 0;
+ while (isdigit(*ret)) {
+ tv_sec = tv_sec * 10 + *ret - '0';
+ ++ret;
+ }
+ now = tv_sec;
+#if !defined(_WIN32)
+ ptm = localtime_r(&now, &tmBuf);
+#else
+ ptm = localtime(&now);
+#endif
+ } else
+#endif
+ {
+ unsigned num = 0;
+ while (isdigit(*e)) {
+ num = num * 10 + *e - '0';
+ ++e;
+ }
+ if (*e != 'q') {
+ continue;
+ }
+ *cp = '\0';
+ if (*f) {
+ ret = ::strptime(ret, f, ptm);
+ if (!ret) {
+ break;
+ }
+ }
+ unsigned long mul = NS_PER_SEC;
+ if (num == 0) {
+ num = INT_MAX;
+ }
+ tv_nsec = 0;
+ while (isdigit(*ret) && num && (mul > 1)) {
+ --num;
+ mul /= 10;
+ tv_nsec = tv_nsec + (*ret - '0') * mul;
+ ++ret;
+ }
+ }
+ f = cp = e;
+ ++f;
+ }
+
+ if (ret) {
+ tv_sec = mktime(ptm);
+ return ret;
+ }
+
+ // Upon error, place a known value into the class, the current time.
+#ifdef __linux__
+ *this = log_time(CLOCK_REALTIME);
+#else
+ time(&now);
+ tv_sec = now;
+ tv_nsec = 0;
+#endif
+ return ret;
+}
+
+log_time log_time::operator-= (const timespec &T) {
+ // No concept of negative time, clamp to EPOCH
+ if (*this <= T) {
+ return *this = EPOCH;
+ }
+
+ if (this->tv_nsec < (unsigned long int)T.tv_nsec) {
+ --this->tv_sec;
+ this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;
+ } else {
+ this->tv_nsec -= T.tv_nsec;
+ }
+ this->tv_sec -= T.tv_sec;
+
+ return *this;
+}
+
+log_time log_time::operator+= (const timespec &T) {
+ this->tv_nsec += (unsigned long int)T.tv_nsec;
+ if (this->tv_nsec >= NS_PER_SEC) {
+ this->tv_nsec -= NS_PER_SEC;
+ ++this->tv_sec;
+ }
+ this->tv_sec += T.tv_sec;
+
+ return *this;
+}
+
+log_time log_time::operator-= (const log_time &T) {
+ // No concept of negative time, clamp to EPOCH
+ if (*this <= T) {
+ return *this = EPOCH;
+ }
+
+ if (this->tv_nsec < T.tv_nsec) {
+ --this->tv_sec;
+ this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;
+ } else {
+ this->tv_nsec -= T.tv_nsec;
+ }
+ this->tv_sec -= T.tv_sec;
+
+ return *this;
+}
+
+log_time log_time::operator+= (const log_time &T) {
+ this->tv_nsec += T.tv_nsec;
+ if (this->tv_nsec >= NS_PER_SEC) {
+ this->tv_nsec -= NS_PER_SEC;
+ ++this->tv_sec;
+ }
+ this->tv_sec += T.tv_sec;
+
+ return *this;
+}
diff --git a/liblog/logd_write.c b/liblog/logd_write.c
index 5766f8c..8f8cc3f 100644
--- a/liblog/logd_write.c
+++ b/liblog/logd_write.c
@@ -13,65 +13,75 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <time.h>
-#include <stdio.h>
-#ifdef HAVE_PTHREADS
-#include <pthread.h>
+#if (FAKE_LOG_DEVICE == 0)
+#include <endian.h>
#endif
-#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
-#include <string.h>
-#include <stdlib.h>
+#if !defined(_WIN32)
+#include <pthread.h>
+#endif
#include <stdarg.h>
-#include <sys/types.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
+#include <sys/types.h>
+#if (FAKE_LOG_DEVICE == 0)
+#include <sys/socket.h>
+#include <sys/un.h>
+#endif
+#include <time.h>
+#include <unistd.h>
-#include <log/logger.h>
+#ifdef __BIONIC__
+#include <android/set_abort_message.h>
+#endif
+
#include <log/logd.h>
-#include <log/log.h>
-
-#define LOGGER_LOG_MAIN "log/main"
-#define LOGGER_LOG_RADIO "log/radio"
-#define LOGGER_LOG_EVENTS "log/events"
-#define LOGGER_LOG_SYSTEM "log/system"
+#include <log/logger.h>
+#include <log/log_read.h>
+#include <private/android_filesystem_config.h>
+#include <private/android_logger.h>
#define LOG_BUF_SIZE 1024
#if FAKE_LOG_DEVICE
-// This will be defined when building for the host.
+/* This will be defined when building for the host. */
#include "fake_log_device.h"
-#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
-#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
-#define log_close(filedes) fakeLogClose(filedes)
-#else
-#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
-#define log_writev(filedes, vector, count) writev(filedes, vector, count)
-#define log_close(filedes) close(filedes)
#endif
static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
-#define UNUSED __attribute__((__unused__))
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
-static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
+#if FAKE_LOG_DEVICE
+static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
+#else
+static int logd_fd = -1;
+static int pstore_fd = -1;
+#endif
/*
* This is used by the C++ code to decide if it should write logs through
- * the C code. Basically, if /dev/log/... is available, we're running in
+ * the C code. Basically, if /dev/socket/logd is available, we're running in
* the simulator rather than a desktop tool and want to use the device.
*/
static enum {
kLogUninitialized, kLogNotAvailable, kLogAvailable
} g_log_status = kLogUninitialized;
+
int __android_log_dev_available(void)
{
if (g_log_status == kLogUninitialized) {
- if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
+ if (access("/dev/socket/logdw", W_OK) == 0)
g_log_status = kLogAvailable;
else
g_log_status = kLogNotAvailable;
@@ -80,61 +90,274 @@
return (g_log_status == kLogAvailable);
}
-static int __write_to_log_null(log_id_t log_fd UNUSED, struct iovec *vec UNUSED,
- size_t nr UNUSED)
+#if !FAKE_LOG_DEVICE
+/* give up, resources too limited */
+static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
+ size_t nr __unused)
{
return -1;
}
+#endif
-static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
+/* log_init_lock assumed */
+static int __write_to_log_initialize()
+{
+ int i, ret = 0;
+
+#if FAKE_LOG_DEVICE
+ for (i = 0; i < LOG_ID_MAX; i++) {
+ char buf[sizeof("/dev/log_system")];
+ snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
+ log_fds[i] = fakeLogOpen(buf, O_WRONLY);
+ }
+#else
+ if (logd_fd >= 0) {
+ i = logd_fd;
+ logd_fd = -1;
+ close(i);
+ }
+ if (pstore_fd >= 0) {
+ i = pstore_fd;
+ pstore_fd = -1;
+ close(i);
+ }
+ pstore_fd = open("/dev/pmsg0", O_WRONLY);
+
+ i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+ if (i < 0) {
+ ret = -errno;
+ write_to_log = __write_to_log_null;
+ } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
+ ret = -errno;
+ close(i);
+ i = -1;
+ write_to_log = __write_to_log_null;
+ } else {
+ struct sockaddr_un un;
+ memset(&un, 0, sizeof(struct sockaddr_un));
+ un.sun_family = AF_UNIX;
+ strcpy(un.sun_path, "/dev/socket/logdw");
+
+ if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
+ ret = -errno;
+ close(i);
+ i = -1;
+ }
+ }
+ logd_fd = i;
+#endif
+
+ return ret;
+}
+
+static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr)
{
ssize_t ret;
+#if FAKE_LOG_DEVICE
int log_fd;
if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
log_fd = log_fds[(int)log_id];
} else {
- return EBADF;
+ return -EBADF;
+ }
+ do {
+ ret = fakeLogWritev(log_fd, vec, nr);
+ if (ret < 0) {
+ ret = -errno;
+ }
+ } while (ret == -EINTR);
+#else
+ static const unsigned header_length = 2;
+ struct iovec newVec[nr + header_length];
+ android_log_header_t header;
+ android_pmsg_log_header_t pmsg_header;
+ struct timespec ts;
+ size_t i, payload_size;
+ static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
+ static pid_t last_pid = (pid_t) -1;
+ static atomic_int_fast32_t dropped;
+
+ if (!nr) {
+ return -EINVAL;
}
- do {
- ret = log_writev(log_fd, vec, nr);
- } while (ret < 0 && errno == EINTR);
+ if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
+ last_uid = getuid();
+ }
+ if (last_pid == (pid_t) -1) {
+ last_pid = getpid();
+ }
+ /*
+ * struct {
+ * // what we provide to pstore
+ * android_pmsg_log_header_t pmsg_header;
+ * // what we provide to socket
+ * android_log_header_t header;
+ * // caller provides
+ * union {
+ * struct {
+ * char prio;
+ * char payload[];
+ * } string;
+ * struct {
+ * uint32_t tag
+ * char payload[];
+ * } binary;
+ * };
+ * };
+ */
+
+ clock_gettime(CLOCK_REALTIME, &ts);
+
+ pmsg_header.magic = LOGGER_MAGIC;
+ pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
+ pmsg_header.uid = last_uid;
+ pmsg_header.pid = last_pid;
+
+ header.tid = gettid();
+ header.realtime.tv_sec = ts.tv_sec;
+ header.realtime.tv_nsec = ts.tv_nsec;
+
+ newVec[0].iov_base = (unsigned char *) &pmsg_header;
+ newVec[0].iov_len = sizeof(pmsg_header);
+ newVec[1].iov_base = (unsigned char *) &header;
+ newVec[1].iov_len = sizeof(header);
+
+ if (logd_fd > 0) {
+ int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
+ if (snapshot) {
+ android_log_event_int_t buffer;
+
+ header.id = LOG_ID_EVENTS;
+ buffer.header.tag = htole32(LIBLOG_LOG_TAG);
+ buffer.payload.type = EVENT_TYPE_INT;
+ buffer.payload.data = htole32(snapshot);
+
+ newVec[2].iov_base = &buffer;
+ newVec[2].iov_len = sizeof(buffer);
+
+ ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
+ if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
+ atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
+ }
+ }
+ }
+
+ header.id = log_id;
+
+ for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
+ newVec[i].iov_base = vec[i - header_length].iov_base;
+ payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
+
+ if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
+ newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
+ if (newVec[i].iov_len) {
+ ++i;
+ }
+ payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
+ break;
+ }
+ }
+ pmsg_header.len += payload_size;
+
+ if (pstore_fd >= 0) {
+ TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
+ }
+
+ if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
+ /*
+ * ignore log messages we send to ourself (logd).
+ * Such log messages are often generated by libraries we depend on
+ * which use standard Android logging.
+ */
+ return 0;
+ }
+
+ if (logd_fd < 0) {
+ return -EBADF;
+ }
+
+ /*
+ * The write below could be lost, but will never block.
+ *
+ * To logd, we drop the pmsg_header
+ *
+ * ENOTCONN occurs if logd dies.
+ * EAGAIN occurs if logd is overloaded.
+ */
+ ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
+ if (ret < 0) {
+ ret = -errno;
+ if (ret == -ENOTCONN) {
+#if !defined(_WIN32)
+ pthread_mutex_lock(&log_init_lock);
+#endif
+ ret = __write_to_log_initialize();
+#if !defined(_WIN32)
+ pthread_mutex_unlock(&log_init_lock);
+#endif
+
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
+ if (ret < 0) {
+ ret = -errno;
+ }
+ }
+ }
+
+ if (ret > (ssize_t)sizeof(header)) {
+ ret -= sizeof(header);
+ } else if (ret == -EAGAIN) {
+ atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
+ }
+#endif
return ret;
}
+#if FAKE_LOG_DEVICE
+static const char *LOG_NAME[LOG_ID_MAX] = {
+ [LOG_ID_MAIN] = "main",
+ [LOG_ID_RADIO] = "radio",
+ [LOG_ID_EVENTS] = "events",
+ [LOG_ID_SYSTEM] = "system",
+ [LOG_ID_CRASH] = "crash"
+};
+
+const char *android_log_id_to_name(log_id_t log_id)
+{
+ if (log_id >= LOG_ID_MAX) {
+ log_id = LOG_ID_MAIN;
+ }
+ return LOG_NAME[log_id];
+}
+#endif
+
static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
{
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
pthread_mutex_lock(&log_init_lock);
#endif
if (write_to_log == __write_to_log_init) {
- log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
- log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
- log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
- log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
+ int ret;
- write_to_log = __write_to_log_kernel;
-
- if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
- log_fds[LOG_ID_EVENTS] < 0) {
- log_close(log_fds[LOG_ID_MAIN]);
- log_close(log_fds[LOG_ID_RADIO]);
- log_close(log_fds[LOG_ID_EVENTS]);
- log_fds[LOG_ID_MAIN] = -1;
- log_fds[LOG_ID_RADIO] = -1;
- log_fds[LOG_ID_EVENTS] = -1;
- write_to_log = __write_to_log_null;
+ ret = __write_to_log_initialize();
+ if (ret < 0) {
+#if !defined(_WIN32)
+ pthread_mutex_unlock(&log_init_lock);
+#endif
+ return ret;
}
- if (log_fds[LOG_ID_SYSTEM] < 0) {
- log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
- }
+ write_to_log = __write_to_log_daemon;
}
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
pthread_mutex_unlock(&log_init_lock);
#endif
@@ -161,11 +384,17 @@
!strcmp(tag, "PHONE") ||
!strcmp(tag, "SMS")) {
log_id = LOG_ID_RADIO;
- // Inform third party apps/ril/radio.. to use Rlog or RLOG
+ /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
tag = tmp_tag;
}
+#if __BIONIC__
+ if (prio == ANDROID_LOG_FATAL) {
+ android_set_abort_message(msg);
+ }
+#endif
+
vec[0].iov_base = (unsigned char *) &prio;
vec[0].iov_len = 1;
vec[1].iov_base = (void *) tag;
@@ -196,7 +425,7 @@
!strcmp(tag, "PHONE") ||
!strcmp(tag, "SMS"))) {
bufID = LOG_ID_RADIO;
- // Inform third party apps/ril/radio.. to use Rlog or RLOG
+ /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
tag = tmp_tag;
}
@@ -266,8 +495,8 @@
}
__android_log_write(ANDROID_LOG_FATAL, tag, buf);
-
- __builtin_trap(); /* trap so we have a chance to debug the situation */
+ abort(); /* abort so we have a chance to debug the situation */
+ /* NOTREACHED */
}
int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
@@ -288,7 +517,7 @@
* handy if we just want to dump an integer into the log.
*/
int __android_log_btwrite(int32_t tag, char type, const void *payload,
- size_t len)
+ size_t len)
{
struct iovec vec[3];
@@ -301,3 +530,25 @@
return write_to_log(LOG_ID_EVENTS, vec, 3);
}
+
+/*
+ * Like __android_log_bwrite, but used for writing strings to the
+ * event log.
+ */
+int __android_log_bswrite(int32_t tag, const char *payload)
+{
+ struct iovec vec[4];
+ char type = EVENT_TYPE_STRING;
+ uint32_t len = strlen(payload);
+
+ vec[0].iov_base = &tag;
+ vec[0].iov_len = sizeof(tag);
+ vec[1].iov_base = &type;
+ vec[1].iov_len = sizeof(type);
+ vec[2].iov_base = &len;
+ vec[2].iov_len = sizeof(len);
+ vec[3].iov_base = (void*)payload;
+ vec[3].iov_len = len;
+
+ return write_to_log(LOG_ID_EVENTS, vec, 4);
+}
diff --git a/liblog/logd_write_kern.c b/liblog/logd_write_kern.c
new file mode 100644
index 0000000..ca63067
--- /dev/null
+++ b/liblog/logd_write_kern.c
@@ -0,0 +1,325 @@
+/*
+ * Copyright (C) 2007-2014 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 <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <android/set_abort_message.h>
+
+#include <log/log.h>
+#include <log/logd.h>
+#include <log/logger.h>
+
+#define LOGGER_LOG_MAIN "log/main"
+#define LOGGER_LOG_RADIO "log/radio"
+#define LOGGER_LOG_EVENTS "log/events"
+#define LOGGER_LOG_SYSTEM "log/system"
+
+#define LOG_BUF_SIZE 1024
+
+#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
+#define log_writev(filedes, vector, count) writev(filedes, vector, count)
+#define log_close(filedes) close(filedes)
+
+static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
+static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
+
+static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
+
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
+static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
+
+/*
+ * This is used by the C++ code to decide if it should write logs through
+ * the C code. Basically, if /dev/log/... is available, we're running in
+ * the simulator rather than a desktop tool and want to use the device.
+ */
+static enum {
+ kLogUninitialized, kLogNotAvailable, kLogAvailable
+} g_log_status = kLogUninitialized;
+int __android_log_dev_available(void)
+{
+ if (g_log_status == kLogUninitialized) {
+ if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
+ g_log_status = kLogAvailable;
+ else
+ g_log_status = kLogNotAvailable;
+ }
+
+ return (g_log_status == kLogAvailable);
+}
+
+static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
+ size_t nr __unused)
+{
+ return -1;
+}
+
+static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
+{
+ ssize_t ret;
+ int log_fd;
+
+ if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
+ if (log_id == LOG_ID_CRASH) {
+ log_id = LOG_ID_MAIN;
+ }
+ log_fd = log_fds[(int)log_id];
+ } else {
+ return -EBADF;
+ }
+
+ do {
+ ret = log_writev(log_fd, vec, nr);
+ if (ret < 0) {
+ ret = -errno;
+ }
+ } while (ret == -EINTR);
+
+ return ret;
+}
+
+static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
+{
+ pthread_mutex_lock(&log_init_lock);
+
+ if (write_to_log == __write_to_log_init) {
+ log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
+ log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
+ log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
+ log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
+
+ write_to_log = __write_to_log_kernel;
+
+ if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
+ log_fds[LOG_ID_EVENTS] < 0) {
+ log_close(log_fds[LOG_ID_MAIN]);
+ log_close(log_fds[LOG_ID_RADIO]);
+ log_close(log_fds[LOG_ID_EVENTS]);
+ log_fds[LOG_ID_MAIN] = -1;
+ log_fds[LOG_ID_RADIO] = -1;
+ log_fds[LOG_ID_EVENTS] = -1;
+ write_to_log = __write_to_log_null;
+ }
+
+ if (log_fds[LOG_ID_SYSTEM] < 0) {
+ log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
+ }
+ }
+
+ pthread_mutex_unlock(&log_init_lock);
+
+ return write_to_log(log_id, vec, nr);
+}
+
+int __android_log_write(int prio, const char *tag, const char *msg)
+{
+ struct iovec vec[3];
+ log_id_t log_id = LOG_ID_MAIN;
+ char tmp_tag[32];
+
+ if (!tag)
+ tag = "";
+
+ /* XXX: This needs to go! */
+ if (!strcmp(tag, "HTC_RIL") ||
+ !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
+ !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
+ !strcmp(tag, "AT") ||
+ !strcmp(tag, "GSM") ||
+ !strcmp(tag, "STK") ||
+ !strcmp(tag, "CDMA") ||
+ !strcmp(tag, "PHONE") ||
+ !strcmp(tag, "SMS")) {
+ log_id = LOG_ID_RADIO;
+ /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
+ snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
+ tag = tmp_tag;
+ }
+
+ if (prio == ANDROID_LOG_FATAL) {
+ android_set_abort_message(msg);
+ }
+
+ vec[0].iov_base = (unsigned char *) &prio;
+ vec[0].iov_len = 1;
+ vec[1].iov_base = (void *) tag;
+ vec[1].iov_len = strlen(tag) + 1;
+ vec[2].iov_base = (void *) msg;
+ vec[2].iov_len = strlen(msg) + 1;
+
+ return write_to_log(log_id, vec, 3);
+}
+
+int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
+{
+ struct iovec vec[3];
+ char tmp_tag[32];
+
+ if (!tag)
+ tag = "";
+
+ /* XXX: This needs to go! */
+ if ((bufID != LOG_ID_RADIO) &&
+ (!strcmp(tag, "HTC_RIL") ||
+ !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
+ !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
+ !strcmp(tag, "AT") ||
+ !strcmp(tag, "GSM") ||
+ !strcmp(tag, "STK") ||
+ !strcmp(tag, "CDMA") ||
+ !strcmp(tag, "PHONE") ||
+ !strcmp(tag, "SMS"))) {
+ bufID = LOG_ID_RADIO;
+ /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
+ snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
+ tag = tmp_tag;
+ }
+
+ vec[0].iov_base = (unsigned char *) &prio;
+ vec[0].iov_len = 1;
+ vec[1].iov_base = (void *) tag;
+ vec[1].iov_len = strlen(tag) + 1;
+ vec[2].iov_base = (void *) msg;
+ vec[2].iov_len = strlen(msg) + 1;
+
+ return write_to_log(bufID, vec, 3);
+}
+
+int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
+{
+ char buf[LOG_BUF_SIZE];
+
+ vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
+
+ return __android_log_write(prio, tag, buf);
+}
+
+int __android_log_print(int prio, const char *tag, const char *fmt, ...)
+{
+ va_list ap;
+ char buf[LOG_BUF_SIZE];
+
+ va_start(ap, fmt);
+ vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
+ va_end(ap);
+
+ return __android_log_write(prio, tag, buf);
+}
+
+int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
+{
+ va_list ap;
+ char buf[LOG_BUF_SIZE];
+
+ va_start(ap, fmt);
+ vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
+ va_end(ap);
+
+ return __android_log_buf_write(bufID, prio, tag, buf);
+}
+
+void __android_log_assert(const char *cond, const char *tag,
+ const char *fmt, ...)
+{
+ char buf[LOG_BUF_SIZE];
+
+ if (fmt) {
+ va_list ap;
+ va_start(ap, fmt);
+ vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
+ va_end(ap);
+ } else {
+ /* Msg not provided, log condition. N.B. Do not use cond directly as
+ * format string as it could contain spurious '%' syntax (e.g.
+ * "%d" in "blocks%devs == 0").
+ */
+ if (cond)
+ snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
+ else
+ strcpy(buf, "Unspecified assertion failed");
+ }
+
+ __android_log_write(ANDROID_LOG_FATAL, tag, buf);
+ abort(); /* abort so we have a chance to debug the situation */
+ /* NOTREACHED */
+}
+
+int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
+{
+ struct iovec vec[2];
+
+ vec[0].iov_base = &tag;
+ vec[0].iov_len = sizeof(tag);
+ vec[1].iov_base = (void*)payload;
+ vec[1].iov_len = len;
+
+ return write_to_log(LOG_ID_EVENTS, vec, 2);
+}
+
+/*
+ * Like __android_log_bwrite, but takes the type as well. Doesn't work
+ * for the general case where we're generating lists of stuff, but very
+ * handy if we just want to dump an integer into the log.
+ */
+int __android_log_btwrite(int32_t tag, char type, const void *payload,
+ size_t len)
+{
+ struct iovec vec[3];
+
+ vec[0].iov_base = &tag;
+ vec[0].iov_len = sizeof(tag);
+ vec[1].iov_base = &type;
+ vec[1].iov_len = sizeof(type);
+ vec[2].iov_base = (void*)payload;
+ vec[2].iov_len = len;
+
+ return write_to_log(LOG_ID_EVENTS, vec, 3);
+}
+
+/*
+ * Like __android_log_bwrite, but used for writing strings to the
+ * event log.
+ */
+int __android_log_bswrite(int32_t tag, const char *payload)
+{
+ struct iovec vec[4];
+ char type = EVENT_TYPE_STRING;
+ uint32_t len = strlen(payload);
+
+ vec[0].iov_base = &tag;
+ vec[0].iov_len = sizeof(tag);
+ vec[1].iov_base = &type;
+ vec[1].iov_len = sizeof(type);
+ vec[2].iov_base = &len;
+ vec[2].iov_len = sizeof(len);
+ vec[3].iov_base = (void*)payload;
+ vec[3].iov_len = len;
+
+ return write_to_log(LOG_ID_EVENTS, vec, 4);
+}
diff --git a/liblog/logprint.c b/liblog/logprint.c
index a7480d5..7ba4c8e 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -17,14 +17,16 @@
#define _GNU_SOURCE /* for asprintf */
-#include <ctype.h>
-#include <stdio.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <assert.h>
#include <arpa/inet.h>
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
#include <log/logd.h>
#include <log/logprint.h>
@@ -39,8 +41,23 @@
android_LogPriority global_pri;
FilterInfo *filters;
AndroidLogPrintFormat format;
+ bool colored_output;
};
+/*
+ * gnome-terminal color tags
+ * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
+ * for ideas on how to set the forground color of the text for xterm.
+ * The color manipulation character stream is defined as:
+ * ESC [ 3 8 ; 5 ; <color#> m
+ */
+#define ANDROID_COLOR_BLUE 75
+#define ANDROID_COLOR_DEFAULT 231
+#define ANDROID_COLOR_GREEN 40
+#define ANDROID_COLOR_ORANGE 166
+#define ANDROID_COLOR_RED 196
+#define ANDROID_COLOR_YELLOW 226
+
static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
{
FilterInfo *p_ret;
@@ -52,15 +69,7 @@
return p_ret;
}
-static void filterinfo_free(FilterInfo *p_info)
-{
- if (p_info == NULL) {
- return;
- }
-
- free(p_info->mTag);
- p_info->mTag = NULL;
-}
+/* balance to above, filterinfo_free left unimplemented */
/*
* Note: also accepts 0-9 priorities
@@ -118,6 +127,23 @@
}
}
+static int colorFromPri (android_LogPriority pri)
+{
+ switch (pri) {
+ case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
+ case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
+ case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
+ case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
+ case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
+ case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
+ case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
+
+ case ANDROID_LOG_DEFAULT:
+ case ANDROID_LOG_UNKNOWN:
+ default: return ANDROID_COLOR_DEFAULT;
+ }
+}
+
static android_LogPriority filterPriForTag(
AndroidLogFormat *p_format, const char *tag)
{
@@ -139,23 +165,6 @@
return p_format->global_pri;
}
-/** for debugging */
-static void dumpFilters(AndroidLogFormat *p_format)
-{
- FilterInfo *p_fi;
-
- for (p_fi = p_format->filters ; p_fi != NULL ; p_fi = p_fi->p_next) {
- char cPri = filterPriToChar(p_fi->mPri);
- if (p_fi->mPri == ANDROID_LOG_DEFAULT) {
- cPri = filterPriToChar(p_format->global_pri);
- }
- fprintf(stderr,"%s:%c\n", p_fi->mTag, cPri);
- }
-
- fprintf(stderr,"*:%c\n", filterPriToChar(p_format->global_pri));
-
-}
-
/**
* returns 1 if this log line should be printed based on its priority
* and tag, and 0 if it should not
@@ -174,6 +183,7 @@
p_ret->global_pri = ANDROID_LOG_VERBOSE;
p_ret->format = FORMAT_BRIEF;
+ p_ret->colored_output = false;
return p_ret;
}
@@ -199,7 +209,10 @@
void android_log_setPrintFormat(AndroidLogFormat *p_format,
AndroidLogPrintFormat format)
{
- p_format->format=format;
+ if (format == FORMAT_COLOR)
+ p_format->colored_output = true;
+ else
+ p_format->format = format;
}
/**
@@ -217,6 +230,7 @@
else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
+ else if (strcmp(formatString, "color") == 0) format = FORMAT_COLOR;
else format = FORMAT_OFF;
return format;
@@ -234,7 +248,6 @@
int android_log_addFilterRule(AndroidLogFormat *p_format,
const char *filterExpression)
{
- size_t i=0;
size_t tagNameLength;
android_LogPriority pri = ANDROID_LOG_DEFAULT;
@@ -331,15 +344,6 @@
return -1;
}
-static inline char * strip_end(char *str)
-{
- char *end = str + strlen(str) - 1;
-
- while (end >= str && isspace(*end))
- *end-- = '\0';
- return str;
-}
-
/**
* Splits a wire-format buffer into an AndroidLogEntry
* entry allocated by caller. Pointers will point directly into buf
@@ -713,18 +717,19 @@
const AndroidLogEntry *entry,
size_t *p_outLength)
{
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
struct tm tmBuf;
#endif
struct tm* ptm;
char timeBuf[32];
- char headerBuf[128];
char prefixBuf[128], suffixBuf[128];
char priChar;
int prefixSuffixIsHeaderFooter = 0;
char * ret = NULL;
priChar = filterPriToChar(entry->priority);
+ size_t prefixLen = 0, suffixLen = 0;
+ size_t len;
/*
* Get the current date/time in pretty form
@@ -735,7 +740,7 @@
* in the time stamp. Don't use forward slashes, parenthesis,
* brackets, asterisks, or other special chars here.
*/
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
ptm = localtime_r(&(entry->tv_sec), &tmBuf);
#else
ptm = localtime(&(entry->tv_sec));
@@ -746,78 +751,84 @@
/*
* Construct a buffer containing the log header and log message.
*/
- size_t prefixLen, suffixLen;
+ if (p_format->colored_output) {
+ prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
+ colorFromPri(entry->priority));
+ prefixLen = MIN(prefixLen, sizeof(prefixBuf));
+ suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
+ suffixLen = MIN(suffixLen, sizeof(suffixBuf));
+ }
switch (p_format->format) {
case FORMAT_TAG:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%c/%-8s: ", priChar, entry->tag);
- strcpy(suffixBuf, "\n"); suffixLen = 1;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
case FORMAT_PROCESS:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
- "%c(%5d) ", priChar, entry->pid);
- suffixLen = snprintf(suffixBuf, sizeof(suffixBuf),
+ len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
" (%s)\n", entry->tag);
+ suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
+ "%c(%5d) ", priChar, entry->pid);
break;
case FORMAT_THREAD:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%c(%5d:%5d) ", priChar, entry->pid, entry->tid);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
case FORMAT_RAW:
- prefixBuf[0] = 0;
- prefixLen = 0;
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
+ prefixBuf[prefixLen] = 0;
+ len = 0;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
case FORMAT_TIME:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%s.%03ld %c/%-8s(%5d): ", timeBuf, entry->tv_nsec / 1000000,
priChar, entry->tag, entry->pid);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
case FORMAT_THREADTIME:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%s.%03ld %5d %5d %c %-8s: ", timeBuf, entry->tv_nsec / 1000000,
entry->pid, entry->tid, priChar, entry->tag);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
case FORMAT_LONG:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"[ %s.%03ld %5d:%5d %c/%-8s ]\n",
timeBuf, entry->tv_nsec / 1000000, entry->pid,
entry->tid, priChar, entry->tag);
- strcpy(suffixBuf, "\n\n");
- suffixLen = 2;
+ strcpy(suffixBuf + suffixLen, "\n\n");
+ suffixLen += 2;
prefixSuffixIsHeaderFooter = 1;
break;
case FORMAT_BRIEF:
default:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+ len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%c/%-8s(%5d): ", priChar, entry->tag, entry->pid);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
+ strcpy(suffixBuf + suffixLen, "\n");
+ ++suffixLen;
break;
}
+
/* snprintf has a weird return value. It returns what would have been
* written given a large enough buffer. In the case that the prefix is
* longer then our buffer(128), it messes up the calculations below
* possibly causing heap corruption. To avoid this we double check and
* set the length at the maximum (size minus null byte)
*/
- if(prefixLen >= sizeof(prefixBuf))
- prefixLen = sizeof(prefixBuf) - 1;
- if(suffixLen >= sizeof(suffixBuf))
- suffixLen = sizeof(suffixBuf) - 1;
+ prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen);
+ suffixLen = MIN(suffixLen, sizeof(suffixBuf));
/* the following code is tragically unreadable */
size_t numLines;
- size_t i;
char *p;
size_t bufferSize;
const char *pm;
@@ -938,88 +949,3 @@
return ret;
}
-
-
-
-void logprint_run_tests()
-{
-#if 0
-
- fprintf(stderr, "tests disabled\n");
-
-#else
-
- int err;
- const char *tag;
- AndroidLogFormat *p_format;
-
- p_format = android_log_format_new();
-
- fprintf(stderr, "running tests\n");
-
- tag = "random";
-
- android_log_addFilterRule(p_format,"*:i");
-
- assert (ANDROID_LOG_INFO == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
- android_log_addFilterRule(p_format, "*");
- assert (ANDROID_LOG_DEBUG == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
- android_log_addFilterRule(p_format, "*:v");
- assert (ANDROID_LOG_VERBOSE == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
- android_log_addFilterRule(p_format, "*:i");
- assert (ANDROID_LOG_INFO == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
-
- android_log_addFilterRule(p_format, "random");
- assert (ANDROID_LOG_VERBOSE == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
- android_log_addFilterRule(p_format, "random:v");
- assert (ANDROID_LOG_VERBOSE == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
- android_log_addFilterRule(p_format, "random:d");
- assert (ANDROID_LOG_DEBUG == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
- android_log_addFilterRule(p_format, "random:w");
- assert (ANDROID_LOG_WARN == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
-
- android_log_addFilterRule(p_format, "crap:*");
- assert (ANDROID_LOG_VERBOSE== filterPriForTag(p_format, "crap"));
- assert(android_log_shouldPrintLine(p_format, "crap", ANDROID_LOG_VERBOSE) > 0);
-
- // invalid expression
- err = android_log_addFilterRule(p_format, "random:z");
- assert (err < 0);
- assert (ANDROID_LOG_WARN == filterPriForTag(p_format, "random"));
- assert(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
-
- // Issue #550946
- err = android_log_addFilterString(p_format, " ");
- assert(err == 0);
- assert(ANDROID_LOG_WARN == filterPriForTag(p_format, "random"));
-
- // note trailing space
- err = android_log_addFilterString(p_format, "*:s random:d ");
- assert(err == 0);
- assert(ANDROID_LOG_DEBUG == filterPriForTag(p_format, "random"));
-
- err = android_log_addFilterString(p_format, "*:s random:z");
- assert(err < 0);
-
-
-#if 0
- char *ret;
- char defaultBuffer[512];
-
- ret = android_log_formatLogLine(p_format,
- defaultBuffer, sizeof(defaultBuffer), 0, ANDROID_LOG_ERROR, 123,
- 123, 123, "random", "nofile", strlen("Hello"), "Hello", NULL);
-#endif
-
-
- fprintf(stderr, "tests complete\n");
-#endif
-}
diff --git a/liblog/tests/Android.mk b/liblog/tests/Android.mk
index db06cf7..8137a75 100644
--- a/liblog/tests/Android.mk
+++ b/liblog/tests/Android.mk
@@ -32,7 +32,7 @@
benchmark_src_files := \
benchmark_main.cpp \
- liblog_benchmark.cpp \
+ liblog_benchmark.cpp
# Build benchmarks for the device. Run with:
# adb shell liblog-benchmarks
@@ -43,12 +43,7 @@
LOCAL_CFLAGS += $(benchmark_c_flags)
LOCAL_SHARED_LIBRARIES += liblog libm
LOCAL_SRC_FILES := $(benchmark_src_files)
-ifndef LOCAL_SDK_VERSION
-LOCAL_C_INCLUDES += bionic bionic/libstdc++/include external/stlport/stlport
-LOCAL_SHARED_LIBRARIES += libstlport
-endif
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_NATIVE_TESTS)/$(LOCAL_MODULE)
-include $(BUILD_EXECUTABLE)
+include $(BUILD_NATIVE_TEST)
# -----------------------------------------------------------------------------
# Unit tests.
@@ -60,9 +55,22 @@
-Wall -Wextra \
-Werror \
-fno-builtin \
+ -std=gnu++11
test_src_files := \
- liblog_test.cpp \
+ liblog_test.cpp
+
+# to prevent breaking the build if bionic not relatively visible to us
+ifneq ($(wildcard $(LOCAL_PATH)/../../../../bionic/libc/bionic/libc_logging.cpp),)
+
+test_src_files += \
+ libc_test.cpp
+
+ifneq ($(TARGET_USES_LOGD),false)
+test_c_flags += -DTARGET_USES_LOGD
+endif
+
+endif
# Build tests for the device (with .so). Run with:
# adb shell /data/nativetest/liblog-unit-tests/liblog-unit-tests
@@ -71,7 +79,6 @@
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CFLAGS += $(test_c_flags)
-LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)
diff --git a/liblog/tests/benchmark_main.cpp b/liblog/tests/benchmark_main.cpp
index 090394c..e5ef970 100644
--- a/liblog/tests/benchmark_main.cpp
+++ b/liblog/tests/benchmark_main.cpp
@@ -17,6 +17,7 @@
#include <benchmark.h>
#include <inttypes.h>
+#include <math.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/liblog/tests/libc_test.cpp b/liblog/tests/libc_test.cpp
new file mode 100644
index 0000000..29501be
--- /dev/null
+++ b/liblog/tests/libc_test.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2014 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 <fcntl.h>
+#include <sys/cdefs.h>
+
+#include <gtest/gtest.h>
+
+// Should be in bionic test suite, *but* we are using liblog to confirm
+// end-to-end logging, so let the overly cute oedipus complex begin ...
+#include "../../../../bionic/libc/bionic/libc_logging.cpp" // not Standalone
+#define _ANDROID_LOG_H // Priorities redefined
+#define _LIBS_LOG_LOG_H // log ids redefined
+typedef unsigned char log_id_t; // log_id_t missing as a result
+#ifdef TARGET_USES_LOGD
+#define _LIBS_LOG_LOG_READ_H // log_time redefined
+#endif
+
+#include <log/log.h>
+#include <log/logger.h>
+#include <log/log_read.h>
+
+TEST(libc, __libc_android_log_event_int) {
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ int value = ts.tv_nsec;
+
+ __libc_android_log_event_int(0, value);
+ usleep(1000000);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ ASSERT_EQ(log_msg.entry.pid, pid);
+
+ if ((log_msg.entry.len != (4 + 1 + 4))
+ || ((int)log_msg.id() != LOG_ID_EVENTS)) {
+ continue;
+ }
+
+ char *eventData = log_msg.msg();
+
+ int incoming = (eventData[0] & 0xFF) |
+ ((eventData[1] & 0xFF) << 8) |
+ ((eventData[2] & 0xFF) << 16) |
+ ((eventData[3] & 0xFF) << 24);
+
+ if (incoming != 0) {
+ continue;
+ }
+
+ if (eventData[4] != EVENT_TYPE_INT) {
+ continue;
+ }
+
+ incoming = (eventData[4 + 1 + 0] & 0xFF) |
+ ((eventData[4 + 1 + 1] & 0xFF) << 8) |
+ ((eventData[4 + 1 + 2] & 0xFF) << 16) |
+ ((eventData[4 + 1 + 3] & 0xFF) << 24);
+
+ if (incoming == value) {
+ ++count;
+ }
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
+
+TEST(libc, __libc_fatal_no_abort) {
+ struct logger_list *logger_list;
+
+ pid_t pid = getpid();
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ (log_id_t)LOG_ID_CRASH, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
+
+ char b[80];
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+
+ __libc_fatal_no_abort("%u.%09u", (unsigned)ts.tv_sec, (unsigned)ts.tv_nsec);
+ snprintf(b, sizeof(b),"%u.%09u", (unsigned)ts.tv_sec, (unsigned)ts.tv_nsec);
+ usleep(1000000);
+
+ int count = 0;
+
+ for (;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ ASSERT_EQ(log_msg.entry.pid, pid);
+
+ if ((int)log_msg.id() != LOG_ID_CRASH) {
+ continue;
+ }
+
+ char *data = log_msg.msg();
+
+ if ((*data == ANDROID_LOG_FATAL)
+ && !strcmp(data + 1, "libc")
+ && !strcmp(data + 1 + strlen(data + 1) + 1, b)) {
+ ++count;
+ }
+ }
+
+ EXPECT_EQ(1, count);
+
+ android_logger_list_close(logger_list);
+}
diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp
index 19406fb..979aded 100644
--- a/liblog/tests/liblog_benchmark.cpp
+++ b/liblog/tests/liblog_benchmark.cpp
@@ -99,7 +99,7 @@
}
BENCHMARK(BM_log_overhead);
-static void caught_latency(int signum)
+static void caught_latency(int /*signum*/)
{
unsigned long long v = 0xDEADBEEFA55A5AA5ULL;
@@ -130,7 +130,7 @@
pid_t pid = getpid();
struct logger_list * logger_list = android_logger_list_open(LOG_ID_EVENTS,
- O_RDONLY, 0, pid);
+ ANDROID_LOG_RDONLY, 0, pid);
if (!logger_list) {
fprintf(stderr, "Unable to open events log: %s\n", strerror(errno));
@@ -143,7 +143,7 @@
for (int j = 0, i = 0; i < iters && j < 10*iters; ++i, ++j) {
log_time ts;
LOG_FAILURE_RETRY((
- clock_gettime(CLOCK_REALTIME, &ts),
+ ts = log_time(CLOCK_REALTIME),
android_btWriteLog(0, EVENT_TYPE_LONG, &ts, sizeof(ts))));
for (;;) {
@@ -193,7 +193,7 @@
}
BENCHMARK(BM_log_latency);
-static void caught_delay(int signum)
+static void caught_delay(int /*signum*/)
{
unsigned long long v = 0xDEADBEEFA55A5AA6ULL;
@@ -208,7 +208,7 @@
pid_t pid = getpid();
struct logger_list * logger_list = android_logger_list_open(LOG_ID_EVENTS,
- O_RDONLY, 0, pid);
+ ANDROID_LOG_RDONLY, 0, pid);
if (!logger_list) {
fprintf(stderr, "Unable to open events log: %s\n", strerror(errno));
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index d71d97a..33f6481 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -21,6 +21,7 @@
#include <log/log.h>
#include <log/logger.h>
#include <log/log_read.h>
+#include <log/logprint.h>
// enhanced version of LOG_FAILURE_RETRY to add support for EAGAIN and
// non-syscall libs. Since we are only using this in the emergency of
@@ -38,30 +39,30 @@
_rc; })
TEST(liblog, __android_log_buf_print) {
- ASSERT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO,
"TEST__android_log_buf_print",
"radio"));
usleep(1000);
- ASSERT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
"TEST__android_log_buf_print",
"system"));
usleep(1000);
- ASSERT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO,
"TEST__android_log_buf_print",
"main"));
usleep(1000);
}
TEST(liblog, __android_log_buf_write) {
- ASSERT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO,
"TEST__android_log_buf_write",
"radio"));
usleep(1000);
- ASSERT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
"TEST__android_log_buf_write",
"system"));
usleep(1000);
- ASSERT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO,
+ EXPECT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO,
"TEST__android_log_buf_write",
"main"));
usleep(1000);
@@ -69,16 +70,16 @@
TEST(liblog, __android_log_btwrite) {
int intBuf = 0xDEADBEEF;
- ASSERT_LT(0, __android_log_btwrite(0,
+ EXPECT_LT(0, __android_log_btwrite(0,
EVENT_TYPE_INT,
&intBuf, sizeof(intBuf)));
long long longBuf = 0xDEADBEEFA55A5AA5;
- ASSERT_LT(0, __android_log_btwrite(0,
+ EXPECT_LT(0, __android_log_btwrite(0,
EVENT_TYPE_LONG,
&longBuf, sizeof(longBuf)));
usleep(1000);
char Buf[] = "\20\0\0\0DeAdBeEfA55a5aA5";
- ASSERT_LT(0, __android_log_btwrite(0,
+ EXPECT_LT(0, __android_log_btwrite(0,
EVENT_TYPE_STRING,
Buf, sizeof(Buf) - 1));
usleep(1000);
@@ -120,8 +121,8 @@
pid_t pid = getpid();
- ASSERT_EQ(0, NULL == (logger_list = android_logger_list_open(
- LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid)));
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
log_time ts(CLOCK_MONOTONIC);
@@ -155,7 +156,7 @@
}
}
- ASSERT_EQ(1, count);
+ EXPECT_EQ(1, count);
android_logger_list_close(logger_list);
}
@@ -163,7 +164,7 @@
static unsigned signaled;
log_time signal_time;
-static void caught_blocking(int signum)
+static void caught_blocking(int /*signum*/)
{
unsigned long long v = 0xDEADBEEFA55A0000ULL;
@@ -171,7 +172,7 @@
++signaled;
if ((signal_time.tv_sec == 0) && (signal_time.tv_nsec == 0)) {
- clock_gettime(CLOCK_MONOTONIC, &signal_time);
+ signal_time = log_time(CLOCK_MONOTONIC);
signal_time.tv_sec += 2;
}
@@ -221,8 +222,8 @@
v += pid & 0xFFFF;
- ASSERT_EQ(0, NULL == (logger_list = android_logger_list_open(
- LOG_ID_EVENTS, O_RDONLY, 1000, pid)));
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY, 1000, pid)));
int count = 0;
@@ -277,13 +278,13 @@
++signals;
break;
}
- } while (!signaled || ({log_time t(CLOCK_MONOTONIC); t < signal_time;}));
+ } while (!signaled || (log_time(CLOCK_MONOTONIC) < signal_time));
alarm(0);
signal(SIGALRM, SIG_DFL);
- ASSERT_LT(1, count);
+ EXPECT_LT(1, count);
- ASSERT_EQ(1, signals);
+ EXPECT_EQ(1, signals);
android_logger_list_close(logger_list);
@@ -295,13 +296,306 @@
const unsigned long long one_percent_ticks = alarm_time;
unsigned long long user_ticks = uticks_end - uticks_start;
unsigned long long system_ticks = sticks_end - sticks_start;
- ASSERT_GT(one_percent_ticks, user_ticks);
- ASSERT_GT(one_percent_ticks, system_ticks);
- ASSERT_GT(one_percent_ticks, user_ticks + system_ticks);
+ EXPECT_GT(one_percent_ticks, user_ticks);
+ EXPECT_GT(one_percent_ticks, system_ticks);
+ EXPECT_GT(one_percent_ticks, user_ticks + system_ticks);
+}
+
+static const char max_payload_tag[] = "TEST_max_payload_XXXX";
+static const char max_payload_buf[LOGGER_ENTRY_MAX_PAYLOAD
+ - sizeof(max_payload_tag) - 1] = "LEONATO\n\
+I learn in this letter that Don Peter of Arragon\n\
+comes this night to Messina\n\
+MESSENGER\n\
+He is very near by this: he was not three leagues off\n\
+when I left him\n\
+LEONATO\n\
+How many gentlemen have you lost in this action?\n\
+MESSENGER\n\
+But few of any sort, and none of name\n\
+LEONATO\n\
+A victory is twice itself when the achiever brings\n\
+home full numbers. I find here that Don Peter hath\n\
+bestowed much honour on a young Florentine called Claudio\n\
+MESSENGER\n\
+Much deserved on his part and equally remembered by\n\
+Don Pedro: he hath borne himself beyond the\n\
+promise of his age, doing, in the figure of a lamb,\n\
+the feats of a lion: he hath indeed better\n\
+bettered expectation than you must expect of me to\n\
+tell you how\n\
+LEONATO\n\
+He hath an uncle here in Messina will be very much\n\
+glad of it.\n\
+MESSENGER\n\
+I have already delivered him letters, and there\n\
+appears much joy in him; even so much that joy could\n\
+not show itself modest enough without a badge of\n\
+bitterness.\n\
+LEONATO\n\
+Did he break out into tears?\n\
+MESSENGER\n\
+In great measure.\n\
+LEONATO\n\
+A kind overflow of kindness: there are no faces\n\
+truer than those that are so washed. How much\n\
+better is it to weep at joy than to joy at weeping!\n\
+BEATRICE\n\
+I pray you, is Signior Mountanto returned from the\n\
+wars or no?\n\
+MESSENGER\n\
+I know none of that name, lady: there was none such\n\
+in the army of any sort.\n\
+LEONATO\n\
+What is he that you ask for, niece?\n\
+HERO\n\
+My cousin means Signior Benedick of Padua.\n\
+MESSENGER\n\
+O, he's returned; and as pleasant as ever he was.\n\
+BEATRICE\n\
+He set up his bills here in Messina and challenged\n\
+Cupid at the flight; and my uncle's fool, reading\n\
+the challenge, subscribed for Cupid, and challenged\n\
+him at the bird-bolt. I pray you, how many hath he\n\
+killed and eaten in these wars? But how many hath\n\
+he killed? for indeed I promised to eat all of his killing.\n\
+LEONATO\n\
+Faith, niece, you tax Signior Benedick too much;\n\
+but he'll be meet with you, I doubt it not.\n\
+MESSENGER\n\
+He hath done good service, lady, in these wars.\n\
+BEATRICE\n\
+You had musty victual, and he hath holp to eat it:\n\
+he is a very valiant trencherman; he hath an\n\
+excellent stomach.\n\
+MESSENGER\n\
+And a good soldier too, lady.\n\
+BEATRICE\n\
+And a good soldier to a lady: but what is he to a lord?\n\
+MESSENGER\n\
+A lord to a lord, a man to a man; stuffed with all\n\
+honourable virtues.\n\
+BEATRICE\n\
+It is so, indeed; he is no less than a stuffed man:\n\
+but for the stuffing,--well, we are all mortal.\n\
+LEONATO\n\
+You must not, sir, mistake my niece. There is a\n\
+kind of merry war betwixt Signior Benedick and her:\n\
+they never meet but there's a skirmish of wit\n\
+between them.\n\
+BEATRICE\n\
+Alas! he gets nothing by that. In our last\n\
+conflict four of his five wits went halting off, and\n\
+now is the whole man governed with one: so that if\n\
+he have wit enough to keep himself warm, let him\n\
+bear it for a difference between himself and his\n\
+horse; for it is all the wealth that he hath left,\n\
+to be known a reasonable creature. Who is his\n\
+companion now? He hath every month a new sworn brother.\n\
+MESSENGER\n\
+Is't possible?\n\
+BEATRICE\n\
+Very easily possible: he wears his faith but as\n\
+the fashion of his hat; it ever changes with the\n\
+next block.\n\
+MESSENGER\n\
+I see, lady, the gentleman is not in your books.\n\
+BEATRICE\n\
+No; an he were, I would burn my study. But, I pray\n\
+you, who is his companion? Is there no young\n\
+squarer now that will make a voyage with him to the devil?\n\
+MESSENGER\n\
+He is most in the company of the right noble Claudio.\n\
+BEATRICE\n\
+O Lord, he will hang upon him like a disease: he\n\
+is sooner caught than the pestilence, and the taker\n\
+runs presently mad. God help the noble Claudio! if\n\
+he have caught the Benedick, it will cost him a\n\
+thousand pound ere a' be cured.\n\
+MESSENGER\n\
+I will hold friends with you, lady.\n\
+BEATRICE\n\
+Do, good friend.\n\
+LEONATO\n\
+You will never run mad, niece.\n\
+BEATRICE\n\
+No, not till a hot January.\n\
+MESSENGER\n\
+Don Pedro is approached.\n\
+Enter DON PEDRO, DON JOHN, CLAUDIO, BENEDICK, and BALTHASAR\n\
+\n\
+DON PEDRO\n\
+Good Signior Leonato, you are come to meet your\n\
+trouble: the fashion of the world is to avoid\n\
+cost, and you encounter it\n\
+LEONATO\n\
+Never came trouble to my house in the likeness";
+
+TEST(liblog, max_payload) {
+ pid_t pid = getpid();
+ char tag[sizeof(max_payload_tag)];
+ memcpy(tag, max_payload_tag, sizeof(tag));
+ snprintf(tag + sizeof(tag) - 5, 5, "%04X", pid & 0xFFFF);
+
+ LOG_FAILURE_RETRY(__android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
+ tag, max_payload_buf));
+
+ struct logger_list *logger_list;
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_SYSTEM, ANDROID_LOG_RDONLY, 100, 0)));
+
+ bool matches = false;
+ ssize_t max_len = 0;
+
+ for(;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ if ((log_msg.entry.pid != pid) || (log_msg.id() != LOG_ID_SYSTEM)) {
+ continue;
+ }
+
+ char *data = log_msg.msg() + 1;
+
+ if (strcmp(data, tag)) {
+ continue;
+ }
+
+ data += strlen(data) + 1;
+
+ const char *left = data;
+ const char *right = max_payload_buf;
+ while (*left && *right && (*left == *right)) {
+ ++left;
+ ++right;
+ }
+
+ if (max_len <= (left - data)) {
+ max_len = left - data + 1;
+ }
+
+ if (max_len > 512) {
+ matches = true;
+ break;
+ }
+ }
+
+ android_logger_list_close(logger_list);
+
+ EXPECT_EQ(true, matches);
+
+ EXPECT_LE(sizeof(max_payload_buf), static_cast<size_t>(max_len));
+}
+
+TEST(liblog, too_big_payload) {
+ pid_t pid = getpid();
+ static const char big_payload_tag[] = "TEST_big_payload_XXXX";
+ char tag[sizeof(big_payload_tag)];
+ memcpy(tag, big_payload_tag, sizeof(tag));
+ snprintf(tag + sizeof(tag) - 5, 5, "%04X", pid & 0xFFFF);
+
+ std::string longString(3266519, 'x');
+
+ ssize_t ret = LOG_FAILURE_RETRY(__android_log_buf_write(LOG_ID_SYSTEM,
+ ANDROID_LOG_INFO, tag, longString.c_str()));
+
+ struct logger_list *logger_list;
+
+ ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
+ LOG_ID_SYSTEM, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 100, 0)));
+
+ ssize_t max_len = 0;
+
+ for(;;) {
+ log_msg log_msg;
+ if (android_logger_list_read(logger_list, &log_msg) <= 0) {
+ break;
+ }
+
+ if ((log_msg.entry.pid != pid) || (log_msg.id() != LOG_ID_SYSTEM)) {
+ continue;
+ }
+
+ char *data = log_msg.msg() + 1;
+
+ if (strcmp(data, tag)) {
+ continue;
+ }
+
+ data += strlen(data) + 1;
+
+ const char *left = data;
+ const char *right = longString.c_str();
+ while (*left && *right && (*left == *right)) {
+ ++left;
+ ++right;
+ }
+
+ if (max_len <= (left - data)) {
+ max_len = left - data + 1;
+ }
+ }
+
+ android_logger_list_close(logger_list);
+
+ EXPECT_LE(LOGGER_ENTRY_MAX_PAYLOAD - sizeof(big_payload_tag),
+ static_cast<size_t>(max_len));
+
+ EXPECT_EQ(ret, max_len + static_cast<ssize_t>(sizeof(big_payload_tag)));
+}
+
+TEST(liblog, dual_reader) {
+ struct logger_list *logger_list1;
+
+ // >25 messages due to liblog.__android_log_buf_print__concurrentXX above.
+ ASSERT_TRUE(NULL != (logger_list1 = android_logger_list_open(
+ LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 25, 0)));
+
+ struct logger_list *logger_list2;
+
+ if (NULL == (logger_list2 = android_logger_list_open(
+ LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 15, 0))) {
+ android_logger_list_close(logger_list1);
+ ASSERT_TRUE(NULL != logger_list2);
+ }
+
+ int count1 = 0;
+ bool done1 = false;
+ int count2 = 0;
+ bool done2 = false;
+
+ do {
+ log_msg log_msg;
+
+ if (!done1) {
+ if (android_logger_list_read(logger_list1, &log_msg) <= 0) {
+ done1 = true;
+ } else {
+ ++count1;
+ }
+ }
+
+ if (!done2) {
+ if (android_logger_list_read(logger_list2, &log_msg) <= 0) {
+ done2 = true;
+ } else {
+ ++count2;
+ }
+ }
+ } while ((!done1) || (!done2));
+
+ android_logger_list_close(logger_list1);
+ android_logger_list_close(logger_list2);
+
+ EXPECT_EQ(25, count1);
+ EXPECT_EQ(15, count2);
}
TEST(liblog, android_logger_get_) {
- struct logger_list * logger_list = android_logger_list_alloc(O_WRONLY, 0, 0);
+ struct logger_list * logger_list = android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0);
for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
log_id_t id = static_cast<log_id_t>(i);
@@ -310,12 +604,81 @@
continue;
}
struct logger * logger;
- ASSERT_EQ(0, NULL == (logger = android_logger_open(logger_list, id)));
- ASSERT_EQ(id, android_logger_get_id(logger));
- ASSERT_LT(0, android_logger_get_log_size(logger));
- ASSERT_LT(0, android_logger_get_log_readable_size(logger));
- ASSERT_LT(0, android_logger_get_log_version(logger));
+ EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
+ EXPECT_EQ(id, android_logger_get_id(logger));
+ EXPECT_LT(0, android_logger_get_log_size(logger));
+ EXPECT_LT(0, android_logger_get_log_readable_size(logger));
+ EXPECT_LT(0, android_logger_get_log_version(logger));
}
android_logger_list_close(logger_list);
}
+
+static bool checkPriForTag(AndroidLogFormat *p_format, const char *tag, android_LogPriority pri) {
+ return android_log_shouldPrintLine(p_format, tag, pri)
+ && !android_log_shouldPrintLine(p_format, tag, (android_LogPriority)(pri - 1));
+}
+
+TEST(liblog, filterRule) {
+ static const char tag[] = "random";
+
+ AndroidLogFormat *p_format = android_log_format_new();
+
+ android_log_addFilterRule(p_format,"*:i");
+
+ EXPECT_TRUE(checkPriForTag(p_format, tag, ANDROID_LOG_INFO));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
+ android_log_addFilterRule(p_format, "*");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_DEBUG));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
+ android_log_addFilterRule(p_format, "*:v");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_VERBOSE));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
+ android_log_addFilterRule(p_format, "*:i");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_INFO));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
+
+ android_log_addFilterRule(p_format, tag);
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_VERBOSE));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
+ android_log_addFilterRule(p_format, "random:v");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_VERBOSE));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
+ android_log_addFilterRule(p_format, "random:d");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_DEBUG));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) > 0);
+ android_log_addFilterRule(p_format, "random:w");
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_WARN));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
+
+ android_log_addFilterRule(p_format, "crap:*");
+ EXPECT_TRUE (checkPriForTag(p_format, "crap", ANDROID_LOG_VERBOSE));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, "crap", ANDROID_LOG_VERBOSE) > 0);
+
+ // invalid expression
+ EXPECT_TRUE (android_log_addFilterRule(p_format, "random:z") < 0);
+ EXPECT_TRUE (checkPriForTag(p_format, tag, ANDROID_LOG_WARN));
+ EXPECT_TRUE(android_log_shouldPrintLine(p_format, tag, ANDROID_LOG_DEBUG) == 0);
+
+ // Issue #550946
+ EXPECT_TRUE(android_log_addFilterString(p_format, " ") == 0);
+ EXPECT_TRUE(checkPriForTag(p_format, tag, ANDROID_LOG_WARN));
+
+ // note trailing space
+ EXPECT_TRUE(android_log_addFilterString(p_format, "*:s random:d ") == 0);
+ EXPECT_TRUE(checkPriForTag(p_format, tag, ANDROID_LOG_DEBUG));
+
+ EXPECT_TRUE(android_log_addFilterString(p_format, "*:s random:z") < 0);
+
+#if 0 // bitrot, seek update
+ char defaultBuffer[512];
+
+ android_log_formatLogLine(p_format,
+ defaultBuffer, sizeof(defaultBuffer), 0, ANDROID_LOG_ERROR, 123,
+ 123, 123, tag, "nofile", strlen("Hello"), "Hello", NULL);
+
+ fprintf(stderr, "%s\n", defaultBuffer);
+#endif
+
+ android_log_format_free(p_format);
+}
diff --git a/liblog/uio.c b/liblog/uio.c
index 24a6507..f77cc49 100644
--- a/liblog/uio.c
+++ b/liblog/uio.c
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#ifndef HAVE_SYS_UIO_H
+#if defined(_WIN32)
#include <log/uio.h>
#include <unistd.h>
@@ -73,4 +73,4 @@
return total;
}
-#endif /* !HAVE_SYS_UIO_H */
+#endif
diff --git a/libmemtrack/memtrack.c b/libmemtrack/memtrack.c
index 9a656df..21d9ebd 100644
--- a/libmemtrack/memtrack.c
+++ b/libmemtrack/memtrack.c
@@ -20,6 +20,10 @@
#include <log/log.h>
+#include <errno.h>
+#include <malloc.h>
+#include <string.h>
+
#include <hardware/memtrack.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
diff --git a/libmincrypt/Android.mk b/libmincrypt/Android.mk
index 7906986..503bcb4 100644
--- a/libmincrypt/Android.mk
+++ b/libmincrypt/Android.mk
@@ -6,6 +6,8 @@
LOCAL_MODULE := libmincrypt
LOCAL_SRC_FILES := dsa_sig.c p256.c p256_ec.c p256_ecdsa.c rsa.c sha.c sha256.c
LOCAL_CFLAGS := -Wall -Werror
+# Clang's slp-vectorize phase has segmentation fault when compiling p256_ec.c.
+LOCAL_CLANG_CFLAGS += -fno-slp-vectorize
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
diff --git a/libmincrypt/dsa_sig.c b/libmincrypt/dsa_sig.c
index 8df6cf7..101314b 100644
--- a/libmincrypt/dsa_sig.c
+++ b/libmincrypt/dsa_sig.c
@@ -26,6 +26,7 @@
#include <string.h>
+#include "mincrypt/dsa_sig.h"
#include "mincrypt/p256.h"
/**
diff --git a/libmincrypt/p256.c b/libmincrypt/p256.c
index 1608d37..555a07a 100644
--- a/libmincrypt/p256.c
+++ b/libmincrypt/p256.c
@@ -49,8 +49,6 @@
{{0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8}};
-static const p256_int p256_one = P256_ONE;
-
void p256_init(p256_int* a) {
memset(a, 0, sizeof(*a));
}
diff --git a/libmincrypt/test/ecdsa_test.c b/libmincrypt/test/ecdsa_test.c
index b5a7b3a..24ec013 100644
--- a/libmincrypt/test/ecdsa_test.c
+++ b/libmincrypt/test/ecdsa_test.c
@@ -24,15 +24,21 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <stdio.h>
#include <ctype.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/cdefs.h>
+#include "mincrypt/dsa_sig.h"
#include "mincrypt/p256.h"
#include "mincrypt/p256_ecdsa.h"
#include "mincrypt/sha256.h"
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
/**
* Messages signed using:
*
@@ -209,7 +215,7 @@
return result;
}
-int main(int arg, char** argv) {
+int main(int arg __unused, char** argv __unused) {
unsigned char hash_buf[SHA256_DIGEST_SIZE];
diff --git a/libmincrypt/test/rsa_test.c b/libmincrypt/test/rsa_test.c
index 17862dc..055138f 100644
--- a/libmincrypt/test/rsa_test.c
+++ b/libmincrypt/test/rsa_test.c
@@ -1,5 +1,4 @@
-/* rsa_test.c
-**
+/*
** Copyright 2013, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
@@ -25,14 +24,19 @@
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <stdio.h>
#include <ctype.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/cdefs.h>
#include "mincrypt/rsa.h"
#include "mincrypt/sha.h"
+#ifndef __unused
+#define __unused __attribute__((unused))
+#endif
+
// RSA test data taken from:
//
// ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
@@ -791,7 +795,7 @@
}
-int main(int arg, char** argv) {
+int main(int arg __unused, char** argv __unused) {
unsigned char hash[SHA_DIGEST_SIZE];
diff --git a/libnativebridge/Android.mk b/libnativebridge/Android.mk
new file mode 100644
index 0000000..83169eb
--- /dev/null
+++ b/libnativebridge/Android.mk
@@ -0,0 +1,40 @@
+LOCAL_PATH:= $(call my-dir)
+
+NATIVE_BRIDGE_COMMON_SRC_FILES := \
+ native_bridge.cc
+
+# Shared library for target
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libnativebridge
+
+LOCAL_SRC_FILES:= $(NATIVE_BRIDGE_COMMON_SRC_FILES)
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_CLANG := true
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_CFLAGS += -Werror -Wall
+LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
+LOCAL_LDFLAGS := -ldl
+LOCAL_MULTILIB := both
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Shared library for host
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libnativebridge
+
+LOCAL_SRC_FILES:= $(NATIVE_BRIDGE_COMMON_SRC_FILES)
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_CLANG := true
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_CFLAGS += -Werror -Wall
+LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
+LOCAL_LDFLAGS := -ldl
+LOCAL_MULTILIB := both
+
+include $(BUILD_HOST_SHARED_LIBRARY)
+
+include $(LOCAL_PATH)/tests/Android.mk
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
new file mode 100644
index 0000000..6fa4b39
--- /dev/null
+++ b/libnativebridge/native_bridge.cc
@@ -0,0 +1,488 @@
+/*
+ * Copyright (C) 2014 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 "nativebridge/native_bridge.h"
+
+#include <cstring>
+#include <cutils/log.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+
+namespace android {
+
+// Environment values required by the apps running with native bridge.
+struct NativeBridgeRuntimeValues {
+ const char* os_arch;
+ const char* cpu_abi;
+ const char* cpu_abi2;
+ const char* *supported_abis;
+ int32_t abi_count;
+};
+
+// The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks.
+static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
+
+enum class NativeBridgeState {
+ kNotSetup, // Initial state.
+ kOpened, // After successful dlopen.
+ kPreInitialized, // After successful pre-initialization.
+ kInitialized, // After successful initialization.
+ kClosed // Closed or errors.
+};
+
+static constexpr const char* kNotSetupString = "kNotSetup";
+static constexpr const char* kOpenedString = "kOpened";
+static constexpr const char* kPreInitializedString = "kPreInitialized";
+static constexpr const char* kInitializedString = "kInitialized";
+static constexpr const char* kClosedString = "kClosed";
+
+static const char* GetNativeBridgeStateString(NativeBridgeState state) {
+ switch (state) {
+ case NativeBridgeState::kNotSetup:
+ return kNotSetupString;
+
+ case NativeBridgeState::kOpened:
+ return kOpenedString;
+
+ case NativeBridgeState::kPreInitialized:
+ return kPreInitializedString;
+
+ case NativeBridgeState::kInitialized:
+ return kInitializedString;
+
+ case NativeBridgeState::kClosed:
+ return kClosedString;
+ }
+}
+
+// Current state of the native bridge.
+static NativeBridgeState state = NativeBridgeState::kNotSetup;
+
+// Whether we had an error at some point.
+static bool had_error = false;
+
+// Handle of the loaded library.
+static void* native_bridge_handle = nullptr;
+// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
+// later.
+static NativeBridgeCallbacks* callbacks = nullptr;
+// Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
+static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
+
+// The app's code cache directory.
+static char* app_code_cache_dir = nullptr;
+
+// Code cache directory (relative to the application private directory)
+// Ideally we'd like to call into framework to retrieve this name. However that's considered an
+// implementation detail and will require either hacks or consistent refactorings. We compromise
+// and hard code the directory name again here.
+static constexpr const char* kCodeCacheDir = "code_cache";
+
+static constexpr uint32_t kNativeBridgeCallbackVersion = 1;
+
+// Characters allowed in a native bridge filename. The first character must
+// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
+static bool CharacterAllowed(char c, bool first) {
+ if (first) {
+ return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
+ } else {
+ return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ||
+ (c == '.') || (c == '_') || (c == '-');
+ }
+}
+
+// We only allow simple names for the library. It is supposed to be a file in
+// /system/lib or /vendor/lib. Only allow a small range of characters, that is
+// names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
+bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
+ const char* ptr = nb_library_filename;
+ if (*ptr == 0) {
+ // Emptry string. Allowed, means no native bridge.
+ return true;
+ } else {
+ // First character must be [a-zA-Z].
+ if (!CharacterAllowed(*ptr, true)) {
+ // Found an invalid fist character, don't accept.
+ ALOGE("Native bridge library %s has been rejected for first character %c", nb_library_filename, *ptr);
+ return false;
+ } else {
+ // For the rest, be more liberal.
+ ptr++;
+ while (*ptr != 0) {
+ if (!CharacterAllowed(*ptr, false)) {
+ // Found an invalid character, don't accept.
+ ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr);
+ return false;
+ }
+ ptr++;
+ }
+ }
+ return true;
+ }
+}
+
+static bool VersionCheck(NativeBridgeCallbacks* cb) {
+ return cb != nullptr && cb->version == kNativeBridgeCallbackVersion;
+}
+
+static void CloseNativeBridge(bool with_error) {
+ state = NativeBridgeState::kClosed;
+ had_error |= with_error;
+ delete[] app_code_cache_dir;
+ app_code_cache_dir = nullptr;
+}
+
+bool LoadNativeBridge(const char* nb_library_filename,
+ const NativeBridgeRuntimeCallbacks* runtime_cbs) {
+ // We expect only one place that calls LoadNativeBridge: Runtime::Init. At that point we are not
+ // multi-threaded, so we do not need locking here.
+
+ if (state != NativeBridgeState::kNotSetup) {
+ // Setup has been called before. Ignore this call.
+ if (nb_library_filename != nullptr) { // Avoids some log-spam for dalvikvm.
+ ALOGW("Called LoadNativeBridge for an already set up native bridge. State is %s.",
+ GetNativeBridgeStateString(state));
+ }
+ // Note: counts as an error, even though the bridge may be functional.
+ had_error = true;
+ return false;
+ }
+
+ if (nb_library_filename == nullptr || *nb_library_filename == 0) {
+ CloseNativeBridge(false);
+ return false;
+ } else {
+ if (!NativeBridgeNameAcceptable(nb_library_filename)) {
+ CloseNativeBridge(true);
+ } else {
+ // Try to open the library.
+ void* handle = dlopen(nb_library_filename, RTLD_LAZY);
+ if (handle != nullptr) {
+ callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
+ kNativeBridgeInterfaceSymbol));
+ if (callbacks != nullptr) {
+ if (VersionCheck(callbacks)) {
+ // Store the handle for later.
+ native_bridge_handle = handle;
+ } else {
+ callbacks = nullptr;
+ dlclose(handle);
+ ALOGW("Unsupported native bridge interface.");
+ }
+ } else {
+ dlclose(handle);
+ }
+ }
+
+ // Two failure conditions: could not find library (dlopen failed), or could not find native
+ // bridge interface (dlsym failed). Both are an error and close the native bridge.
+ if (callbacks == nullptr) {
+ CloseNativeBridge(true);
+ } else {
+ runtime_callbacks = runtime_cbs;
+ state = NativeBridgeState::kOpened;
+ }
+ }
+ return state == NativeBridgeState::kOpened;
+ }
+}
+
+#if defined(__arm__)
+static const char* kRuntimeISA = "arm";
+#elif defined(__aarch64__)
+static const char* kRuntimeISA = "arm64";
+#elif defined(__mips__)
+static const char* kRuntimeISA = "mips";
+#elif defined(__i386__)
+static const char* kRuntimeISA = "x86";
+#elif defined(__x86_64__)
+static const char* kRuntimeISA = "x86_64";
+#else
+static const char* kRuntimeISA = "unknown";
+#endif
+
+
+bool NeedsNativeBridge(const char* instruction_set) {
+ if (instruction_set == nullptr) {
+ ALOGE("Null instruction set in NeedsNativeBridge.");
+ return false;
+ }
+ return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0;
+}
+
+#ifdef __APPLE__
+template<typename T> void UNUSED(const T&) {}
+#endif
+
+bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
+ if (state != NativeBridgeState::kOpened) {
+ ALOGE("Invalid state: native bridge is expected to be opened.");
+ CloseNativeBridge(true);
+ return false;
+ }
+
+ if (app_data_dir_in == nullptr) {
+ ALOGE("Application private directory cannot be null.");
+ CloseNativeBridge(true);
+ return false;
+ }
+
+ // Create the path to the application code cache directory.
+ // The memory will be release after Initialization or when the native bridge is closed.
+ const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/'
+ app_code_cache_dir = new char[len];
+ snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir);
+
+ // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo.
+ // Failure is not fatal and will keep the native bridge in kPreInitialized.
+ state = NativeBridgeState::kPreInitialized;
+
+#ifndef __APPLE__
+ if (instruction_set == nullptr) {
+ return true;
+ }
+ size_t isa_len = strlen(instruction_set);
+ if (isa_len > 10) {
+ // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for
+ // x86_64 [including the trailing \0]). This is so we don't have to change here if there will
+ // be another instruction set in the future.
+ ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.",
+ instruction_set);
+ return true;
+ }
+
+ // If the file does not exist, the mount command will fail,
+ // so we save the extra file existence check.
+ char cpuinfo_path[1024];
+
+#ifdef HAVE_ANDROID_OS
+ snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib"
+#ifdef __LP64__
+ "64"
+#endif // __LP64__
+ "/%s/cpuinfo", instruction_set);
+#else // !HAVE_ANDROID_OS
+ // To be able to test on the host, we hardwire a relative path.
+ snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo");
+#endif
+
+ // Bind-mount.
+ if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source.
+ "/proc/cpuinfo", // Target.
+ nullptr, // FS type.
+ MS_BIND, // Mount flags: bind mount.
+ nullptr)) == -1) { // "Data."
+ ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
+ }
+#else // __APPLE__
+ UNUSED(instruction_set);
+ ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible.");
+#endif
+
+ return true;
+}
+
+static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) {
+ if (value != nullptr) {
+ jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;");
+ if (field_id == nullptr) {
+ env->ExceptionClear();
+ ALOGW("Could not find %s field.", field);
+ return;
+ }
+
+ jstring str = env->NewStringUTF(value);
+ if (str == nullptr) {
+ env->ExceptionClear();
+ ALOGW("Could not create string %s.", value);
+ return;
+ }
+
+ env->SetStaticObjectField(build_class, field_id, str);
+ }
+}
+
+// Set up the environment for the bridged app.
+static void SetupEnvironment(NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) {
+ // Need a JNIEnv* to do anything.
+ if (env == nullptr) {
+ ALOGW("No JNIEnv* to set up app environment.");
+ return;
+ }
+
+ // Query the bridge for environment values.
+ const struct NativeBridgeRuntimeValues* env_values = callbacks->getAppEnv(isa);
+ if (env_values == nullptr) {
+ return;
+ }
+
+ // Keep the JNIEnv clean.
+ jint success = env->PushLocalFrame(16); // That should be small and large enough.
+ if (success < 0) {
+ // Out of memory, really borked.
+ ALOGW("Out of memory while setting up app environment.");
+ env->ExceptionClear();
+ return;
+ }
+
+ // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge.
+ if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr ||
+ env_values->abi_count >= 0) {
+ jclass bclass_id = env->FindClass("android/os/Build");
+ if (bclass_id != nullptr) {
+ SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi);
+ SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2);
+ } else {
+ // For example in a host test environment.
+ env->ExceptionClear();
+ ALOGW("Could not find Build class.");
+ }
+ }
+
+ if (env_values->os_arch != nullptr) {
+ jclass sclass_id = env->FindClass("java/lang/System");
+ if (sclass_id != nullptr) {
+ jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty",
+ "(Ljava/lang/String;Ljava/lang/String;)V");
+ if (set_prop_id != nullptr) {
+ // Init os.arch to the value reqired by the apps running with native bridge.
+ env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"),
+ env->NewStringUTF(env_values->os_arch));
+ } else {
+ env->ExceptionClear();
+ ALOGW("Could not find System#setUnchangeableSystemProperty.");
+ }
+ } else {
+ env->ExceptionClear();
+ ALOGW("Could not find System class.");
+ }
+ }
+
+ // Make it pristine again.
+ env->PopLocalFrame(nullptr);
+}
+
+bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
+ // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that
+ // point we are not multi-threaded, so we do not need locking here.
+
+ if (state == NativeBridgeState::kPreInitialized) {
+ // Check for code cache: if it doesn't exist try to create it.
+ struct stat st;
+ if (stat(app_code_cache_dir, &st) == -1) {
+ if (errno == ENOENT) {
+ if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
+ ALOGE("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
+ CloseNativeBridge(true);
+ }
+ } else {
+ ALOGE("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
+ CloseNativeBridge(true);
+ }
+ } else if (!S_ISDIR(st.st_mode)) {
+ ALOGE("Code cache is not a directory %s.", app_code_cache_dir);
+ CloseNativeBridge(true);
+ }
+
+ // If we're still PreInitialized (dind't fail the code cache checks) try to initialize.
+ if (state == NativeBridgeState::kPreInitialized) {
+ if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) {
+ SetupEnvironment(callbacks, env, instruction_set);
+ state = NativeBridgeState::kInitialized;
+ // We no longer need the code cache path, release the memory.
+ delete[] app_code_cache_dir;
+ app_code_cache_dir = nullptr;
+ } else {
+ // Unload the library.
+ dlclose(native_bridge_handle);
+ CloseNativeBridge(true);
+ }
+ }
+ } else {
+ CloseNativeBridge(true);
+ }
+
+ return state == NativeBridgeState::kInitialized;
+}
+
+void UnloadNativeBridge() {
+ // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that
+ // point we are not multi-threaded, so we do not need locking here.
+
+ switch(state) {
+ case NativeBridgeState::kOpened:
+ case NativeBridgeState::kPreInitialized:
+ case NativeBridgeState::kInitialized:
+ // Unload.
+ dlclose(native_bridge_handle);
+ CloseNativeBridge(false);
+ break;
+
+ case NativeBridgeState::kNotSetup:
+ // Not even set up. Error.
+ CloseNativeBridge(true);
+ break;
+
+ case NativeBridgeState::kClosed:
+ // Ignore.
+ break;
+ }
+}
+
+bool NativeBridgeError() {
+ return had_error;
+}
+
+bool NativeBridgeAvailable() {
+ return state == NativeBridgeState::kOpened
+ || state == NativeBridgeState::kPreInitialized
+ || state == NativeBridgeState::kInitialized;
+}
+
+bool NativeBridgeInitialized() {
+ // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after
+ // Runtime::DidForkFromZygote. In that case we do not need a lock.
+ return state == NativeBridgeState::kInitialized;
+}
+
+void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
+ if (NativeBridgeInitialized()) {
+ return callbacks->loadLibrary(libpath, flag);
+ }
+ return nullptr;
+}
+
+void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
+ uint32_t len) {
+ if (NativeBridgeInitialized()) {
+ return callbacks->getTrampoline(handle, name, shorty, len);
+ }
+ return nullptr;
+}
+
+bool NativeBridgeIsSupported(const char* libpath) {
+ if (NativeBridgeInitialized()) {
+ return callbacks->isSupported(libpath);
+ }
+ return false;
+}
+
+}; // namespace android
diff --git a/libnativebridge/tests/Android.mk b/libnativebridge/tests/Android.mk
new file mode 100644
index 0000000..f28c490
--- /dev/null
+++ b/libnativebridge/tests/Android.mk
@@ -0,0 +1,46 @@
+# Build the unit tests.
+LOCAL_PATH := $(call my-dir)
+
+include $(LOCAL_PATH)/Android.nativebridge-dummy.mk
+
+include $(CLEAR_VARS)
+
+# Build the unit tests.
+test_src_files := \
+ CodeCacheCreate_test.cpp \
+ CodeCacheExists_test.cpp \
+ CompleteFlow_test.cpp \
+ InvalidCharsNativeBridge_test.cpp \
+ NeedsNativeBridge_test.cpp \
+ PreInitializeNativeBridge_test.cpp \
+ PreInitializeNativeBridgeFail1_test.cpp \
+ PreInitializeNativeBridgeFail2_test.cpp \
+ ReSetupNativeBridge_test.cpp \
+ UnavailableNativeBridge_test.cpp \
+ ValidNameNativeBridge_test.cpp
+
+
+shared_libraries := \
+ liblog \
+ libnativebridge \
+ libnativebridge-dummy
+
+$(foreach file,$(test_src_files), \
+ $(eval include $(CLEAR_VARS)) \
+ $(eval LOCAL_CLANG := true) \
+ $(eval LOCAL_CPPFLAGS := -std=gnu++11) \
+ $(eval LOCAL_SHARED_LIBRARIES := $(shared_libraries)) \
+ $(eval LOCAL_SRC_FILES := $(file)) \
+ $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
+ $(eval include $(BUILD_NATIVE_TEST)) \
+)
+
+$(foreach file,$(test_src_files), \
+ $(eval include $(CLEAR_VARS)) \
+ $(eval LOCAL_CLANG := true) \
+ $(eval LOCAL_CPPFLAGS := -std=gnu++11) \
+ $(eval LOCAL_SHARED_LIBRARIES := $(shared_libraries)) \
+ $(eval LOCAL_SRC_FILES := $(file)) \
+ $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
+ $(eval include $(BUILD_HOST_NATIVE_TEST)) \
+)
diff --git a/libnativebridge/tests/Android.nativebridge-dummy.mk b/libnativebridge/tests/Android.nativebridge-dummy.mk
new file mode 100644
index 0000000..1caf50a
--- /dev/null
+++ b/libnativebridge/tests/Android.nativebridge-dummy.mk
@@ -0,0 +1,34 @@
+LOCAL_PATH:= $(call my-dir)
+
+NATIVE_BRIDGE_COMMON_SRC_FILES := \
+ DummyNativeBridge.cpp
+
+# Shared library for target
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libnativebridge-dummy
+
+LOCAL_SRC_FILES:= $(NATIVE_BRIDGE_COMMON_SRC_FILES)
+LOCAL_CLANG := true
+LOCAL_CFLAGS += -Werror -Wall
+LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
+LOCAL_LDFLAGS := -ldl
+LOCAL_MULTILIB := both
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Shared library for host
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libnativebridge-dummy
+
+LOCAL_SRC_FILES:= $(NATIVE_BRIDGE_COMMON_SRC_FILES)
+LOCAL_CLANG := true
+LOCAL_CFLAGS += -Werror -Wall
+LOCAL_CPPFLAGS := -std=gnu++11 -fvisibility=protected
+LOCAL_LDFLAGS := -ldl
+LOCAL_MULTILIB := both
+
+include $(BUILD_HOST_SHARED_LIBRARY)
diff --git a/libnativebridge/tests/CodeCacheCreate_test.cpp b/libnativebridge/tests/CodeCacheCreate_test.cpp
new file mode 100644
index 0000000..58270c4
--- /dev/null
+++ b/libnativebridge/tests/CodeCacheCreate_test.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace android {
+
+// Tests that the bridge initialization creates the code_cache if it doesn't
+// exists.
+TEST_F(NativeBridgeTest, CodeCacheCreate) {
+ // Make sure that code_cache does not exists
+ struct stat st;
+ ASSERT_EQ(-1, stat(kCodeCache, &st));
+ ASSERT_EQ(ENOENT, errno);
+
+ // Init
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
+ ASSERT_TRUE(PreInitializeNativeBridge(".", "isa"));
+ ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_FALSE(NativeBridgeError());
+
+ // Check that code_cache was created
+ ASSERT_EQ(0, stat(kCodeCache, &st));
+ ASSERT_TRUE(S_ISDIR(st.st_mode));
+
+ // Clean up
+ UnloadNativeBridge();
+ ASSERT_EQ(0, rmdir(kCodeCache));
+
+ ASSERT_FALSE(NativeBridgeError());
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/CodeCacheExists_test.cpp b/libnativebridge/tests/CodeCacheExists_test.cpp
new file mode 100644
index 0000000..8ba0158
--- /dev/null
+++ b/libnativebridge/tests/CodeCacheExists_test.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace android {
+
+// Tests that the bridge is initialized without errors if the code_cache already
+// exists.
+TEST_F(NativeBridgeTest, CodeCacheExists) {
+ // Make sure that code_cache does not exists
+ struct stat st;
+ ASSERT_EQ(-1, stat(kCodeCache, &st));
+ ASSERT_EQ(ENOENT, errno);
+
+ // Create the code_cache
+ ASSERT_EQ(0, mkdir(kCodeCache, S_IRWXU | S_IRWXG | S_IXOTH));
+
+ // Init
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
+ ASSERT_TRUE(PreInitializeNativeBridge(".", "isa"));
+ ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_FALSE(NativeBridgeError());
+
+ // Check that the code cache is still there
+ ASSERT_EQ(0, stat(kCodeCache, &st));
+ ASSERT_TRUE(S_ISDIR(st.st_mode));
+
+ // Clean up
+ UnloadNativeBridge();
+ ASSERT_EQ(0, rmdir(kCodeCache));
+
+ ASSERT_FALSE(NativeBridgeError());
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/CompleteFlow_test.cpp b/libnativebridge/tests/CompleteFlow_test.cpp
new file mode 100644
index 0000000..b033792
--- /dev/null
+++ b/libnativebridge/tests/CompleteFlow_test.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <unistd.h>
+
+namespace android {
+
+TEST_F(NativeBridgeTest, CompleteFlow) {
+ // Init
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(PreInitializeNativeBridge(".", "isa"));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+
+ // Basic calls to check that nothing crashes
+ ASSERT_FALSE(NativeBridgeIsSupported(nullptr));
+ ASSERT_EQ(nullptr, NativeBridgeLoadLibrary(nullptr, 0));
+ ASSERT_EQ(nullptr, NativeBridgeGetTrampoline(nullptr, nullptr, nullptr, 0));
+
+ // Unload
+ UnloadNativeBridge();
+
+ ASSERT_FALSE(NativeBridgeAvailable());
+ ASSERT_FALSE(NativeBridgeError());
+
+ // Clean-up code_cache
+ ASSERT_EQ(0, rmdir(kCodeCache));
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/DummyNativeBridge.cpp b/libnativebridge/tests/DummyNativeBridge.cpp
new file mode 100644
index 0000000..b9894f6
--- /dev/null
+++ b/libnativebridge/tests/DummyNativeBridge.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+// A dummy implementation of the native-bridge interface.
+
+#include "nativebridge/native_bridge.h"
+
+// NativeBridgeCallbacks implementations
+extern "C" bool native_bridge_initialize(const android::NativeBridgeRuntimeCallbacks* /* art_cbs */,
+ const char* /* app_code_cache_dir */,
+ const char* /* isa */) {
+ return true;
+}
+
+extern "C" void* native_bridge_loadLibrary(const char* /* libpath */, int /* flag */) {
+ return nullptr;
+}
+
+extern "C" void* native_bridge_getTrampoline(void* /* handle */, const char* /* name */,
+ const char* /* shorty */, uint32_t /* len */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge_isSupported(const char* /* libpath */) {
+ return false;
+}
+
+extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge_getAppEnv(
+ const char* /* abi */) {
+ return nullptr;
+}
+
+android::NativeBridgeCallbacks NativeBridgeItf {
+ .version = 1,
+ .initialize = &native_bridge_initialize,
+ .loadLibrary = &native_bridge_loadLibrary,
+ .getTrampoline = &native_bridge_getTrampoline,
+ .isSupported = &native_bridge_isSupported,
+ .getAppEnv = &native_bridge_getAppEnv
+};
diff --git a/libnativebridge/tests/InvalidCharsNativeBridge_test.cpp b/libnativebridge/tests/InvalidCharsNativeBridge_test.cpp
new file mode 100644
index 0000000..8f7973d
--- /dev/null
+++ b/libnativebridge/tests/InvalidCharsNativeBridge_test.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+namespace android {
+
+static const char* kTestName = "../librandom$@-bridge_not.existing.so";
+
+TEST_F(NativeBridgeTest, InvalidChars) {
+ // Do one test actually calling setup.
+ EXPECT_EQ(false, NativeBridgeError());
+ LoadNativeBridge(kTestName, nullptr);
+ // This should lead to an error for invalid characters.
+ EXPECT_EQ(true, NativeBridgeError());
+
+ // Further tests need to use NativeBridgeNameAcceptable, as the error
+ // state can't be changed back.
+ EXPECT_EQ(false, NativeBridgeNameAcceptable("."));
+ EXPECT_EQ(false, NativeBridgeNameAcceptable(".."));
+ EXPECT_EQ(false, NativeBridgeNameAcceptable("_"));
+ EXPECT_EQ(false, NativeBridgeNameAcceptable("-"));
+ EXPECT_EQ(false, NativeBridgeNameAcceptable("lib@.so"));
+ EXPECT_EQ(false, NativeBridgeNameAcceptable("lib$.so"));
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/NativeBridgeTest.h b/libnativebridge/tests/NativeBridgeTest.h
new file mode 100644
index 0000000..6a5c126
--- /dev/null
+++ b/libnativebridge/tests/NativeBridgeTest.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2014 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 NATIVE_BRIDGE_TEST_H_
+#define NATIVE_BRIDGE_TEST_H_
+
+#define LOG_TAG "NativeBridge_test"
+
+#include <nativebridge/native_bridge.h>
+#include <gtest/gtest.h>
+
+constexpr const char* kNativeBridgeLibrary = "libnativebridge-dummy.so";
+constexpr const char* kCodeCache = "./code_cache";
+
+namespace android {
+
+class NativeBridgeTest : public testing::Test {
+};
+
+}; // namespace android
+
+#endif // NATIVE_BRIDGE_H_
+
diff --git a/libnativebridge/tests/NeedsNativeBridge_test.cpp b/libnativebridge/tests/NeedsNativeBridge_test.cpp
new file mode 100644
index 0000000..e1c0876
--- /dev/null
+++ b/libnativebridge/tests/NeedsNativeBridge_test.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+namespace android {
+
+static const char* kISAs[] = { "arm", "arm64", "mips", "x86", "x86_64", "random", "64arm", "64_x86",
+ "64_x86_64", "", "reallylongstringabcd", nullptr };
+
+#if defined(__arm__)
+static const char* kRuntimeISA = "arm";
+#elif defined(__aarch64__)
+static const char* kRuntimeISA = "arm64";
+#elif defined(__mips__)
+static const char* kRuntimeISA = "mips";
+#elif defined(__i386__)
+static const char* kRuntimeISA = "x86";
+#elif defined(__x86_64__)
+static const char* kRuntimeISA = "x86_64";
+#else
+static const char* kRuntimeISA = "unknown";
+#endif
+
+TEST_F(NativeBridgeTest, NeedsNativeBridge) {
+ EXPECT_EQ(false, NeedsNativeBridge(kRuntimeISA));
+
+ const size_t kISACount = sizeof(kISAs)/sizeof(kISAs[0]);
+ for (size_t i = 0; i < kISACount; i++) {
+ EXPECT_EQ(kISAs[i] == nullptr ? false : strcmp(kISAs[i], kRuntimeISA) != 0,
+ NeedsNativeBridge(kISAs[i]));
+ }
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp b/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp
new file mode 100644
index 0000000..69c30a1
--- /dev/null
+++ b/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <cstdio>
+#include <cstring>
+#include <cutils/log.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+namespace android {
+
+TEST_F(NativeBridgeTest, PreInitializeNativeBridgeFail1) {
+ // Needs a valid application directory.
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
+ ASSERT_FALSE(PreInitializeNativeBridge(nullptr, "isa"));
+ ASSERT_TRUE(NativeBridgeError());
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/PreInitializeNativeBridgeFail2_test.cpp b/libnativebridge/tests/PreInitializeNativeBridgeFail2_test.cpp
new file mode 100644
index 0000000..74e96e0
--- /dev/null
+++ b/libnativebridge/tests/PreInitializeNativeBridgeFail2_test.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <cstdio>
+#include <cstring>
+#include <cutils/log.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+namespace android {
+
+TEST_F(NativeBridgeTest, PreInitializeNativeBridgeFail2) {
+ // Needs LoadNativeBridge() first
+ ASSERT_FALSE(PreInitializeNativeBridge(nullptr, "isa"));
+ ASSERT_TRUE(NativeBridgeError());
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/PreInitializeNativeBridge_test.cpp b/libnativebridge/tests/PreInitializeNativeBridge_test.cpp
new file mode 100644
index 0000000..cec26ce
--- /dev/null
+++ b/libnativebridge/tests/PreInitializeNativeBridge_test.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+#include <cstdio>
+#include <cstring>
+#include <cutils/log.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+namespace android {
+
+static constexpr const char* kTestData = "PreInitializeNativeBridge test.";
+
+TEST_F(NativeBridgeTest, PreInitializeNativeBridge) {
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
+#ifndef __APPLE__ // Mac OS does not support bind-mount.
+#ifndef HAVE_ANDROID_OS // Cannot write into the hard-wired location.
+ // Try to create our mount namespace.
+ if (unshare(CLONE_NEWNS) != -1) {
+ // Create a dummy file.
+ FILE* cpuinfo = fopen("./cpuinfo", "w");
+ ASSERT_NE(nullptr, cpuinfo) << strerror(errno);
+ fprintf(cpuinfo, kTestData);
+ fclose(cpuinfo);
+
+ ASSERT_TRUE(PreInitializeNativeBridge("does not matter 1", "short 2"));
+
+ // Read /proc/cpuinfo
+ FILE* proc_cpuinfo = fopen("/proc/cpuinfo", "r");
+ ASSERT_NE(nullptr, proc_cpuinfo) << strerror(errno);
+ char buf[1024];
+ EXPECT_NE(nullptr, fgets(buf, sizeof(buf), proc_cpuinfo)) << "Error reading.";
+ fclose(proc_cpuinfo);
+
+ EXPECT_EQ(0, strcmp(buf, kTestData));
+
+ // Delete the file.
+ ASSERT_EQ(0, unlink("./cpuinfo")) << "Error unlinking temporary file.";
+ // Ending the test will tear down the mount namespace.
+ } else {
+ GTEST_LOG_(WARNING) << "Could not create mount namespace. Are you running this as root?";
+ }
+#endif
+#endif
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/ReSetupNativeBridge_test.cpp b/libnativebridge/tests/ReSetupNativeBridge_test.cpp
new file mode 100644
index 0000000..944e5d7
--- /dev/null
+++ b/libnativebridge/tests/ReSetupNativeBridge_test.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2014 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 "NativeBridgeTest.h"
+
+namespace android {
+
+TEST_F(NativeBridgeTest, ReSetup) {
+ EXPECT_EQ(false, NativeBridgeError());
+ LoadNativeBridge("", nullptr);
+ EXPECT_EQ(false, NativeBridgeError());
+ LoadNativeBridge("", nullptr);
+ // This should lead to an error for trying to re-setup a native bridge.
+ EXPECT_EQ(true, NativeBridgeError());
+}
+
+} // namespace android
diff --git a/libnl_2/cache.c b/libnativebridge/tests/UnavailableNativeBridge_test.cpp
similarity index 61%
copy from libnl_2/cache.c
copy to libnativebridge/tests/UnavailableNativeBridge_test.cpp
index c21974d..ad374a5 100644
--- a/libnl_2/cache.c
+++ b/libnativebridge/tests/UnavailableNativeBridge_test.cpp
@@ -14,24 +14,16 @@
* limitations under the License.
*/
-/* NOTICE: This is a clean room re-implementation of libnl */
+#include "NativeBridgeTest.h"
-#include "netlink/cache.h"
-#include "netlink/object.h"
+namespace android {
-void nl_cache_free(struct nl_cache *cache)
-{
-
+TEST_F(NativeBridgeTest, NoNativeBridge) {
+ EXPECT_EQ(false, NativeBridgeAvailable());
+ // Try to initialize. This should fail as we are not set up.
+ EXPECT_EQ(false, InitializeNativeBridge(nullptr, nullptr));
+ EXPECT_EQ(true, NativeBridgeError());
+ EXPECT_EQ(false, NativeBridgeAvailable());
}
-void nl_cache_clear(struct nl_cache *cache)
-{
-
-}
-
-void nl_cache_remove(struct nl_object *obj)
-{
-
-}
-
-
+} // namespace android
diff --git a/libnativebridge/tests/ValidNameNativeBridge_test.cpp b/libnativebridge/tests/ValidNameNativeBridge_test.cpp
new file mode 100644
index 0000000..690be4a
--- /dev/null
+++ b/libnativebridge/tests/ValidNameNativeBridge_test.cpp
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+#include <NativeBridgeTest.h>
+
+namespace android {
+
+static const char* kTestName = "librandom-bridge_not.existing.so";
+
+TEST_F(NativeBridgeTest, ValidName) {
+ // Check that the name is acceptable.
+ EXPECT_EQ(true, NativeBridgeNameAcceptable(kTestName));
+
+ // Now check what happens on LoadNativeBridge.
+ EXPECT_EQ(false, NativeBridgeError());
+ LoadNativeBridge(kTestName, nullptr);
+ // This will lead to an error as the library doesn't exist.
+ EXPECT_EQ(true, NativeBridgeError());
+ EXPECT_EQ(false, NativeBridgeAvailable());
+}
+
+} // namespace android
diff --git a/libnetutils/Android.mk b/libnetutils/Android.mk
index aba4621..1f61511 100644
--- a/libnetutils/Android.mk
+++ b/libnetutils/Android.mk
@@ -1,7 +1,7 @@
-LOCAL_PATH:= $(call my-dir)
+LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= \
+LOCAL_SRC_FILES := \
dhcpclient.c \
dhcpmsg.c \
dhcp_utils.c \
@@ -12,6 +12,8 @@
libcutils \
liblog
-LOCAL_MODULE:= libnetutils
+LOCAL_MODULE := libnetutils
+
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index e1df874..c6b9fe4 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -1,16 +1,16 @@
/*
* Copyright 2008, 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
+ * 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
+ * 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
+ * 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.
*/
@@ -33,7 +33,7 @@
static const int NAP_TIME = 200; /* wait for 200ms at a time */
/* when polling for property values */
static const char DAEMON_NAME_RENEW[] = "iprenew";
-static char errmsg[100];
+static char errmsg[100] = "\0";
/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
* or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
* and other properties on a successful bind
@@ -72,14 +72,16 @@
maxnaps = 1;
}
- while (maxnaps-- > 0) {
- usleep(NAP_TIME * 1000);
+ while (maxnaps-- >= 0) {
if (property_get(name, value, NULL)) {
- if (desired_value == NULL ||
+ if (desired_value == NULL ||
strcmp(value, desired_value) == 0) {
return 0;
}
}
+ if (maxnaps >= 0) {
+ usleep(NAP_TIME * 1000);
+ }
}
return -1; /* failure */
}
@@ -166,12 +168,45 @@
return 0;
}
-static const char *ipaddr_to_string(in_addr_t addr)
+/*
+ * Get any available DHCP results.
+ */
+int dhcp_get_results(const char *interface,
+ char *ipaddr,
+ char *gateway,
+ uint32_t *prefixLength,
+ char *dns[],
+ char *server,
+ uint32_t *lease,
+ char *vendorInfo,
+ char *domain,
+ char *mtu)
{
- struct in_addr in_addr;
+ char result_prop_name[PROPERTY_KEY_MAX];
+ char prop_value[PROPERTY_VALUE_MAX];
- in_addr.s_addr = addr;
- return inet_ntoa(in_addr);
+ /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
+ char p2p_interface[MAX_INTERFACE_LENGTH];
+ get_p2p_interface_replacement(interface, p2p_interface);
+ snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
+ DHCP_PROP_NAME_PREFIX,
+ p2p_interface);
+
+ memset(prop_value, '\0', PROPERTY_VALUE_MAX);
+ if (!property_get(result_prop_name, prop_value, NULL)) {
+ snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
+ return -1;
+ }
+ if (strcmp(prop_value, "ok") == 0) {
+ if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
+ server, lease, vendorInfo, domain, mtu) == -1) {
+ return -1;
+ }
+ return 0;
+ } else {
+ snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
+ return -1;
+ }
}
/*
@@ -183,16 +218,7 @@
* Example:
* service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
*/
-int dhcp_do_request(const char *interface,
- char *ipaddr,
- char *gateway,
- uint32_t *prefixLength,
- char *dns[],
- char *server,
- uint32_t *lease,
- char *vendorInfo,
- char *domain,
- char *mtu)
+int dhcp_start(const char *interface)
{
char result_prop_name[PROPERTY_KEY_MAX];
char daemon_prop_name[PROPERTY_KEY_MAX];
@@ -236,22 +262,7 @@
return -1;
}
- if (!property_get(result_prop_name, prop_value, NULL)) {
- /* shouldn't ever happen, given the success of wait_for_property() */
- snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
- return -1;
- }
- if (strcmp(prop_value, "ok") == 0) {
- char dns_prop_name[PROPERTY_KEY_MAX];
- if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
- server, lease, vendorInfo, domain, mtu) == -1) {
- return -1;
- }
- return 0;
- } else {
- snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
- return -1;
- }
+ return 0;
}
/**
@@ -327,16 +338,7 @@
* service iprenew_<interface> /system/bin/dhcpcd -n
*
*/
-int dhcp_do_request_renew(const char *interface,
- char *ipaddr,
- char *gateway,
- uint32_t *prefixLength,
- char *dns[],
- char *server,
- uint32_t *lease,
- char *vendorInfo,
- char *domain,
- char *mtu)
+int dhcp_start_renew(const char *interface)
{
char result_prop_name[PROPERTY_KEY_MAX];
char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
@@ -366,16 +368,5 @@
return -1;
}
- if (!property_get(result_prop_name, prop_value, NULL)) {
- /* shouldn't ever happen, given the success of wait_for_property() */
- snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
- return -1;
- }
- if (strcmp(prop_value, "ok") == 0) {
- return fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
- server, lease, vendorInfo, domain, mtu);
- } else {
- snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
- return -1;
- }
+ return 0;
}
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index 34500e7..240a789 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -150,7 +150,7 @@
void dump_dhcp_info(dhcp_info *info)
{
- char addr[20], gway[20], mask[20];
+ char addr[20], gway[20];
ALOGD("--- dhcp %s (%d) ---",
dhcp_type_to_name(info->type), info->type);
strcpy(addr, ipaddr(info->ipaddr));
@@ -236,13 +236,13 @@
#if VERBOSE
-static void hex2str(char *buf, const unsigned char *array, int len)
+static void hex2str(char *buf, size_t buf_size, const unsigned char *array, int len)
{
int i;
char *cp = buf;
-
+ char *buf_end = buf + buf_size;
for (i = 0; i < len; i++) {
- cp += sprintf(cp, " %02x ", array[i]);
+ cp += snprintf(cp, buf_end - cp, " %02x ", array[i]);
}
}
@@ -278,20 +278,22 @@
ALOGD("giaddr = %s", ipaddr(msg->giaddr));
c = msg->hlen > 16 ? 16 : msg->hlen;
- hex2str(buf, msg->chaddr, c);
+ hex2str(buf, sizeof(buf), msg->chaddr, c);
ALOGD("chaddr = {%s}", buf);
for (n = 0; n < 64; n++) {
- if ((msg->sname[n] < ' ') || (msg->sname[n] > 127)) {
- if (msg->sname[n] == 0) break;
+ unsigned char x = msg->sname[n];
+ if ((x < ' ') || (x > 127)) {
+ if (x == 0) break;
msg->sname[n] = '.';
}
}
msg->sname[63] = 0;
for (n = 0; n < 128; n++) {
- if ((msg->file[n] < ' ') || (msg->file[n] > 127)) {
- if (msg->file[n] == 0) break;
+ unsigned char x = msg->file[n];
+ if ((x < ' ') || (x > 127)) {
+ if (x == 0) break;
msg->file[n] = '.';
}
}
@@ -325,7 +327,7 @@
memcpy(buf, &x[2], n);
buf[n] = '\0';
} else {
- hex2str(buf, &x[2], optsz);
+ hex2str(buf, sizeof(buf), &x[2], optsz);
}
if (x[0] == OPT_MESSAGE_TYPE)
name = dhcp_type_to_name(x[2]);
@@ -351,28 +353,28 @@
static int is_valid_reply(dhcp_msg *msg, dhcp_msg *reply, int sz)
{
if (sz < DHCP_MSG_FIXED_SIZE) {
- if (verbose) ALOGD("netcfg: Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
+ if (verbose) ALOGD("Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
return 0;
}
if (reply->op != OP_BOOTREPLY) {
- if (verbose) ALOGD("netcfg: Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
+ if (verbose) ALOGD("Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
return 0;
}
if (reply->xid != msg->xid) {
- if (verbose) ALOGD("netcfg: Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
- ntohl(msg->xid));
+ if (verbose) ALOGD("Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
+ ntohl(msg->xid));
return 0;
}
if (reply->htype != msg->htype) {
- if (verbose) ALOGD("netcfg: Wrong Htype %d != %d\n", reply->htype, msg->htype);
+ if (verbose) ALOGD("Wrong Htype %d != %d\n", reply->htype, msg->htype);
return 0;
}
if (reply->hlen != msg->hlen) {
- if (verbose) ALOGD("netcfg: Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
+ if (verbose) ALOGD("Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
return 0;
}
if (memcmp(msg->chaddr, reply->chaddr, msg->hlen)) {
- if (verbose) ALOGD("netcfg: Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
+ if (verbose) ALOGD("Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
return 0;
}
return 1;
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index 4d004f6..7d2a5fb 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -123,7 +123,7 @@
{
int ret;
if (ifc_ctl_sock == -1) {
- ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
+ ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (ifc_ctl_sock < 0) {
printerr("socket() failed: %s\n", strerror(errno));
}
@@ -137,7 +137,7 @@
int ifc_init6(void)
{
if (ifc_ctl_sock6 == -1) {
- ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
+ ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (ifc_ctl_sock6 < 0) {
printerr("socket() failed: %s\n", strerror(errno));
}
@@ -316,7 +316,7 @@
req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
memcpy(RTA_DATA(rta), addr, addrlen);
- s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+ s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
close(s);
return -errno;
@@ -421,7 +421,6 @@
int ifc_set_hwaddr(const char *name, const void *ptr)
{
- int r;
struct ifreq ifr;
ifc_init_ifr(name, &ifr);
@@ -563,17 +562,7 @@
return ret;
}
-/* deprecated v4-only */
-int ifc_add_host_route(const char *name, in_addr_t dst)
-{
- struct in_addr in_dst, in_gw;
-
- in_dst.s_addr = dst;
- in_gw.s_addr = 0;
-
- return ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 32, in_gw);
-}
-
+// Needed by code in hidden partner repositories / branches, so don't delete.
int ifc_enable(const char *ifname)
{
int result;
@@ -584,6 +573,7 @@
return result;
}
+// Needed by code in hidden partner repositories / branches, so don't delete.
int ifc_disable(const char *ifname)
{
unsigned addr, count;
@@ -608,14 +598,16 @@
{
#ifdef HAVE_ANDROID_OS
int result, success;
- in_addr_t myaddr;
+ in_addr_t myaddr = 0;
struct ifreq ifr;
struct in6_ifreq ifr6;
if (reset_mask & RESET_IPV4_ADDRESSES) {
/* IPv4. Clear connections on the IP address. */
ifc_init();
- ifc_get_info(ifname, &myaddr, NULL, NULL);
+ if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
+ ifc_get_info(ifname, &myaddr, NULL, NULL);
+ }
ifc_init_ifr(ifname, &ifr);
init_sockaddr_in(&ifr.ifr_addr, myaddr);
result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
@@ -648,118 +640,6 @@
}
/*
- * Remove the routes associated with the named interface.
- */
-int ifc_remove_host_routes(const char *name)
-{
- char ifname[64];
- in_addr_t dest, gway, mask;
- int flags, refcnt, use, metric, mtu, win, irtt;
- struct rtentry rt;
- FILE *fp;
- struct in_addr addr;
-
- fp = fopen("/proc/net/route", "r");
- if (fp == NULL)
- return -1;
- /* Skip the header line */
- if (fscanf(fp, "%*[^\n]\n") < 0) {
- fclose(fp);
- return -1;
- }
- ifc_init();
- for (;;) {
- int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
- ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
- &mtu, &win, &irtt);
- if (nread != 11) {
- break;
- }
- if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
- || strcmp(ifname, name) != 0) {
- continue;
- }
- memset(&rt, 0, sizeof(rt));
- rt.rt_dev = (void *)name;
- init_sockaddr_in(&rt.rt_dst, dest);
- init_sockaddr_in(&rt.rt_gateway, gway);
- init_sockaddr_in(&rt.rt_genmask, mask);
- addr.s_addr = dest;
- if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
- ALOGD("failed to remove route for %s to %s: %s",
- ifname, inet_ntoa(addr), strerror(errno));
- }
- }
- fclose(fp);
- ifc_close();
- return 0;
-}
-
-/*
- * Return the address of the default gateway
- *
- * TODO: factor out common code from this and remove_host_routes()
- * so that we only scan /proc/net/route in one place.
- *
- * DEPRECATED
- */
-int ifc_get_default_route(const char *ifname)
-{
- char name[64];
- in_addr_t dest, gway, mask;
- int flags, refcnt, use, metric, mtu, win, irtt;
- int result;
- FILE *fp;
-
- fp = fopen("/proc/net/route", "r");
- if (fp == NULL)
- return 0;
- /* Skip the header line */
- if (fscanf(fp, "%*[^\n]\n") < 0) {
- fclose(fp);
- return 0;
- }
- ifc_init();
- result = 0;
- for (;;) {
- int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
- name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
- &mtu, &win, &irtt);
- if (nread != 11) {
- break;
- }
- if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
- && dest == 0
- && strcmp(ifname, name) == 0) {
- result = gway;
- break;
- }
- }
- fclose(fp);
- ifc_close();
- return result;
-}
-
-/*
- * Sets the specified gateway as the default route for the named interface.
- * DEPRECATED
- */
-int ifc_set_default_route(const char *ifname, in_addr_t gateway)
-{
- struct in_addr addr;
- int result;
-
- ifc_init();
- addr.s_addr = gateway;
- if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
- ALOGD("failed to add %s as default route for %s: %s",
- inet_ntoa(addr), ifname, strerror(errno));
- }
- ifc_close();
- return result;
-}
-
-/*
* Removes the default route for the named interface.
*/
int ifc_remove_default_route(const char *ifname)
@@ -821,151 +701,3 @@
return 0;
}
-
-int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
- struct in6_addr gw)
-{
- struct in6_rtmsg rtmsg;
- int result;
- int ifindex;
-
- memset(&rtmsg, 0, sizeof(rtmsg));
-
- ifindex = if_nametoindex(ifname);
- if (ifindex == 0) {
- printerr("if_nametoindex() failed: interface %s\n", ifname);
- return -ENXIO;
- }
-
- rtmsg.rtmsg_ifindex = ifindex;
- rtmsg.rtmsg_dst = dst;
- rtmsg.rtmsg_dst_len = prefix_length;
- rtmsg.rtmsg_flags = RTF_UP;
-
- if (prefix_length == 128) {
- rtmsg.rtmsg_flags |= RTF_HOST;
- }
-
- if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
- rtmsg.rtmsg_flags |= RTF_GATEWAY;
- rtmsg.rtmsg_gateway = gw;
- }
-
- ifc_init6();
-
- if (ifc_ctl_sock6 < 0) {
- return -errno;
- }
-
- result = ioctl(ifc_ctl_sock6, action, &rtmsg);
- if (result < 0) {
- if (errno == EEXIST) {
- result = 0;
- } else {
- result = -errno;
- }
- }
- ifc_close6();
- return result;
-}
-
-int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
- const char *gw)
-{
- int ret = 0;
- struct sockaddr_in ipv4_dst, ipv4_gw;
- struct sockaddr_in6 ipv6_dst, ipv6_gw;
- struct addrinfo hints, *addr_ai, *gw_ai;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
- hints.ai_flags = AI_NUMERICHOST;
-
- ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
-
- if (ret != 0) {
- printerr("getaddrinfo failed: invalid address %s\n", dst);
- return -EINVAL;
- }
-
- if (gw == NULL || (strlen(gw) == 0)) {
- if (addr_ai->ai_family == AF_INET6) {
- gw = "::";
- } else if (addr_ai->ai_family == AF_INET) {
- gw = "0.0.0.0";
- }
- }
-
- if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
- ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
- printerr("ifc_add_route: invalid prefix length");
- freeaddrinfo(addr_ai);
- return -EINVAL;
- }
-
- ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
- if (ret != 0) {
- printerr("getaddrinfo failed: invalid gateway %s\n", gw);
- freeaddrinfo(addr_ai);
- return -EINVAL;
- }
-
- if (addr_ai->ai_family != gw_ai->ai_family) {
- printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
- freeaddrinfo(addr_ai);
- freeaddrinfo(gw_ai);
- return -EINVAL;
- }
-
- if (addr_ai->ai_family == AF_INET6) {
- memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
- memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
- ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
- prefix_length, ipv6_gw.sin6_addr);
- } else if (addr_ai->ai_family == AF_INET) {
- memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
- memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
- ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
- prefix_length, ipv4_gw.sin_addr);
- } else {
- printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
- addr_ai->ai_family);
- ret = -EAFNOSUPPORT;
- }
-
- freeaddrinfo(addr_ai);
- freeaddrinfo(gw_ai);
- return ret;
-}
-
-/*
- * DEPRECATED
- */
-int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
- struct in_addr gw)
-{
- int i =ifc_act_on_ipv4_route(SIOCADDRT, ifname, dst, prefix_length, gw);
- if (DBG) printerr("ifc_add_ipv4_route(%s, xx, %d, xx) = %d", ifname, prefix_length, i);
- return i;
-}
-
-/*
- * DEPRECATED
- */
-int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
- struct in6_addr gw)
-{
- return ifc_act_on_ipv6_route(SIOCADDRT, ifname, dst, prefix_length, gw);
-}
-
-int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
-{
- int i = ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
- if (DBG) printerr("ifc_add_route(%s, %s, %d, %s) = %d", ifname, dst, prefix_length, gw, i);
- return i;
-}
-
-int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
-{
- return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
-}
diff --git a/libnetutils/packet.c b/libnetutils/packet.c
index 3cdefb0..cd26d05 100644
--- a/libnetutils/packet.c
+++ b/libnetutils/packet.c
@@ -15,6 +15,7 @@
*/
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/socket.h>
@@ -41,7 +42,7 @@
int open_raw_socket(const char *ifname __attribute__((unused)), uint8_t *hwaddr, int if_index)
{
- int s, flag;
+ int s;
struct sockaddr_ll bindaddr;
if((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
diff --git a/libnl_2/.gitignore b/libnl_2/.gitignore
deleted file mode 100644
index d4ca744..0000000
--- a/libnl_2/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-include/netlink/version.h.in
-cscope.*
diff --git a/libnl_2/Android.mk b/libnl_2/Android.mk
deleted file mode 100644
index 3721fc6..0000000
--- a/libnl_2/Android.mk
+++ /dev/null
@@ -1,39 +0,0 @@
-#######################################
-# * Netlink cache not implemented
-# * Library is not thread safe
-#######################################
-
-LOCAL_PATH := $(call my-dir)
-
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
- attr.c \
- cache.c \
- genl/genl.c \
- genl/family.c \
- handlers.c \
- msg.c \
- netlink.c \
- object.c \
- socket.c \
- dbg.c
-
-LOCAL_C_INCLUDES += \
- external/libnl-headers
-
-# Static Library
-LOCAL_MODULE := libnl_2
-LOCAL_MODULE_TAGS := optional
-LOCAL_32_BIT_ONLY := true
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES :=
-LOCAL_WHOLE_STATIC_LIBRARIES:= libnl_2
-LOCAL_SHARED_LIBRARIES:= liblog
-LOCAL_MODULE := libnl_2
-LOCAL_MODULE_TAGS := optional
-LOCAL_32_BIT_ONLY := true
-include $(BUILD_SHARED_LIBRARY)
diff --git a/libnl_2/README b/libnl_2/README
deleted file mode 100644
index 14db6db..0000000
--- a/libnl_2/README
+++ /dev/null
@@ -1,88 +0,0 @@
-Netlink Protocol Library
-
-This library is a clean room re-implementation of libnl 2.0 and
-re-licensed under Apache 2.0. It was developed primarily to support
-wpa_supplicant. However, with additional development can be extended
-to support other netlink applications.
-
-Netlink Protocol Format (RFC3549)
-
-+-----------------+-+-------------------+-+
-|Netlink Message |P| Generic Netlink |P|
-| Header |A| Message Header |A|
-|(struct nlmsghdr)|D|(struct genlmsghdr)|D|
-+-----------------+-+-------------------+-+-------------+
-|len:4|type:2|flags:2|seq:4 pid:4|cmd:1|ver:1|reserved:2|
-+--------------------------------+----------------------+
-+-----------------+-+-----------------+-+-----------------+-+-----------------+-+---+
-|Netlink Attribute|P|Netlink Attribute|P|Netlink Attribute|P|Netlink Attribute|P|...|
-| #0 Header |A| #0 Payload |A| #1 Header |A| #1 Payload |A| |
-| (struct nlattr) |D| (void) |D| (struct nlattr) |D| (void) |D| |
-+-----------------+-+-----------------+-+-----------------+-+-----------------+-+---+
-|len:2(==4+payload)|type:2|payload|pad|
-+-------------------------+-------+---+
-
-NETLINK OVERVIEW
-
-* Each netlink message consists of a bitstream with a netlink header.
-* After this header a second header *can* be used specific to the netlink
- family in use. This library was tested using the generic netlink
- protocol defined by struct genlmsghdr to support nl80211.
-* After the header(s) netlink attributes can be appended to the message
- which hold can hold basic types such as unsigned integers and strings.
-* Attributes can also be nested. This is accomplished by calling "nla_nest_start"
- which creates an empty attribute with nest attributes as its payload. Then to
- close the nest, "nla_nest_end" is called.
-* All data structures in this implementation are byte-aligned (Currently 4 bytes).
-* Acknowledgements (ACKs) are sent as NLMSG_ERROR netlink message types (0x2) and
- have an error value of 0.
-
-KNOWN ISSUES
-
- GENERAL
- * Not tested for thread safety
-
- Android.mk
- * No dynamic library because of netlink cache not implemented and
- not tested for thread safety
-
- attr.c
- * nla_parse - does not use nla_policy argument
-
- cache.c
- * netlink cache not implemented and only supports one netlink family id
- which is stored in the nl_cache pointer instead of an actual cache
-
- netlink.c
- * nl_recvmsgs - does not support nl_cb_overwrite_recv()
- * nl_recv - sets/unsets asynchronous socket flag
-
-SOURCE FILES
-
-* Android.mk - Android makefile
-* README - This file
-* attr.c - Netlink attributes
-* cache.c - Netlink cache
-* genl/family.c - Generic netlink family id
-* genl/genl.c - Generic netlink
-* handlers.c - Netlink callbacks
-* msg.c - Netlink messages construction
-* netlink.c - Netlink socket communication
-* object.c - libnl object wrapper
-* socket.c - Netlink kernel socket utils
-
-IMPORTANT HEADER FILES - NOTE: These are based on the the origin GPL libnl headers
-
-* netlink-types.h - Contains many important structs for libnl
- to represent netlink objects
-* netlink/netlink-kernel.h - Netlink kernel headers and field constants.
-* netlink/msg.h - macros for iterating over netlink messages
-* netlink/attr.h - netlink attribute constants, iteration macros and setters
-
-REFERENCES
-
-* nl80211.h
-* netlink_types.h
-* $LINUX_KERNEL/net/wireless/nl80211.c
-* http://www.infradead.org/~tgr/libnl/doc-3.0/index.html
-* http://www.netfilter.org/projects/libmnl/doxygen/index.html
diff --git a/libnl_2/attr.c b/libnl_2/attr.c
deleted file mode 100644
index 2ef7590..0000000
--- a/libnl_2/attr.c
+++ /dev/null
@@ -1,239 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <errno.h>
-#include "netlink/netlink.h"
-#include "netlink/msg.h"
-#include "netlink/attr.h"
-#include "netlink-types.h"
-
-/* Return payload of string attribute. */
-char *nla_get_string(struct nlattr *nla)
-{
- return (char *) nla_data(nla);
-}
-
-/* Return payload of 16 bit integer attribute. */
-uint16_t nla_get_u16(struct nlattr *nla)
-{
- return *((uint16_t *) nla_data(nla));
-}
-
-/* Return payload of 32 bit integer attribute. */
-uint32_t nla_get_u32(struct nlattr *nla)
-{
- return *((uint32_t *) nla_data(nla));
-}
-
-/* Return value of 8 bit integer attribute. */
-uint8_t nla_get_u8(struct nlattr *nla)
-{
- return *((uint8_t *) nla_data(nla));
-}
-
-/* Return payload of uint64_t attribute. */
-uint64_t nla_get_u64(struct nlattr *nla)
-{
- uint64_t tmp;
- nla_memcpy(&tmp, nla, sizeof(tmp));
- return tmp;
-}
-
-/* Head of payload */
-void *nla_data(const struct nlattr *nla)
-{
- return (void *) ((char *) nla + NLA_HDRLEN);
-}
-
-/* Return length of the payload . */
-int nla_len(const struct nlattr *nla)
-{
- return nla->nla_len - NLA_HDRLEN;
-}
-
-int nla_padlen(int payload)
-{
- return NLA_ALIGN(payload) - payload;
-}
-
-/* Start a new level of nested attributes. */
-struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
-{
- struct nlattr *start = (struct nlattr *)nlmsg_tail(msg->nm_nlh);
- int rc;
-
- rc = nla_put(msg, attrtype, 0, NULL);
- if (rc < 0)
- return NULL;
-
- return start;
-}
-
-/* Finalize nesting of attributes. */
-int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
-{
- /* Set attribute size */
- start->nla_len = (unsigned char *)nlmsg_tail(nlmsg_hdr(msg)) -
- (unsigned char *)start;
- return 0;
-}
-
-/* Return next attribute in a stream of attributes. */
-struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
-{
- struct nlattr *next_nla = NULL;
- if (nla->nla_len >= sizeof(struct nlattr) &&
- nla->nla_len <= *remaining){
- next_nla = (struct nlattr *) \
- ((char *) nla + NLA_ALIGN(nla->nla_len));
- *remaining = *remaining - NLA_ALIGN(nla->nla_len);
- }
-
- return next_nla;
-
-}
-
-/* Check if the attribute header and payload can be accessed safely. */
-int nla_ok(const struct nlattr *nla, int remaining)
-{
- return remaining > 0 &&
- nla->nla_len >= sizeof(struct nlattr) &&
- sizeof(struct nlattr) <= (unsigned int) remaining &&
- nla->nla_len <= remaining;
-}
-
-/* Create attribute index based on a stream of attributes. */
-/* NOTE: Policy not used ! */
-int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
- int len, struct nla_policy *policy)
-{
- struct nlattr *pos;
- int rem;
-
- /* First clear table */
- memset(tb, 0, (maxtype + 1) * sizeof(struct nlattr *));
-
- nla_for_each_attr(pos, head, len, rem) {
- int type = nla_type(pos);
-
- if ((type <= maxtype) && (type != 0))
- tb[type] = pos;
- }
-
- return 0;
-}
-
-
-/* Create attribute index based on nested attribute. */
-int nla_parse_nested(struct nlattr *tb[], int maxtype,
- struct nlattr *nla, struct nla_policy *policy)
-{
- return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
-}
-
-
-/* Add a unspecific attribute to netlink message. */
-int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
-{
- struct nlattr *nla;
-
- /* Reserve space and init nla header */
- nla = nla_reserve(msg, attrtype, datalen);
- if (nla) {
- memcpy(nla_data(nla), data, datalen);
- return 0;
- }
-
- return -EINVAL;
-}
-
-/* Add 8 bit integer attribute to netlink message. */
-int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
-{
- return nla_put(msg, attrtype, sizeof(uint8_t), &value);
-}
-
-/* Add 16 bit integer attribute to netlink message. */
-int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
-{
- return nla_put(msg, attrtype, sizeof(uint16_t), &value);
-}
-
-/* Add 32 bit integer attribute to netlink message. */
-int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
-{
- return nla_put(msg, attrtype, sizeof(uint32_t), &value);
-}
-
-/* Add 64 bit integer attribute to netlink message. */
-int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
-{
- return nla_put(msg, attrtype, sizeof(uint64_t), &value);
-}
-
-/* Add nested attributes to netlink message. */
-/* Takes the attributes found in the nested message and appends them
- * to the message msg nested in a container of the type attrtype. The
- * nested message may not have a family specific header */
-int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
-{
- int rc;
-
- rc = nla_put(msg, attrtype, nlmsg_attrlen(nlmsg_hdr(nested), 0),
- nlmsg_attrdata(nlmsg_hdr(nested), 0));
- return rc;
-
-}
-
-/* Return type of the attribute. */
-int nla_type(const struct nlattr *nla)
-{
- return (int)nla->nla_type & NLA_TYPE_MASK;
-}
-
-/* Reserves room for an attribute in specified netlink message and fills
- * in the attribute header (type,length). Return NULL if insufficient space */
-struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int data_len)
-{
-
- struct nlattr *nla;
- const unsigned int NEW_SIZE = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) +
- NLA_ALIGN(NLA_HDRLEN + data_len);
-
- /* Check enough space for attribute */
- if (NEW_SIZE > msg->nm_size)
- return NULL;
-
- nla = (struct nlattr *)nlmsg_tail(msg->nm_nlh);
- nla->nla_type = attrtype;
- nla->nla_len = NLA_HDRLEN + data_len;
- memset((unsigned char *)nla + nla->nla_len, 0, nla_padlen(data_len));
- msg->nm_nlh->nlmsg_len = NEW_SIZE;
- return nla;
-}
-
-/* Copy attribute payload to another memory area. */
-int nla_memcpy(void *dest, struct nlattr *src, int count)
-{
- if (!src || !dest)
- return 0;
- if (count > nla_len(src))
- count = nla_len(src);
- memcpy(dest, nla_data(src), count);
- return count;
-}
diff --git a/libnl_2/dbg.c b/libnl_2/dbg.c
deleted file mode 100644
index 9764de6..0000000
--- a/libnl_2/dbg.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "netlink/netlink.h"
-#include <android/log.h>
-
-void libnl_printf(int level, char *format, ...)
-{
- va_list ap;
-
- level = ANDROID_LOG_ERROR;
- va_start(ap, format);
- __android_log_vprint(level, "libnl_2", format, ap);
- va_end(ap);
-}
diff --git a/libnl_2/genl/family.c b/libnl_2/genl/family.c
deleted file mode 100644
index 1beee6e..0000000
--- a/libnl_2/genl/family.c
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include "netlink-types.h"
-
-static struct genl_family *genl_family_find_byname(const char *name)
-{
- return NULL;
-}
-
-/* Release reference and none outstanding */
-void genl_family_put(struct genl_family *family)
-{
- family->ce_refcnt--;
- if (family->ce_refcnt <= 0)
- free(family);
-}
-
-unsigned int genl_family_get_id(struct genl_family *family)
-{
- const int NO_FAMILY_ID = 0;
-
- if (!family)
- return NO_FAMILY_ID;
- else
- return family->gf_id;
-
-}
-
diff --git a/libnl_2/genl/genl.c b/libnl_2/genl/genl.c
deleted file mode 100644
index 1a39c6a..0000000
--- a/libnl_2/genl/genl.c
+++ /dev/null
@@ -1,302 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <errno.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <linux/netlink.h>
-#include <netlink/genl/ctrl.h>
-#include <netlink/genl/family.h>
-#include "netlink-types.h"
-
-/* Get head of attribute data. */
-struct nlattr *genlmsg_attrdata(const struct genlmsghdr *gnlh, int hdrlen)
-{
- return (struct nlattr *) \
- ((char *) gnlh + GENL_HDRLEN + NLMSG_ALIGN(hdrlen));
-
-}
-
-/* Get length of attribute data. */
-int genlmsg_attrlen(const struct genlmsghdr *gnlh, int hdrlen)
-{
- struct nlattr *nla;
- struct nlmsghdr *nlh;
-
- nla = genlmsg_attrdata(gnlh, hdrlen);
- nlh = (struct nlmsghdr *) ((char *) gnlh - NLMSG_HDRLEN);
- return (char *) nlmsg_tail(nlh) - (char *) nla;
-}
-
-/* Add generic netlink header to netlink message. */
-void *genlmsg_put(struct nl_msg *msg, uint32_t pid, uint32_t seq, int family,
- int hdrlen, int flags, uint8_t cmd, uint8_t version)
-{
- int new_size;
- struct nlmsghdr *nlh;
- struct timeval tv;
- struct genlmsghdr *gmh;
-
- /* Make sure nl_msg has enough space */
- new_size = NLMSG_HDRLEN + GENL_HDRLEN + hdrlen;
- if ((sizeof(struct nl_msg) + new_size) > msg->nm_size)
- goto fail;
-
- /* Fill in netlink header */
- nlh = msg->nm_nlh;
- nlh->nlmsg_len = new_size;
- nlh->nlmsg_type = family;
- nlh->nlmsg_pid = getpid();
- nlh->nlmsg_flags = flags | NLM_F_REQUEST | NLM_F_ACK;
-
- /* Get current time for sequence number */
- if (gettimeofday(&tv, NULL))
- nlh->nlmsg_seq = 1;
- else
- nlh->nlmsg_seq = (int) tv.tv_sec;
-
- /* Setup genlmsghdr in new message */
- gmh = (struct genlmsghdr *) ((char *)nlh + NLMSG_HDRLEN);
- gmh->cmd = (__u8) cmd;
- gmh->version = version;
-
- return gmh;
-fail:
- return NULL;
-
-}
-
-/* Socket has already been alloced to connect it to kernel? */
-int genl_connect(struct nl_sock *sk)
-{
- return nl_connect(sk, NETLINK_GENERIC);
-
-}
-
-int genl_ctrl_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
-{
- int rc = -1;
- int nl80211_genl_id = -1;
- char sendbuf[sizeof(struct nlmsghdr)+sizeof(struct genlmsghdr)];
- struct nlmsghdr nlmhdr;
- struct genlmsghdr gmhhdr;
- struct iovec sendmsg_iov;
- struct msghdr msg;
- int num_char;
- const int RECV_BUF_SIZE = getpagesize();
- char *recvbuf;
- struct iovec recvmsg_iov;
- int nl80211_flag = 0, nlm_f_multi = 0, nlmsg_done = 0;
- struct nlmsghdr *nlh;
-
- /* REQUEST GENERIC NETLINK FAMILY ID */
- /* Message buffer */
- nlmhdr.nlmsg_len = sizeof(sendbuf);
- nlmhdr.nlmsg_type = NETLINK_GENERIC;
- nlmhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP;
- nlmhdr.nlmsg_seq = sock->s_seq_next;
- nlmhdr.nlmsg_pid = sock->s_local.nl_pid;
-
- /* Generic netlink header */
- memset(&gmhhdr, 0, sizeof(gmhhdr));
- gmhhdr.cmd = CTRL_CMD_GETFAMILY;
- gmhhdr.version = CTRL_ATTR_FAMILY_ID;
-
- /* Combine netlink and generic netlink headers */
- memcpy(&sendbuf[0], &nlmhdr, sizeof(nlmhdr));
- memcpy(&sendbuf[0]+sizeof(nlmhdr), &gmhhdr, sizeof(gmhhdr));
-
- /* Create IO vector with Netlink message */
- sendmsg_iov.iov_base = &sendbuf;
- sendmsg_iov.iov_len = sizeof(sendbuf);
-
- /* Socket message */
- msg.msg_name = (void *) &sock->s_peer;
- msg.msg_namelen = sizeof(sock->s_peer);
- msg.msg_iov = &sendmsg_iov;
- msg.msg_iovlen = 1; /* Only sending one iov */
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = 0;
-
- /* Send message and verify sent */
- num_char = sendmsg(sock->s_fd, &msg, 0);
- if (num_char == -1)
- return -errno;
-
- /* RECEIVE GENL CMD RESPONSE */
-
- /* Create receive iov buffer */
- recvbuf = (char *) malloc(RECV_BUF_SIZE);
-
- /* Attach to iov */
- recvmsg_iov.iov_base = recvbuf;
- recvmsg_iov.iov_len = RECV_BUF_SIZE;
-
- msg.msg_iov = &recvmsg_iov;
- msg.msg_iovlen = 1;
-
- /***************************************************************/
- /* Receive message. If multipart message, keep receiving until */
- /* message type is NLMSG_DONE */
- /***************************************************************/
-
- do {
-
- int recvmsg_len, nlmsg_rem;
-
- /* Receive message */
- memset(recvbuf, 0, RECV_BUF_SIZE);
- recvmsg_len = recvmsg(sock->s_fd, &msg, 0);
-
- /* Make sure receive successful */
- if (recvmsg_len < 0) {
- rc = -errno;
- goto error_recvbuf;
- }
-
- /* Parse nlmsghdr */
- nlmsg_for_each_msg(nlh, (struct nlmsghdr *) recvbuf, \
- recvmsg_len, nlmsg_rem) {
- struct nlattr *nla;
- int nla_rem;
-
- /* Check type */
- switch (nlh->nlmsg_type) {
- case NLMSG_DONE:
- goto return_genl_id;
- break;
- case NLMSG_ERROR:
-
- /* Should check nlmsgerr struct received */
- fprintf(stderr, "Receive message error\n");
- goto error_recvbuf;
- case NLMSG_OVERRUN:
- fprintf(stderr, "Receive data partly lost\n");
- goto error_recvbuf;
- case NLMSG_MIN_TYPE:
- case NLMSG_NOOP:
- break;
- default:
- break;
- }
-
-
-
- /* Check flags */
- if (nlh->nlmsg_flags & NLM_F_MULTI)
- nlm_f_multi = 1;
- else
- nlm_f_multi = 0;
-
- if (nlh->nlmsg_type & NLMSG_DONE)
- nlmsg_done = 1;
- else
- nlmsg_done = 0;
-
- /* Iteratve over attributes */
- nla_for_each_attr(nla,
- nlmsg_attrdata(nlh, GENL_HDRLEN),
- nlmsg_attrlen(nlh, GENL_HDRLEN),
- nla_rem){
-
- /* If this family is nl80211 */
- if (nla->nla_type == CTRL_ATTR_FAMILY_NAME &&
- !strcmp((char *)nla_data(nla),
- "nl80211"))
- nl80211_flag = 1;
-
- /* Save the family id */
- else if (nl80211_flag &&
- nla->nla_type == CTRL_ATTR_FAMILY_ID) {
- nl80211_genl_id =
- *((int *)nla_data(nla));
- nl80211_flag = 0;
- }
-
- }
-
- }
-
- } while (nlm_f_multi && !nlmsg_done);
-
-return_genl_id:
- /* Return family id as cache pointer */
- *result = (struct nl_cache *) nl80211_genl_id;
- rc = 0;
-error_recvbuf:
- free(recvbuf);
-error:
- return rc;
-}
-
-/* Checks the netlink cache to find family reference by name string */
-/* NOTE: Caller needs to call genl_family_put() when done with *
- * returned object */
-struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache, \
- const char *name)
-{
- struct genl_family *gf = (struct genl_family *) \
- malloc(sizeof(struct genl_family));
- if (!gf)
- goto fail;
- memset(gf, 0, sizeof(*gf));
-
- /* Add ref */
- gf->ce_refcnt++;
-
- /* Overriding cache pointer as family id for now */
- gf->gf_id = (uint16_t) ((uint32_t) cache);
- strncpy(gf->gf_name, name, GENL_NAMSIZ);
-
- return gf;
-fail:
- return NULL;
-
-}
-
-int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
-{
- struct nl_cache *cache = NULL;
- struct genl_family *gf = NULL;
- int id = -1;
-
- /* Hack to support wpa_supplicant */
- if (strcmp(name, "nlctrl") == 0)
- return NETLINK_GENERIC;
-
- if (strcmp(name, "nl80211") != 0) {
- fprintf(stderr, "%s is not supported\n", name);
- return id;
- }
-
- if (!genl_ctrl_alloc_cache(sk, &cache)) {
- gf = genl_ctrl_search_by_name(cache, name);
- if (gf)
- id = genl_family_get_id(gf);
- }
-
- if (gf)
- genl_family_put(gf);
- if (cache)
- nl_cache_free(cache);
-
- return id;
-}
diff --git a/libnl_2/handlers.c b/libnl_2/handlers.c
deleted file mode 100644
index 48dcab4..0000000
--- a/libnl_2/handlers.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <malloc.h>
-#include "netlink-types.h"
-#include "netlink/handlers.h"
-
-/* Allocate a new callback handle. */
-struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
-{
- struct nl_cb *cb;
-
- cb = (struct nl_cb *) malloc(sizeof(struct nl_cb));
- if (cb == NULL)
- goto fail;
- memset(cb, 0, sizeof(*cb));
-
- return nl_cb_get(cb);
-fail:
- return NULL;
-}
-
-/* Clone an existing callback handle */
-struct nl_cb *nl_cb_clone(struct nl_cb *orig)
-{
- struct nl_cb *new_cb;
-
- new_cb = nl_cb_alloc(NL_CB_DEFAULT);
- if (new_cb == NULL)
- goto fail;
-
- /* Copy original and set refcount to 1 */
- memcpy(new_cb, orig, sizeof(*orig));
- new_cb->cb_refcnt = 1;
-
- return new_cb;
-fail:
- return NULL;
-}
-
-/* Set up a callback. */
-int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, \
- nl_recvmsg_msg_cb_t func, void *arg)
-{
- cb->cb_set[type] = func;
- cb->cb_args[type] = arg;
- return 0;
-}
-
-
-
-/* Set up an error callback. */
-int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, \
- nl_recvmsg_err_cb_t func, void *arg)
-{
- cb->cb_err = func;
- cb->cb_err_arg = arg;
- return 0;
-
-}
-
-struct nl_cb *nl_cb_get(struct nl_cb *cb)
-{
- cb->cb_refcnt++;
- return cb;
-}
-
-void nl_cb_put(struct nl_cb *cb)
-{
- if (!cb)
- return;
- cb->cb_refcnt--;
- if (cb->cb_refcnt <= 0)
- free(cb);
-}
diff --git a/libnl_2/msg.c b/libnl_2/msg.c
deleted file mode 100644
index 1303e8a..0000000
--- a/libnl_2/msg.c
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <malloc.h>
-#include <unistd.h>
-#include <sys/socket.h>
-#include <linux/netlink.h>
-#include "netlink-types.h"
-
-/* Allocate a new netlink message with the default maximum payload size. */
-struct nl_msg *nlmsg_alloc(void)
-{
- /* Whole page will store nl_msg + nlmsghdr + genlmsghdr + payload */
- const int page_sz = getpagesize();
- struct nl_msg *nm;
- struct nlmsghdr *nlh;
-
- /* Netlink message */
- nm = (struct nl_msg *) malloc(page_sz);
- if (!nm)
- goto fail;
-
- /* Netlink message header pointer */
- nlh = (struct nlmsghdr *) ((char *) nm + sizeof(struct nl_msg));
-
- /* Initialize */
- memset(nm, 0, page_sz);
- nm->nm_size = page_sz;
-
- nm->nm_src.nl_family = AF_NETLINK;
- nm->nm_src.nl_pid = getpid();
-
- nm->nm_dst.nl_family = AF_NETLINK;
- nm->nm_dst.nl_pid = 0; /* Kernel */
-
- /* Initialize and add to netlink message */
- nlh->nlmsg_len = NLMSG_HDRLEN;
- nm->nm_nlh = nlh;
-
- /* Add to reference count and return nl_msg */
- nlmsg_get(nm);
- return nm;
-fail:
- return NULL;
-}
-
-/* Return pointer to message payload. */
-void *nlmsg_data(const struct nlmsghdr *nlh)
-{
- return (char *) nlh + NLMSG_HDRLEN;
-}
-
-/* Add reference count to nl_msg */
-void nlmsg_get(struct nl_msg *nm)
-{
- nm->nm_refcnt++;
-}
-
-/* Release a reference from an netlink message. */
-void nlmsg_free(struct nl_msg *nm)
-{
- if (nm) {
- nm->nm_refcnt--;
- if (nm->nm_refcnt <= 0)
- free(nm);
- }
-
-}
-
-/* Return actual netlink message. */
-struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
-{
- return n->nm_nlh;
-}
-
-/* Return head of attributes data / payload section */
-struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
-{
- unsigned char *data = nlmsg_data(nlh);
- return (struct nlattr *)(data + NLMSG_ALIGN(hdrlen));
-}
-
-/* Returns pointer to end of netlink message */
-void *nlmsg_tail(const struct nlmsghdr *nlh)
-{
- return (void *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len));
-}
-
-/* Next netlink message in message stream */
-struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
-{
- struct nlmsghdr *next_nlh = NULL;
- int len = nlmsg_len(nlh);
-
- len = NLMSG_ALIGN(len);
- if (*remaining > 0 &&
- len <= *remaining &&
- len >= (int) sizeof(struct nlmsghdr)) {
- next_nlh = (struct nlmsghdr *)((char *)nlh + len);
- *remaining -= len;
- }
-
- return next_nlh;
-}
-
-int nlmsg_datalen(const struct nlmsghdr *nlh)
-{
- return nlh->nlmsg_len - NLMSG_HDRLEN;
-}
-
-/* Length of attributes data */
-int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
-{
- return nlmsg_datalen(nlh) - NLMSG_ALIGN(hdrlen);
-}
-
-/* Length of netlink message */
-int nlmsg_len(const struct nlmsghdr *nlh)
-{
- return nlh->nlmsg_len;
-}
-
-/* Check if the netlink message fits into the remaining bytes */
-int nlmsg_ok(const struct nlmsghdr *nlh, int rem)
-{
- return rem >= (int)sizeof(struct nlmsghdr) &&
- rem >= nlmsg_len(nlh) &&
- nlmsg_len(nlh) >= (int) sizeof(struct nlmsghdr) &&
- nlmsg_len(nlh) <= (rem);
-}
-
-int nlmsg_padlen(int payload)
-{
- return NLMSG_ALIGN(payload) - payload;
-}
diff --git a/libnl_2/netlink.c b/libnl_2/netlink.c
deleted file mode 100644
index ee3d600..0000000
--- a/libnl_2/netlink.c
+++ /dev/null
@@ -1,273 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include "netlink-types.h"
-
-#define NL_BUFFER_SZ (32768U)
-
-/* Checks message for completeness and sends it out */
-int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
-{
- struct nlmsghdr *nlh = msg->nm_nlh;
- struct timeval tv;
-
- if (!nlh) {
- int errsv = errno;
- fprintf(stderr, "Netlink message header is NULL!\n");
- return -errsv;
- }
-
- /* Complete the nl_msg header */
- if (gettimeofday(&tv, NULL))
- nlh->nlmsg_seq = 1;
- else
- nlh->nlmsg_seq = (int) tv.tv_sec;
- nlh->nlmsg_pid = sk->s_local.nl_pid;
- nlh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK;
-
- return nl_send(sk, msg);
-}
-
-/* Receives a netlink message, allocates a buffer in *buf and stores
- * the message content. The peer's netlink address is stored in
- * *nla. The caller is responsible for freeing the buffer allocated in
- * *buf if a positive value is returned. Interrupted system calls are
- * handled by repeating the read. The input buffer size is determined
- * by peeking before the actual read is done */
-int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, \
- unsigned char **buf, struct ucred **creds)
-{
- int rc = -1;
- int sk_flags;
- int RECV_BUF_SIZE = getpagesize();
- int errsv;
- struct iovec recvmsg_iov;
- struct msghdr msg;
-
- /* Allocate buffer */
- *buf = (unsigned char *) malloc(RECV_BUF_SIZE);
- if (!(*buf)) {
- rc = -ENOMEM;
- goto fail;
- }
-
- /* Prepare to receive message */
- recvmsg_iov.iov_base = *buf;
- recvmsg_iov.iov_len = RECV_BUF_SIZE;
-
- msg.msg_name = (void *) &sk->s_peer;
- msg.msg_namelen = sizeof(sk->s_peer);
- msg.msg_iov = &recvmsg_iov;
- msg.msg_iovlen = 1;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = 0;
-
- /* Make non blocking and then restore previous setting */
- sk_flags = fcntl(sk->s_fd, F_GETFL, 0);
- fcntl(sk->s_fd, F_SETFL, O_NONBLOCK);
- rc = recvmsg(sk->s_fd, &msg, 0);
- errsv = errno;
- fcntl(sk->s_fd, F_SETFL, sk_flags);
-
- if (rc < 0) {
- rc = -errsv;
- free(*buf);
- *buf = NULL;
- }
-
-fail:
- return rc;
-}
-
-/* Receive a set of messages from a netlink socket */
-/* NOTE: Does not currently support callback replacements!!! */
-int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
-{
- struct sockaddr_nl nla;
- struct ucred *creds;
-
- int rc, cb_rc = NL_OK, done = 0;
-
- do {
- unsigned char *buf;
- int i, rem, flags;
- struct nlmsghdr *nlh;
- struct nlmsgerr *nlme;
- struct nl_msg *msg;
-
- done = 0;
- rc = nl_recv(sk, &nla, &buf, &creds);
- if (rc < 0)
- break;
-
- nlmsg_for_each_msg(nlh, (struct nlmsghdr *) buf, rc, rem) {
-
- if (rc <= 0 || cb_rc == NL_STOP)
- break;
-
- /* Check for callbacks */
-
- msg = (struct nl_msg *) malloc(sizeof(struct nl_msg));
- memset(msg, 0, sizeof(*msg));
- msg->nm_nlh = nlh;
-
- /* Check netlink message type */
-
- switch (msg->nm_nlh->nlmsg_type) {
- case NLMSG_ERROR: /* Used for ACK too */
- /* Certainly we should be doing some
- * checking here to make sure this
- * message is intended for us */
- nlme = nlmsg_data(msg->nm_nlh);
- if (nlme->error == 0)
- msg->nm_nlh->nlmsg_flags |= NLM_F_ACK;
-
- rc = nlme->error;
- cb_rc = cb->cb_err(&nla, nlme, cb->cb_err_arg);
- nlme = NULL;
- break;
-
- case NLMSG_DONE:
- done = 1;
-
- case NLMSG_OVERRUN:
- case NLMSG_NOOP:
- default:
- break;
- };
-
- for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
-
- if (cb->cb_set[i]) {
- switch (i) {
- case NL_CB_VALID:
- if (rc > 0)
- cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
- break;
-
- case NL_CB_FINISH:
- if ((msg->nm_nlh->nlmsg_flags & NLM_F_MULTI) &&
- (msg->nm_nlh->nlmsg_type & NLMSG_DONE))
- cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
-
- break;
-
- case NL_CB_ACK:
- if (msg->nm_nlh->nlmsg_flags & NLM_F_ACK)
- cb_rc = cb->cb_set[i](msg, cb->cb_args[i]);
-
- break;
- default:
- break;
- }
- }
- }
-
- free(msg);
- if (done)
- break;
- }
- free(buf);
- buf = NULL;
-
- if (done)
- break;
- } while (rc > 0 && cb_rc != NL_STOP);
-
-success:
-fail:
- return rc;
-}
-
-/* Send raw data over netlink socket */
-int nl_send(struct nl_sock *sk, struct nl_msg *msg)
-{
- struct nlmsghdr *nlh = nlmsg_hdr(msg);
- struct iovec msg_iov;
-
- /* Create IO vector with Netlink message */
- msg_iov.iov_base = nlh;
- msg_iov.iov_len = nlh->nlmsg_len;
-
- return nl_send_iovec(sk, msg, &msg_iov, 1);
-}
-
-/* Send netlink message */
-int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg,
- struct iovec *iov, unsigned iovlen)
-{
- int rc;
-
- /* Socket message */
- struct msghdr mh = {
- .msg_name = (void *) &sk->s_peer,
- .msg_namelen = sizeof(sk->s_peer),
- .msg_iov = iov,
- .msg_iovlen = iovlen,
- .msg_control = NULL,
- .msg_controllen = 0,
- .msg_flags = 0
- };
-
- /* Send message and verify sent */
- rc = nl_sendmsg(sk, (struct nl_msg *) &mh, 0);
- if (rc < 0)
- fprintf(stderr, "Error sending netlink message: %d\n", errno);
- return rc;
-
-}
-
-/* Send netlink message with control over sendmsg() message header */
-int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
-{
- return sendmsg(sk->s_fd, (struct msghdr *) msg, (int) hdr);
-}
-
-/* Create and connect netlink socket */
-int nl_connect(struct nl_sock *sk, int protocol)
-{
- struct sockaddr addr;
- socklen_t addrlen;
- int rc;
-
- /* Create RX socket */
- sk->s_fd = socket(PF_NETLINK, SOCK_RAW, protocol);
- if (sk->s_fd < 0)
- return -errno;
-
- /* Set size of RX and TX buffers */
- if (nl_socket_set_buffer_size(sk, NL_BUFFER_SZ, NL_BUFFER_SZ) < 0)
- return -errno;
-
- /* Bind RX socket */
- rc = bind(sk->s_fd, (struct sockaddr *)&sk->s_local, \
- sizeof(sk->s_local));
- if (rc < 0)
- return -errno;
- addrlen = sizeof(addr);
- getsockname(sk->s_fd, &addr, &addrlen);
-
- return 0;
-
-}
diff --git a/libnl_2/object.c b/libnl_2/object.c
deleted file mode 100644
index c53accf..0000000
--- a/libnl_2/object.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include "netlink-types.h"
-
-void nl_object_put(struct nl_object *obj)
-{
- obj->ce_refcnt--;
- if (!obj->ce_refcnt)
- nl_object_free(obj);
-}
-
-void nl_object_free(struct nl_object *obj)
-{
- nl_cache_remove(obj);
-}
-
-
diff --git a/libnl_2/socket.c b/libnl_2/socket.c
deleted file mode 100644
index f51cf56..0000000
--- a/libnl_2/socket.c
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * 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.
- */
-
-/* NOTICE: This is a clean room re-implementation of libnl */
-
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <malloc.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include "netlink-types.h"
-
-/* Join group */
-int nl_socket_add_membership(struct nl_sock *sk, int group)
-{
- return setsockopt(sk->s_fd, SOL_NETLINK,
- NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
-}
-
-/* Allocate new netlink socket. */
-static struct nl_sock *_nl_socket_alloc(void)
-{
- struct nl_sock *sk;
- struct timeval tv;
- struct nl_cb *cb;
-
- sk = (struct nl_sock *) malloc(sizeof(struct nl_sock));
- if (!sk)
- return NULL;
- memset(sk, 0, sizeof(*sk));
-
- /* Get current time */
-
- if (gettimeofday(&tv, NULL))
- goto fail;
- else
- sk->s_seq_next = (int) tv.tv_sec;
-
- /* Create local socket */
- sk->s_local.nl_family = AF_NETLINK;
- sk->s_local.nl_pid = 0; /* Kernel fills in pid */
- sk->s_local.nl_groups = 0; /* No groups */
-
- /* Create peer socket */
- sk->s_peer.nl_family = AF_NETLINK;
- sk->s_peer.nl_pid = 0; /* Kernel */
- sk->s_peer.nl_groups = 0; /* No groups */
-
- return sk;
-fail:
- free(sk);
- return NULL;
-}
-
-/* Allocate new netlink socket. */
-struct nl_sock *nl_socket_alloc(void)
-{
- struct nl_sock *sk = _nl_socket_alloc();
- struct nl_cb *cb;
-
- if (!sk)
- return NULL;
-
- cb = nl_cb_alloc(NL_CB_DEFAULT);
- if (!cb)
- goto cb_fail;
- sk->s_cb = cb;
- return sk;
-cb_fail:
- free(sk);
- return NULL;
-}
-
-/* Allocate new socket with custom callbacks. */
-struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
-{
- struct nl_sock *sk = _nl_socket_alloc();
-
- if (!sk)
- return NULL;
-
- sk->s_cb = cb;
- nl_cb_get(cb);
-
- return sk;
-}
-
-/* Free a netlink socket. */
-void nl_socket_free(struct nl_sock *sk)
-{
- nl_cb_put(sk->s_cb);
- close(sk->s_fd);
- free(sk);
-}
-
-/* Sets socket buffer size of netlink socket */
-int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
-{
- if (setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF, \
- &rxbuf, (socklen_t) sizeof(rxbuf)))
- goto error;
-
- if (setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF, \
- &txbuf, (socklen_t) sizeof(txbuf)))
- goto error;
-
- return 0;
-error:
- return -errno;
-
-}
-
-int nl_socket_get_fd(struct nl_sock *sk)
-{
- return sk->s_fd;
-}
-
-void nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb)
-{
- nl_cb_put(sk->s_cb);
- sk->s_cb = cb;
- nl_cb_get(cb);
-}
-
-struct nl_cb *nl_socket_get_cb(struct nl_sock *sk)
-{
- return nl_cb_get(sk->s_cb);
-}
-
-int nl_socket_set_nonblocking(struct nl_sock *sk)
-{
- if (sk->s_fd == -1)
- return -NLE_BAD_SOCK;
-
- if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0)
- return -errno;
-
- return 0;
-}
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index c24384c..697db25 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -34,8 +34,9 @@
col32cb16blend.S \
t32cb16blend.S \
-ifeq ($(ARCH_ARM_HAVE_NEON),armv7-a)
+ifeq ($(ARCH_ARM_HAVE_NEON),true)
PIXELFLINGER_SRC_FILES_arm += col32cb16blend_neon.S
+PIXELFLINGER_CFLAGS_arm += -D__ARM_HAVE_NEON
endif
PIXELFLINGER_SRC_FILES_arm64 := \
@@ -44,11 +45,13 @@
arch-arm64/col32cb16blend.S \
arch-arm64/t32cb16blend.S \
+ifndef ARCH_MIPS_REV6
PIXELFLINGER_SRC_FILES_mips := \
codeflinger/MIPSAssembler.cpp \
codeflinger/mips_disassem.c \
arch-mips/t32cb16blend.S \
+endif
#
# Shared library
#
@@ -59,28 +62,18 @@
LOCAL_SRC_FILES_arm64 := $(PIXELFLINGER_SRC_FILES_arm64)
LOCAL_SRC_FILES_mips := $(PIXELFLINGER_SRC_FILES_mips)
LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_C_INCLUDES += $(LOCAL_EXPORT_C_INCLUDE_DIRS)
LOCAL_SHARED_LIBRARIES := libcutils liblog
-ifneq ($(BUILD_TINY_ANDROID),true)
# Really this should go away entirely or at least not depend on
# libhardware, but this at least gets us built.
LOCAL_SHARED_LIBRARIES += libhardware_legacy
LOCAL_CFLAGS += -DWITH_LIB_HARDWARE
-endif
+# t32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+# arch-arm64/col32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
include $(BUILD_SHARED_LIBRARY)
-#
-# Static library version
-#
-
-include $(CLEAR_VARS)
-LOCAL_MODULE:= libpixelflinger_static
-LOCAL_SRC_FILES := $(PIXELFLINGER_SRC_FILES)
-LOCAL_SRC_FILES_arm := $(PIXELFLINGER_SRC_FILES_arm)
-LOCAL_SRC_FILES_arm64 := $(PIXELFLINGER_SRC_FILES_arm64)
-LOCAL_SRC_FILES_mips := $(PIXELFLINGER_SRC_FILES_mips)
-LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
-include $(BUILD_STATIC_LIBRARY)
-
-
include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/libpixelflinger/buffer.cpp b/libpixelflinger/buffer.cpp
index cbdab5a..dcb95c5 100644
--- a/libpixelflinger/buffer.cpp
+++ b/libpixelflinger/buffer.cpp
@@ -130,7 +130,7 @@
}
}
-void readRGB565(const surface_t* s, context_t* c,
+void readRGB565(const surface_t* s, context_t* /*c*/,
uint32_t x, uint32_t y, pixel_t* pixel)
{
uint16_t v = *(reinterpret_cast<uint16_t*>(s->data) + (x + (s->stride * y)));
@@ -144,7 +144,7 @@
pixel->s[3] = 5;
}
-void readABGR8888(const surface_t* s, context_t* c,
+void readABGR8888(const surface_t* s, context_t* /*c*/,
uint32_t x, uint32_t y, pixel_t* pixel)
{
uint32_t v = *(reinterpret_cast<uint32_t*>(s->data) + (x + (s->stride * y)));
diff --git a/libpixelflinger/codeflinger/Arm64Assembler.cpp b/libpixelflinger/codeflinger/Arm64Assembler.cpp
index f37072a..bd11818 100644
--- a/libpixelflinger/codeflinger/Arm64Assembler.cpp
+++ b/libpixelflinger/codeflinger/Arm64Assembler.cpp
@@ -273,7 +273,7 @@
*mPC++ = (0x54 << 24) | cc;
}
-void ArmToArm64Assembler::BL(int cc, const char* label)
+void ArmToArm64Assembler::BL(int /*cc*/, const char* /*label*/)
{
NOT_IMPLEMENTED(); //Not Required
}
@@ -289,7 +289,7 @@
*mPC++ = A64_MOVZ_X(mZeroReg,0,0);
}
-void ArmToArm64Assembler::epilog(uint32_t touched)
+void ArmToArm64Assembler::epilog(uint32_t /*touched*/)
{
// write epilog code
static const int XLR = 30;
@@ -530,23 +530,23 @@
if(s != 0) { NOT_IMPLEMENTED(); return;} //Not required
*mPC++ = A64_MADD_W(Rd, Rm, Rs, mZeroReg);
}
-void ArmToArm64Assembler::UMULL(int cc, int s,
- int RdLo, int RdHi, int Rm, int Rs)
+void ArmToArm64Assembler::UMULL(int /*cc*/, int /*s*/,
+ int /*RdLo*/, int /*RdHi*/, int /*Rm*/, int /*Rs*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::UMUAL(int cc, int s,
- int RdLo, int RdHi, int Rm, int Rs)
+void ArmToArm64Assembler::UMUAL(int /*cc*/, int /*s*/,
+ int /*RdLo*/, int /*RdHi*/, int /*Rm*/, int /*Rs*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::SMULL(int cc, int s,
- int RdLo, int RdHi, int Rm, int Rs)
+void ArmToArm64Assembler::SMULL(int /*cc*/, int /*s*/,
+ int /*RdLo*/, int /*RdHi*/, int /*Rm*/, int /*Rs*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::SMUAL(int cc, int s,
- int RdLo, int RdHi, int Rm, int Rs)
+void ArmToArm64Assembler::SMUAL(int /*cc*/, int /*s*/,
+ int /*RdLo*/, int /*RdHi*/, int /*Rm*/, int /*Rs*/)
{
NOT_IMPLEMENTED(); //Not required
}
@@ -554,15 +554,15 @@
// ----------------------------------------------------------------------------
// branches relative to PC...
// ----------------------------------------------------------------------------
-void ArmToArm64Assembler::B(int cc, uint32_t* pc){
+void ArmToArm64Assembler::B(int /*cc*/, uint32_t* /*pc*/){
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::BL(int cc, uint32_t* pc){
+void ArmToArm64Assembler::BL(int /*cc*/, uint32_t* /*pc*/){
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::BX(int cc, int Rn){
+void ArmToArm64Assembler::BX(int /*cc*/, int /*Rn*/){
NOT_IMPLEMENTED(); //Not required
}
@@ -661,11 +661,11 @@
{
return dataTransfer(opLDRH, cc, Rd, Rn, op_type);
}
-void ArmToArm64Assembler::LDRSB(int cc, int Rd, int Rn, uint32_t offset)
+void ArmToArm64Assembler::LDRSB(int /*cc*/, int /*Rd*/, int /*Rn*/, uint32_t /*offset*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::LDRSH(int cc, int Rd, int Rn, uint32_t offset)
+void ArmToArm64Assembler::LDRSH(int /*cc*/, int /*Rd*/, int /*Rn*/, uint32_t /*offset*/)
{
NOT_IMPLEMENTED(); //Not required
}
@@ -723,15 +723,15 @@
// ----------------------------------------------------------------------------
// special...
// ----------------------------------------------------------------------------
-void ArmToArm64Assembler::SWP(int cc, int Rn, int Rd, int Rm)
+void ArmToArm64Assembler::SWP(int /*cc*/, int /*Rn*/, int /*Rd*/, int /*Rm*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::SWPB(int cc, int Rn, int Rd, int Rm)
+void ArmToArm64Assembler::SWPB(int /*cc*/, int /*Rn*/, int /*Rd*/, int /*Rm*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::SWI(int cc, uint32_t comment)
+void ArmToArm64Assembler::SWI(int /*cc*/, uint32_t /*comment*/)
{
NOT_IMPLEMENTED(); //Not required
}
@@ -739,31 +739,31 @@
// ----------------------------------------------------------------------------
// DSP instructions...
// ----------------------------------------------------------------------------
-void ArmToArm64Assembler::PLD(int Rn, uint32_t offset) {
+void ArmToArm64Assembler::PLD(int /*Rn*/, uint32_t /*offset*/) {
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::CLZ(int cc, int Rd, int Rm)
+void ArmToArm64Assembler::CLZ(int /*cc*/, int /*Rd*/, int /*Rm*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::QADD(int cc, int Rd, int Rm, int Rn)
+void ArmToArm64Assembler::QADD(int /*cc*/, int /*Rd*/, int /*Rm*/, int /*Rn*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::QDADD(int cc, int Rd, int Rm, int Rn)
+void ArmToArm64Assembler::QDADD(int /*cc*/, int /*Rd*/, int /*Rm*/, int /*Rn*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::QSUB(int cc, int Rd, int Rm, int Rn)
+void ArmToArm64Assembler::QSUB(int /*cc*/, int /*Rd*/, int /*Rm*/, int /*Rn*/)
{
NOT_IMPLEMENTED(); //Not required
}
-void ArmToArm64Assembler::QDSUB(int cc, int Rd, int Rm, int Rn)
+void ArmToArm64Assembler::QDSUB(int /*cc*/, int /*Rd*/, int /*Rm*/, int /*Rn*/)
{
NOT_IMPLEMENTED(); //Not required
}
@@ -817,15 +817,15 @@
*mPC++ = A64_MADD_W(Rd, mTmpReg1, mTmpReg2, Rn);
}
-void ArmToArm64Assembler::SMLAL(int cc, int xy,
- int RdHi, int RdLo, int Rs, int Rm)
+void ArmToArm64Assembler::SMLAL(int /*cc*/, int /*xy*/,
+ int /*RdHi*/, int /*RdLo*/, int /*Rs*/, int /*Rm*/)
{
NOT_IMPLEMENTED(); //Not required
return;
}
-void ArmToArm64Assembler::SMLAW(int cc, int y,
- int Rd, int Rm, int Rs, int Rn)
+void ArmToArm64Assembler::SMLAW(int /*cc*/, int /*y*/,
+ int /*Rd*/, int /*Rm*/, int /*Rs*/, int /*Rn*/)
{
NOT_IMPLEMENTED(); //Not required
return;
@@ -890,13 +890,13 @@
return OPERAND_REG_IMM;
}
-uint32_t ArmToArm64Assembler::reg_rrx(int Rm)
+uint32_t ArmToArm64Assembler::reg_rrx(int /*Rm*/)
{
NOT_IMPLEMENTED();
return OPERAND_UNSUPPORTED;
}
-uint32_t ArmToArm64Assembler::reg_reg(int Rm, int type, int Rs)
+uint32_t ArmToArm64Assembler::reg_reg(int /*Rm*/, int /*type*/, int /*Rs*/)
{
NOT_IMPLEMENTED(); //Not required
return OPERAND_UNSUPPORTED;
@@ -937,7 +937,7 @@
}
}
-uint32_t ArmToArm64Assembler::reg_scale_post(int Rm, int type, uint32_t shift)
+uint32_t ArmToArm64Assembler::reg_scale_post(int /*Rm*/, int /*type*/, uint32_t /*shift*/)
{
NOT_IMPLEMENTED(); //Not required
return OPERAND_UNSUPPORTED;
@@ -975,7 +975,7 @@
}
}
-uint32_t ArmToArm64Assembler::reg_post(int Rm)
+uint32_t ArmToArm64Assembler::reg_post(int /*Rm*/)
{
NOT_IMPLEMENTED(); //Not required
return OPERAND_UNSUPPORTED;
diff --git a/libpixelflinger/codeflinger/CodeCache.cpp b/libpixelflinger/codeflinger/CodeCache.cpp
index 7446da2..d770302 100644
--- a/libpixelflinger/codeflinger/CodeCache.cpp
+++ b/libpixelflinger/codeflinger/CodeCache.cpp
@@ -89,7 +89,7 @@
gExecutableStore = mmap(NULL, kMaxCodeCacheCapacity,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE, fd, 0);
- LOG_ALWAYS_FATAL_IF(gExecutableStore == NULL,
+ LOG_ALWAYS_FATAL_IF(gExecutableStore == MAP_FAILED,
"Creating code cache, mmap failed with error "
"'%s'", strerror(errno));
close(fd);
@@ -201,9 +201,9 @@
mCacheInUse += assemblySize;
mWhen++;
// synchronize caches...
- const long base = long(assembly->base());
- const long curr = base + long(assembly->size());
- __builtin___clear_cache((void*)base, (void*)curr);
+ char* base = reinterpret_cast<char*>(assembly->base());
+ char* curr = reinterpret_cast<char*>(base + assembly->size());
+ __builtin___clear_cache(base, curr);
}
pthread_mutex_unlock(&mLock);
diff --git a/libpixelflinger/codeflinger/GGLAssembler.cpp b/libpixelflinger/codeflinger/GGLAssembler.cpp
index 2422d7b..325caba 100644
--- a/libpixelflinger/codeflinger/GGLAssembler.cpp
+++ b/libpixelflinger/codeflinger/GGLAssembler.cpp
@@ -694,7 +694,7 @@
// ---------------------------------------------------------------------------
void GGLAssembler::build_alpha_test(component_t& fragment,
- const fragment_parts_t& parts)
+ const fragment_parts_t& /*parts*/)
{
if (mAlphaTest != GGL_ALWAYS) {
comment("Alpha Test");
@@ -796,7 +796,7 @@
}
}
-void GGLAssembler::build_iterate_f(const fragment_parts_t& parts)
+void GGLAssembler::build_iterate_f(const fragment_parts_t& /*parts*/)
{
const needs_t& needs = mBuilderContext.needs;
if (GGL_READ_NEEDS(P_FOG, needs.p)) {
diff --git a/libpixelflinger/codeflinger/disassem.c b/libpixelflinger/codeflinger/disassem.c
index aeb8034..39dd614 100644
--- a/libpixelflinger/codeflinger/disassem.c
+++ b/libpixelflinger/codeflinger/disassem.c
@@ -301,19 +301,14 @@
static void disassemble_printaddr(u_int address);
u_int
-disasm(const disasm_interface_t *di, u_int loc, int altfmt)
+disasm(const disasm_interface_t *di, u_int loc, int __unused altfmt)
{
const struct arm32_insn *i_ptr = &arm32_i[0];
-
- u_int insn;
- int matchp;
+ u_int insn = di->di_readword(loc);
+ int matchp = 0;
int branch;
char* f_ptr;
- int fmt;
-
- fmt = 0;
- matchp = 0;
- insn = di->di_readword(loc);
+ int fmt = 0;
/* di->di_printf("loc=%08x insn=%08x : ", loc, insn);*/
@@ -670,7 +665,7 @@
}
static void
-disasm_insn_ldcstc(const disasm_interface_t *di, u_int insn, u_int loc)
+disasm_insn_ldcstc(const disasm_interface_t *di, u_int insn, u_int __unused loc)
{
if (((insn >> 8) & 0xf) == 1)
di->di_printf("f%d, ", (insn >> 12) & 0x07);
diff --git a/libpixelflinger/codeflinger/disassem.h b/libpixelflinger/codeflinger/disassem.h
index 02747cd..c7c60b6 100644
--- a/libpixelflinger/codeflinger/disassem.h
+++ b/libpixelflinger/codeflinger/disassem.h
@@ -49,8 +49,8 @@
typedef struct {
u_int (*di_readword)(u_int);
- void (*di_printaddr)(u_int);
- void (*di_printf)(const char *, ...);
+ void (*di_printaddr)(u_int);
+ int (*di_printf)(const char *, ...);
} disasm_interface_t;
/* Prototypes for callable functions */
diff --git a/libpixelflinger/codeflinger/load_store.cpp b/libpixelflinger/codeflinger/load_store.cpp
index 0a46eaa..e5a1ae0 100644
--- a/libpixelflinger/codeflinger/load_store.cpp
+++ b/libpixelflinger/codeflinger/load_store.cpp
@@ -20,10 +20,6 @@
#include <cutils/log.h>
#include "GGLAssembler.h"
-#ifdef __ARM_ARCH__
-#include <machine/cpu-features.h>
-#endif
-
namespace android {
// ----------------------------------------------------------------------------
@@ -117,20 +113,6 @@
#endif
assert(h);
-#if __ARM_ARCH__ >= 7
- const int mask = (1<<maskLen)-1;
- if ((h == bits) && !l && (s != d.reg)) {
- MOV(AL, 0, d.reg, s); // component = packed;
- } else if ((h == bits) && l) {
- MOV(AL, 0, d.reg, reg_imm(s, LSR, l)); // component = packed >> l;
- } else if (!l && isValidImmediate(mask)) {
- AND(AL, 0, d.reg, s, imm(mask)); // component = packed & mask;
- } else if (!l && isValidImmediate(~mask)) {
- BIC(AL, 0, d.reg, s, imm(~mask)); // component = packed & mask;
- } else {
- UBFX(AL, d.reg, s, l, maskLen); // component = (packed & mask) >> l;
- }
-#else
if (h != bits) {
const int mask = ((1<<maskLen)-1) << l;
if (isValidImmediate(mask)) {
@@ -153,7 +135,6 @@
if (s != d.reg) {
MOV(AL, 0, d.reg, s);
}
-#endif
d.s = maskLen;
}
diff --git a/libpixelflinger/codeflinger/texturing.cpp b/libpixelflinger/codeflinger/texturing.cpp
index b2cfbb3..29a3742 100644
--- a/libpixelflinger/codeflinger/texturing.cpp
+++ b/libpixelflinger/codeflinger/texturing.cpp
@@ -25,10 +25,6 @@
#include "GGLAssembler.h"
-#ifdef __ARM_ARCH__
-#include <machine/cpu-features.h>
-#endif
-
namespace android {
// ---------------------------------------------------------------------------
@@ -694,7 +690,7 @@
}
void GGLAssembler::filter8(
- const fragment_parts_t& parts,
+ const fragment_parts_t& /*parts*/,
pixel_t& texel, const texture_unit_t& tmu,
int U, int V, pointer_t& txPtr,
int FRAC_BITS)
@@ -761,7 +757,7 @@
}
void GGLAssembler::filter16(
- const fragment_parts_t& parts,
+ const fragment_parts_t& /*parts*/,
pixel_t& texel, const texture_unit_t& tmu,
int U, int V, pointer_t& txPtr,
int FRAC_BITS)
@@ -879,118 +875,18 @@
}
void GGLAssembler::filter24(
- const fragment_parts_t& parts,
- pixel_t& texel, const texture_unit_t& tmu,
- int U, int V, pointer_t& txPtr,
- int FRAC_BITS)
+ const fragment_parts_t& /*parts*/,
+ pixel_t& texel, const texture_unit_t& /*tmu*/,
+ int /*U*/, int /*V*/, pointer_t& txPtr,
+ int /*FRAC_BITS*/)
{
// not supported yet (currently disabled)
load(txPtr, texel, 0);
}
-#if __ARM_ARCH__ >= 6
-// ARMv6 version, using UXTB16, and scheduled for Cortex-A8 pipeline
void GGLAssembler::filter32(
- const fragment_parts_t& parts,
- pixel_t& texel, const texture_unit_t& tmu,
- int U, int V, pointer_t& txPtr,
- int FRAC_BITS)
-{
- const int adjust = FRAC_BITS*2 - 8;
- const int round = 0;
- const int prescale = 16 - adjust;
-
- Scratch scratches(registerFile());
-
- int pixel= scratches.obtain();
- int dh = scratches.obtain();
- int u = scratches.obtain();
- int k = scratches.obtain();
-
- int temp = scratches.obtain();
- int dl = scratches.obtain();
-
- int offsetrt = scratches.obtain();
- int offsetlb = scratches.obtain();
-
- int pixellb = offsetlb;
-
- // RB -> U * V
- CONTEXT_LOAD(offsetrt, generated_vars.rt);
- CONTEXT_LOAD(offsetlb, generated_vars.lb);
- if(!round) {
- MOV(AL, 0, U, reg_imm(U, LSL, prescale));
- }
- ADD(AL, 0, u, offsetrt, offsetlb);
-
- LDR(AL, pixel, txPtr.reg, reg_scale_pre(u));
- if (round) {
- SMULBB(AL, u, U, V);
- RSB(AL, 0, U, U, imm(1<<FRAC_BITS));
- } else {
- SMULWB(AL, u, U, V);
- RSB(AL, 0, U, U, imm(1<<(FRAC_BITS+prescale)));
- }
- UXTB16(AL, temp, pixel, 0);
- if (round) {
- ADD(AL, 0, u, u, imm(1<<(adjust-1)));
- MOV(AL, 0, u, reg_imm(u, LSR, adjust));
- }
- LDR(AL, pixellb, txPtr.reg, reg_scale_pre(offsetlb));
- MUL(AL, 0, dh, temp, u);
- UXTB16(AL, temp, pixel, 8);
- MUL(AL, 0, dl, temp, u);
- RSB(AL, 0, k, u, imm(0x100));
-
- // LB -> (1-U) * V
- if (round) {
- SMULBB(AL, u, U, V);
- } else {
- SMULWB(AL, u, U, V);
- }
- UXTB16(AL, temp, pixellb, 0);
- if (round) {
- ADD(AL, 0, u, u, imm(1<<(adjust-1)));
- MOV(AL, 0, u, reg_imm(u, LSR, adjust));
- }
- MLA(AL, 0, dh, temp, u, dh);
- UXTB16(AL, temp, pixellb, 8);
- MLA(AL, 0, dl, temp, u, dl);
- SUB(AL, 0, k, k, u);
-
- // LT -> (1-U)*(1-V)
- RSB(AL, 0, V, V, imm(1<<FRAC_BITS));
- LDR(AL, pixel, txPtr.reg);
- if (round) {
- SMULBB(AL, u, U, V);
- } else {
- SMULWB(AL, u, U, V);
- }
- UXTB16(AL, temp, pixel, 0);
- if (round) {
- ADD(AL, 0, u, u, imm(1<<(adjust-1)));
- MOV(AL, 0, u, reg_imm(u, LSR, adjust));
- }
- MLA(AL, 0, dh, temp, u, dh);
- UXTB16(AL, temp, pixel, 8);
- MLA(AL, 0, dl, temp, u, dl);
-
- // RT -> U*(1-V)
- LDR(AL, pixel, txPtr.reg, reg_scale_pre(offsetrt));
- SUB(AL, 0, u, k, u);
- UXTB16(AL, temp, pixel, 0);
- MLA(AL, 0, dh, temp, u, dh);
- UXTB16(AL, temp, pixel, 8);
- MLA(AL, 0, dl, temp, u, dl);
-
- UXTB16(AL, dh, dh, 8);
- UXTB16(AL, dl, dl, 8);
- ORR(AL, 0, texel.reg, dh, reg_imm(dl, LSL, 8));
-}
-#else
-void GGLAssembler::filter32(
- const fragment_parts_t& parts,
- pixel_t& texel, const texture_unit_t& tmu,
+ const fragment_parts_t& /*parts*/,
+ pixel_t& texel, const texture_unit_t& /*tmu*/,
int U, int V, pointer_t& txPtr,
int FRAC_BITS)
{
@@ -1075,7 +971,6 @@
AND(AL, 0, dl, dl, reg_imm(mask, LSL, 8));
ORR(AL, 0, texel.reg, dh, dl);
}
-#endif
void GGLAssembler::build_texture_environment(
component_t& fragment,
diff --git a/include/pixelflinger/format.h b/libpixelflinger/include/pixelflinger/format.h
similarity index 100%
rename from include/pixelflinger/format.h
rename to libpixelflinger/include/pixelflinger/format.h
diff --git a/include/pixelflinger/pixelflinger.h b/libpixelflinger/include/pixelflinger/pixelflinger.h
similarity index 100%
rename from include/pixelflinger/pixelflinger.h
rename to libpixelflinger/include/pixelflinger/pixelflinger.h
diff --git a/include/private/pixelflinger/ggl_context.h b/libpixelflinger/include/private/pixelflinger/ggl_context.h
similarity index 100%
rename from include/private/pixelflinger/ggl_context.h
rename to libpixelflinger/include/private/pixelflinger/ggl_context.h
diff --git a/include/private/pixelflinger/ggl_fixed.h b/libpixelflinger/include/private/pixelflinger/ggl_fixed.h
similarity index 99%
rename from include/private/pixelflinger/ggl_fixed.h
rename to libpixelflinger/include/private/pixelflinger/ggl_fixed.h
index d0493f3..787f620 100644
--- a/include/private/pixelflinger/ggl_fixed.h
+++ b/libpixelflinger/include/private/pixelflinger/ggl_fixed.h
@@ -190,7 +190,7 @@
);
return res;
}
-#elif defined(__mips__)
+#elif defined(__mips__) && __mips_isa_rev < 6
/*inline MIPS implementations*/
inline GGLfixed gglMulx(GGLfixed a, GGLfixed b, int shift) CONST;
diff --git a/libpixelflinger/picker.cpp b/libpixelflinger/picker.cpp
index 030ef19..aa55229 100644
--- a/libpixelflinger/picker.cpp
+++ b/libpixelflinger/picker.cpp
@@ -26,7 +26,7 @@
// ----------------------------------------------------------------------------
-void ggl_init_picker(context_t* c)
+void ggl_init_picker(context_t* /*c*/)
{
}
diff --git a/libpixelflinger/pixelflinger.cpp b/libpixelflinger/pixelflinger.cpp
index 84e584e..fd449b2 100644
--- a/libpixelflinger/pixelflinger.cpp
+++ b/libpixelflinger/pixelflinger.cpp
@@ -662,7 +662,7 @@
}
}
-void ggl_enable_stencil_test(context_t* c, int enable)
+void ggl_enable_stencil_test(context_t* /*c*/, int /*enable*/)
{
}
@@ -727,18 +727,10 @@
int64_t ggl_system_time()
{
-#if defined(HAVE_POSIX_CLOCKS)
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
-#else
- // we don't support the clocks here.
- struct timeval t;
- t.tv_sec = t.tv_usec = 0;
- gettimeofday(&t, NULL);
- return int64_t(t.tv_sec)*1000000000LL + int64_t(t.tv_usec)*1000LL;
-#endif
}
// ----------------------------------------------------------------------------
diff --git a/libpixelflinger/raster.cpp b/libpixelflinger/raster.cpp
index 32b2a97..26d8e45 100644
--- a/libpixelflinger/raster.cpp
+++ b/libpixelflinger/raster.cpp
@@ -51,7 +51,7 @@
}
void ggl_copyPixels(void* con, GGLint xs, GGLint ys,
- GGLsizei width, GGLsizei height, GGLenum type)
+ GGLsizei width, GGLsizei height, GGLenum /*type*/)
{
GGL_CONTEXT(c, con);
diff --git a/libpixelflinger/scanline.cpp b/libpixelflinger/scanline.cpp
index aa23ca6..3d14531 100644
--- a/libpixelflinger/scanline.cpp
+++ b/libpixelflinger/scanline.cpp
@@ -26,6 +26,10 @@
#include <cutils/memory.h>
#include <cutils/log.h>
+#ifdef __arm__
+#include <machine/cpu-features.h>
+#endif
+
#include "buffer.h"
#include "scanline.h"
@@ -35,7 +39,7 @@
#include "codeflinger/ARMAssembler.h"
#elif defined(__aarch64__)
#include "codeflinger/Arm64Assembler.h"
-#elif defined(__mips__)
+#elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
#include "codeflinger/MIPSAssembler.h"
#endif
//#include "codeflinger/ARMAssemblerOptimizer.h"
@@ -55,7 +59,7 @@
# define ANDROID_CODEGEN ANDROID_CODEGEN_GENERATED
#endif
-#if defined(__arm__) || defined(__mips__) || defined(__aarch64__)
+#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)
# define ANDROID_ARM_CODEGEN 1
#else
# define ANDROID_ARM_CODEGEN 0
@@ -69,7 +73,7 @@
*/
#define DEBUG_NEEDS 0
-#ifdef __mips__
+#if defined( __mips__) && !defined(__LP64__) && __mips_isa_rev < 6
#define ASSEMBLY_SCRATCH_SIZE 4096
#elif defined(__aarch64__)
#define ASSEMBLY_SCRATCH_SIZE 8192
@@ -130,7 +134,7 @@
#elif defined(__aarch64__)
extern "C" void scanline_t32cb16blend_arm64(uint16_t*, uint32_t*, size_t);
extern "C" void scanline_col32cb16blend_arm64(uint16_t *dst, uint32_t col, size_t ct);
-#elif defined(__mips__)
+#elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
extern "C" void scanline_t32cb16blend_mips(uint16_t*, uint32_t*, size_t);
#endif
@@ -282,7 +286,7 @@
#if ANDROID_ARM_CODEGEN
-#if defined(__mips__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
static CodeCache gCodeCache(32 * 1024);
#elif defined(__aarch64__)
static CodeCache gCodeCache(48 * 1024);
@@ -408,10 +412,10 @@
GGLAssembler assembler( new ArmToArm64Assembler(a) );
#endif
// generate the scanline code for the given needs
- int err = assembler.scanline(c->state.needs, c);
+ bool err = assembler.scanline(c->state.needs, c) != 0;
if (ggl_likely(!err)) {
// finally, cache this assembly
- err = gCodeCache.cache(a->key(), a);
+ err = gCodeCache.cache(a->key(), a) < 0;
}
if (ggl_unlikely(err)) {
ALOGE("error generating or caching assembly. Reverting to NOP.");
@@ -534,7 +538,7 @@
return x;
}
-void blend_factor(context_t* c, pixel_t* r,
+void blend_factor(context_t* /*c*/, pixel_t* r,
uint32_t factor, const pixel_t* src, const pixel_t* dst)
{
switch (factor) {
@@ -1161,7 +1165,7 @@
* blender.blend(<32-bit-src-pixel-value>,<ptr-to-16-bit-dest-pixel>)
*/
struct blender_32to16 {
- blender_32to16(context_t* c) { }
+ blender_32to16(context_t* /*c*/) { }
void write(uint32_t s, uint16_t* dst) {
if (s == 0)
return;
@@ -1218,7 +1222,7 @@
* where dstFactor=srcA*(1-srcA) srcFactor=srcA
*/
struct blender_32to16_srcA {
- blender_32to16_srcA(const context_t* c) { }
+ blender_32to16_srcA(const context_t* /*c*/) { }
void write(uint32_t s, uint16_t* dst) {
if (!s) {
return;
@@ -2171,7 +2175,7 @@
void scanline_t32cb16blend(context_t* c)
{
-#if ((ANDROID_CODEGEN >= ANDROID_CODEGEN_ASM) && (defined(__arm__) || defined(__mips__) || defined(__aarch64__)))
+#if ((ANDROID_CODEGEN >= ANDROID_CODEGEN_ASM) && (defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)))
int32_t x = c->iterators.xl;
size_t ct = c->iterators.xr - x;
int32_t y = c->iterators.y;
@@ -2317,7 +2321,7 @@
memset(dst, 0xFF, size);
}
-void scanline_noop(context_t* c)
+void scanline_noop(context_t* /*c*/)
{
}
diff --git a/libpixelflinger/tests/arch-arm64/assembler/Android.mk b/libpixelflinger/tests/arch-arm64/assembler/Android.mk
index 36db49c..448d298 100644
--- a/libpixelflinger/tests/arch-arm64/assembler/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/assembler/Android.mk
@@ -5,15 +5,20 @@
arm64_assembler_test.cpp\
asm_test_jacket.S
+# asm_test_jacket.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
LOCAL_SHARED_LIBRARIES := \
libcutils \
libpixelflinger
LOCAL_C_INCLUDES := \
- system/core/libpixelflinger
+ $(LOCAL_PATH)/../../..
LOCAL_MODULE:= test-pixelflinger-arm64-assembler-test
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+LOCAL_MULTILIB := 64
+
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp b/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
index 456be58..5f58797 100644
--- a/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
+++ b/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
@@ -409,7 +409,7 @@
{
const long base = long(instrMem);
const long curr = base + long(instrMemSize);
- __builtin___clear_cache((void*)base, (void*)curr);
+ __builtin___clear_cache((char*)base, (char*)curr);
}
void dataOpTest(dataOpTest_t test, ARMAssemblerInterface *a64asm, uint32_t Rd = 0,
uint32_t Rn = 1, uint32_t Rm = 2, uint32_t Rs = 3)
@@ -493,16 +493,17 @@
if(i == Rd) continue;
if(regs[i] != savedRegs[i])
{
- printf("Test %x failed Reg(%d) tampered Expected(0x%"PRIx64"),"
- "Actual(0x%"PRIx64") t\n", test.id, i, savedRegs[i], regs[i]);
+ printf("Test %x failed Reg(%d) tampered Expected(0x%" PRIx64 "),"
+ "Actual(0x%" PRIx64 ") t\n", test.id, i, savedRegs[i],
+ regs[i]);
return;
}
}
if(test.checkRd == 1 && (uint64_t)regs[Rd] != test.postRdValue)
{
- printf("Test %x failed, Expected(%"PRIx64"), Actual(%"PRIx64")\n",
- test.id, test.postRdValue, regs[Rd]);
+ printf("Test %x failed, Expected(%" PRIx64 "), Actual(%" PRIx64 ")\n",
+ test.id, test.postRdValue, regs[Rd]);
}
else if(test.checkFlag == 1 && flags[test.postFlag] == 0)
{
@@ -610,7 +611,7 @@
if(regs[i] != savedRegs[i])
{
printf("Test %x failed Reg(%d) tampered"
- " Expected(0x%"PRIx64"), Actual(0x%"PRIx64") t\n",
+ " Expected(0x%" PRIx64 "), Actual(0x%" PRIx64 ") t\n",
test.id, i, savedRegs[i], regs[i]);
return;
}
@@ -619,13 +620,13 @@
if((uint64_t)regs[Rd] != test.postRdValue)
{
printf("Test %x failed, "
- "Expected in Rd(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+ "Expected in Rd(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
test.id, test.postRdValue, regs[Rd]);
}
else if((uint64_t)regs[Rn] != (uint64_t)(&dataMem[test.postRnValue]))
{
printf("Test %x failed, "
- "Expected in Rn(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+ "Expected in Rn(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
test.id, test.postRnValue, regs[Rn] - (uint64_t)dataMem);
}
else if(test.checkMem == true)
@@ -638,7 +639,7 @@
if(value != test.postMemValue)
{
printf("Test %x failed, "
- "Expected in Mem(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+ "Expected in Mem(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
test.id, test.postMemValue, value);
}
else
@@ -697,8 +698,8 @@
if(regs[j] != j)
{
printf("LDM/STM Test %x failed "
- "Reg%d expected(0x%x) Actual(0x%"PRIx64") \n",
- patterns[i],j,j,regs[j]);
+ "Reg%d expected(0x%x) Actual(0x%" PRIx64 ") \n",
+ patterns[i], j, j, regs[j]);
break;
}
}
diff --git a/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk b/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
index ac890c7..5d69203 100644
--- a/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
@@ -5,6 +5,8 @@
col32cb16blend_test.c \
../../../arch-arm64/col32cb16blend.S
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
LOCAL_SHARED_LIBRARIES :=
LOCAL_C_INCLUDES :=
@@ -13,4 +15,6 @@
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+LOCAL_MULTILIB := 64
+
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/arch-arm64/disassembler/Android.mk b/libpixelflinger/tests/arch-arm64/disassembler/Android.mk
index baf4070..d8f7e69 100644
--- a/libpixelflinger/tests/arch-arm64/disassembler/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/disassembler/Android.mk
@@ -7,11 +7,10 @@
LOCAL_SHARED_LIBRARIES :=
-LOCAL_C_INCLUDES := \
- system/core/libpixelflinger/codeflinger
-
LOCAL_MODULE:= test-pixelflinger-arm64-disassembler-test
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+LOCAL_MULTILIB := 64
+
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk b/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
index 1cce1bd..2c1379b 100644
--- a/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
@@ -5,6 +5,8 @@
t32cb16blend_test.c \
../../../arch-arm64/t32cb16blend.S
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
LOCAL_SHARED_LIBRARIES :=
LOCAL_C_INCLUDES :=
@@ -13,4 +15,6 @@
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+LOCAL_MULTILIB := 64
+
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/codegen/Android.mk b/libpixelflinger/tests/codegen/Android.mk
index aa320fc..2f9ca2f 100644
--- a/libpixelflinger/tests/codegen/Android.mk
+++ b/libpixelflinger/tests/codegen/Android.mk
@@ -9,10 +9,10 @@
libpixelflinger
LOCAL_C_INCLUDES := \
- system/core/libpixelflinger
+ $(LOCAL_PATH)/../..
LOCAL_MODULE:= test-opengl-codegen
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/codegen/codegen.cpp b/libpixelflinger/tests/codegen/codegen.cpp
index e9f6c61..148b6f4 100644
--- a/libpixelflinger/tests/codegen/codegen.cpp
+++ b/libpixelflinger/tests/codegen/codegen.cpp
@@ -9,16 +9,18 @@
#include "codeflinger/CodeCache.h"
#include "codeflinger/GGLAssembler.h"
#include "codeflinger/ARMAssembler.h"
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
#include "codeflinger/MIPSAssembler.h"
+#endif
#include "codeflinger/Arm64Assembler.h"
-#if defined(__arm__) || defined(__mips__) || defined(__aarch64__)
+#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)
# define ANDROID_ARM_CODEGEN 1
#else
# define ANDROID_ARM_CODEGEN 0
#endif
-#if defined (__mips__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
#define ASSEMBLY_SCRATCH_SIZE 4096
#elif defined(__aarch64__)
#define ASSEMBLY_SCRATCH_SIZE 8192
@@ -52,7 +54,7 @@
GGLAssembler assembler( new ARMAssembler(a) );
#endif
-#if defined(__mips__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
GGLAssembler assembler( new ArmToMipsAssembler(a) );
#endif
diff --git a/libpixelflinger/tests/gglmul/Android.mk b/libpixelflinger/tests/gglmul/Android.mk
index 64f88b7..75bd39e 100644
--- a/libpixelflinger/tests/gglmul/Android.mk
+++ b/libpixelflinger/tests/gglmul/Android.mk
@@ -7,10 +7,10 @@
LOCAL_SHARED_LIBRARIES :=
LOCAL_C_INCLUDES := \
- system/core/libpixelflinger
+ $(LOCAL_PATH)/../../include
LOCAL_MODULE:= test-pixelflinger-gglmul
LOCAL_MODULE_TAGS := tests
-include $(BUILD_EXECUTABLE)
+include $(BUILD_NATIVE_TEST)
diff --git a/libpixelflinger/tests/gglmul/gglmul_test.cpp b/libpixelflinger/tests/gglmul/gglmul_test.cpp
index 073368e..5d460d6 100644
--- a/libpixelflinger/tests/gglmul/gglmul_test.cpp
+++ b/libpixelflinger/tests/gglmul/gglmul_test.cpp
@@ -28,6 +28,7 @@
#include <stdio.h>
#include <stdint.h>
+#include <inttypes.h>
#include "private/pixelflinger/ggl_fixed.h"
@@ -260,12 +261,12 @@
if(actual == expected)
printf(" Passed\n");
else
- printf(" Failed Actual(%ld) Expected(%ld)\n",
+ printf(" Failed Actual(%" PRId64 ") Expected(%" PRId64 ")\n",
actual, expected);
}
}
-int main(int argc, char** argv)
+int main(int /*argc*/, char** /*argv*/)
{
gglClampx_test();
gglClz_test();
diff --git a/libprocessgroup/Android.mk b/libprocessgroup/Android.mk
new file mode 100644
index 0000000..051999a
--- /dev/null
+++ b/libprocessgroup/Android.mk
@@ -0,0 +1,21 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := processgroup.cpp
+LOCAL_MODULE := libprocessgroup
+LOCAL_SHARED_LIBRARIES := liblog libutils
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Wall -Werror
+LOCAL_REQUIRED_MODULE := processgroup_cleanup
+include external/libcxx/libcxx.mk
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := cleanup.cpp
+LOCAL_MODULE := processgroup_cleanup
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Wall -Werror
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_STATIC_LIBRARIES := libc libcutils
+include $(BUILD_EXECUTABLE)
diff --git a/libprocessgroup/cleanup.cpp b/libprocessgroup/cleanup.cpp
new file mode 100644
index 0000000..cca8dc4
--- /dev/null
+++ b/libprocessgroup/cleanup.cpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2014 Google, Inc
+ *
+ * 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 <string.h>
+#include <unistd.h>
+#include <sys/syslimits.h>
+
+#include "processgroup_priv.h"
+
+int main(int argc, char **argv)
+{
+ char buf[PATH_MAX];
+ if (argc != 2)
+ return -1;
+
+ memcpy(buf, PROCESSGROUP_CGROUP_PATH, sizeof(PROCESSGROUP_CGROUP_PATH));
+ strlcat(buf, argv[1], sizeof(buf));
+ return rmdir(buf);
+}
diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h
new file mode 100644
index 0000000..11bd8cc
--- /dev/null
+++ b/libprocessgroup/include/processgroup/processgroup.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014 Google, Inc
+ *
+ * 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 _PROCESSGROUP_H_
+#define _PROCESSGROUP_H_
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+int killProcessGroup(uid_t uid, int initialPid, int signal);
+
+int createProcessGroup(uid_t uid, int initialPid);
+
+void removeAllProcessGroups(void);
+
+__END_DECLS
+
+#endif
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
new file mode 100644
index 0000000..a80965f
--- /dev/null
+++ b/libprocessgroup/processgroup.cpp
@@ -0,0 +1,343 @@
+/*
+ * Copyright 2014 Google, Inc
+ *
+ * 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.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "libprocessgroup"
+
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <log/log.h>
+#include <private/android_filesystem_config.h>
+
+#include <utils/SystemClock.h>
+
+#include <processgroup/processgroup.h>
+#include "processgroup_priv.h"
+
+struct ctx {
+ bool initialized;
+ int fd;
+ char buf[128];
+ char *buf_ptr;
+ size_t buf_len;
+};
+
+static int convertUidToPath(char *path, size_t size, uid_t uid)
+{
+ return snprintf(path, size, "%s/%s%d",
+ PROCESSGROUP_CGROUP_PATH,
+ PROCESSGROUP_UID_PREFIX,
+ uid);
+}
+
+static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid)
+{
+ return snprintf(path, size, "%s/%s%d/%s%d",
+ PROCESSGROUP_CGROUP_PATH,
+ PROCESSGROUP_UID_PREFIX,
+ uid,
+ PROCESSGROUP_PID_PREFIX,
+ pid);
+}
+
+static int initCtx(uid_t uid, int pid, struct ctx *ctx)
+{
+ int ret;
+ char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
+ convertUidPidToPath(path, sizeof(path), uid, pid);
+ strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
+
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ ret = -errno;
+ SLOGW("failed to open %s: %s", path, strerror(errno));
+ return ret;
+ }
+
+ ctx->fd = fd;
+ ctx->buf_ptr = ctx->buf;
+ ctx->buf_len = 0;
+ ctx->initialized = true;
+
+ SLOGV("Initialized context for %s", path);
+
+ return 0;
+}
+
+static int refillBuffer(struct ctx *ctx)
+{
+ memmove(ctx->buf, ctx->buf_ptr, ctx->buf_len);
+ ctx->buf_ptr = ctx->buf;
+
+ ssize_t ret = read(ctx->fd, ctx->buf_ptr + ctx->buf_len,
+ sizeof(ctx->buf) - ctx->buf_len - 1);
+ if (ret < 0) {
+ return -errno;
+ } else if (ret == 0) {
+ return 0;
+ }
+
+ ctx->buf_len += ret;
+ ctx->buf[ctx->buf_len] = 0;
+ SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
+
+ assert(ctx->buf_len <= sizeof(ctx->buf));
+
+ return ret;
+}
+
+static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
+{
+ if (!ctx->initialized) {
+ int ret = initCtx(uid, appProcessPid, ctx);
+ if (ret < 0) {
+ return ret;
+ }
+ }
+
+ char *eptr;
+ while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
+ int ret = refillBuffer(ctx);
+ if (ret == 0) {
+ return -ERANGE;
+ }
+ if (ret < 0) {
+ return ret;
+ }
+ }
+
+ *eptr = '\0';
+ char *pid_eptr = NULL;
+ errno = 0;
+ long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
+ if (errno != 0) {
+ return -errno;
+ }
+ if (pid_eptr != eptr) {
+ return -EINVAL;
+ }
+
+ ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
+ ctx->buf_ptr = eptr + 1;
+
+ return (pid_t)pid;
+}
+
+static int removeProcessGroup(uid_t uid, int pid)
+{
+ int ret;
+ char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
+
+ convertUidPidToPath(path, sizeof(path), uid, pid);
+ ret = rmdir(path);
+
+ convertUidToPath(path, sizeof(path), uid);
+ rmdir(path);
+
+ return ret;
+}
+
+static void removeUidProcessGroups(const char *uid_path)
+{
+ DIR *uid = opendir(uid_path);
+ if (uid != NULL) {
+ struct dirent cur;
+ struct dirent *dir;
+ while ((readdir_r(uid, &cur, &dir) == 0) && dir) {
+ char path[PROCESSGROUP_MAX_PATH_LEN];
+
+ if (dir->d_type != DT_DIR) {
+ continue;
+ }
+
+ if (strncmp(dir->d_name, PROCESSGROUP_PID_PREFIX, strlen(PROCESSGROUP_PID_PREFIX))) {
+ continue;
+ }
+
+ snprintf(path, sizeof(path), "%s/%s", uid_path, dir->d_name);
+ SLOGV("removing %s\n", path);
+ rmdir(path);
+ }
+ closedir(uid);
+ }
+}
+
+void removeAllProcessGroups()
+{
+ SLOGV("removeAllProcessGroups()");
+ DIR *root = opendir(PROCESSGROUP_CGROUP_PATH);
+ if (root == NULL) {
+ SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno));
+ } else {
+ struct dirent cur;
+ struct dirent *dir;
+ while ((readdir_r(root, &cur, &dir) == 0) && dir) {
+ char path[PROCESSGROUP_MAX_PATH_LEN];
+
+ if (dir->d_type != DT_DIR) {
+ continue;
+ }
+ if (strncmp(dir->d_name, PROCESSGROUP_UID_PREFIX, strlen(PROCESSGROUP_UID_PREFIX))) {
+ continue;
+ }
+
+ snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name);
+ removeUidProcessGroups(path);
+ SLOGV("removing %s\n", path);
+ rmdir(path);
+ }
+ closedir(root);
+ }
+}
+
+static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
+{
+ int processes = 0;
+ struct ctx ctx;
+ pid_t pid;
+
+ ctx.initialized = false;
+
+ while ((pid = getOneAppProcess(uid, initialPid, &ctx)) >= 0) {
+ processes++;
+ if (pid == 0) {
+ // Should never happen... but if it does, trying to kill this
+ // will boomerang right back and kill us! Let's not let that happen.
+ SLOGW("Yikes, we've been told to kill pid 0! How about we don't do that.");
+ continue;
+ }
+ if (pid != initialPid) {
+ // We want to be noisy about killing processes so we can understand
+ // what is going on in the log; however, don't be noisy about the base
+ // process, since that it something we always kill, and we have already
+ // logged elsewhere about killing it.
+ SLOGI("Killing pid %d in uid %d as part of process group %d", pid, uid, initialPid);
+ }
+ int ret = kill(pid, signal);
+ if (ret == -1) {
+ SLOGW("failed to kill pid %d: %s", pid, strerror(errno));
+ }
+ }
+
+ if (ctx.initialized) {
+ close(ctx.fd);
+ }
+
+ return processes;
+}
+
+int killProcessGroup(uid_t uid, int initialPid, int signal)
+{
+ int processes;
+ int sleep_us = 100;
+ int64_t startTime = android::uptimeMillis();
+
+ while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
+ SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
+ if (sleep_us < 128000) {
+ usleep(sleep_us);
+ sleep_us *= 2;
+ } else {
+ SLOGE("failed to kill %d processes for processgroup %d\n",
+ processes, initialPid);
+ break;
+ }
+ }
+
+ SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
+ android::uptimeMillis()-startTime, processes);
+
+ if (processes == 0) {
+ return removeProcessGroup(uid, initialPid);
+ } else {
+ return -1;
+ }
+}
+
+static int mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
+{
+ int ret;
+
+ ret = mkdir(path, mode);
+ if (ret < 0 && errno != EEXIST) {
+ return -errno;
+ }
+
+ ret = chown(path, uid, gid);
+ if (ret < 0) {
+ ret = -errno;
+ rmdir(path);
+ return ret;
+ }
+
+ return 0;
+}
+
+int createProcessGroup(uid_t uid, int initialPid)
+{
+ char path[PROCESSGROUP_MAX_PATH_LEN] = {0};
+ int ret;
+
+ convertUidToPath(path, sizeof(path), uid);
+
+ ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
+ if (ret < 0) {
+ SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
+ return ret;
+ }
+
+ convertUidPidToPath(path, sizeof(path), uid, initialPid);
+
+ ret = mkdirAndChown(path, 0750, AID_SYSTEM, AID_SYSTEM);
+ if (ret < 0) {
+ SLOGE("failed to make and chown %s: %s", path, strerror(-ret));
+ return ret;
+ }
+
+ strlcat(path, PROCESSGROUP_CGROUP_PROCS_FILE, sizeof(path));
+
+ int fd = open(path, O_WRONLY);
+ if (fd < 0) {
+ ret = -errno;
+ SLOGE("failed to open %s: %s", path, strerror(errno));
+ return ret;
+ }
+
+ char pid[PROCESSGROUP_MAX_PID_LEN + 1] = {0};
+ int len = snprintf(pid, sizeof(pid), "%d", initialPid);
+
+ ret = write(fd, pid, len);
+ if (ret < 0) {
+ ret = -errno;
+ SLOGE("failed to write '%s' to %s: %s", pid, path, strerror(errno));
+ } else {
+ ret = 0;
+ }
+
+ close(fd);
+ return ret;
+}
+
diff --git a/libprocessgroup/processgroup_priv.h b/libprocessgroup/processgroup_priv.h
new file mode 100644
index 0000000..1895bf9
--- /dev/null
+++ b/libprocessgroup/processgroup_priv.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2014 Google, Inc
+ *
+ * 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 _PROCESSGROUP_PRIV_H_
+#define _PROCESSGROUP_PRIV_H_
+
+#define PROCESSGROUP_CGROUP_PATH "/acct"
+#define PROCESSGROUP_UID_PREFIX "uid_"
+#define PROCESSGROUP_PID_PREFIX "pid_"
+#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
+#define PROCESSGROUP_MAX_UID_LEN 11
+#define PROCESSGROUP_MAX_PID_LEN 11
+#define PROCESSGROUP_MAX_PATH_LEN \
+ (sizeof(PROCESSGROUP_CGROUP_PATH) + \
+ sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \
+ PROCESSGROUP_MAX_UID_LEN + \
+ sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \
+ PROCESSGROUP_MAX_PID_LEN + \
+ sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \
+ 1)
+
+#endif
diff --git a/libsparse/Android.mk b/libsparse/Android.mk
index a21e090..925b98b 100644
--- a/libsparse/Android.mk
+++ b/libsparse/Android.mk
@@ -16,7 +16,8 @@
LOCAL_SRC_FILES := $(libsparse_src_files)
LOCAL_MODULE := libsparse_host
LOCAL_STATIC_LIBRARIES := libz
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -24,9 +25,10 @@
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := $(libsparse_src_files)
LOCAL_MODULE := libsparse
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
@@ -34,8 +36,9 @@
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := $(libsparse_src_files)
LOCAL_MODULE := libsparse_static
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
@@ -48,6 +51,7 @@
LOCAL_STATIC_LIBRARIES := \
libsparse_host \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
@@ -58,6 +62,7 @@
LOCAL_STATIC_LIBRARIES := \
libsparse_static \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
@@ -69,6 +74,7 @@
LOCAL_STATIC_LIBRARIES := \
libsparse_host \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
@@ -78,22 +84,27 @@
LOCAL_STATIC_LIBRARIES := \
libsparse_static \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
+ifneq ($(HOST_OS),windows)
+
include $(CLEAR_VARS)
LOCAL_SRC_FILES := append2simg.c
LOCAL_MODULE := append2simg
LOCAL_STATIC_LIBRARIES := \
libsparse_host \
libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
+endif
include $(CLEAR_VARS)
LOCAL_MODULE := simg_dump.py
LOCAL_SRC_FILES := simg_dump.py
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_IS_HOST_MODULE := true
+LOCAL_CFLAGS := -Werror
include $(BUILD_PREBUILT)
-
diff --git a/libsparse/append2simg.c b/libsparse/append2simg.c
index 180584f..65e6cc2 100644
--- a/libsparse/append2simg.c
+++ b/libsparse/append2simg.c
@@ -16,6 +16,7 @@
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE 1
+#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
@@ -56,6 +57,11 @@
char *input_path;
off64_t input_len;
+ int tmp_fd;
+ char *tmp_path;
+
+ int ret;
+
if (argc == 3) {
output_path = argv[1];
input_path = argv[2];
@@ -64,6 +70,12 @@
exit(-1);
}
+ ret = asprintf(&tmp_path, "%s.append2simg", output_path);
+ if (ret < 0) {
+ fprintf(stderr, "Couldn't allocate filename\n");
+ exit(-1);
+ }
+
output = open(output_path, O_RDWR | O_BINARY);
if (output < 0) {
fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno));
@@ -99,14 +111,30 @@
}
sparse_output->len += input_len;
+ tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664);
+ if (tmp_fd < 0) {
+ fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
lseek64(output, 0, SEEK_SET);
- if (sparse_file_write(sparse_output, output, false, true, false) < 0) {
+ if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) {
fprintf(stderr, "Failed to write sparse file\n");
exit(-1);
}
sparse_file_destroy(sparse_output);
+ close(tmp_fd);
close(output);
close(input);
+
+ ret = rename(tmp_path, output_path);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
+ free(tmp_path);
+
exit(0);
}
diff --git a/adb/usb_vendors.h b/libsparse/defs.h
similarity index 74%
rename from adb/usb_vendors.h
rename to libsparse/defs.h
index cee23a1..34e63c5 100644
--- a/adb/usb_vendors.h
+++ b/libsparse/defs.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2014 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.
@@ -14,12 +14,10 @@
* limitations under the License.
*/
-#ifndef __USB_VENDORS_H
-#define __USB_VENDORS_H
+#ifndef _LIBSPARSE_DEFS_H_
-extern int vendorIds[];
-extern unsigned vendorIdCount;
-
-void usb_vendors_init(void);
-
+#ifndef __unused
+#define __unused __attribute__((__unused__))
#endif
+
+#endif /* _LIBSPARSE_DEFS_H_ */
diff --git a/libsparse/img2simg.c b/libsparse/img2simg.c
index 6b1caa5..a0db36f 100644
--- a/libsparse/img2simg.c
+++ b/libsparse/img2simg.c
@@ -47,7 +47,6 @@
{
int in;
int out;
- unsigned int i;
int ret;
struct sparse_file *s;
unsigned int block_size = 4096;
diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h
index 17d085c..8b757d2 100644
--- a/libsparse/include/sparse/sparse.h
+++ b/libsparse/include/sparse/sparse.h
@@ -20,6 +20,10 @@
#include <stdbool.h>
#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct sparse_file;
/**
@@ -273,4 +277,8 @@
*/
extern void (*sparse_print_verbose)(const char *fmt, ...);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/libsparse/output_file.c b/libsparse/output_file.c
index e63c4a9..cd30800 100644
--- a/libsparse/output_file.c
+++ b/libsparse/output_file.c
@@ -29,9 +29,10 @@
#include <unistd.h>
#include <zlib.h>
+#include "defs.h"
#include "output_file.h"
-#include "sparse_format.h"
#include "sparse_crc32.h"
+#include "sparse_format.h"
#ifndef USE_MINGW
#include <sys/mman.h>
@@ -264,7 +265,7 @@
.close = gz_file_close,
};
-static int callback_file_open(struct output_file *out, int fd)
+static int callback_file_open(struct output_file *out __unused, int fd __unused)
{
return 0;
}
@@ -287,14 +288,13 @@
return 0;
}
-static int callback_file_pad(struct output_file *out, int64_t len)
+static int callback_file_pad(struct output_file *out __unused, int64_t len __unused)
{
return -1;
}
static int callback_file_write(struct output_file *out, void *data, int len)
{
- int ret;
struct output_file_callback *outc = to_output_file_callback(out);
return outc->write(outc->priv, data, len);
@@ -340,7 +340,7 @@
static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len)
{
chunk_header_t chunk_header;
- int ret, chunk;
+ int ret;
if (skip_len % out->block_size) {
error("don't care size %"PRIi64" is not a multiple of the block size %u",
@@ -367,9 +367,8 @@
uint32_t fill_val)
{
chunk_header_t chunk_header;
- int rnd_up_len, zero_len, count;
+ int rnd_up_len, count;
int ret;
- unsigned int i;
/* Round up the fill length to a multiple of the block size */
rnd_up_len = ALIGN(len, out->block_size);
@@ -535,8 +534,6 @@
void output_file_close(struct output_file *out)
{
- int ret;
-
out->sparse_ops->write_end_chunk(out);
out->ops->close(out);
}
@@ -631,8 +628,8 @@
}
struct output_file *output_file_open_callback(int (*write)(void *, const void *, int),
- void *priv, unsigned int block_size, int64_t len, int gz, int sparse,
- int chunks, int crc)
+ void *priv, unsigned int block_size, int64_t len,
+ int gz __unused, int sparse, int chunks, int crc)
{
int ret;
struct output_file_callback *outc;
diff --git a/libsparse/sparse.c b/libsparse/sparse.c
index 741e8c6..65c09e0 100644
--- a/libsparse/sparse.c
+++ b/libsparse/sparse.c
@@ -19,6 +19,7 @@
#include <sparse/sparse.h>
+#include "defs.h"
#include "sparse_file.h"
#include "output_file.h"
@@ -100,26 +101,32 @@
return chunks;
}
-static void sparse_file_write_block(struct output_file *out,
+static int sparse_file_write_block(struct output_file *out,
struct backed_block *bb)
{
+ int ret = -EINVAL;
+
switch (backed_block_type(bb)) {
case BACKED_BLOCK_DATA:
- write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
+ ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
break;
case BACKED_BLOCK_FILE:
- write_file_chunk(out, backed_block_len(bb),
- backed_block_filename(bb), backed_block_file_offset(bb));
+ ret = write_file_chunk(out, backed_block_len(bb),
+ backed_block_filename(bb),
+ backed_block_file_offset(bb));
break;
case BACKED_BLOCK_FD:
- write_fd_chunk(out, backed_block_len(bb),
- backed_block_fd(bb), backed_block_file_offset(bb));
+ ret = write_fd_chunk(out, backed_block_len(bb),
+ backed_block_fd(bb),
+ backed_block_file_offset(bb));
break;
case BACKED_BLOCK_FILL:
- write_fill_chunk(out, backed_block_len(bb),
- backed_block_fill_val(bb));
+ ret = write_fill_chunk(out, backed_block_len(bb),
+ backed_block_fill_val(bb));
break;
}
+
+ return ret;
}
static int write_all_blocks(struct sparse_file *s, struct output_file *out)
@@ -127,6 +134,7 @@
struct backed_block *bb;
unsigned int last_block = 0;
int64_t pad;
+ int ret = 0;
for (bb = backed_block_iter_new(s->backed_block_list); bb;
bb = backed_block_iter_next(bb)) {
@@ -134,7 +142,9 @@
unsigned int blocks = backed_block_block(bb) - last_block;
write_skip_chunk(out, (int64_t)blocks * s->block_size);
}
- sparse_file_write_block(out, bb);
+ ret = sparse_file_write_block(out, bb);
+ if (ret)
+ return ret;
last_block = backed_block_block(bb) +
DIV_ROUND_UP(backed_block_len(bb), s->block_size);
}
@@ -189,7 +199,7 @@
return ret;
}
-static int out_counter_write(void *priv, const void *data, int len)
+static int out_counter_write(void *priv, const void *data __unused, int len)
{
int64_t *count = priv;
*count += len;
@@ -229,6 +239,7 @@
struct backed_block *bb;
struct backed_block *start;
int64_t file_len = 0;
+ int ret;
/*
* overhead is sparse file header, initial skip chunk, split chunk, end
@@ -248,7 +259,11 @@
for (bb = start; bb; bb = backed_block_iter_next(bb)) {
count = 0;
/* will call out_counter_write to update count */
- sparse_file_write_block(out_counter, bb);
+ ret = sparse_file_write_block(out_counter, bb);
+ if (ret) {
+ bb = NULL;
+ goto out;
+ }
if (file_len + count > len) {
/*
* If the remaining available size is more than 1/8th of the
@@ -259,16 +274,17 @@
backed_block_split(from->backed_block_list, bb, len - file_len);
last_bb = bb;
}
- goto out;
+ goto move;
}
file_len += count;
last_bb = bb;
}
-out:
+move:
backed_block_list_move(from->backed_block_list,
to->backed_block_list, start, last_bb);
+out:
output_file_close(out_counter);
return bb;
@@ -278,7 +294,6 @@
struct sparse_file **out_s, int out_s_count)
{
struct backed_block *bb;
- unsigned int overhead;
struct sparse_file *s;
struct sparse_file *tmp;
int c = 0;
diff --git a/libsparse/sparse_read.c b/libsparse/sparse_read.c
index 704bcfa..8e188e9 100644
--- a/libsparse/sparse_read.c
+++ b/libsparse/sparse_read.c
@@ -29,6 +29,8 @@
#include <sparse/sparse.h>
+#include "defs.h"
+#include "output_file.h"
#include "sparse_crc32.h"
#include "sparse_file.h"
#include "sparse_format.h"
@@ -175,24 +177,19 @@
}
static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size,
- int fd, unsigned int blocks, unsigned int block, uint32_t *crc32)
+ int fd __unused, unsigned int blocks,
+ unsigned int block __unused, uint32_t *crc32)
{
- int ret;
- int chunk;
- int64_t len = (int64_t)blocks * s->block_size;
- uint32_t fill_val;
- uint32_t *fillbuf;
- unsigned int i;
-
if (chunk_size != 0) {
return -EINVAL;
}
if (crc32) {
+ int64_t len = (int64_t)blocks * s->block_size;
memset(copybuf, 0, COPY_BUF_SIZE);
while (len) {
- chunk = min(len, COPY_BUF_SIZE);
+ int chunk = min(len, COPY_BUF_SIZE);
*crc32 = sparse_crc32(*crc32, copybuf, chunk);
len -= chunk;
}
@@ -364,7 +361,6 @@
int64_t remain = s->len;
int64_t offset = 0;
unsigned int to_read;
- char *ptr;
unsigned int i;
bool sparse_block;
diff --git a/libsuspend/Android.mk b/libsuspend/Android.mk
index a2fa3e0..1ba2f59 100644
--- a/libsuspend/Android.mk
+++ b/libsuspend/Android.mk
@@ -18,6 +18,7 @@
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := $(libsuspend_libraries)
+LOCAL_CFLAGS := -Werror
#LOCAL_CFLAGS += -DLOG_NDEBUG=0
include $(BUILD_SHARED_LIBRARY)
@@ -27,5 +28,6 @@
LOCAL_MODULE_TAGS := optional
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
#LOCAL_CFLAGS += -DLOG_NDEBUG=0
include $(BUILD_STATIC_LIBRARY)
diff --git a/libsuspend/autosuspend.c b/libsuspend/autosuspend.c
index eb1f66e..edd1007 100644
--- a/libsuspend/autosuspend.c
+++ b/libsuspend/autosuspend.c
@@ -38,10 +38,13 @@
goto out;
}
+/* Remove autosleep so userspace can manager suspend/resume and keep stats */
+#if 0
autosuspend_ops = autosuspend_autosleep_init();
if (autosuspend_ops) {
goto out;
}
+#endif
autosuspend_ops = autosuspend_wakeup_count_init();
if (autosuspend_ops) {
diff --git a/libsuspend/autosuspend_autosleep.c b/libsuspend/autosuspend_autosleep.c
index 5451615..0d31e74 100644
--- a/libsuspend/autosuspend_autosleep.c
+++ b/libsuspend/autosuspend_autosleep.c
@@ -84,7 +84,6 @@
struct autosuspend_ops *autosuspend_autosleep_init(void)
{
- int ret;
char buf[80];
autosleep_fd = open(SYS_POWER_AUTOSLEEP, O_WRONLY);
diff --git a/libsuspend/autosuspend_earlysuspend.c b/libsuspend/autosuspend_earlysuspend.c
index 1df8c6a..2bece4c 100644
--- a/libsuspend/autosuspend_earlysuspend.c
+++ b/libsuspend/autosuspend_earlysuspend.c
@@ -75,13 +75,8 @@
return err < 0 ? err : 0;
}
-static void *earlysuspend_thread_func(void *arg)
+static void *earlysuspend_thread_func(void __unused *arg)
{
- char buf[80];
- char wakeup_count[20];
- int wakeup_count_len;
- int ret;
-
while (1) {
if (wait_for_fb_sleep()) {
ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n");
diff --git a/libsuspend/autosuspend_wakeup_count.c b/libsuspend/autosuspend_wakeup_count.c
index a88e677..7483a8f 100644
--- a/libsuspend/autosuspend_wakeup_count.c
+++ b/libsuspend/autosuspend_wakeup_count.c
@@ -25,6 +25,7 @@
#include <unistd.h>
#define LOG_TAG "libsuspend"
+//#define LOG_NDEBUG 0
#include <cutils/log.h>
#include "autosuspend_ops.h"
@@ -37,6 +38,7 @@
static pthread_t suspend_thread;
static sem_t suspend_lockout;
static const char *sleep_state = "mem";
+static void (*wakeup_func)(void) = NULL;
static void *suspend_thread_func(void *arg __attribute__((unused)))
{
@@ -80,6 +82,11 @@
if (ret < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", SYS_POWER_STATE, buf);
+ } else {
+ void (*func)(void) = wakeup_func;
+ if (func != NULL) {
+ (*func)();
+ }
}
}
@@ -131,6 +138,15 @@
return ret;
}
+void set_wakeup_callback(void (*func)(void))
+{
+ if (wakeup_func != NULL) {
+ ALOGE("Duplicate wakeup callback applied, keeping original");
+ return;
+ }
+ wakeup_func = func;
+}
+
struct autosuspend_ops autosuspend_wakeup_count_ops = {
.enable = autosuspend_wakeup_count_enable,
.disable = autosuspend_wakeup_count_disable,
diff --git a/libsuspend/include/suspend/autosuspend.h b/libsuspend/include/suspend/autosuspend.h
index f56fc6a..10e3d27 100644
--- a/libsuspend/include/suspend/autosuspend.h
+++ b/libsuspend/include/suspend/autosuspend.h
@@ -43,6 +43,13 @@
*/
int autosuspend_disable(void);
+/*
+ * set_wakeup_callback
+ *
+ * Set a function to be called each time the device wakes up from suspend.
+ */
+void set_wakeup_callback(void (*func)(void));
+
__END_DECLS
#endif
diff --git a/libsync/Android.mk b/libsync/Android.mk
index 626b762..fd1c88c 100644
--- a/libsync/Android.mk
+++ b/libsync/Android.mk
@@ -7,6 +7,7 @@
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
@@ -15,4 +16,5 @@
LOCAL_MODULE_TAGS := optional tests
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
diff --git a/libsync/sync.c b/libsync/sync.c
index 4892866..d73bb11 100644
--- a/libsync/sync.c
+++ b/libsync/sync.c
@@ -17,6 +17,7 @@
*/
#include <fcntl.h>
+#include <malloc.h>
#include <stdint.h>
#include <string.h>
diff --git a/libsync/tests/Android.mk b/libsync/tests/Android.mk
new file mode 100644
index 0000000..ad20e50
--- /dev/null
+++ b/libsync/tests/Android.mk
@@ -0,0 +1,31 @@
+#
+# Copyright 2014 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)
+include external/libcxx/libcxx.mk
+LOCAL_CLANG := true
+LOCAL_MODULE := sync-unit-tests
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_CFLAGS += -g -Wall -Werror -std=gnu++11 -Wno-missing-field-initializers -Wno-sign-compare
+LOCAL_SHARED_LIBRARIES += libsync
+LOCAL_STATIC_LIBRARIES += libgtest_main
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/..
+LOCAL_SRC_FILES := \
+ sync_test.cpp
+include $(BUILD_NATIVE_TEST)
diff --git a/libsync/tests/sync_test.cpp b/libsync/tests/sync_test.cpp
new file mode 100644
index 0000000..55cd687
--- /dev/null
+++ b/libsync/tests/sync_test.cpp
@@ -0,0 +1,615 @@
+#include <gtest/gtest.h>
+#include <sync/sync.h>
+#include <sw_sync.h>
+#include <fcntl.h>
+#include <vector>
+#include <string>
+#include <cassert>
+#include <iostream>
+#include <unistd.h>
+#include <thread>
+#include <poll.h>
+#include <mutex>
+#include <algorithm>
+#include <tuple>
+#include <random>
+#include <unordered_map>
+
+// TODO: better stress tests?
+// Handle more than 64 fd's simultaneously, i.e. fix sync_fence_info's 4k limit.
+// Handle wraparound in timelines like nvidia.
+
+using namespace std;
+
+namespace {
+
+// C++ wrapper class for sync timeline.
+class SyncTimeline {
+ int m_fd = -1;
+ bool m_fdInitialized = false;
+public:
+ SyncTimeline(const SyncTimeline &) = delete;
+ SyncTimeline& operator=(SyncTimeline&) = delete;
+ SyncTimeline() noexcept {
+ int fd = sw_sync_timeline_create();
+ if (fd == -1)
+ return;
+ m_fdInitialized = true;
+ m_fd = fd;
+ }
+ void destroy() {
+ if (m_fdInitialized) {
+ close(m_fd);
+ m_fd = -1;
+ m_fdInitialized = false;
+ }
+ }
+ ~SyncTimeline() {
+ destroy();
+ }
+ bool isValid() const {
+ if (m_fdInitialized) {
+ int status = fcntl(m_fd, F_GETFD, 0);
+ if (status == 0)
+ return true;
+ else
+ return false;
+ }
+ else {
+ return false;
+ }
+ }
+ int getFd() const {
+ return m_fd;
+ }
+ int inc(int val = 1) {
+ return sw_sync_timeline_inc(m_fd, val);
+ }
+};
+
+struct SyncPointInfo {
+ std::string driverName;
+ std::string objectName;
+ uint64_t timeStampNs;
+ int status; // 1 sig, 0 active, neg is err
+};
+
+// Wrapper class for sync fence.
+class SyncFence {
+ int m_fd = -1;
+ bool m_fdInitialized = false;
+ static int s_fenceCount;
+
+ void setFd(int fd) {
+ m_fd = fd;
+ m_fdInitialized = true;
+ }
+ void clearFd() {
+ m_fd = -1;
+ m_fdInitialized = false;
+ }
+public:
+ bool isValid() const {
+ if (m_fdInitialized) {
+ int status = fcntl(m_fd, F_GETFD, 0);
+ if (status == 0)
+ return true;
+ else
+ return false;
+ }
+ else {
+ return false;
+ }
+ }
+ SyncFence& operator=(SyncFence &&rhs) noexcept {
+ destroy();
+ if (rhs.isValid()) {
+ setFd(rhs.getFd());
+ rhs.clearFd();
+ }
+ return *this;
+ }
+ SyncFence(SyncFence &&fence) noexcept {
+ if (fence.isValid()) {
+ setFd(fence.getFd());
+ fence.clearFd();
+ }
+ }
+ SyncFence(const SyncFence &fence) noexcept {
+ // This is ok, as sync fences are immutable after construction, so a dup
+ // is basically the same thing as a copy.
+ if (fence.isValid()) {
+ int fd = dup(fence.getFd());
+ if (fd == -1)
+ return;
+ setFd(fd);
+ }
+ }
+ SyncFence(const SyncTimeline &timeline,
+ int value,
+ const char *name = nullptr) noexcept {
+ std::string autoName = "allocFence";
+ autoName += s_fenceCount;
+ s_fenceCount++;
+ int fd = sw_sync_fence_create(timeline.getFd(), name ? name : autoName.c_str(), value);
+ if (fd == -1)
+ return;
+ setFd(fd);
+ }
+ SyncFence(const SyncFence &a, const SyncFence &b, const char *name = nullptr) noexcept {
+ std::string autoName = "mergeFence";
+ autoName += s_fenceCount;
+ s_fenceCount++;
+ int fd = sync_merge(name ? name : autoName.c_str(), a.getFd(), b.getFd());
+ if (fd == -1)
+ return;
+ setFd(fd);
+ }
+ SyncFence(const vector<SyncFence> &sources) noexcept {
+ assert(sources.size());
+ SyncFence temp(*begin(sources));
+ for (auto itr = ++begin(sources); itr != end(sources); ++itr) {
+ temp = SyncFence(*itr, temp);
+ }
+ if (temp.isValid()) {
+ setFd(temp.getFd());
+ temp.clearFd();
+ }
+ }
+ void destroy() {
+ if (isValid()) {
+ close(m_fd);
+ clearFd();
+ }
+ }
+ ~SyncFence() {
+ destroy();
+ }
+ int getFd() const {
+ return m_fd;
+ }
+ int wait(int timeout = -1) {
+ return sync_wait(m_fd, timeout);
+ }
+ vector<SyncPointInfo> getInfo() const {
+ struct sync_pt_info *pointInfo = nullptr;
+ vector<SyncPointInfo> fenceInfo;
+ sync_fence_info_data *info = sync_fence_info(getFd());
+ if (!info) {
+ return fenceInfo;
+ }
+ while ((pointInfo = sync_pt_info(info, pointInfo))) {
+ fenceInfo.push_back(SyncPointInfo{
+ pointInfo->driver_name,
+ pointInfo->obj_name,
+ pointInfo->timestamp_ns,
+ pointInfo->status});
+ }
+ sync_fence_info_free(info);
+ return fenceInfo;
+ }
+ int getSize() const {
+ return getInfo().size();
+ }
+ int getSignaledCount() const {
+ return countWithStatus(1);
+ }
+ int getActiveCount() const {
+ return countWithStatus(0);
+ }
+ int getErrorCount() const {
+ return countWithStatus(-1);
+ }
+private:
+ int countWithStatus(int status) const {
+ int count = 0;
+ for (auto &info : getInfo()) {
+ if (info.status == status) {
+ count++;
+ }
+ }
+ return count;
+ }
+};
+
+int SyncFence::s_fenceCount = 0;
+
+TEST(AllocTest, Timeline) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+}
+
+TEST(AllocTest, Fence) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fence(timeline, 1);
+ ASSERT_TRUE(fence.isValid());
+}
+
+TEST(AllocTest, FenceNegative) {
+ int timeline = sw_sync_timeline_create();
+ ASSERT_GT(timeline, 0);
+
+ // bad fd.
+ ASSERT_LT(sw_sync_fence_create(-1, "fence", 1), 0);
+
+ // No name - segfaults in user space.
+ // Maybe we should be friendlier here?
+ /*
+ ASSERT_LT(sw_sync_fence_create(timeline, nullptr, 1), 0);
+ */
+ close(timeline);
+}
+
+TEST(FenceTest, OneTimelineWait) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fence(timeline, 5);
+ ASSERT_TRUE(fence.isValid());
+
+ // Wait on fence until timeout.
+ ASSERT_EQ(fence.wait(0), -1);
+ ASSERT_EQ(errno, ETIME);
+
+ // Advance timeline from 0 -> 1
+ ASSERT_EQ(timeline.inc(1), 0);
+
+ // Wait on fence until timeout.
+ ASSERT_EQ(fence.wait(0), -1);
+ ASSERT_EQ(errno, ETIME);
+
+ // Signal the fence.
+ ASSERT_EQ(timeline.inc(4), 0);
+
+ // Wait successfully.
+ ASSERT_EQ(fence.wait(0), 0);
+
+ // Go even futher, and confirm wait still succeeds.
+ ASSERT_EQ(timeline.inc(10), 0);
+ ASSERT_EQ(fence.wait(0), 0);
+}
+
+TEST(FenceTest, OneTimelinePoll) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fence(timeline, 100);
+ ASSERT_TRUE(fence.isValid());
+
+ fd_set set;
+ FD_ZERO(&set);
+ FD_SET(fence.getFd(), &set);
+
+ // Poll the fence, and wait till timeout.
+ timeval time = {0};
+ ASSERT_EQ(select(fence.getFd() + 1, &set, nullptr, nullptr, &time), 0);
+
+ // Advance the timeline.
+ timeline.inc(100);
+ timeline.inc(100);
+
+ // Select should return that the fd is read for reading.
+ FD_ZERO(&set);
+ FD_SET(fence.getFd(), &set);
+
+ ASSERT_EQ(select(fence.getFd() + 1, &set, nullptr, nullptr, &time), 1);
+ ASSERT_TRUE(FD_ISSET(fence.getFd(), &set));
+}
+
+TEST(FenceTest, OneTimelineMerge) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ // create fence a,b,c and then merge them all into fence d.
+ SyncFence a(timeline, 1), b(timeline, 2), c(timeline, 3);
+ ASSERT_TRUE(a.isValid());
+ ASSERT_TRUE(b.isValid());
+ ASSERT_TRUE(c.isValid());
+
+ SyncFence d({a,b,c});
+ ASSERT_TRUE(d.isValid());
+
+ // confirm all fences have one active point (even d).
+ ASSERT_EQ(a.getActiveCount(), 1);
+ ASSERT_EQ(b.getActiveCount(), 1);
+ ASSERT_EQ(c.getActiveCount(), 1);
+ ASSERT_EQ(d.getActiveCount(), 1);
+
+ // confirm that d is not signaled until the max of a,b,c
+ timeline.inc(1);
+ ASSERT_EQ(a.getSignaledCount(), 1);
+ ASSERT_EQ(d.getActiveCount(), 1);
+
+ timeline.inc(1);
+ ASSERT_EQ(b.getSignaledCount(), 1);
+ ASSERT_EQ(d.getActiveCount(), 1);
+
+ timeline.inc(1);
+ ASSERT_EQ(c.getSignaledCount(), 1);
+ ASSERT_EQ(d.getActiveCount(), 0);
+ ASSERT_EQ(d.getSignaledCount(), 1);
+}
+
+TEST(FenceTest, MergeSameFence) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fence(timeline, 5);
+ ASSERT_TRUE(fence.isValid());
+
+ SyncFence selfMergeFence(fence, fence);
+ ASSERT_TRUE(selfMergeFence.isValid());
+
+ ASSERT_EQ(selfMergeFence.getSignaledCount(), 0);
+
+ timeline.inc(5);
+ ASSERT_EQ(selfMergeFence.getSignaledCount(), 1);
+}
+
+TEST(FenceTest, WaitOnDestroyedTimeline) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fenceSig(timeline, 100);
+ SyncFence fenceKill(timeline, 200);
+
+ // Spawn a thread to wait on a fence when the timeline is killed.
+ thread waitThread{
+ [&]() {
+ ASSERT_EQ(timeline.inc(100), 0);
+
+ ASSERT_EQ(fenceKill.wait(-1), -1);
+ ASSERT_EQ(errno, ENOENT);
+ }
+ };
+
+ // Wait for the thread to spool up.
+ fenceSig.wait();
+
+ // Kill the timeline.
+ timeline.destroy();
+
+ // wait for the thread to clean up.
+ waitThread.join();
+}
+
+TEST(FenceTest, PollOnDestroyedTimeline) {
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ SyncFence fenceSig(timeline, 100);
+ SyncFence fenceKill(timeline, 200);
+
+ // Spawn a thread to wait on a fence when the timeline is killed.
+ thread waitThread{
+ [&]() {
+ ASSERT_EQ(timeline.inc(100), 0);
+
+ // Wait on the fd.
+ struct pollfd fds;
+ fds.fd = fenceKill.getFd();
+ fds.events = POLLIN | POLLERR;
+ ASSERT_EQ(poll(&fds, 1, -1), 1);
+ ASSERT_TRUE(fds.revents & POLLERR);
+ }
+ };
+
+ // Wait for the thread to spool up.
+ fenceSig.wait();
+
+ // Kill the timeline.
+ timeline.destroy();
+
+ // wait for the thread to clean up.
+ waitThread.join();
+}
+
+TEST(FenceTest, MultiTimelineWait) {
+ SyncTimeline timelineA, timelineB, timelineC;
+
+ SyncFence fenceA(timelineA, 5);
+ SyncFence fenceB(timelineB, 5);
+ SyncFence fenceC(timelineC, 5);
+
+ // Make a larger fence using 3 other fences from different timelines.
+ SyncFence mergedFence({fenceA, fenceB, fenceC});
+ ASSERT_TRUE(mergedFence.isValid());
+
+ // Confirm fence isn't signaled
+ ASSERT_EQ(mergedFence.getActiveCount(), 3);
+ ASSERT_EQ(mergedFence.wait(0), -1);
+ ASSERT_EQ(errno, ETIME);
+
+ timelineA.inc(5);
+ ASSERT_EQ(mergedFence.getActiveCount(), 2);
+ ASSERT_EQ(mergedFence.getSignaledCount(), 1);
+
+ timelineB.inc(5);
+ ASSERT_EQ(mergedFence.getActiveCount(), 1);
+ ASSERT_EQ(mergedFence.getSignaledCount(), 2);
+
+ timelineC.inc(5);
+ ASSERT_EQ(mergedFence.getActiveCount(), 0);
+ ASSERT_EQ(mergedFence.getSignaledCount(), 3);
+
+ // confirm you can successfully wait.
+ ASSERT_EQ(mergedFence.wait(100), 0);
+}
+
+TEST(StressTest, TwoThreadsSharedTimeline) {
+ const int iterations = 1 << 16;
+ int counter = 0;
+ SyncTimeline timeline;
+ ASSERT_TRUE(timeline.isValid());
+
+ // Use a single timeline to synchronize two threads
+ // hammmering on the same counter.
+ auto threadMain = [&](int threadId) {
+ for (int i = 0; i < iterations; i++) {
+ SyncFence fence(timeline, i * 2 + threadId);
+ ASSERT_TRUE(fence.isValid());
+
+ // Wait on the prior thread to complete.
+ ASSERT_EQ(fence.wait(), 0);
+
+ // Confirm the previous thread's writes are visible and then inc.
+ ASSERT_EQ(counter, i * 2 + threadId);
+ counter++;
+
+ // Kick off the other thread.
+ ASSERT_EQ(timeline.inc(), 0);
+ }
+ };
+
+ thread a{threadMain, 0};
+ thread b{threadMain, 1};
+ a.join();
+ b.join();
+
+ // make sure the threads did not trample on one another.
+ ASSERT_EQ(counter, iterations * 2);
+}
+
+class ConsumerStressTest : public ::testing::TestWithParam<int> {};
+
+TEST_P(ConsumerStressTest, MultiProducerSingleConsumer) {
+ mutex lock;
+ int counter = 0;
+ int iterations = 1 << 12;
+
+ vector<SyncTimeline> producerTimelines(GetParam());
+ vector<thread> threads;
+ SyncTimeline consumerTimeline;
+
+ // Producer threads run this lambda.
+ auto threadMain = [&](int threadId) {
+ for (int i = 0; i < iterations; i++) {
+ SyncFence fence(consumerTimeline, i);
+ ASSERT_TRUE(fence.isValid());
+
+ // Wait for the consumer to finish. Use alternate
+ // means of waiting on the fence.
+ if ((iterations + threadId) % 8 != 0) {
+ ASSERT_EQ(fence.wait(), 0);
+ }
+ else {
+ while (fence.getSignaledCount() != 1) {
+ ASSERT_EQ(fence.getErrorCount(), 0);
+ }
+ }
+
+ // Every producer increments the counter, the consumer checks + erases it.
+ lock.lock();
+ counter++;
+ lock.unlock();
+
+ ASSERT_EQ(producerTimelines[threadId].inc(), 0);
+ }
+ };
+
+ for (int i = 0; i < GetParam(); i++) {
+ threads.push_back(thread{threadMain, i});
+ }
+
+ // Consumer thread runs this loop.
+ for (int i = 1; i <= iterations; i++) {
+ // Create a fence representing all producers final timelines.
+ vector<SyncFence> fences;
+ for (auto& timeline : producerTimelines) {
+ fences.push_back(SyncFence(timeline, i));
+ }
+ SyncFence mergeFence(fences);
+ ASSERT_TRUE(mergeFence.isValid());
+
+ // Make sure we see an increment from every producer thread. Vary
+ // the means by which we wait.
+ if (iterations % 8 != 0) {
+ ASSERT_EQ(mergeFence.wait(), 0);
+ }
+ else {
+ while (mergeFence.getSignaledCount() != mergeFence.getSize()) {
+ ASSERT_EQ(mergeFence.getErrorCount(), 0);
+ }
+ }
+ ASSERT_EQ(counter, GetParam()*i);
+
+ // Release the producer threads.
+ ASSERT_EQ(consumerTimeline.inc(), 0);
+ }
+
+ for_each(begin(threads), end(threads), [](thread& thread) { thread.join(); });
+}
+INSTANTIATE_TEST_CASE_P(
+ ParameterizedStressTest,
+ ConsumerStressTest,
+ ::testing::Values(2,4,16));
+
+class MergeStressTest : public ::testing::TestWithParam<tuple<int, int>> {};
+
+template <typename K, typename V> using dict = unordered_map<K,V>;
+
+TEST_P(MergeStressTest, RandomMerge) {
+ int timelineCount = get<0>(GetParam());
+ int mergeCount = get<1>(GetParam());
+
+ vector<SyncTimeline> timelines(timelineCount);
+
+ default_random_engine generator;
+ uniform_int_distribution<int> timelineDist(0, timelines.size()-1);
+ uniform_int_distribution<int> syncPointDist(0, numeric_limits<int>::max());
+
+ SyncFence fence(timelines[0], 0);
+ ASSERT_TRUE(fence.isValid());
+
+ unordered_map<int, int> fenceMap;
+ fenceMap.insert(make_tuple(0, 0));
+
+ // Randomly create syncpoints out of a fixed set of timelines, and merge them together.
+ for (int i = 0; i < mergeCount; i++) {
+
+ // Generate syncpoint.
+ int timelineOffset = timelineDist(generator);
+ const SyncTimeline& timeline = timelines[timelineOffset];
+ int syncPoint = syncPointDist(generator);
+
+ // Keep track of the latest syncpoint in each timeline.
+ auto itr = fenceMap.find(timelineOffset);
+ if (itr == end(fenceMap)) {
+ fenceMap.insert(tie(timelineOffset, syncPoint));
+ }
+ else {
+ int oldSyncPoint = itr->second;
+ fenceMap.erase(itr);
+ fenceMap.insert(tie(timelineOffset, max(syncPoint, oldSyncPoint)));
+ }
+
+ // Merge.
+ fence = SyncFence(fence, SyncFence(timeline, syncPoint));
+ ASSERT_TRUE(fence.isValid());
+ }
+
+ // Confirm our map matches the fence.
+ ASSERT_EQ(fence.getSize(), fenceMap.size());
+
+ // Trigger the merged fence.
+ for (auto& item: fenceMap) {
+ ASSERT_EQ(fence.wait(0), -1);
+ ASSERT_EQ(errno, ETIME);
+
+ // Increment the timeline to the last syncpoint.
+ timelines[item.first].inc(item.second);
+ }
+
+ // Check that the fence is triggered.
+ ASSERT_EQ(fence.wait(0), 0);
+}
+
+INSTANTIATE_TEST_CASE_P(
+ ParameterizedMergeStressTest,
+ MergeStressTest,
+ ::testing::Combine(::testing::Values(16,32), ::testing::Values(32, 1024, 1024*32)));
+
+}
+
diff --git a/libsysutils/Android.mk b/libsysutils/Android.mk
index 1451b0d..7bf53e3 100644
--- a/libsysutils/Android.mk
+++ b/libsysutils/Android.mk
@@ -1,5 +1,3 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
-
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
@@ -16,12 +14,12 @@
LOCAL_MODULE:= libsysutils
-LOCAL_C_INCLUDES :=
+LOCAL_CFLAGS := -Werror
-LOCAL_CFLAGS :=
-
-LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_SHARED_LIBRARIES := \
+ libcutils \
+ liblog \
+ libnl
include $(BUILD_SHARED_LIBRARY)
-endif
diff --git a/libsysutils/EventLogTags.logtags b/libsysutils/EventLogTags.logtags
index 7aa5cad..713f8cd 100644
--- a/libsysutils/EventLogTags.logtags
+++ b/libsysutils/EventLogTags.logtags
@@ -1,5 +1,5 @@
# See system/core/logcat/event.logtags for a description of the format of this file.
# FrameworkListener dispatchCommand overflow
-78001 dispatchCommand_overflow
-65537 netlink_failure (uid|1)
+78001 exp_det_dispatchCommand_overflow
+65537 exp_det_netlink_failure (uid|1)
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index a5ffda2..e7b3dd6 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -39,6 +39,11 @@
init(socketName, false);
}
+FrameworkListener::FrameworkListener(int sock) :
+ SocketListener(sock, true) {
+ init(NULL, false);
+}
+
void FrameworkListener::init(const char *socketName UNUSED, bool withSeq) {
mCommands = new FrameworkCommandCollection();
errorRate = 0;
@@ -87,7 +92,6 @@
char *qlimit = tmp + sizeof(tmp) - 1;
bool esc = false;
bool quote = false;
- int k;
bool haveCmdNum = !mWithSeq;
memset(argv, 0, sizeof(argv));
@@ -161,7 +165,7 @@
goto overflow;
argv[argc++] = strdup(tmp);
#if 0
- for (k = 0; k < argc; k++) {
+ for (int k = 0; k < argc; k++) {
SLOGD("arg[%d] = '%s'", k, argv[k]);
}
#endif
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index 34f2016..29a86d8 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -29,26 +29,39 @@
#include <net/if.h>
#include <linux/if.h>
+#include <linux/if_addr.h>
+#include <linux/if_link.h>
#include <linux/netfilter/nfnetlink.h>
+#include <linux/netfilter/nfnetlink_log.h>
#include <linux/netfilter_ipv4/ipt_ULOG.h>
+
/* From kernel's net/netfilter/xt_quota2.c */
-const int QLOG_NL_EVENT = 112;
+const int LOCAL_QLOG_NL_EVENT = 112;
+const int LOCAL_NFLOG_PACKET = NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET;
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
-const int NetlinkEvent::NlActionUnknown = 0;
-const int NetlinkEvent::NlActionAdd = 1;
-const int NetlinkEvent::NlActionRemove = 2;
-const int NetlinkEvent::NlActionChange = 3;
-const int NetlinkEvent::NlActionLinkUp = 4;
-const int NetlinkEvent::NlActionLinkDown = 5;
-const int NetlinkEvent::NlActionAddressUpdated = 6;
-const int NetlinkEvent::NlActionAddressRemoved = 7;
-const int NetlinkEvent::NlActionRdnss = 8;
+#include <netlink/attr.h>
+#include <netlink/genl/genl.h>
+#include <netlink/handlers.h>
+#include <netlink/msg.h>
+
+// STOPSHIP: remove these deprecated constants once we have updated prebuilts
+const int NetlinkEvent::NlActionUnknown = static_cast<int>(Action::kUnknown);
+const int NetlinkEvent::NlActionAdd = static_cast<int>(Action::kAdd);
+const int NetlinkEvent::NlActionRemove = static_cast<int>(Action::kRemove);
+const int NetlinkEvent::NlActionChange = static_cast<int>(Action::kChange);
+const int NetlinkEvent::NlActionLinkDown = static_cast<int>(Action::kLinkDown);
+const int NetlinkEvent::NlActionLinkUp = static_cast<int>(Action::kLinkUp);
+const int NetlinkEvent::NlActionAddressUpdated = static_cast<int>(Action::kAddressUpdated);
+const int NetlinkEvent::NlActionAddressRemoved = static_cast<int>(Action::kAddressRemoved);
+const int NetlinkEvent::NlActionRdnss = static_cast<int>(Action::kRdnss);
+const int NetlinkEvent::NlActionRouteUpdated = static_cast<int>(Action::kRouteUpdated);
+const int NetlinkEvent::NlActionRouteRemoved = static_cast<int>(Action::kRouteRemoved);
NetlinkEvent::NetlinkEvent() {
- mAction = NlActionUnknown;
+ mAction = Action::kUnknown;
memset(mParams, 0, sizeof(mParams));
mPath = NULL;
mSubsystem = NULL;
@@ -78,38 +91,116 @@
}
/*
+ * Returns the message name for a message in the NETLINK_ROUTE family, or NULL
+ * if parsing that message is not supported.
+ */
+static const char *rtMessageName(int type) {
+#define NL_EVENT_RTM_NAME(rtm) case rtm: return #rtm;
+ switch (type) {
+ NL_EVENT_RTM_NAME(RTM_NEWLINK);
+ NL_EVENT_RTM_NAME(RTM_DELLINK);
+ NL_EVENT_RTM_NAME(RTM_NEWADDR);
+ NL_EVENT_RTM_NAME(RTM_DELADDR);
+ NL_EVENT_RTM_NAME(RTM_NEWROUTE);
+ NL_EVENT_RTM_NAME(RTM_DELROUTE);
+ NL_EVENT_RTM_NAME(RTM_NEWNDUSEROPT);
+ NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT);
+ NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET);
+ default:
+ return NULL;
+ }
+#undef NL_EVENT_RTM_NAME
+}
+
+/*
+ * Checks that a binary NETLINK_ROUTE message is long enough for a payload of
+ * size bytes.
+ */
+static bool checkRtNetlinkLength(const struct nlmsghdr *nh, size_t size) {
+ if (nh->nlmsg_len < NLMSG_LENGTH(size)) {
+ SLOGE("Got a short %s message\n", rtMessageName(nh->nlmsg_type));
+ return false;
+ }
+ return true;
+}
+
+/*
+ * Utility function to log errors.
+ */
+static bool maybeLogDuplicateAttribute(bool isDup,
+ const char *attributeName,
+ const char *messageName) {
+ if (isDup) {
+ SLOGE("Multiple %s attributes in %s, ignoring\n", attributeName, messageName);
+ return true;
+ }
+ return false;
+}
+
+/*
+ * Parse a RTM_NEWLINK message.
+ */
+bool NetlinkEvent::parseIfInfoMessage(const struct nlmsghdr *nh) {
+ struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA(nh);
+ if (!checkRtNetlinkLength(nh, sizeof(*ifi)))
+ return false;
+
+ if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
+ return false;
+ }
+
+ int len = IFLA_PAYLOAD(nh);
+ struct rtattr *rta;
+ for (rta = IFLA_RTA(ifi); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
+ switch(rta->rta_type) {
+ case IFLA_IFNAME:
+ asprintf(&mParams[0], "INTERFACE=%s", (char *) RTA_DATA(rta));
+ mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp :
+ Action::kLinkDown;
+ mSubsystem = strdup("net");
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/*
* Parse a RTM_NEWADDR or RTM_DELADDR message.
*/
-bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr,
- int rtasize) {
- struct rtattr *rta;
+bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) {
+ struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh);
struct ifa_cacheinfo *cacheinfo = NULL;
char addrstr[INET6_ADDRSTRLEN] = "";
+ char ifname[IFNAMSIZ];
+
+ if (!checkRtNetlinkLength(nh, sizeof(*ifaddr)))
+ return false;
// Sanity check.
+ int type = nh->nlmsg_type;
if (type != RTM_NEWADDR && type != RTM_DELADDR) {
SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type);
return false;
}
// For log messages.
- const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR";
+ const char *msgtype = rtMessageName(type);
- for (rta = IFA_RTA(ifaddr); RTA_OK(rta, rtasize);
- rta = RTA_NEXT(rta, rtasize)) {
+ struct rtattr *rta;
+ int len = IFA_PAYLOAD(nh);
+ for (rta = IFA_RTA(ifaddr); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
if (rta->rta_type == IFA_ADDRESS) {
// Only look at the first address, because we only support notifying
// one change at a time.
- if (*addrstr != '\0') {
- SLOGE("Multiple IFA_ADDRESSes in %s, ignoring\n", msgtype);
+ if (maybeLogDuplicateAttribute(*addrstr != '\0', "IFA_ADDRESS", msgtype))
continue;
- }
// Convert the IP address to a string.
if (ifaddr->ifa_family == AF_INET) {
struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta);
if (RTA_PAYLOAD(rta) < sizeof(*addr4)) {
- SLOGE("Short IPv4 address (%d bytes) in %s",
+ SLOGE("Short IPv4 address (%zu bytes) in %s",
RTA_PAYLOAD(rta), msgtype);
continue;
}
@@ -117,7 +208,7 @@
} else if (ifaddr->ifa_family == AF_INET6) {
struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta);
if (RTA_PAYLOAD(rta) < sizeof(*addr6)) {
- SLOGE("Short IPv6 address (%d bytes) in %s",
+ SLOGE("Short IPv6 address (%zu bytes) in %s",
RTA_PAYLOAD(rta), msgtype);
continue;
}
@@ -128,40 +219,23 @@
}
// Find the interface name.
- char ifname[IFNAMSIZ + 1];
if (!if_indextoname(ifaddr->ifa_index, ifname)) {
SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype);
return false;
}
- // Fill in interface information.
- mAction = (type == RTM_NEWADDR) ? NlActionAddressUpdated :
- NlActionAddressRemoved;
- mSubsystem = strdup("net");
- asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr,
- ifaddr->ifa_prefixlen);
- asprintf(&mParams[1], "INTERFACE=%s", ifname);
- asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
- asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
} else if (rta->rta_type == IFA_CACHEINFO) {
// Address lifetime information.
- if (cacheinfo) {
- // We only support one address.
- SLOGE("Multiple IFA_CACHEINFOs in %s, ignoring\n", msgtype);
+ if (maybeLogDuplicateAttribute(cacheinfo, "IFA_CACHEINFO", msgtype))
continue;
- }
if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) {
- SLOGE("Short IFA_CACHEINFO (%d vs. %d bytes) in %s",
+ SLOGE("Short IFA_CACHEINFO (%zu vs. %zu bytes) in %s",
RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype);
continue;
}
cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta);
- asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
- asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
- asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
- asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
}
}
@@ -170,15 +244,180 @@
return false;
}
+ // Fill in netlink event information.
+ mAction = (type == RTM_NEWADDR) ? Action::kAddressUpdated :
+ Action::kAddressRemoved;
+ mSubsystem = strdup("net");
+ asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr,
+ ifaddr->ifa_prefixlen);
+ asprintf(&mParams[1], "INTERFACE=%s", ifname);
+ asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
+ asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
+
+ if (cacheinfo) {
+ asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
+ asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
+ asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
+ asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
+ }
+
return true;
}
/*
-<<<<<<< HEAD
+ * Parse a QLOG_NL_EVENT message.
+ */
+bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) {
+ const char *devname;
+ ulog_packet_msg_t *pm = (ulog_packet_msg_t *) NLMSG_DATA(nh);
+ if (!checkRtNetlinkLength(nh, sizeof(*pm)))
+ return false;
+
+ devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
+ asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
+ asprintf(&mParams[1], "INTERFACE=%s", devname);
+ mSubsystem = strdup("qlog");
+ mAction = Action::kChange;
+ return true;
+}
+
+/*
+ * Parse a LOCAL_NFLOG_PACKET message.
+ */
+bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) {
+ int uid = -1;
+ int len = 0;
+ char* raw = NULL;
+
+ struct nlattr *uid_attr = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_UID);
+ if (uid_attr) {
+ uid = ntohl(nla_get_u32(uid_attr));
+ }
+
+ struct nlattr *payload = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD);
+ if (payload) {
+ /* First 256 bytes is plenty */
+ len = nla_len(payload);
+ if (len > 256) len = 256;
+ raw = (char*) nla_data(payload);
+ }
+
+ char* hex = (char*) calloc(1, 5 + (len * 2));
+ strcpy(hex, "HEX=");
+ for (int i = 0; i < len; i++) {
+ hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf];
+ hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf];
+ }
+
+ asprintf(&mParams[0], "UID=%d", uid);
+ mParams[1] = hex;
+ mSubsystem = strdup("strict");
+ mAction = Action::kChange;
+ return true;
+}
+
+/*
+ * Parse a RTM_NEWROUTE or RTM_DELROUTE message.
+ */
+bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) {
+ uint8_t type = nh->nlmsg_type;
+ const char *msgname = rtMessageName(type);
+
+ // Sanity check.
+ if (type != RTM_NEWROUTE && type != RTM_DELROUTE) {
+ SLOGE("%s: incorrect message type %d (%s)\n", __func__, type, msgname);
+ return false;
+ }
+
+ struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA(nh);
+ if (!checkRtNetlinkLength(nh, sizeof(*rtm)))
+ return false;
+
+ if (// Ignore static routes we've set up ourselves.
+ (rtm->rtm_protocol != RTPROT_KERNEL &&
+ rtm->rtm_protocol != RTPROT_RA) ||
+ // We're only interested in global unicast routes.
+ (rtm->rtm_scope != RT_SCOPE_UNIVERSE) ||
+ (rtm->rtm_type != RTN_UNICAST) ||
+ // We don't support source routing.
+ (rtm->rtm_src_len != 0) ||
+ // Cloned routes aren't real routes.
+ (rtm->rtm_flags & RTM_F_CLONED)) {
+ return false;
+ }
+
+ int family = rtm->rtm_family;
+ int prefixLength = rtm->rtm_dst_len;
+
+ // Currently we only support: destination, (one) next hop, ifindex.
+ char dst[INET6_ADDRSTRLEN] = "";
+ char gw[INET6_ADDRSTRLEN] = "";
+ char dev[IFNAMSIZ] = "";
+
+ size_t len = RTM_PAYLOAD(nh);
+ struct rtattr *rta;
+ for (rta = RTM_RTA(rtm); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
+ switch (rta->rta_type) {
+ case RTA_DST:
+ if (maybeLogDuplicateAttribute(*dst, "RTA_DST", msgname))
+ continue;
+ if (!inet_ntop(family, RTA_DATA(rta), dst, sizeof(dst)))
+ return false;
+ continue;
+ case RTA_GATEWAY:
+ if (maybeLogDuplicateAttribute(*gw, "RTA_GATEWAY", msgname))
+ continue;
+ if (!inet_ntop(family, RTA_DATA(rta), gw, sizeof(gw)))
+ return false;
+ continue;
+ case RTA_OIF:
+ if (maybeLogDuplicateAttribute(*dev, "RTA_OIF", msgname))
+ continue;
+ if (!if_indextoname(* (int *) RTA_DATA(rta), dev))
+ return false;
+ default:
+ continue;
+ }
+ }
+
+ // If there's no RTA_DST attribute, then:
+ // - If the prefix length is zero, it's the default route.
+ // - If the prefix length is nonzero, there's something we don't understand.
+ // Ignore the event.
+ if (!*dst && !prefixLength) {
+ if (family == AF_INET) {
+ strncpy(dst, "0.0.0.0", sizeof(dst));
+ } else if (family == AF_INET6) {
+ strncpy(dst, "::", sizeof(dst));
+ }
+ }
+
+ // A useful route must have a destination and at least either a gateway or
+ // an interface.
+ if (!*dst || (!*gw && !*dev))
+ return false;
+
+ // Fill in netlink event information.
+ mAction = (type == RTM_NEWROUTE) ? Action::kRouteUpdated :
+ Action::kRouteRemoved;
+ mSubsystem = strdup("net");
+ asprintf(&mParams[0], "ROUTE=%s/%d", dst, prefixLength);
+ asprintf(&mParams[1], "GATEWAY=%s", (*gw) ? gw : "");
+ asprintf(&mParams[2], "INTERFACE=%s", (*dev) ? dev : "");
+
+ return true;
+}
+
+/*
* Parse a RTM_NEWNDUSEROPT message.
*/
-bool NetlinkEvent::parseNdUserOptMessage(struct nduseroptmsg *msg, int len) {
+bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) {
+ struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(nh);
+ if (!checkRtNetlinkLength(nh, sizeof(*msg)))
+ return false;
+
// Check the length is valid.
+ int len = NLMSG_PAYLOAD(nh, sizeof(*msg));
if (msg->nduseropt_opts_len > len) {
SLOGE("RTM_NEWNDUSEROPT invalid length %d > %d\n",
msg->nduseropt_opts_len, len);
@@ -201,7 +440,7 @@
}
// Find the interface name.
- char ifname[IFNAMSIZ + 1];
+ char ifname[IFNAMSIZ];
if (!if_indextoname(msg->nduseropt_ifindex, ifname)) {
SLOGE("RTM_NEWNDUSEROPT on unknown ifindex %d\n",
msg->nduseropt_ifindex);
@@ -259,7 +498,7 @@
}
buf[pos] = '\0';
- mAction = NlActionRdnss;
+ mAction = Action::kRdnss;
mSubsystem = strdup("net");
asprintf(&mParams[0], "INTERFACE=%s", ifname);
asprintf(&mParams[1], "LIFETIME=%u", lifetime);
@@ -274,101 +513,57 @@
/*
* Parse a binary message from a NETLINK_ROUTE netlink socket.
+ *
+ * Note that this function can only parse one message, because the message's
+ * content has to be stored in the class's member variables (mAction,
+ * mSubsystem, etc.). Invalid or unrecognized messages are skipped, but if
+ * there are multiple valid messages in the buffer, only the first one will be
+ * returned.
+ *
+ * TODO: consider only ever looking at the first message.
*/
bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
- const struct nlmsghdr *nh;
+ struct nlmsghdr *nh;
for (nh = (struct nlmsghdr *) buffer;
NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE);
nh = NLMSG_NEXT(nh, size)) {
+ if (!rtMessageName(nh->nlmsg_type)) {
+ SLOGD("Unexpected netlink message type %d\n", nh->nlmsg_type);
+ continue;
+ }
+
if (nh->nlmsg_type == RTM_NEWLINK) {
- int len = nh->nlmsg_len - sizeof(*nh);
- struct ifinfomsg *ifi;
+ if (parseIfInfoMessage(nh))
+ return true;
- if (sizeof(*ifi) > (size_t) len) {
- SLOGE("Got a short RTM_NEWLINK message\n");
- continue;
- }
-
- ifi = (ifinfomsg *)NLMSG_DATA(nh);
- if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
- continue;
- }
-
- struct rtattr *rta = (struct rtattr *)
- ((char *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
- len = NLMSG_PAYLOAD(nh, sizeof(*ifi));
-
- while(RTA_OK(rta, len)) {
- switch(rta->rta_type) {
- case IFLA_IFNAME:
- char buffer[16 + IFNAMSIZ];
- snprintf(buffer, sizeof(buffer), "INTERFACE=%s",
- (char *) RTA_DATA(rta));
- mParams[0] = strdup(buffer);
- mAction = (ifi->ifi_flags & IFF_LOWER_UP) ?
- NlActionLinkUp : NlActionLinkDown;
- mSubsystem = strdup("net");
- break;
- }
-
- rta = RTA_NEXT(rta, len);
- }
-
- } else if (nh->nlmsg_type == QLOG_NL_EVENT) {
- char *devname;
- ulog_packet_msg_t *pm;
- size_t len = nh->nlmsg_len - sizeof(*nh);
- if (sizeof(*pm) > len) {
- SLOGE("Got a short QLOG message\n");
- continue;
- }
- pm = (ulog_packet_msg_t *)NLMSG_DATA(nh);
- devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
- asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
- asprintf(&mParams[1], "INTERFACE=%s", devname);
- mSubsystem = strdup("qlog");
- mAction = NlActionChange;
+ } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) {
+ if (parseUlogPacketMessage(nh))
+ return true;
} else if (nh->nlmsg_type == RTM_NEWADDR ||
nh->nlmsg_type == RTM_DELADDR) {
- int len = nh->nlmsg_len - sizeof(*nh);
- struct ifaddrmsg *ifa;
+ if (parseIfAddrMessage(nh))
+ return true;
- if (sizeof(*ifa) > (size_t) len) {
- SLOGE("Got a short RTM_xxxADDR message\n");
- continue;
- }
-
- ifa = (ifaddrmsg *)NLMSG_DATA(nh);
- size_t rtasize = IFA_PAYLOAD(nh);
- if (!parseIfAddrMessage(nh->nlmsg_type, ifa, rtasize)) {
- continue;
- }
+ } else if (nh->nlmsg_type == RTM_NEWROUTE ||
+ nh->nlmsg_type == RTM_DELROUTE) {
+ if (parseRtMessage(nh))
+ return true;
} else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) {
- int len = nh->nlmsg_len - sizeof(*nh);
- struct nduseroptmsg *ndmsg = (struct nduseroptmsg *) NLMSG_DATA(nh);
+ if (parseNdUserOptMessage(nh))
+ return true;
- if (sizeof(*ndmsg) > (size_t) len) {
- SLOGE("Got a short RTM_NEWNDUSEROPT message\n");
- continue;
- }
+ } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) {
+ if (parseNfPacketMessage(nh))
+ return true;
- size_t optsize = NLMSG_PAYLOAD(nh, sizeof(*ndmsg));
- if (!parseNdUserOptMessage(ndmsg, optsize)) {
- continue;
- }
-
-
- } else {
- SLOGD("Unexpected netlink message. type=0x%x\n",
- nh->nlmsg_type);
}
}
- return true;
+ return false;
}
/* If the string between 'str' and 'end' begins with 'prefixlen' characters
@@ -399,7 +594,6 @@
const char *s = buffer;
const char *end;
int param_idx = 0;
- int i;
int first = 1;
if (size == 0)
@@ -424,11 +618,11 @@
const char* a;
if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
if (!strcmp(a, "add"))
- mAction = NlActionAdd;
+ mAction = Action::kAdd;
else if (!strcmp(a, "remove"))
- mAction = NlActionRemove;
+ mAction = Action::kRemove;
else if (!strcmp(a, "change"))
- mAction = NlActionChange;
+ mAction = Action::kChange;
} else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
mSeq = atoi(a);
} else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
@@ -443,7 +637,8 @@
}
bool NetlinkEvent::decode(char *buffer, int size, int format) {
- if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
+ if (format == NetlinkListener::NETLINK_FORMAT_BINARY
+ || format == NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST) {
return parseBinaryNetlinkMessage(buffer, size);
} else {
return parseAsciiNetlinkMessage(buffer, size);
diff --git a/libsysutils/src/NetlinkListener.cpp b/libsysutils/src/NetlinkListener.cpp
index 9c447ca..637aa1e 100644
--- a/libsysutils/src/NetlinkListener.cpp
+++ b/libsysutils/src/NetlinkListener.cpp
@@ -47,8 +47,13 @@
ssize_t count;
uid_t uid = -1;
- count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_uid_recv(
- socket, mBuffer, sizeof(mBuffer), &uid));
+ bool require_group = true;
+ if (mFormat == NETLINK_FORMAT_BINARY_UNICAST) {
+ require_group = false;
+ }
+
+ count = TEMP_FAILURE_RETRY(uevent_kernel_recv(socket,
+ mBuffer, sizeof(mBuffer), require_group, &uid));
if (count < 0) {
if (uid > 0)
LOG_EVENT_INT(65537, uid);
@@ -57,10 +62,12 @@
}
NetlinkEvent *evt = new NetlinkEvent();
- if (!evt->decode(mBuffer, count, mFormat)) {
- SLOGE("Error decoding NetlinkEvent");
- } else {
+ if (evt->decode(mBuffer, count, mFormat)) {
onEvent(evt);
+ } else if (mFormat != NETLINK_FORMAT_BINARY) {
+ // Don't complain if parseBinaryNetlinkMessage returns false. That can
+ // just mean that the buffer contained no messages we're interested in.
+ SLOGE("Error decoding NetlinkEvent");
}
delete evt;
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index d3ce8f5..bb9b6a1 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -1,5 +1,6 @@
#include <alloca.h>
#include <errno.h>
+#include <malloc.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
@@ -220,7 +221,9 @@
sigaction(SIGPIPE, &old_action, &new_action);
- errno = e;
+ if (e != 0) {
+ errno = e;
+ }
return ret;
}
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index 5c75206..527a6a0 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -70,6 +70,10 @@
}
int SocketListener::startListener() {
+ return startListener(4);
+}
+
+int SocketListener::startListener(int backlog) {
if (!mSocketName && mSock == -1) {
SLOGE("Failed to start unbound listener");
@@ -84,7 +88,7 @@
SLOGV("got mSock = %d for %s", mSock, mSocketName);
}
- if (mListen && listen(mSock, 4) < 0) {
+ if (mListen && listen(mSock, backlog) < 0) {
SLOGE("Unable to listen on socket (%s)", strerror(errno));
return -1;
} else if (!mListen)
diff --git a/libusbhost/Android.mk b/libusbhost/Android.mk
index 9565cc5..5c12f2c 100644
--- a/libusbhost/Android.mk
+++ b/libusbhost/Android.mk
@@ -14,7 +14,7 @@
# limitations under the License.
#
-LOCAL_PATH := $(my-dir)
+LOCAL_PATH := $(call my-dir)
# Static library for Linux host
# ========================================================
@@ -25,6 +25,7 @@
LOCAL_MODULE := libusbhost
LOCAL_SRC_FILES := usbhost.c
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -38,7 +39,7 @@
LOCAL_MODULE := libusbhost
LOCAL_SRC_FILES := usbhost.c
-LOCAL_CFLAGS := -g -DUSE_LIBLOG
+LOCAL_CFLAGS := -g -DUSE_LIBLOG -Werror
# needed for logcat
LOCAL_SHARED_LIBRARIES := libcutils
@@ -52,5 +53,6 @@
LOCAL_MODULE := libusbhost
LOCAL_SRC_FILES := usbhost.c
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index ab1b856..684f401 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -263,11 +263,12 @@
D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ?
"new" : "gone", path, i);
if (i > 0 && i < MAX_USBFS_WD_COUNT) {
+ int local_ret = 0;
if (event->mask & IN_CREATE) {
- ret = inotify_add_watch(context->fd, path,
+ local_ret = inotify_add_watch(context->fd, path,
IN_CREATE | IN_DELETE);
- if (ret >= 0)
- context->wds[i] = ret;
+ if (local_ret >= 0)
+ context->wds[i] = local_ret;
done = find_existing_devices_bus(path, context->cb_added,
context->data);
} else if (event->mask & IN_DELETE) {
@@ -694,6 +695,6 @@
int usb_request_cancel(struct usb_request *req)
{
struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
- return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, &urb);
+ return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb);
}
diff --git a/libutils/Android.mk b/libutils/Android.mk
index 1710d36..e9c5f89 100644
--- a/libutils/Android.mk
+++ b/libutils/Android.mk
@@ -14,9 +14,6 @@
LOCAL_PATH:= $(call my-dir)
-# libutils is a little unique: It's built twice, once for the host
-# and once for the device.
-
commonSources:= \
BasicHashtable.cpp \
BlobCache.cpp \
@@ -26,6 +23,7 @@
LinearAllocator.cpp \
LinearTransform.cpp \
Log.cpp \
+ NativeHandle.cpp \
Printer.cpp \
ProcessCallStack.cpp \
PropertyMap.cpp \
@@ -41,9 +39,9 @@
Tokenizer.cpp \
Unicode.cpp \
VectorImpl.cpp \
- misc.cpp
+ misc.cpp \
-host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
+host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror
ifeq ($(HOST_OS),windows)
ifeq ($(strip $(USE_CYGWIN),),)
@@ -52,13 +50,6 @@
endif
endif
-host_commonLdlibs :=
-
-ifeq ($(TARGET_OS),linux)
-host_commonLdlibs += -lrt -ldl
-endif
-
-
# For the host
# =====================================================
include $(CLEAR_VARS)
@@ -66,24 +57,13 @@
ifeq ($(HOST_OS), linux)
LOCAL_SRC_FILES += Looper.cpp
endif
+ifeq ($(HOST_OS),darwin)
+LOCAL_CFLAGS += -Wno-unused-parameter
+endif
LOCAL_MODULE:= libutils
LOCAL_STATIC_LIBRARIES := liblog
LOCAL_CFLAGS += $(host_commonCflags)
-LOCAL_LDLIBS += $(host_commonLdlibs)
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-
-# For the host, 64-bit
-# =====================================================
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= $(commonSources)
-ifeq ($(HOST_OS), linux)
-LOCAL_SRC_FILES += Looper.cpp
-endif
-LOCAL_MODULE:= lib64utils
-LOCAL_STATIC_LIBRARIES := liblog
-LOCAL_CFLAGS += $(host_commonCflags) -m64
-LOCAL_LDLIBS += $(host_commonLdlibs)
+LOCAL_MULTILIB := both
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -98,19 +78,10 @@
Looper.cpp \
Trace.cpp
-ifeq ($(TARGET_OS),linux)
-LOCAL_LDLIBS += -lrt -ldl
-endif
-
ifeq ($(TARGET_ARCH),mips)
LOCAL_CFLAGS += -DALIGN_DOUBLE
endif
-
-LOCAL_C_INCLUDES += \
- bionic/libc/private \
- external/zlib
-
-LOCAL_LDLIBS += -lpthread
+LOCAL_CFLAGS += -Werror
LOCAL_STATIC_LIBRARIES := \
libcutils
@@ -120,8 +91,6 @@
liblog \
libdl
-include external/stlport/libstlport.mk
-
LOCAL_MODULE:= libutils
include $(BUILD_STATIC_LIBRARY)
@@ -134,17 +103,11 @@
libbacktrace \
libcutils \
libdl \
- liblog \
-
-include external/stlport/libstlport.mk
+ liblog
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
-# Include subdirectory makefiles
-# ============================================================
-# If we're building with ONE_SHOT_MAKEFILE (mm, mmm), then what the framework
-# team really wants is to build the stuff defined by this makefile.
-ifeq (,$(ONE_SHOT_MAKEFILE))
+# Build the tests in the tests/ subdirectory.
include $(call first-makefiles-under,$(LOCAL_PATH))
-endif
diff --git a/libutils/BlobCache.cpp b/libutils/BlobCache.cpp
index 0fb1d8e..0ea09cf 100644
--- a/libutils/BlobCache.cpp
+++ b/libutils/BlobCache.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "BlobCache"
//#define LOG_NDEBUG 0
+#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
@@ -27,10 +28,10 @@
namespace android {
// BlobCache::Header::mMagicNumber value
-static const uint32_t blobCacheMagic = '_Bb$';
+static const uint32_t blobCacheMagic = ('_' << 24) + ('B' << 16) + ('b' << 8) + '$';
// BlobCache::Header::mBlobCacheVersion value
-static const uint32_t blobCacheVersion = 1;
+static const uint32_t blobCacheVersion = 2;
// BlobCache::Header::mDeviceVersion value
static const uint32_t blobCacheDeviceVersion = 1;
@@ -48,24 +49,24 @@
mRandState[1] = (now >> 16) & 0xFFFF;
mRandState[2] = (now >> 32) & 0xFFFF;
#endif
- ALOGV("initializing random seed using %lld", now);
+ ALOGV("initializing random seed using %lld", (unsigned long long)now);
}
void BlobCache::set(const void* key, size_t keySize, const void* value,
size_t valueSize) {
if (mMaxKeySize < keySize) {
- ALOGV("set: not caching because the key is too large: %d (limit: %d)",
+ ALOGV("set: not caching because the key is too large: %zu (limit: %zu)",
keySize, mMaxKeySize);
return;
}
if (mMaxValueSize < valueSize) {
- ALOGV("set: not caching because the value is too large: %d (limit: %d)",
+ ALOGV("set: not caching because the value is too large: %zu (limit: %zu)",
valueSize, mMaxValueSize);
return;
}
if (mMaxTotalSize < keySize + valueSize) {
ALOGV("set: not caching because the combined key/value size is too "
- "large: %d (limit: %d)", keySize + valueSize, mMaxTotalSize);
+ "large: %zu (limit: %zu)", keySize + valueSize, mMaxTotalSize);
return;
}
if (keySize == 0) {
@@ -94,15 +95,15 @@
continue;
} else {
ALOGV("set: not caching new key/value pair because the "
- "total cache size limit would be exceeded: %d "
- "(limit: %d)",
+ "total cache size limit would be exceeded: %zu "
+ "(limit: %zu)",
keySize + valueSize, mMaxTotalSize);
break;
}
}
mCacheEntries.add(CacheEntry(keyBlob, valueBlob));
mTotalSize = newTotalSize;
- ALOGV("set: created new cache entry with %d byte key and %d byte value",
+ ALOGV("set: created new cache entry with %zu byte key and %zu byte value",
keySize, valueSize);
} else {
// Update the existing cache entry.
@@ -116,14 +117,14 @@
continue;
} else {
ALOGV("set: not caching new value because the total cache "
- "size limit would be exceeded: %d (limit: %d)",
+ "size limit would be exceeded: %zu (limit: %zu)",
keySize + valueSize, mMaxTotalSize);
break;
}
}
mCacheEntries.editItemAt(index).setValue(valueBlob);
mTotalSize = newTotalSize;
- ALOGV("set: updated existing cache entry with %d byte key and %d byte "
+ ALOGV("set: updated existing cache entry with %zu byte key and %zu byte "
"value", keySize, valueSize);
}
break;
@@ -133,7 +134,7 @@
size_t BlobCache::get(const void* key, size_t keySize, void* value,
size_t valueSize) {
if (mMaxKeySize < keySize) {
- ALOGV("get: not searching because the key is too large: %d (limit %d)",
+ ALOGV("get: not searching because the key is too large: %zu (limit %zu)",
keySize, mMaxKeySize);
return 0;
}
@@ -141,7 +142,7 @@
CacheEntry dummyEntry(dummyKey, NULL);
ssize_t index = mCacheEntries.indexOf(dummyEntry);
if (index < 0) {
- ALOGV("get: no cache entry found for key of size %d", keySize);
+ ALOGV("get: no cache entry found for key of size %zu", keySize);
return 0;
}
@@ -150,10 +151,10 @@
sp<Blob> valueBlob(mCacheEntries[index].getValue());
size_t valueBlobSize = valueBlob->getSize();
if (valueBlobSize <= valueSize) {
- ALOGV("get: copying %d bytes to caller's buffer", valueBlobSize);
+ ALOGV("get: copying %zu bytes to caller's buffer", valueBlobSize);
memcpy(value, valueBlob->getData(), valueBlobSize);
} else {
- ALOGV("get: caller's buffer is too small for value: %d (needs %d)",
+ ALOGV("get: caller's buffer is too small for value: %zu (needs %zu)",
valueSize, valueBlobSize);
}
return valueBlobSize;
@@ -164,14 +165,13 @@
}
size_t BlobCache::getFlattenedSize() const {
- size_t size = sizeof(Header);
+ size_t size = align4(sizeof(Header));
for (size_t i = 0; i < mCacheEntries.size(); i++) {
const CacheEntry& e(mCacheEntries[i]);
sp<Blob> keyBlob = e.getKey();
sp<Blob> valueBlob = e.getValue();
- size = align4(size);
- size += sizeof(EntryHeader) + keyBlob->getSize() +
- valueBlob->getSize();
+ size += align4(sizeof(EntryHeader) + keyBlob->getSize() +
+ valueBlob->getSize());
}
return size;
}
@@ -199,7 +199,8 @@
size_t valueSize = valueBlob->getSize();
size_t entrySize = sizeof(EntryHeader) + keySize + valueSize;
- if (byteOffset + entrySize > size) {
+ size_t totalSize = align4(entrySize);
+ if (byteOffset + totalSize > size) {
ALOGE("flatten: not enough room for cache entries");
return BAD_VALUE;
}
@@ -212,7 +213,13 @@
memcpy(eheader->mData, keyBlob->getData(), keySize);
memcpy(eheader->mData + keySize, valueBlob->getData(), valueSize);
- byteOffset += align4(entrySize);
+ if (totalSize > entrySize) {
+ // We have padding bytes. Those will get written to storage, and contribute to the CRC,
+ // so make sure we zero-them to have reproducible results.
+ memset(eheader->mData + keySize + valueSize, 0, totalSize - entrySize);
+ }
+
+ byteOffset += totalSize;
}
return OK;
@@ -229,7 +236,7 @@
}
const Header* header = reinterpret_cast<const Header*>(buffer);
if (header->mMagicNumber != blobCacheMagic) {
- ALOGE("unflatten: bad magic number: %d", header->mMagicNumber);
+ ALOGE("unflatten: bad magic number: %" PRIu32, header->mMagicNumber);
return BAD_VALUE;
}
if (header->mBlobCacheVersion != blobCacheVersion ||
@@ -255,7 +262,8 @@
size_t valueSize = eheader->mValueSize;
size_t entrySize = sizeof(EntryHeader) + keySize + valueSize;
- if (byteOffset + entrySize > size) {
+ size_t totalSize = align4(entrySize);
+ if (byteOffset + totalSize > size) {
mCacheEntries.clear();
ALOGE("unflatten: not enough room for cache entry headers");
return BAD_VALUE;
@@ -264,7 +272,7 @@
const uint8_t* data = eheader->mData;
set(data, keySize, data + keySize, valueSize);
- byteOffset += align4(entrySize);
+ byteOffset += totalSize;
}
return OK;
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index 9ce370e..91e45d8 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -23,10 +23,17 @@
#include <utils/FileMap.h>
#include <utils/Log.h>
+#if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
+# define PRId32 "I32d"
+# define PRIx32 "I32x"
+# define PRId64 "I64d"
+#else
+#include <inttypes.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
-#ifdef HAVE_POSIX_FILEMAP
+#if !defined(__MINGW32__)
#include <sys/mman.h>
#endif
@@ -39,125 +46,103 @@
/*static*/ long FileMap::mPageSize = -1;
-
-/*
- * Constructor. Create an empty object.
- */
+// Constructor. Create an empty object.
FileMap::FileMap(void)
- : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
+ : mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
mDataPtr(NULL), mDataLength(0)
{
}
-/*
- * Destructor.
- */
+// Destructor.
FileMap::~FileMap(void)
{
- assert(mRefCount == 0);
-
- //printf("+++ removing FileMap %p %u\n", mDataPtr, mDataLength);
-
- mRefCount = -100; // help catch double-free
if (mFileName != NULL) {
free(mFileName);
}
-#ifdef HAVE_POSIX_FILEMAP
- if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
- ALOGD("munmap(%p, %d) failed\n", mBasePtr, (int) mBaseLength);
- }
-#endif
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
- ALOGD("UnmapViewOfFile(%p) failed, error = %ld\n", mBasePtr,
+ ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
GetLastError() );
}
if (mFileMapping != INVALID_HANDLE_VALUE) {
CloseHandle(mFileMapping);
}
- CloseHandle(mFileHandle);
+#else
+ if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
+ ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
+ }
#endif
}
-/*
- * Create a new mapping on an open file.
- *
- * Closing the file descriptor does not unmap the pages, so we don't
- * claim ownership of the fd.
- *
- * Returns "false" on failure.
- */
+// Create a new mapping on an open file.
+//
+// Closing the file descriptor does not unmap the pages, so we don't
+// claim ownership of the fd.
+//
+// Returns "false" on failure.
bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
bool readOnly)
{
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
int adjust;
off64_t adjOffset;
size_t adjLength;
if (mPageSize == -1) {
SYSTEM_INFO si;
-
+
GetSystemInfo( &si );
mPageSize = si.dwAllocationGranularity;
}
DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
-
+
mFileHandle = (HANDLE) _get_osfhandle(fd);
mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
if (mFileMapping == NULL) {
- ALOGE("CreateFileMapping(%p, %lx) failed with error %ld\n",
+ ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
mFileHandle, protect, GetLastError() );
return false;
}
-
+
adjust = offset % mPageSize;
adjOffset = offset - adjust;
adjLength = length + adjust;
-
- mBasePtr = MapViewOfFile( mFileMapping,
+
+ mBasePtr = MapViewOfFile( mFileMapping,
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
0,
(DWORD)(adjOffset),
adjLength );
if (mBasePtr == NULL) {
- ALOGE("MapViewOfFile(%ld, %ld) failed with error %ld\n",
+ ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
adjOffset, adjLength, GetLastError() );
CloseHandle(mFileMapping);
mFileMapping = INVALID_HANDLE_VALUE;
return false;
}
-#endif
-#ifdef HAVE_POSIX_FILEMAP
+#else // !defined(__MINGW32__)
int prot, flags, adjust;
off64_t adjOffset;
size_t adjLength;
void* ptr;
- assert(mRefCount == 1);
assert(fd >= 0);
assert(offset >= 0);
assert(length > 0);
- /* init on first use */
+ // init on first use
if (mPageSize == -1) {
-#if NOT_USING_KLIBC
mPageSize = sysconf(_SC_PAGESIZE);
if (mPageSize == -1) {
ALOGE("could not get _SC_PAGESIZE\n");
return false;
}
-#else
- /* this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM */
- mPageSize = 4096;
-#endif
}
- adjust = offset % mPageSize;
-try_again:
+ adjust = offset % mPageSize;
adjOffset = offset - adjust;
adjLength = length + adjust;
@@ -168,19 +153,12 @@
ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
if (ptr == MAP_FAILED) {
- // Cygwin does not seem to like file mapping files from an offset.
- // So if we fail, try again with offset zero
- if (adjOffset > 0) {
- adjust = offset;
- goto try_again;
- }
-
- ALOGE("mmap(%ld,%ld) failed: %s\n",
- (long) adjOffset, (long) adjLength, strerror(errno));
+ ALOGE("mmap(%lld,%zu) failed: %s\n",
+ (long long)adjOffset, adjLength, strerror(errno));
return false;
}
mBasePtr = ptr;
-#endif /* HAVE_POSIX_FILEMAP */
+#endif // !defined(__MINGW32__)
mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
mBaseLength = adjLength;
@@ -190,18 +168,16 @@
assert(mBasePtr != NULL);
- ALOGV("MAP: base %p/%d data %p/%d\n",
- mBasePtr, (int) mBaseLength, mDataPtr, (int) mDataLength);
+ ALOGV("MAP: base %p/%zu data %p/%zu\n",
+ mBasePtr, mBaseLength, mDataPtr, mDataLength);
return true;
}
-/*
- * Provide guidance to the system.
- */
+// Provide guidance to the system.
+#if !defined(_WIN32)
int FileMap::advise(MapAdvice advice)
{
-#if HAVE_MADVISE
int cc, sysAdvice;
switch (advice) {
@@ -219,7 +195,11 @@
if (cc != 0)
ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
return cc;
-#else
- return -1;
-#endif // HAVE_MADVISE
}
+
+#else
+int FileMap::advise(MapAdvice /* advice */)
+{
+ return -1;
+}
+#endif
diff --git a/libutils/LinearAllocator.cpp b/libutils/LinearAllocator.cpp
index a07a291..8b90696 100644
--- a/libutils/LinearAllocator.cpp
+++ b/libutils/LinearAllocator.cpp
@@ -92,7 +92,7 @@
: mNextPage(0)
{}
- void* operator new(size_t size, void* buf) { return buf; }
+ void* operator new(size_t /*size*/, void* buf) { return buf; }
void* start() {
return (void*) (((size_t)this) + sizeof(Page));
@@ -103,7 +103,7 @@
}
private:
- Page(const Page& other) {}
+ Page(const Page& /*other*/) {}
Page* mNextPage;
};
diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp
index 9a2dd6c..d739f11 100644
--- a/libutils/Looper.cpp
+++ b/libutils/Looper.cpp
@@ -20,6 +20,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
+#include <inttypes.h>
namespace android {
@@ -68,7 +69,8 @@
Looper::Looper(bool allowNonCallbacks) :
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
- mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
+ mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
+ mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
int wakeFds[2];
int result = pipe(wakeFds);
LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
@@ -84,25 +86,16 @@
LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
errno);
- mIdling = false;
-
- // Allocate the epoll instance and register the wake pipe.
- mEpollFd = epoll_create(EPOLL_SIZE_HINT);
- LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
-
- struct epoll_event eventItem;
- memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
- eventItem.events = EPOLLIN;
- eventItem.data.fd = mWakeReadPipeFd;
- result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
- errno);
+ AutoMutex _l(mLock);
+ rebuildEpollLocked();
}
Looper::~Looper() {
close(mWakeReadPipeFd);
close(mWakeWritePipeFd);
- close(mEpollFd);
+ if (mEpollFd >= 0) {
+ close(mEpollFd);
+ }
}
void Looper::initTLSKey() {
@@ -156,6 +149,50 @@
return mAllowNonCallbacks;
}
+void Looper::rebuildEpollLocked() {
+ // Close old epoll instance if we have one.
+ if (mEpollFd >= 0) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
+#endif
+ close(mEpollFd);
+ }
+
+ // Allocate the new epoll instance and register the wake pipe.
+ mEpollFd = epoll_create(EPOLL_SIZE_HINT);
+ LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
+
+ struct epoll_event eventItem;
+ memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
+ eventItem.events = EPOLLIN;
+ eventItem.data.fd = mWakeReadPipeFd;
+ int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);
+ LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
+ errno);
+
+ for (size_t i = 0; i < mRequests.size(); i++) {
+ const Request& request = mRequests.valueAt(i);
+ struct epoll_event eventItem;
+ request.initEventItem(&eventItem);
+
+ int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, request.fd, & eventItem);
+ if (epollResult < 0) {
+ ALOGE("Error adding epoll events for fd %d while rebuilding epoll set, errno=%d",
+ request.fd, errno);
+ }
+ }
+}
+
+void Looper::scheduleEpollRebuildLocked() {
+ if (!mEpollRebuildRequired) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ scheduleEpollRebuildLocked - scheduling epoll set rebuild", this);
+#endif
+ mEpollRebuildRequired = true;
+ wake();
+ }
+}
+
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
int result = 0;
for (;;) {
@@ -206,7 +243,7 @@
timeoutMillis = messageTimeoutMillis;
}
#if DEBUG_POLL_AND_WAKE
- ALOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
+ ALOGD("%p ~ pollOnce - next message in %" PRId64 "ns, adjusted timeout: timeoutMillis=%d",
this, mNextMessageUptime - now, timeoutMillis);
#endif
}
@@ -217,17 +254,24 @@
mResponseIndex = 0;
// We are about to idle.
- mIdling = true;
+ mPolling = true;
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
// No longer idling.
- mIdling = false;
+ mPolling = false;
// Acquire lock.
mLock.lock();
+ // Rebuild epoll set if needed.
+ if (mEpollRebuildRequired) {
+ mEpollRebuildRequired = false;
+ rebuildEpollLocked();
+ goto Done;
+ }
+
// Check for poll error.
if (eventCount < 0) {
if (errno == EINTR) {
@@ -326,10 +370,14 @@
ALOGD("%p ~ pollOnce - invoking fd event callback %p: fd=%d, events=0x%x, data=%p",
this, response.request.callback.get(), fd, events, data);
#endif
+ // Invoke the callback. Note that the file descriptor may be closed by
+ // the callback (and potentially even reused) before the function returns so
+ // we need to be a little careful when removing the file descriptor afterwards.
int callbackResult = response.request.callback->handleEvent(fd, events, data);
if (callbackResult == 0) {
- removeFd(fd);
+ removeFd(fd, response.request.seq);
}
+
// Clear the callback reference in the response structure promptly because we
// will not clear the response vector itself until the next poll.
response.request.callback.clear();
@@ -425,23 +473,20 @@
ident = POLL_CALLBACK;
}
- int epollEvents = 0;
- if (events & EVENT_INPUT) epollEvents |= EPOLLIN;
- if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT;
-
{ // acquire lock
AutoMutex _l(mLock);
Request request;
request.fd = fd;
request.ident = ident;
+ request.events = events;
+ request.seq = mNextRequestSeq++;
request.callback = callback;
request.data = data;
+ if (mNextRequestSeq == -1) mNextRequestSeq = 0; // reserve sequence number -1
struct epoll_event eventItem;
- memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
- eventItem.events = epollEvents;
- eventItem.data.fd = fd;
+ request.initEventItem(&eventItem);
ssize_t requestIndex = mRequests.indexOfKey(fd);
if (requestIndex < 0) {
@@ -454,8 +499,36 @@
} else {
int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_MOD, fd, & eventItem);
if (epollResult < 0) {
- ALOGE("Error modifying epoll events for fd %d, errno=%d", fd, errno);
- return -1;
+ if (errno == ENOENT) {
+ // Tolerate ENOENT because it means that an older file descriptor was
+ // closed before its callback was unregistered and meanwhile a new
+ // file descriptor with the same number has been created and is now
+ // being registered for the first time. This error may occur naturally
+ // when a callback has the side-effect of closing the file descriptor
+ // before returning and unregistering itself. Callback sequence number
+ // checks further ensure that the race is benign.
+ //
+ // Unfortunately due to kernel limitations we need to rebuild the epoll
+ // set from scratch because it may contain an old file handle that we are
+ // now unable to remove since its file descriptor is no longer valid.
+ // No such problem would have occurred if we were using the poll system
+ // call instead, but that approach carries others disadvantages.
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ addFd - EPOLL_CTL_MOD failed due to file descriptor "
+ "being recycled, falling back on EPOLL_CTL_ADD, errno=%d",
+ this, errno);
+#endif
+ epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ if (epollResult < 0) {
+ ALOGE("Error modifying or adding epoll events for fd %d, errno=%d",
+ fd, errno);
+ return -1;
+ }
+ scheduleEpollRebuildLocked();
+ } else {
+ ALOGE("Error modifying epoll events for fd %d, errno=%d", fd, errno);
+ return -1;
+ }
}
mRequests.replaceValueAt(requestIndex, request);
}
@@ -464,8 +537,12 @@
}
int Looper::removeFd(int fd) {
+ return removeFd(fd, -1);
+}
+
+int Looper::removeFd(int fd, int seq) {
#if DEBUG_CALLBACKS
- ALOGD("%p ~ removeFd - fd=%d", this, fd);
+ ALOGD("%p ~ removeFd - fd=%d, seq=%d", this, fd, seq);
#endif
{ // acquire lock
@@ -475,13 +552,43 @@
return 0;
}
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
- if (epollResult < 0) {
- ALOGE("Error removing epoll events for fd %d, errno=%d", fd, errno);
- return -1;
+ // Check the sequence number if one was given.
+ if (seq != -1 && mRequests.valueAt(requestIndex).seq != seq) {
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ removeFd - sequence number mismatch, oldSeq=%d",
+ this, mRequests.valueAt(requestIndex).seq);
+#endif
+ return 0;
}
+ // Always remove the FD from the request map even if an error occurs while
+ // updating the epoll set so that we avoid accidentally leaking callbacks.
mRequests.removeItemsAt(requestIndex);
+
+ int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
+ if (epollResult < 0) {
+ if (seq != -1 && (errno == EBADF || errno == ENOENT)) {
+ // Tolerate EBADF or ENOENT when the sequence number is known because it
+ // means that the file descriptor was closed before its callback was
+ // unregistered. This error may occur naturally when a callback has the
+ // side-effect of closing the file descriptor before returning and
+ // unregistering itself.
+ //
+ // Unfortunately due to kernel limitations we need to rebuild the epoll
+ // set from scratch because it may contain an old file handle that we are
+ // now unable to remove since its file descriptor is no longer valid.
+ // No such problem would have occurred if we were using the poll system
+ // call instead, but that approach carries others disadvantages.
+#if DEBUG_CALLBACKS
+ ALOGD("%p ~ removeFd - EPOLL_CTL_DEL failed due to file descriptor "
+ "being closed, errno=%d", this, errno);
+#endif
+ scheduleEpollRebuildLocked();
+ } else {
+ ALOGE("Error removing epoll events for fd %d, errno=%d", fd, errno);
+ return -1;
+ }
+ }
} // release lock
return 1;
}
@@ -500,7 +607,7 @@
void Looper::sendMessageAtTime(nsecs_t uptime, const sp<MessageHandler>& handler,
const Message& message) {
#if DEBUG_CALLBACKS
- ALOGD("%p ~ sendMessageAtTime - uptime=%lld, handler=%p, what=%d",
+ ALOGD("%p ~ sendMessageAtTime - uptime=%" PRId64 ", handler=%p, what=%d",
this, uptime, handler.get(), message.what);
#endif
@@ -566,8 +673,18 @@
} // release lock
}
-bool Looper::isIdling() const {
- return mIdling;
+bool Looper::isPolling() const {
+ return mPolling;
+}
+
+void Looper::Request::initEventItem(struct epoll_event* eventItem) const {
+ int epollEvents = 0;
+ if (events & EVENT_INPUT) epollEvents |= EPOLLIN;
+ if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT;
+
+ memset(eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
+ eventItem->events = epollEvents;
+ eventItem->data.fd = fd;
}
} // namespace android
diff --git a/libutils/NativeHandle.cpp b/libutils/NativeHandle.cpp
new file mode 100644
index 0000000..e4daca7
--- /dev/null
+++ b/libutils/NativeHandle.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 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 <utils/NativeHandle.h>
+#include <cutils/native_handle.h>
+
+namespace android {
+
+sp<NativeHandle> NativeHandle::create(
+ native_handle_t* handle, bool ownsHandle) {
+ return handle ? new NativeHandle(handle, ownsHandle) : NULL;
+}
+
+NativeHandle::NativeHandle(native_handle_t* handle, bool ownsHandle)
+: mHandle(handle), mOwnsHandle(ownsHandle)
+{}
+
+NativeHandle::~NativeHandle() {
+ if (mOwnsHandle) {
+ native_handle_close(mHandle);
+ native_handle_delete(mHandle);
+ }
+}
+
+} // namespace android
diff --git a/libutils/Printer.cpp b/libutils/Printer.cpp
index 263e740..1dc8632 100644
--- a/libutils/Printer.cpp
+++ b/libutils/Printer.cpp
@@ -25,10 +25,6 @@
#include <stdio.h>
#include <stdlib.h>
-#ifndef __BIONIC__
-#define fdprintf dprintf
-#endif
-
namespace android {
/*
@@ -120,7 +116,7 @@
}
#ifndef USE_MINGW
- fdprintf(mFd, mFormatString, mPrefix, string);
+ dprintf(mFd, mFormatString, mPrefix, string);
#endif
}
diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp
index f837bcb..db07e56 100644
--- a/libutils/ProcessCallStack.cpp
+++ b/libutils/ProcessCallStack.cpp
@@ -90,6 +90,11 @@
ALOGE("%s: Failed to open %s", __FUNCTION__, path);
}
+ if (procName == NULL) {
+ // Reading /proc/self/task/%d/comm failed due to a race
+ return String8::format("[err-unknown-tid-%d]", tid);
+ }
+
// Strip ending newline
strtok(procName, "\n");
diff --git a/libutils/RefBase.cpp b/libutils/RefBase.cpp
index f398a82..02907ad 100644
--- a/libutils/RefBase.cpp
+++ b/libutils/RefBase.cpp
@@ -17,6 +17,14 @@
#define LOG_TAG "RefBase"
// #define LOG_NDEBUG 0
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <typeinfo>
+#include <unistd.h>
+
#include <utils/RefBase.h>
#include <utils/Atomic.h>
@@ -24,13 +32,9 @@
#include <utils/Log.h>
#include <utils/threads.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <typeinfo>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
// compile with refcounting debugging enabled
#define DEBUG_REFS 0
@@ -109,7 +113,7 @@
char inc = refs->ref >= 0 ? '+' : '-';
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
#if DEBUG_REFS_CALLSTACK_ENABLED
- refs->stack.dump(LOG_TAG);
+ refs->stack.log(LOG_TAG);
#endif
refs = refs->next;
}
@@ -123,7 +127,7 @@
char inc = refs->ref >= 0 ? '+' : '-';
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
#if DEBUG_REFS_CALLSTACK_ENABLED
- refs->stack.dump(LOG_TAG);
+ refs->stack.log(LOG_TAG);
#endif
refs = refs->next;
}
@@ -388,7 +392,7 @@
{
weakref_impl* const impl = static_cast<weakref_impl*>(this);
impl->addWeakRef(id);
- const int32_t c = android_atomic_inc(&impl->mWeak);
+ const int32_t c __unused = android_atomic_inc(&impl->mWeak);
ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
}
@@ -615,7 +619,7 @@
{
}
-bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id)
+bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
{
return (flags&FIRST_INC_STRONG) ? true : false;
}
@@ -626,13 +630,15 @@
// ---------------------------------------------------------------------------
-void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
#if DEBUG_REFS
+void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
for (size_t i=0 ; i<n ; i++) {
renamer(i);
}
-#endif
}
+#else
+void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
+#endif
void RefBase::renameRefId(weakref_type* ref,
const void* old_id, const void* new_id) {
diff --git a/libutils/StopWatch.cpp b/libutils/StopWatch.cpp
index b1708d6..8c7b596 100644
--- a/libutils/StopWatch.cpp
+++ b/libutils/StopWatch.cpp
@@ -21,7 +21,9 @@
#include <stdio.h>
/* for PRId64 */
+#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS 1
+#endif
#include <inttypes.h>
#include <utils/Log.h>
diff --git a/libutils/String16.cpp b/libutils/String16.cpp
index b09b728..91efdaa 100644
--- a/libutils/String16.cpp
+++ b/libutils/String16.cpp
@@ -16,7 +16,6 @@
#include <utils/String16.h>
-#include <utils/Debug.h>
#include <utils/Log.h>
#include <utils/Unicode.h>
#include <utils/String8.h>
@@ -67,8 +66,6 @@
return getEmptyString();
}
- const uint8_t* const u8end = u8cur + u8len;
-
SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
if (buf) {
u8cur = (const uint8_t*) u8str;
diff --git a/libutils/String8.cpp b/libutils/String8.cpp
index e852d77..3323b82 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -323,8 +323,17 @@
status_t String8::appendFormatV(const char* fmt, va_list args)
{
- int result = NO_ERROR;
- int n = vsnprintf(NULL, 0, fmt, args);
+ int n, result = NO_ERROR;
+ va_list tmp_args;
+
+ /* args is undefined after vsnprintf.
+ * So we need a copy here to avoid the
+ * second vsnprintf access undefined args.
+ */
+ va_copy(tmp_args, args);
+ n = vsnprintf(NULL, 0, fmt, tmp_args);
+ va_end(tmp_args);
+
if (n != 0) {
size_t oldLength = length();
char* buf = lockBuffer(oldLength + n);
@@ -399,6 +408,30 @@
return p ? p-mString : -1;
}
+bool String8::removeAll(const char* other) {
+ ssize_t index = find(other);
+ if (index < 0) return false;
+
+ char* buf = lockBuffer(size());
+ if (!buf) return false; // out of memory
+
+ size_t skip = strlen(other);
+ size_t len = size();
+ size_t tail = index;
+ while (size_t(index) < len) {
+ ssize_t next = find(other, index + skip);
+ if (next < 0) {
+ next = len;
+ }
+
+ memmove(buf + tail, buf + index + skip, next - index - skip);
+ tail += next - index - skip;
+ index = next;
+ }
+ unlockBuffer(tail);
+ return true;
+}
+
void String8::toLower()
{
toLower(0, size());
@@ -542,7 +575,6 @@
{
const char* lastSlash;
const char* lastDot;
- int extLen;
const char* const str = mString;
// only look at the filename
diff --git a/libutils/SystemClock.cpp b/libutils/SystemClock.cpp
index 413250f..ac3dd98 100644
--- a/libutils/SystemClock.cpp
+++ b/libutils/SystemClock.cpp
@@ -68,13 +68,7 @@
*/
#define DEBUG_TIMESTAMP 0
-static const char *gettime_method_names[] = {
- "clock_gettime",
- "ioctl",
- "systemTime",
-};
-
-#if DEBUG_TIMESTAMP
+#if DEBUG_TIMESTAMP && defined(__arm__)
static inline void checkTimeStamps(int64_t timestamp,
int64_t volatile *prevTimestampPtr,
int volatile *prevMethodPtr,
@@ -85,11 +79,16 @@
* gettid, and int64_t is different on the ARM platform
* (ie long vs long long).
*/
-#ifdef ARCH_ARM
int64_t prevTimestamp = *prevTimestampPtr;
int prevMethod = *prevMethodPtr;
if (timestamp < prevTimestamp) {
+ static const char *gettime_method_names[] = {
+ "clock_gettime",
+ "ioctl",
+ "systemTime",
+ };
+
ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d",
prevTimestamp, gettime_method_names[prevMethod],
timestamp, gettime_method_names[curMethod],
@@ -99,7 +98,6 @@
// write is interrupted or not observed as a whole.
*prevTimestampPtr = timestamp;
*prevMethodPtr = curMethod;
-#endif
}
#else
#define checkTimeStamps(timestamp, prevTimestampPtr, prevMethodPtr, curMethod)
diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp
index ff74914..1e014c6 100644
--- a/libutils/Threads.cpp
+++ b/libutils/Threads.cpp
@@ -17,36 +17,39 @@
// #define LOG_NDEBUG 0
#define LOG_TAG "libutils.threads"
-#include <utils/threads.h>
-#include <utils/Log.h>
-
-#include <cutils/sched_policy.h>
-
+#include <assert.h>
+#include <errno.h>
+#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
-#include <memory.h>
-#include <errno.h>
-#include <assert.h>
#include <unistd.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
# include <sched.h>
# include <sys/resource.h>
-#ifdef HAVE_ANDROID_OS
-# include <bionic_pthread.h>
-#endif
-#elif defined(HAVE_WIN32_THREADS)
+#else
# include <windows.h>
# include <stdint.h>
# include <process.h>
# define HAVE_CREATETHREAD // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW
#endif
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
#include <sys/prctl.h>
#endif
+#include <utils/threads.h>
+#include <utils/Log.h>
+
+#include <cutils/sched_policy.h>
+
+#ifdef HAVE_ANDROID_OS
+# define __android_unused
+#else
+# define __android_unused __attribute__((__unused__))
+#endif
+
/*
* ===========================================================================
* Thread wrappers
@@ -56,7 +59,7 @@
using namespace android;
// ----------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// ----------------------------------------------------------------------------
/*
@@ -87,7 +90,7 @@
} else {
set_sched_policy(0, SP_FOREGROUND);
}
-
+
if (name) {
androidSetThreadName(name);
free(name);
@@ -97,7 +100,7 @@
};
void androidSetThreadName(const char* name) {
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
// Mac OS doesn't have this, and we build libutil for the host too
int hasAt = 0;
int hasDot = 0;
@@ -119,12 +122,12 @@
int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
void *userData,
- const char* threadName,
+ const char* threadName __android_unused,
int32_t threadPriority,
size_t threadStackSize,
android_thread_id_t *threadId)
{
- pthread_attr_t attr;
+ pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -143,14 +146,14 @@
t->entryFunction = entryFunction;
t->userData = userData;
entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
- userData = t;
+ userData = t;
}
#endif
if (threadStackSize) {
pthread_attr_setstacksize(&attr, threadStackSize);
}
-
+
errno = 0;
pthread_t thread;
int result = pthread_create(&thread, &attr,
@@ -185,7 +188,7 @@
}
// ----------------------------------------------------------------------------
-#elif defined(HAVE_WIN32_THREADS)
+#else // !defined(_WIN32)
// ----------------------------------------------------------------------------
/*
@@ -251,9 +254,9 @@
int androidCreateRawThreadEtc(android_thread_func_t fn,
void *userData,
- const char* threadName,
- int32_t threadPriority,
- size_t threadStackSize,
+ const char* /*threadName*/,
+ int32_t /*threadPriority*/,
+ size_t /*threadStackSize*/,
android_thread_id_t *threadId)
{
return doCreateThread( fn, userData, threadId);
@@ -265,9 +268,7 @@
}
// ----------------------------------------------------------------------------
-#else
-#error "Threads not supported"
-#endif
+#endif // !defined(_WIN32)
// ----------------------------------------------------------------------------
@@ -300,21 +301,12 @@
gCreateThreadFn = func;
}
-pid_t androidGetTid()
-{
-#ifdef HAVE_GETTID
- return gettid();
-#else
- return getpid();
-#endif
-}
-
#ifdef HAVE_ANDROID_OS
int androidSetThreadPriority(pid_t tid, int pri)
{
int rc = 0;
-
-#if defined(HAVE_PTHREADS)
+
+#if !defined(_WIN32)
int lasterr = 0;
if (pri >= ANDROID_PRIORITY_BACKGROUND) {
@@ -333,12 +325,12 @@
errno = lasterr;
}
#endif
-
+
return rc;
}
int androidGetThreadPriority(pid_t tid) {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
return getpriority(PRIO_PROCESS, tid);
#else
return ANDROID_PRIORITY_NORMAL;
@@ -355,9 +347,9 @@
* ===========================================================================
*/
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
Mutex::Mutex()
{
@@ -419,9 +411,7 @@
return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
}
-#else
-#error "Somebody forgot to implement threads for this platform."
-#endif
+#endif // !defined(_WIN32)
/*
@@ -430,9 +420,9 @@
* ===========================================================================
*/
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
/*
* Windows doesn't have a condition variable solution. It's possible
@@ -480,7 +470,7 @@
//printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
// condState->waitersCount, getThreadId());
LeaveCriticalSection(&condState->waitersCountLock);
-
+
DWORD timeout = INFINITE;
if (abstime) {
nsecs_t reltime = *abstime - systemTime();
@@ -488,27 +478,27 @@
reltime = 0;
timeout = reltime/1000000;
}
-
+
// Atomically release the external mutex and wait on the semaphore.
DWORD res =
SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
-
+
//printf("+++ wait: awake (tid=%ld)\n", getThreadId());
-
+
// Reacquire lock to avoid race conditions.
EnterCriticalSection(&condState->waitersCountLock);
-
+
// No longer waiting.
condState->waitersCount--;
-
+
// Check to see if we're the last waiter after a broadcast.
bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
-
+
//printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
// lastWaiter, condState->wasBroadcast, condState->waitersCount);
-
+
LeaveCriticalSection(&condState->waitersCountLock);
-
+
// If we're the last waiter thread during this particular broadcast
// then signal broadcast() that we're all awake. It'll drop the
// internal mutex.
@@ -524,11 +514,11 @@
// Grab the internal mutex.
WaitForSingleObject(condState->internalMutex, INFINITE);
}
-
+
// Release the internal and grab the external.
ReleaseMutex(condState->internalMutex);
WaitForSingleObject(hMutex, INFINITE);
-
+
return res == WAIT_OBJECT_0 ? NO_ERROR : -1;
}
} WinCondition;
@@ -571,7 +561,7 @@
{
WinCondition* condState = (WinCondition*) mState;
HANDLE hMutex = (HANDLE) mutex.mState;
-
+
return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
}
@@ -653,9 +643,7 @@
ReleaseMutex(condState->internalMutex);
}
-#else
-#error "condition variables not supported on this platform"
-#endif
+#endif // !defined(_WIN32)
// ----------------------------------------------------------------------------
@@ -698,7 +686,7 @@
mStatus = NO_ERROR;
mExitPending = false;
mThread = thread_id_t(-1);
-
+
// hold a strong reference on ourself
mHoldSelf = this;
@@ -712,7 +700,7 @@
res = androidCreateRawThreadEtc(_threadLoop,
this, name, priority, stack, &mThread);
}
-
+
if (res == false) {
mStatus = UNKNOWN_ERROR; // something happened!
mRunning = false;
@@ -721,7 +709,7 @@
return UNKNOWN_ERROR;
}
-
+
// Do not refer to mStatus here: The thread is already running (may, in fact
// already have exited with a valid mStatus result). The NO_ERROR indication
// here merely indicates successfully starting the thread and does not
@@ -785,14 +773,14 @@
break;
}
}
-
+
// Release our strong reference, to let a chance to the thread
// to die a peaceful death.
strong.clear();
// And immediately, re-acquire a strong reference for the next loop
strong = weak.promote();
} while(strong != 0);
-
+
return 0;
}
@@ -813,7 +801,7 @@
return WOULD_BLOCK;
}
-
+
mExitPending = true;
while (mRunning == true) {
@@ -858,7 +846,7 @@
pid_t tid;
if (mRunning) {
pthread_t pthread = android_thread_id_t_to_pthread(mThread);
- tid = __pthread_gettid(pthread);
+ tid = pthread_gettid_np(pthread);
} else {
ALOGW("Thread (this=%p): getTid() is undefined before run()", this);
tid = -1;
diff --git a/libutils/Timers.cpp b/libutils/Timers.cpp
index 5293cd2..fb70e15 100644
--- a/libutils/Timers.cpp
+++ b/libutils/Timers.cpp
@@ -18,23 +18,14 @@
// Timer functions.
//
#include <utils/Timers.h>
-#include <utils/Log.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
+#include <limits.h>
#include <sys/time.h>
#include <time.h>
-#include <errno.h>
-#include <limits.h>
-#ifdef HAVE_WIN32_THREADS
-#include <windows.h>
-#endif
-
+#if defined(HAVE_ANDROID_OS)
nsecs_t systemTime(int clock)
{
-#if defined(HAVE_POSIX_CLOCKS)
static const clockid_t clocks[] = {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
@@ -46,14 +37,19 @@
t.tv_sec = t.tv_nsec = 0;
clock_gettime(clocks[clock], &t);
return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
+}
#else
- // we don't support the clocks here.
+nsecs_t systemTime(int /*clock*/)
+{
+ // Clock support varies widely across hosts. Mac OS doesn't support
+ // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows
+ // is windows.
struct timeval t;
t.tv_sec = t.tv_usec = 0;
gettimeofday(&t, NULL);
return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
-#endif
}
+#endif
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
{
diff --git a/libutils/Tokenizer.cpp b/libutils/Tokenizer.cpp
index 7067533..610002f 100644
--- a/libutils/Tokenizer.cpp
+++ b/libutils/Tokenizer.cpp
@@ -43,9 +43,7 @@
}
Tokenizer::~Tokenizer() {
- if (mFileMap) {
- mFileMap->release();
- }
+ delete mFileMap;
if (mOwnBuffer) {
delete[] mBuffer;
}
@@ -74,7 +72,7 @@
fileMap->advise(FileMap::SEQUENTIAL);
buffer = static_cast<char*>(fileMap->getDataPtr());
} else {
- fileMap->release();
+ delete fileMap;
fileMap = NULL;
// Fall back to reading into a buffer since we can't mmap files in sysfs.
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp
index a66e3bb..fb876c9 100644
--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -24,17 +24,10 @@
# undef nhtos
# undef htons
-# ifdef HAVE_LITTLE_ENDIAN
-# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
-# define htonl(x) ntohl(x)
-# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
-# define htons(x) ntohs(x)
-# else
-# define ntohl(x) (x)
-# define htonl(x) (x)
-# define ntohs(x) (x)
-# define htons(x) (x)
-# endif
+# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
+# define htonl(x) ntohl(x)
+# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
+# define htons(x) ntohs(x)
#else
# include <netinet/in.h>
#endif
@@ -47,8 +40,9 @@
// Surrogates aren't valid for UTF-32 characters, so define some
// constants that will let us screen them out.
static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
-static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
-static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
+// Unused, here for completeness:
+// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
+// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
@@ -342,7 +336,8 @@
while (cur_utf16 < end_utf16) {
char32_t utf32;
// surrogate pairs
- if ((*cur_utf16 & 0xFC00) == 0xD800) {
+ if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
+ && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
utf32 = (*cur_utf16++ - 0xD800) << 10;
utf32 |= *cur_utf16++ - 0xDC00;
utf32 += 0x10000;
@@ -576,7 +571,7 @@
char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) {
const uint8_t* const u8end = src + srcLen;
const uint8_t* u8cur = src;
- const uint16_t* const u16end = dst + dstLen;
+ const char16_t* const u16end = dst + dstLen;
char16_t* u16cur = dst;
while (u8cur < u8end && u16cur < u16end) {
diff --git a/libutils/misc.cpp b/libutils/misc.cpp
index 58eb499..ed1ba23 100644
--- a/libutils/misc.cpp
+++ b/libutils/misc.cpp
@@ -27,7 +27,7 @@
#include <errno.h>
#include <stdio.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
#endif
@@ -42,13 +42,13 @@
int priority;
};
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
#endif
void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
pthread_mutex_lock(&gSyspropMutex);
if (gSyspropList == NULL) {
gSyspropList = new Vector<sysprop_change_callback_info>();
@@ -72,7 +72,7 @@
}
void report_sysprop_change() {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
pthread_mutex_lock(&gSyspropMutex);
Vector<sysprop_change_callback_info> listeners;
if (gSyspropList != NULL) {
diff --git a/libutils/tests/Android.mk b/libutils/tests/Android.mk
index caedaff..634f44f 100644
--- a/libutils/tests/Android.mk
+++ b/libutils/tests/Android.mk
@@ -1,9 +1,28 @@
-# Build the unit tests.
-LOCAL_PATH := $(call my-dir)
-include $(CLEAR_VARS)
+#
+# Copyright (C) 2014 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.
+#
# Build the unit tests.
-test_src_files := \
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+
+LOCAL_MODULE := libutils_tests
+
+LOCAL_SRC_FILES := \
BasicHashtable_test.cpp \
BlobCache_test.cpp \
BitSet_test.cpp \
@@ -11,24 +30,12 @@
LruCache_test.cpp \
String8_test.cpp \
Unicode_test.cpp \
- Vector_test.cpp
+ Vector_test.cpp \
-shared_libraries := \
+LOCAL_SHARED_LIBRARIES := \
libz \
liblog \
libcutils \
libutils \
- libstlport
-static_libraries := \
- libgtest \
- libgtest_main
-
-$(foreach file,$(test_src_files), \
- $(eval include $(CLEAR_VARS)) \
- $(eval LOCAL_SHARED_LIBRARIES := $(shared_libraries)) \
- $(eval LOCAL_STATIC_LIBRARIES := $(static_libraries)) \
- $(eval LOCAL_SRC_FILES := $(file)) \
- $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
- $(eval include $(BUILD_NATIVE_TEST)) \
-)
+include $(BUILD_NATIVE_TEST)
diff --git a/libutils/tests/BasicHashtable_test.cpp b/libutils/tests/BasicHashtable_test.cpp
index 7dcf750..4b3a717 100644
--- a/libutils/tests/BasicHashtable_test.cpp
+++ b/libutils/tests/BasicHashtable_test.cpp
@@ -21,12 +21,12 @@
#include <gtest/gtest.h>
#include <unistd.h>
-namespace android {
+namespace {
typedef int SimpleKey;
typedef int SimpleValue;
-typedef key_value_pair_t<SimpleKey, SimpleValue> SimpleEntry;
-typedef BasicHashtable<SimpleKey, SimpleEntry> SimpleHashtable;
+typedef android::key_value_pair_t<SimpleKey, SimpleValue> SimpleEntry;
+typedef android::BasicHashtable<SimpleKey, SimpleEntry> SimpleHashtable;
struct ComplexKey {
int k;
@@ -56,10 +56,6 @@
ssize_t ComplexKey::instanceCount = 0;
-template<> inline hash_t hash_type(const ComplexKey& value) {
- return hash_type(value.k);
-}
-
struct ComplexValue {
int v;
@@ -80,9 +76,18 @@
ssize_t ComplexValue::instanceCount = 0;
+} // namespace
+
+
+namespace android {
+
typedef key_value_pair_t<ComplexKey, ComplexValue> ComplexEntry;
typedef BasicHashtable<ComplexKey, ComplexEntry> ComplexHashtable;
+template<> inline hash_t hash_type(const ComplexKey& value) {
+ return hash_type(value.k);
+}
+
class BasicHashtableTest : public testing::Test {
protected:
virtual void SetUp() {
@@ -397,7 +402,7 @@
const SimpleEntry& entry = h.entryAt(index);
ASSERT_GE(entry.key, 0);
ASSERT_LT(entry.key, N);
- ASSERT_EQ(false, set[entry.key]);
+ ASSERT_FALSE(set[entry.key]);
ASSERT_EQ(entry.key * 10, entry.value);
set[entry.key] = true;
diff --git a/libutils/tests/BitSet_test.cpp b/libutils/tests/BitSet_test.cpp
index 752e56d..38b668a 100644
--- a/libutils/tests/BitSet_test.cpp
+++ b/libutils/tests/BitSet_test.cpp
@@ -23,7 +23,7 @@
namespace android {
-class BitSetTest : public testing::Test {
+class BitSet32Test : public testing::Test {
protected:
BitSet32 b1;
BitSet32 b2;
@@ -34,7 +34,7 @@
};
-TEST_F(BitSetTest, BitWiseOr) {
+TEST_F(BitSet32Test, BitWiseOr) {
b1.markBit(2);
b2.markBit(4);
@@ -49,7 +49,7 @@
EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4));
EXPECT_TRUE(b2.hasBit(4) && b2.count() == 1u);
}
-TEST_F(BitSetTest, BitWiseAnd_Disjoint) {
+TEST_F(BitSet32Test, BitWiseAnd_Disjoint) {
b1.markBit(2);
b1.markBit(4);
b1.markBit(6);
@@ -65,7 +65,7 @@
EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4) && b1.hasBit(6));
}
-TEST_F(BitSetTest, BitWiseAnd_NonDisjoint) {
+TEST_F(BitSet32Test, BitWiseAnd_NonDisjoint) {
b1.markBit(2);
b1.markBit(4);
b1.markBit(6);
@@ -84,4 +84,187 @@
EXPECT_EQ(b2.count(), 3u);
EXPECT_TRUE(b2.hasBit(3) && b2.hasBit(6) && b2.hasBit(9));
}
+
+TEST_F(BitSet32Test, MarkFirstUnmarkedBit) {
+ b1.markBit(1);
+
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.count(), 2u);
+ EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1));
+
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.count(), 3u);
+ EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1) && b1.hasBit(2));
+}
+
+TEST_F(BitSet32Test, ClearFirstMarkedBit) {
+ b1.markBit(0);
+ b1.markBit(10);
+
+ b1.clearFirstMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(10));
+
+ b1.markBit(30);
+ b1.clearFirstMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(30));
+}
+
+TEST_F(BitSet32Test, ClearLastMarkedBit) {
+ b1.markBit(10);
+ b1.markBit(31);
+
+ b1.clearLastMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(10));
+
+ b1.markBit(5);
+ b1.clearLastMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(5));
+}
+
+TEST_F(BitSet32Test, FillAndClear) {
+ EXPECT_TRUE(b1.isEmpty());
+ for (size_t i = 0; i < 32; i++) {
+ b1.markFirstUnmarkedBit();
+ }
+ EXPECT_TRUE(b1.isFull());
+ b1.clear();
+ EXPECT_TRUE(b1.isEmpty());
+}
+
+TEST_F(BitSet32Test, GetIndexOfBit) {
+ b1.markBit(1);
+ b1.markBit(4);
+ EXPECT_EQ(b1.getIndexOfBit(1), 0);
+ EXPECT_EQ(b1.getIndexOfBit(4), 1);
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.getIndexOfBit(1), 1);
+ EXPECT_EQ(b1.getIndexOfBit(4), 2);
+}
+
+class BitSet64Test : public testing::Test {
+protected:
+ BitSet64 b1;
+ BitSet64 b2;
+ virtual void TearDown() {
+ b1.clear();
+ b2.clear();
+ }
+};
+
+
+TEST_F(BitSet64Test, BitWiseOr) {
+ b1.markBit(20);
+ b2.markBit(40);
+
+ BitSet64 tmp = b1 | b2;
+ EXPECT_EQ(tmp.count(), 2u);
+ EXPECT_TRUE(tmp.hasBit(20) && tmp.hasBit(40));
+ // Check that the operator is symmetric
+ EXPECT_TRUE((b2 | b1) == (b1 | b2));
+
+ b1 |= b2;
+ EXPECT_EQ(b1.count(), 2u);
+ EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40));
+ EXPECT_TRUE(b2.hasBit(40) && b2.count() == 1u);
+}
+TEST_F(BitSet64Test, BitWiseAnd_Disjoint) {
+ b1.markBit(20);
+ b1.markBit(40);
+ b1.markBit(60);
+
+ BitSet64 tmp = b1 & b2;
+ EXPECT_TRUE(tmp.isEmpty());
+ // Check that the operator is symmetric
+ EXPECT_TRUE((b2 & b1) == (b1 & b2));
+
+ b2 &= b1;
+ EXPECT_TRUE(b2.isEmpty());
+ EXPECT_EQ(b1.count(), 3u);
+ EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40) && b1.hasBit(60));
+}
+
+TEST_F(BitSet64Test, BitWiseAnd_NonDisjoint) {
+ b1.markBit(20);
+ b1.markBit(40);
+ b1.markBit(60);
+ b2.markBit(30);
+ b2.markBit(60);
+ b2.markBit(63);
+
+ BitSet64 tmp = b1 & b2;
+ EXPECT_EQ(tmp.count(), 1u);
+ EXPECT_TRUE(tmp.hasBit(60));
+ // Check that the operator is symmetric
+ EXPECT_TRUE((b2 & b1) == (b1 & b2));
+
+ b1 &= b2;
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_EQ(b2.count(), 3u);
+ EXPECT_TRUE(b2.hasBit(30) && b2.hasBit(60) && b2.hasBit(63));
+}
+
+TEST_F(BitSet64Test, MarkFirstUnmarkedBit) {
+ b1.markBit(1);
+
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.count(), 2u);
+ EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1));
+
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.count(), 3u);
+ EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1) && b1.hasBit(2));
+}
+
+TEST_F(BitSet64Test, ClearFirstMarkedBit) {
+ b1.markBit(0);
+ b1.markBit(10);
+
+ b1.clearFirstMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(10));
+
+ b1.markBit(50);
+ b1.clearFirstMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(50));
+}
+
+TEST_F(BitSet64Test, ClearLastMarkedBit) {
+ b1.markBit(10);
+ b1.markBit(63);
+
+ b1.clearLastMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(10));
+
+ b1.markBit(5);
+ b1.clearLastMarkedBit();
+ EXPECT_EQ(b1.count(), 1u);
+ EXPECT_TRUE(b1.hasBit(5));
+}
+
+TEST_F(BitSet64Test, FillAndClear) {
+ EXPECT_TRUE(b1.isEmpty());
+ for (size_t i = 0; i < 64; i++) {
+ b1.markFirstUnmarkedBit();
+ }
+ EXPECT_TRUE(b1.isFull());
+ b1.clear();
+ EXPECT_TRUE(b1.isEmpty());
+}
+
+TEST_F(BitSet64Test, GetIndexOfBit) {
+ b1.markBit(10);
+ b1.markBit(40);
+ EXPECT_EQ(b1.getIndexOfBit(10), 0);
+ EXPECT_EQ(b1.getIndexOfBit(40), 1);
+ b1.markFirstUnmarkedBit();
+ EXPECT_EQ(b1.getIndexOfBit(10), 1);
+ EXPECT_EQ(b1.getIndexOfBit(40), 2);
+}
+
} // namespace android
diff --git a/libutils/tests/BlobCache_test.cpp b/libutils/tests/BlobCache_test.cpp
index 7202123..dac4e2c 100644
--- a/libutils/tests/BlobCache_test.cpp
+++ b/libutils/tests/BlobCache_test.cpp
@@ -44,7 +44,7 @@
};
TEST_F(BlobCacheTest, CacheSingleValueSucceeds) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
ASSERT_EQ('e', buf[0]);
@@ -54,7 +54,7 @@
}
TEST_F(BlobCacheTest, CacheTwoValuesSucceeds) {
- char buf[2] = { 0xee, 0xee };
+ unsigned char buf[2] = { 0xee, 0xee };
mBC->set("ab", 2, "cd", 2);
mBC->set("ef", 2, "gh", 2);
ASSERT_EQ(size_t(2), mBC->get("ab", 2, buf, 2));
@@ -66,7 +66,7 @@
}
TEST_F(BlobCacheTest, GetOnlyWritesInsideBounds) {
- char buf[6] = { 0xee, 0xee, 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[6] = { 0xee, 0xee, 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf+1, 4));
ASSERT_EQ(0xee, buf[0]);
@@ -78,7 +78,7 @@
}
TEST_F(BlobCacheTest, GetOnlyWritesIfBufferIsLargeEnough) {
- char buf[3] = { 0xee, 0xee, 0xee };
+ unsigned char buf[3] = { 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 3));
ASSERT_EQ(0xee, buf[0]);
@@ -92,7 +92,7 @@
}
TEST_F(BlobCacheTest, MultipleSetsCacheLatestValue) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
mBC->set("abcd", 4, "ijkl", 4);
ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
@@ -103,7 +103,7 @@
}
TEST_F(BlobCacheTest, SecondSetKeepsFirstValueIfTooLarge) {
- char buf[MAX_VALUE_SIZE+1] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[MAX_VALUE_SIZE+1] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
mBC->set("abcd", 4, buf, MAX_VALUE_SIZE+1);
ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4));
@@ -115,7 +115,7 @@
TEST_F(BlobCacheTest, DoesntCacheIfKeyIsTooBig) {
char key[MAX_KEY_SIZE+1];
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
for (int i = 0; i < MAX_KEY_SIZE+1; i++) {
key[i] = 'a';
}
@@ -165,7 +165,7 @@
TEST_F(BlobCacheTest, CacheMaxKeySizeSucceeds) {
char key[MAX_KEY_SIZE];
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
for (int i = 0; i < MAX_KEY_SIZE; i++) {
key[i] = 'a';
}
@@ -214,7 +214,7 @@
}
TEST_F(BlobCacheTest, CacheMinKeyAndValueSizeSucceeds) {
- char buf[1] = { 0xee };
+ unsigned char buf[1] = { 0xee };
mBC->set("x", 1, "y", 1);
ASSERT_EQ(size_t(1), mBC->get("x", 1, buf, 1));
ASSERT_EQ('y', buf[0]);
@@ -282,7 +282,7 @@
};
TEST_F(BlobCacheFlattenTest, FlattenOneValue) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
roundTrip();
ASSERT_EQ(size_t(4), mBC2->get("abcd", 4, buf, 4));
@@ -348,7 +348,7 @@
}
TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadMagic) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
size_t size = mBC->getFlattenedSize();
@@ -365,7 +365,7 @@
}
TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheVersion) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
size_t size = mBC->getFlattenedSize();
@@ -384,7 +384,7 @@
}
TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheDeviceVersion) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
size_t size = mBC->getFlattenedSize();
@@ -403,7 +403,7 @@
}
TEST_F(BlobCacheFlattenTest, UnflattenCatchesBufferTooSmall) {
- char buf[4] = { 0xee, 0xee, 0xee, 0xee };
+ unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee };
mBC->set("abcd", 4, "efgh", 4);
size_t size = mBC->getFlattenedSize();
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/tests/LruCache_test.cpp
index e573952..6534211 100644
--- a/libutils/tests/LruCache_test.cpp
+++ b/libutils/tests/LruCache_test.cpp
@@ -20,7 +20,7 @@
#include <cutils/log.h>
#include <gtest/gtest.h>
-namespace android {
+namespace {
typedef int SimpleKey;
typedef const char* StringValue;
@@ -53,10 +53,6 @@
ssize_t ComplexKey::instanceCount = 0;
-template<> inline hash_t hash_type(const ComplexKey& value) {
- return hash_type(value.k);
-}
-
struct ComplexValue {
int v;
@@ -77,8 +73,17 @@
ssize_t ComplexValue::instanceCount = 0;
+} // namespace
+
+
+namespace android {
+
typedef LruCache<ComplexKey, ComplexValue> ComplexCache;
+template<> inline android::hash_t hash_type(const ComplexKey& value) {
+ return hash_type(value.k);
+}
+
class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> {
public:
EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(NULL) { }
@@ -184,7 +189,7 @@
for (size_t i = 0; i < kNumKeys; i++) {
strings[i] = (char *)malloc(16);
- sprintf(strings[i], "%d", i);
+ sprintf(strings[i], "%zu", i);
}
srandom(12345);
diff --git a/libziparchive/Android.mk b/libziparchive/Android.mk
index e754c3b..3937449 100644
--- a/libziparchive/Android.mk
+++ b/libziparchive/Android.mk
@@ -14,56 +14,65 @@
# limitations under the License.
LOCAL_PATH := $(call my-dir)
+
+source_files := zip_archive.cc
+
include $(CLEAR_VARS)
-
-source_files := \
- zip_archive.h \
- zip_archive.cc
-
-includes := external/zlib
-
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := ${source_files}
-
LOCAL_STATIC_LIBRARIES := libz
LOCAL_SHARED_LIBRARIES := libutils
LOCAL_MODULE:= libziparchive
-
-LOCAL_C_INCLUDES += ${includes}
+LOCAL_CFLAGS := -Werror -Wall
+LOCAL_CPPFLAGS := -Wold-style-cast
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
-LOCAL_MODULE := libziparchive
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := ${source_files}
-LOCAL_C_INCLUDES += ${includes}
-
LOCAL_STATIC_LIBRARIES := libz libutils
LOCAL_MODULE:= libziparchive-host
+LOCAL_CFLAGS := -Werror
+ifneq ($(strip $(USE_MINGW)),)
+ LOCAL_CFLAGS += -mno-ms-bitfields
+endif
+LOCAL_MULTILIB := both
include $(BUILD_HOST_STATIC_LIBRARY)
include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := ${source_files}
+LOCAL_STATIC_LIBRARIES := libz libutils
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE:= libziparchive-host
+LOCAL_CFLAGS := -Werror
+LOCAL_MULTILIB := both
+include $(BUILD_HOST_SHARED_LIBRARY)
+
+# Tests.
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_MODULE := ziparchive-tests
LOCAL_CPP_EXTENSION := .cc
-LOCAL_CFLAGS += \
- -DGTEST_OS_LINUX_ANDROID \
- -DGTEST_HAS_STD_STRING
-LOCAL_SRC_FILES := zip_archive_test.cc
+LOCAL_CFLAGS := -Werror
+LOCAL_SRC_FILES := zip_archive_test.cc entry_name_utils_test.cc
LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_STATIC_LIBRARIES := libziparchive libz libgtest libgtest_main libutils
+LOCAL_STATIC_LIBRARIES := libziparchive libz libutils
include $(BUILD_NATIVE_TEST)
include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_MODULE := ziparchive-tests-host
LOCAL_CPP_EXTENSION := .cc
LOCAL_CFLAGS += \
- -DGTEST_OS_LINUX \
- -DGTEST_HAS_STD_STRING
-LOCAL_SRC_FILES := zip_archive_test.cc
-LOCAL_STATIC_LIBRARIES := libziparchive-host \
- libz \
- libgtest_host \
- libgtest_main_host \
- liblog \
- libutils
+ -Werror \
+ -Wno-unnamed-type-template-args
+LOCAL_SRC_FILES := zip_archive_test.cc entry_name_utils_test.cc
+LOCAL_SHARED_LIBRARIES := libziparchive-host liblog
+LOCAL_STATIC_LIBRARIES := \
+ libz \
+ libutils
include $(BUILD_HOST_NATIVE_TEST)
diff --git a/libziparchive/entry_name_utils-inl.h b/libziparchive/entry_name_utils-inl.h
new file mode 100644
index 0000000..ddbc286
--- /dev/null
+++ b/libziparchive/entry_name_utils-inl.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2014 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 LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
+#define LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+// Check if |length| bytes at |entry_name| constitute a valid entry name.
+// Entry names must be valid UTF-8 and must not contain '0'.
+inline bool IsValidEntryName(const uint8_t* entry_name, const size_t length) {
+ for (size_t i = 0; i < length; ++i) {
+ const uint8_t byte = entry_name[i];
+ if (byte == 0) {
+ return false;
+ } else if ((byte & 0x80) == 0) {
+ // Single byte sequence.
+ continue;
+ } else if ((byte & 0xc0) == 0x80 || (byte & 0xfe) == 0xfe) {
+ // Invalid sequence.
+ return false;
+ } else {
+ // 2-5 byte sequences.
+ for (uint8_t first = byte << 1; first & 0x80; first <<= 1) {
+ ++i;
+
+ // Missing continuation byte..
+ if (i == length) {
+ return false;
+ }
+
+ // Invalid continuation byte.
+ const uint8_t continuation_byte = entry_name[i];
+ if ((continuation_byte & 0xc0) != 0x80) {
+ return false;
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+
+#endif // LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
diff --git a/libziparchive/entry_name_utils_test.cc b/libziparchive/entry_name_utils_test.cc
new file mode 100644
index 0000000..20715bb
--- /dev/null
+++ b/libziparchive/entry_name_utils_test.cc
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2014 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 "entry_name_utils-inl.h"
+
+#include <gtest/gtest.h>
+
+TEST(entry_name_utils, NullChars) {
+ // 'A', 'R', '\0', 'S', 'E'
+ const uint8_t zeroes[] = { 0x41, 0x52, 0x00, 0x53, 0x45 };
+ ASSERT_FALSE(IsValidEntryName(zeroes, sizeof(zeroes)));
+
+ const uint8_t zeroes_continuation_chars[] = { 0xc2, 0xa1, 0xc2, 0x00 };
+ ASSERT_FALSE(IsValidEntryName(zeroes_continuation_chars,
+ sizeof(zeroes_continuation_chars)));
+}
+
+TEST(entry_name_utils, InvalidSequence) {
+ // 0xfe is an invalid start byte
+ const uint8_t invalid[] = { 0x41, 0xfe };
+ ASSERT_FALSE(IsValidEntryName(invalid, sizeof(invalid)));
+
+ // 0x91 is an invalid start byte (it's a valid continuation byte).
+ const uint8_t invalid2[] = { 0x41, 0x91 };
+ ASSERT_FALSE(IsValidEntryName(invalid2, sizeof(invalid2)));
+}
+
+TEST(entry_name_utils, TruncatedContinuation) {
+ // Malayalam script with truncated bytes. There should be 2 bytes
+ // after 0xe0
+ const uint8_t truncated[] = { 0xe0, 0xb4, 0x85, 0xe0, 0xb4 };
+ ASSERT_FALSE(IsValidEntryName(truncated, sizeof(truncated)));
+
+ // 0xc2 is the start of a 2 byte sequence that we've subsequently
+ // dropped.
+ const uint8_t truncated2[] = { 0xc2, 0xc2, 0xa1 };
+ ASSERT_FALSE(IsValidEntryName(truncated2, sizeof(truncated2)));
+}
+
+TEST(entry_name_utils, BadContinuation) {
+ // 0x41 is an invalid continuation char, since it's MSBs
+ // aren't "10..." (are 01).
+ const uint8_t bad[] = { 0xc2, 0xa1, 0xc2, 0x41 };
+ ASSERT_FALSE(IsValidEntryName(bad, sizeof(bad)));
+
+ // 0x41 is an invalid continuation char, since it's MSBs
+ // aren't "10..." (are 11).
+ const uint8_t bad2[] = { 0xc2, 0xa1, 0xc2, 0xfe };
+ ASSERT_FALSE(IsValidEntryName(bad2, sizeof(bad2)));
+}
diff --git a/libziparchive/testdata/declaredlength.zip b/libziparchive/testdata/declaredlength.zip
new file mode 100644
index 0000000..773380c
--- /dev/null
+++ b/libziparchive/testdata/declaredlength.zip
Binary files differ
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 8ef0962..6475649 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -17,72 +17,194 @@
/*
* Read-only access to Zip archives, with minimal heap allocation.
*/
-#include "ziparchive/zip_archive.h"
-#include <zlib.h>
+#include <memory>
+#include <vector>
#include <assert.h>
#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
#include <limits.h>
#include <log/log.h>
-#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <utils/Compat.h>
#include <utils/FileMap.h>
+#include <zlib.h>
#include <JNIHelp.h> // TEMP_FAILURE_RETRY may or may not be in unistd
-// This is for windows. If we don't open a file in binary mode, weirds
+#include "entry_name_utils-inl.h"
+#include "ziparchive/zip_archive.h"
+
+
+// This is for windows. If we don't open a file in binary mode, weird
// things will happen.
#ifndef O_BINARY
#define O_BINARY 0
#endif
-/*
- * Zip file constants.
- */
-static const uint32_t kEOCDSignature = 0x06054b50;
-static const uint32_t kEOCDLen = 2;
-static const uint32_t kEOCDNumEntries = 8; // offset to #of entries in file
-static const uint32_t kEOCDSize = 12; // size of the central directory
-static const uint32_t kEOCDFileOffset = 16; // offset to central directory
+#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
+ TypeName(); \
+ TypeName(const TypeName&); \
+ void operator=(const TypeName&)
-static const uint32_t kMaxCommentLen = 65535; // longest possible in ushort
-static const uint32_t kMaxEOCDSearch = (kMaxCommentLen + kEOCDLen);
+// The "end of central directory" (EOCD) record. Each archive
+// contains exactly once such record which appears at the end of
+// the archive. It contains archive wide information like the
+// number of entries in the archive and the offset to the central
+// directory of the offset.
+struct EocdRecord {
+ static const uint32_t kSignature = 0x06054b50;
-static const uint32_t kLFHSignature = 0x04034b50;
-static const uint32_t kLFHLen = 30; // excluding variable-len fields
-static const uint32_t kLFHGPBFlags = 6; // general purpose bit flags
-static const uint32_t kLFHCRC = 14; // offset to CRC
-static const uint32_t kLFHCompLen = 18; // offset to compressed length
-static const uint32_t kLFHUncompLen = 22; // offset to uncompressed length
-static const uint32_t kLFHNameLen = 26; // offset to filename length
-static const uint32_t kLFHExtraLen = 28; // offset to extra length
+ // End of central directory signature, should always be
+ // |kSignature|.
+ uint32_t eocd_signature;
+ // The number of the current "disk", i.e, the "disk" that this
+ // central directory is on.
+ //
+ // This implementation assumes that each archive spans a single
+ // disk only. i.e, that disk_num == 1.
+ uint16_t disk_num;
+ // The disk where the central directory starts.
+ //
+ // This implementation assumes that each archive spans a single
+ // disk only. i.e, that cd_start_disk == 1.
+ uint16_t cd_start_disk;
+ // The number of central directory records on this disk.
+ //
+ // This implementation assumes that each archive spans a single
+ // disk only. i.e, that num_records_on_disk == num_records.
+ uint16_t num_records_on_disk;
+ // The total number of central directory records.
+ uint16_t num_records;
+ // The size of the central directory (in bytes).
+ uint32_t cd_size;
+ // The offset of the start of the central directory, relative
+ // to the start of the file.
+ uint32_t cd_start_offset;
+ // Length of the central directory comment.
+ uint16_t comment_length;
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(EocdRecord);
+} __attribute__((packed));
-static const uint32_t kCDESignature = 0x02014b50;
-static const uint32_t kCDELen = 46; // excluding variable-len fields
-static const uint32_t kCDEMethod = 10; // offset to compression method
-static const uint32_t kCDEModWhen = 12; // offset to modification timestamp
-static const uint32_t kCDECRC = 16; // offset to entry CRC
-static const uint32_t kCDECompLen = 20; // offset to compressed length
-static const uint32_t kCDEUncompLen = 24; // offset to uncompressed length
-static const uint32_t kCDENameLen = 28; // offset to filename length
-static const uint32_t kCDEExtraLen = 30; // offset to extra length
-static const uint32_t kCDECommentLen = 32; // offset to comment length
-static const uint32_t kCDELocalOffset = 42; // offset to local hdr
+// A structure representing the fixed length fields for a single
+// record in the central directory of the archive. In addition to
+// the fixed length fields listed here, each central directory
+// record contains a variable length "file_name" and "extra_field"
+// whose lengths are given by |file_name_length| and |extra_field_length|
+// respectively.
+struct CentralDirectoryRecord {
+ static const uint32_t kSignature = 0x02014b50;
-static const uint32_t kDDOptSignature = 0x08074b50; // *OPTIONAL* data descriptor signature
-static const uint32_t kDDSignatureLen = 4;
-static const uint32_t kDDLen = 12;
-static const uint32_t kDDMaxLen = 16; // max of 16 bytes with a signature, 12 bytes without
-static const uint32_t kDDCrc32 = 0; // offset to crc32
-static const uint32_t kDDCompLen = 4; // offset to compressed length
-static const uint32_t kDDUncompLen = 8; // offset to uncompressed length
+ // The start of record signature. Must be |kSignature|.
+ uint32_t record_signature;
+ // Tool version. Ignored by this implementation.
+ uint16_t version_made_by;
+ // Tool version. Ignored by this implementation.
+ uint16_t version_needed;
+ // The "general purpose bit flags" for this entry. The only
+ // flag value that we currently check for is the "data descriptor"
+ // flag.
+ uint16_t gpb_flags;
+ // The compression method for this entry, one of |kCompressStored|
+ // and |kCompressDeflated|.
+ uint16_t compression_method;
+ // The file modification time and date for this entry.
+ uint16_t last_mod_time;
+ uint16_t last_mod_date;
+ // The CRC-32 checksum for this entry.
+ uint32_t crc32;
+ // The compressed size (in bytes) of this entry.
+ uint32_t compressed_size;
+ // The uncompressed size (in bytes) of this entry.
+ uint32_t uncompressed_size;
+ // The length of the entry file name in bytes. The file name
+ // will appear immediately after this record.
+ uint16_t file_name_length;
+ // The length of the extra field info (in bytes). This data
+ // will appear immediately after the entry file name.
+ uint16_t extra_field_length;
+ // The length of the entry comment (in bytes). This data will
+ // appear immediately after the extra field.
+ uint16_t comment_length;
+ // The start disk for this entry. Ignored by this implementation).
+ uint16_t file_start_disk;
+ // File attributes. Ignored by this implementation.
+ uint16_t internal_file_attributes;
+ // File attributes. Ignored by this implementation.
+ uint32_t external_file_attributes;
+ // The offset to the local file header for this entry, from the
+ // beginning of this archive.
+ uint32_t local_file_header_offset;
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(CentralDirectoryRecord);
+} __attribute__((packed));
-static const uint32_t kGPBDDFlagMask = 0x0008; // mask value that signifies that the entry has a DD
+// The local file header for a given entry. This duplicates information
+// present in the central directory of the archive. It is an error for
+// the information here to be different from the central directory
+// information for a given entry.
+struct LocalFileHeader {
+ static const uint32_t kSignature = 0x04034b50;
-static const uint32_t kMaxErrorLen = 1024;
+ // The local file header signature, must be |kSignature|.
+ uint32_t lfh_signature;
+ // Tool version. Ignored by this implementation.
+ uint16_t version_needed;
+ // The "general purpose bit flags" for this entry. The only
+ // flag value that we currently check for is the "data descriptor"
+ // flag.
+ uint16_t gpb_flags;
+ // The compression method for this entry, one of |kCompressStored|
+ // and |kCompressDeflated|.
+ uint16_t compression_method;
+ // The file modification time and date for this entry.
+ uint16_t last_mod_time;
+ uint16_t last_mod_date;
+ // The CRC-32 checksum for this entry.
+ uint32_t crc32;
+ // The compressed size (in bytes) of this entry.
+ uint32_t compressed_size;
+ // The uncompressed size (in bytes) of this entry.
+ uint32_t uncompressed_size;
+ // The length of the entry file name in bytes. The file name
+ // will appear immediately after this record.
+ uint16_t file_name_length;
+ // The length of the extra field info (in bytes). This data
+ // will appear immediately after the entry file name.
+ uint16_t extra_field_length;
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(LocalFileHeader);
+} __attribute__((packed));
+
+struct DataDescriptor {
+ // The *optional* data descriptor start signature.
+ static const uint32_t kOptSignature = 0x08074b50;
+
+ // CRC-32 checksum of the entry.
+ uint32_t crc32;
+ // Compressed size of the entry.
+ uint32_t compressed_size;
+ // Uncompressed size of the entry.
+ uint32_t uncompressed_size;
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(DataDescriptor);
+} __attribute__((packed));
+
+#undef DISALLOW_IMPLICIT_CONSTRUCTORS
+
+static const uint32_t kGPBDDFlagMask = 0x0008; // mask value that signifies that the entry has a DD
+
+// The maximum size of a central directory or a file
+// comment in bytes.
+static const uint32_t kMaxCommentLen = 65535;
+
+// The maximum number of bytes to scan backwards for the EOCD start.
+static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
static const char* kErrorMessages[] = {
"Unknown return code.",
@@ -169,11 +291,12 @@
*/
struct ZipArchive {
/* open Zip archive */
- int fd;
+ const int fd;
+ const bool close_file;
/* mapped central directory area */
off64_t directory_offset;
- android::FileMap* directory_map;
+ android::FileMap directory_map;
/* number of entries in the Zip archive */
uint16_t num_entries;
@@ -186,21 +309,23 @@
*/
uint32_t hash_table_size;
ZipEntryName* hash_table;
-};
-// Returns 0 on success and negative values on failure.
-static android::FileMap* MapFileSegment(const int fd, const off64_t start,
- const size_t length, const bool read_only,
- const char* debug_file_name) {
- android::FileMap* file_map = new android::FileMap;
- const bool success = file_map->create(debug_file_name, fd, start, length, read_only);
- if (!success) {
- file_map->release();
- return NULL;
+ ZipArchive(const int fd, bool assume_ownership) :
+ fd(fd),
+ close_file(assume_ownership),
+ directory_offset(0),
+ num_entries(0),
+ hash_table_size(0),
+ hash_table(NULL) {}
+
+ ~ZipArchive() {
+ if (close_file && fd >= 0) {
+ close(fd);
+ }
+
+ free(hash_table);
}
-
- return file_map;
-}
+};
static int32_t CopyFileToFile(int fd, uint8_t* begin, const uint32_t length, uint64_t *crc_out) {
static const uint32_t kBufSize = 32768;
@@ -217,8 +342,7 @@
ssize_t actual = TEMP_FAILURE_RETRY(read(fd, buf, get_size));
if (actual != get_size) {
- ALOGW("CopyFileToFile: copy read failed (%d vs %zd)",
- (int) actual, get_size);
+ ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, get_size);
return kIoError;
}
@@ -249,8 +373,10 @@
return val;
}
-static uint32_t ComputeHash(const char* str, uint16_t len) {
+static uint32_t ComputeHash(const ZipEntryName& name) {
uint32_t hash = 0;
+ uint16_t len = name.name_length;
+ const uint8_t* str = name.name;
while (len--) {
hash = hash * 31 + *str++;
@@ -265,21 +391,21 @@
*/
static int64_t EntryToIndex(const ZipEntryName* hash_table,
const uint32_t hash_table_size,
- const char* name, uint16_t length) {
- const uint32_t hash = ComputeHash(name, length);
+ const ZipEntryName& name) {
+ const uint32_t hash = ComputeHash(name);
// NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
uint32_t ent = hash & (hash_table_size - 1);
while (hash_table[ent].name != NULL) {
- if (hash_table[ent].name_length == length &&
- memcmp(hash_table[ent].name, name, length) == 0) {
+ if (hash_table[ent].name_length == name.name_length &&
+ memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
return ent;
}
ent = (ent + 1) & (hash_table_size - 1);
}
- ALOGV("Zip: Unable to find entry %.*s", length, name);
+ ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
return kEntryNotFound;
}
@@ -287,8 +413,8 @@
* Add a new entry to the hash table.
*/
static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
- const char* name, uint16_t length) {
- const uint64_t hash = ComputeHash(name, length);
+ const ZipEntryName& name) {
+ const uint64_t hash = ComputeHash(name);
uint32_t ent = hash & (hash_table_size - 1);
/*
@@ -296,53 +422,35 @@
* Further, we guarantee that the hashtable size is not 0.
*/
while (hash_table[ent].name != NULL) {
- if (hash_table[ent].name_length == length &&
- memcmp(hash_table[ent].name, name, length) == 0) {
+ if (hash_table[ent].name_length == name.name_length &&
+ memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
// We've found a duplicate entry. We don't accept it
- ALOGW("Zip: Found duplicate entry %.*s", length, name);
+ ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
return kDuplicateEntry;
}
ent = (ent + 1) & (hash_table_size - 1);
}
- hash_table[ent].name = name;
- hash_table[ent].name_length = length;
+ hash_table[ent].name = name.name;
+ hash_table[ent].name_length = name.name_length;
return 0;
}
-/*
- * Get 2 little-endian bytes.
- */
-static uint16_t get2LE(const uint8_t* src) {
- return src[0] | (src[1] << 8);
-}
-
-/*
- * Get 4 little-endian bytes.
- */
-static uint32_t get4LE(const uint8_t* src) {
- uint32_t result;
-
- result = src[0];
- result |= src[1] << 8;
- result |= src[2] << 16;
- result |= src[3] << 24;
-
- return result;
-}
-
static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
ZipArchive* archive, off64_t file_length,
- uint32_t read_amount, uint8_t* scan_buffer) {
+ off64_t read_amount, uint8_t* scan_buffer) {
const off64_t search_start = file_length - read_amount;
if (lseek64(fd, search_start, SEEK_SET) != search_start) {
- ALOGW("Zip: seek %lld failed: %s", search_start, strerror(errno));
+ ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
+ strerror(errno));
return kIoError;
}
- ssize_t actual = TEMP_FAILURE_RETRY(read(fd, scan_buffer, read_amount));
- if (actual != (ssize_t) read_amount) {
- ALOGW("Zip: read %zd failed: %s", read_amount, strerror(errno));
+ ssize_t actual = TEMP_FAILURE_RETRY(
+ read(fd, scan_buffer, static_cast<size_t>(read_amount)));
+ if (actual != static_cast<ssize_t>(read_amount)) {
+ ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
+ strerror(errno));
return kIoError;
}
@@ -352,9 +460,10 @@
* doing an initial minimal read; if we don't find it, retry with a
* second read as above.)
*/
- int i;
- for (i = read_amount - kEOCDLen; i >= 0; i--) {
- if (scan_buffer[i] == 0x50 && get4LE(&scan_buffer[i]) == kEOCDSignature) {
+ int i = read_amount - sizeof(EocdRecord);
+ for (; i >= 0; i--) {
+ if (scan_buffer[i] == 0x50 &&
+ ((*reinterpret_cast<uint32_t*>(&scan_buffer[i])) == EocdRecord::kSignature)) {
ALOGV("+++ Found EOCD at buf+%d", i);
break;
}
@@ -365,46 +474,48 @@
}
const off64_t eocd_offset = search_start + i;
- const uint8_t* eocd_ptr = scan_buffer + i;
-
- assert(eocd_offset < file_length);
+ const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
+ /*
+ * Verify that there's no trailing space at the end of the central directory
+ * and its comment.
+ */
+ const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
+ + eocd->comment_length;
+ if (calculated_length != file_length) {
+ ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
+ static_cast<int64_t>(file_length - calculated_length));
+ return kInvalidFile;
+ }
/*
* Grab the CD offset and size, and the number of entries in the
- * archive. Verify that they look reasonable. Widen dir_size and
- * dir_offset to the file offset type.
+ * archive and verify that they look reasonable.
*/
- const uint16_t num_entries = get2LE(eocd_ptr + kEOCDNumEntries);
- const off64_t dir_size = get4LE(eocd_ptr + kEOCDSize);
- const off64_t dir_offset = get4LE(eocd_ptr + kEOCDFileOffset);
-
- if (dir_offset + dir_size > eocd_offset) {
- ALOGW("Zip: bad offsets (dir %lld, size %lld, eocd %lld)",
- dir_offset, dir_size, eocd_offset);
+ if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
+ ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
+ eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
return kInvalidOffset;
}
- if (num_entries == 0) {
+ if (eocd->num_records == 0) {
ALOGW("Zip: empty archive?");
return kEmptyArchive;
}
- ALOGV("+++ num_entries=%d dir_size=%d dir_offset=%d", num_entries, dir_size,
- dir_offset);
+ ALOGV("+++ num_entries=%" PRIu32 "dir_size=%" PRIu32 " dir_offset=%" PRIu32,
+ eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
/*
* It all looks good. Create a mapping for the CD, and set the fields
* in archive.
*/
- android::FileMap* map = MapFileSegment(fd, dir_offset, dir_size,
- true /* read only */, debug_file_name);
- if (map == NULL) {
- archive->directory_map = NULL;
+ if (!archive->directory_map.create(debug_file_name, fd,
+ static_cast<off64_t>(eocd->cd_start_offset),
+ static_cast<size_t>(eocd->cd_size), true /* read only */) ) {
return kMmapFailed;
}
- archive->directory_map = map;
- archive->num_entries = num_entries;
- archive->directory_offset = dir_offset;
+ archive->num_entries = eocd->num_records;
+ archive->directory_offset = eocd->cd_start_offset;
return 0;
}
@@ -429,13 +540,13 @@
return kInvalidFile;
}
- if (file_length > (off64_t) 0xffffffff) {
- ALOGV("Zip: zip file too long %d", file_length);
+ if (file_length > static_cast<off64_t>(0xffffffff)) {
+ ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
return kInvalidFile;
}
- if (file_length < (int64_t) kEOCDLen) {
- ALOGV("Zip: length %ld is too small to be zip", file_length);
+ if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
+ ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
return kInvalidFile;
}
@@ -451,12 +562,12 @@
*
* We start by pulling in the last part of the file.
*/
- uint32_t read_amount = kMaxEOCDSearch;
- if (file_length < (off64_t) read_amount) {
+ off64_t read_amount = kMaxEOCDSearch;
+ if (file_length < read_amount) {
read_amount = file_length;
}
- uint8_t* scan_buffer = (uint8_t*) malloc(read_amount);
+ uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
file_length, read_amount, scan_buffer);
@@ -471,10 +582,10 @@
* Returns 0 on success.
*/
static int32_t ParseZipArchive(ZipArchive* archive) {
- int32_t result = -1;
- const uint8_t* cd_ptr = (const uint8_t*) archive->directory_map->getDataPtr();
- size_t cd_length = archive->directory_map->getDataLength();
- uint16_t num_entries = archive->num_entries;
+ const uint8_t* const cd_ptr =
+ reinterpret_cast<const uint8_t*>(archive->directory_map.getDataPtr());
+ const size_t cd_length = archive->directory_map.getDataLength();
+ const uint16_t num_entries = archive->num_entries;
/*
* Create hash table. We have a minimum 75% load factor, possibly as
@@ -482,57 +593,66 @@
* least one unused entry to avoid an infinite loop during creation.
*/
archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
- archive->hash_table = (ZipEntryName*) calloc(archive->hash_table_size,
- sizeof(ZipEntryName));
+ archive->hash_table = reinterpret_cast<ZipEntryName*>(calloc(archive->hash_table_size,
+ sizeof(ZipEntryName)));
/*
* Walk through the central directory, adding entries to the hash
* table and verifying values.
*/
+ const uint8_t* const cd_end = cd_ptr + cd_length;
const uint8_t* ptr = cd_ptr;
for (uint16_t i = 0; i < num_entries; i++) {
- if (get4LE(ptr) != kCDESignature) {
- ALOGW("Zip: missed a central dir sig (at %d)", i);
- goto bail;
+ const CentralDirectoryRecord* cdr =
+ reinterpret_cast<const CentralDirectoryRecord*>(ptr);
+ if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
+ ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
+ return -1;
}
- if (ptr + kCDELen > cd_ptr + cd_length) {
- ALOGW("Zip: ran off the end (at %d)", i);
- goto bail;
+ if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
+ ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
+ return -1;
}
- const off64_t local_header_offset = get4LE(ptr + kCDELocalOffset);
+ const off64_t local_header_offset = cdr->local_file_header_offset;
if (local_header_offset >= archive->directory_offset) {
- ALOGW("Zip: bad LFH offset %lld at entry %d", local_header_offset, i);
- goto bail;
+ ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
+ static_cast<int64_t>(local_header_offset), i);
+ return -1;
}
- const uint16_t file_name_length = get2LE(ptr + kCDENameLen);
- const uint16_t extra_length = get2LE(ptr + kCDEExtraLen);
- const uint16_t comment_length = get2LE(ptr + kCDECommentLen);
+ const uint16_t file_name_length = cdr->file_name_length;
+ const uint16_t extra_length = cdr->extra_field_length;
+ const uint16_t comment_length = cdr->comment_length;
+ const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
+
+ /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
+ if (!IsValidEntryName(file_name, file_name_length)) {
+ return -1;
+ }
/* add the CDE filename to the hash table */
+ ZipEntryName entry_name;
+ entry_name.name = file_name;
+ entry_name.name_length = file_name_length;
const int add_result = AddToHash(archive->hash_table,
- archive->hash_table_size, (const char*) ptr + kCDELen, file_name_length);
- if (add_result) {
+ archive->hash_table_size, entry_name);
+ if (add_result != 0) {
ALOGW("Zip: Error adding entry to hash table %d", add_result);
- result = add_result;
- goto bail;
+ return add_result;
}
- ptr += kCDELen + file_name_length + extra_length + comment_length;
- if ((size_t)(ptr - cd_ptr) > cd_length) {
- ALOGW("Zip: bad CD advance (%d vs %zd) at entry %d",
- (int) (ptr - cd_ptr), cd_length, i);
- goto bail;
+ ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
+ if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
+ ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
+ ptr - cd_ptr, cd_length, i);
+ return -1;
}
}
- ALOGV("+++ zip good scan %d entries", num_entries);
+ ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
- result = 0;
-
-bail:
- return result;
+ return 0;
}
static int32_t OpenArchiveInternal(ZipArchive* archive,
@@ -550,27 +670,20 @@
}
int32_t OpenArchiveFd(int fd, const char* debug_file_name,
- ZipArchiveHandle* handle) {
- ZipArchive* archive = (ZipArchive*) malloc(sizeof(ZipArchive));
- memset(archive, 0, sizeof(*archive));
+ ZipArchiveHandle* handle, bool assume_ownership) {
+ ZipArchive* archive = new ZipArchive(fd, assume_ownership);
*handle = archive;
-
- archive->fd = fd;
-
return OpenArchiveInternal(archive, debug_file_name);
}
int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
- ZipArchive* archive = (ZipArchive*) malloc(sizeof(ZipArchive));
- memset(archive, 0, sizeof(*archive));
+ const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
+ ZipArchive* archive = new ZipArchive(fd, true);
*handle = archive;
- const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
if (fd < 0) {
ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
return kIoError;
- } else {
- archive->fd = fd;
}
return OpenArchiveInternal(archive, fileName);
@@ -580,43 +693,26 @@
* Close a ZipArchive, closing the file and freeing the contents.
*/
void CloseArchive(ZipArchiveHandle handle) {
- ZipArchive* archive = (ZipArchive*) handle;
+ ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
ALOGV("Closing archive %p", archive);
-
- if (archive->fd >= 0) {
- close(archive->fd);
- }
-
- if (archive->directory_map != NULL) {
- archive->directory_map->release();
- }
- free(archive->hash_table);
-
- /* ensure nobody tries to use the ZipArchive after it's closed */
- archive->directory_offset = -1;
- archive->fd = -1;
- archive->num_entries = -1;
- archive->hash_table_size = -1;
- archive->hash_table = NULL;
+ delete archive;
}
static int32_t UpdateEntryFromDataDescriptor(int fd,
ZipEntry *entry) {
- uint8_t ddBuf[kDDMaxLen];
+ uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
if (actual != sizeof(ddBuf)) {
return kIoError;
}
- const uint32_t ddSignature = get4LE(ddBuf);
- uint16_t ddOffset = 0;
- if (ddSignature == kDDOptSignature) {
- ddOffset = 4;
- }
+ const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
+ const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
+ const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
- entry->crc32 = get4LE(ddBuf + ddOffset + kDDCrc32);
- entry->compressed_length = get4LE(ddBuf + ddOffset + kDDCompLen);
- entry->uncompressed_length = get4LE(ddBuf + ddOffset + kDDUncompLen);
+ entry->crc32 = descriptor->crc32;
+ entry->compressed_length = descriptor->compressed_size;
+ entry->uncompressed_length = descriptor->uncompressed_size;
return 0;
}
@@ -629,42 +725,44 @@
// as a side effect of this call.
static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
off64_t off) {
-#ifdef HAVE_PREAD
+#if !defined(_WIN32)
return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
#else
// The only supported platform that doesn't support pread at the moment
// is Windows. Only recent versions of windows support unix like forks,
// and even there the semantics are quite different.
if (lseek64(fd, off, SEEK_SET) != off) {
- ALOGW("Zip: failed seek to offset %lld", off);
+ ALOGW("Zip: failed seek to offset %" PRId64, off);
return kIoError;
}
return TEMP_FAILURE_RETRY(read(fd, buf, len));
-#endif // HAVE_PREAD
+#endif
}
static int32_t FindEntry(const ZipArchive* archive, const int ent,
ZipEntry* data) {
const uint16_t nameLen = archive->hash_table[ent].name_length;
- const char* name = archive->hash_table[ent].name;
// Recover the start of the central directory entry from the filename
// pointer. The filename is the first entry past the fixed-size data,
// so we can just subtract back from that.
- const unsigned char* ptr = (const unsigned char*) name;
- ptr -= kCDELen;
+ const uint8_t* ptr = archive->hash_table[ent].name;
+ ptr -= sizeof(CentralDirectoryRecord);
// This is the base of our mmapped region, we have to sanity check that
// the name that's in the hash table is a pointer to a location within
// this mapped region.
- const unsigned char* base_ptr = (const unsigned char*)
- archive->directory_map->getDataPtr();
- if (ptr < base_ptr || ptr > base_ptr + archive->directory_map->getDataLength()) {
+ const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
+ archive->directory_map.getDataPtr());
+ if (ptr < base_ptr || ptr > base_ptr + archive->directory_map.getDataLength()) {
ALOGW("Zip: Invalid entry pointer");
return kInvalidOffset;
}
+ const CentralDirectoryRecord *cdr =
+ reinterpret_cast<const CentralDirectoryRecord*>(ptr);
+
// The offset of the start of the central directory in the zipfile.
// We keep this lying around so that we can sanity check all our lengths
// and our per-file structures.
@@ -673,52 +771,49 @@
// Fill out the compression method, modification time, crc32
// and other interesting attributes from the central directory. These
// will later be compared against values from the local file header.
- data->method = get2LE(ptr + kCDEMethod);
- data->mod_time = get4LE(ptr + kCDEModWhen);
- data->crc32 = get4LE(ptr + kCDECRC);
- data->compressed_length = get4LE(ptr + kCDECompLen);
- data->uncompressed_length = get4LE(ptr + kCDEUncompLen);
+ data->method = cdr->compression_method;
+ data->mod_time = cdr->last_mod_time;
+ data->crc32 = cdr->crc32;
+ data->compressed_length = cdr->compressed_size;
+ data->uncompressed_length = cdr->uncompressed_size;
// Figure out the local header offset from the central directory. The
// actual file data will begin after the local header and the name /
// extra comments.
- const off64_t local_header_offset = get4LE(ptr + kCDELocalOffset);
- if (local_header_offset + (off64_t) kLFHLen >= cd_offset) {
+ const off64_t local_header_offset = cdr->local_file_header_offset;
+ if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
ALOGW("Zip: bad local hdr offset in zip");
return kInvalidOffset;
}
- uint8_t lfh_buf[kLFHLen];
+ uint8_t lfh_buf[sizeof(LocalFileHeader)];
ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
local_header_offset);
if (actual != sizeof(lfh_buf)) {
- ALOGW("Zip: failed reading lfh name from offset %lld", local_header_offset);
+ ALOGW("Zip: failed reading lfh name from offset %" PRId64,
+ static_cast<int64_t>(local_header_offset));
return kIoError;
}
- if (get4LE(lfh_buf) != kLFHSignature) {
- ALOGW("Zip: didn't find signature at start of lfh, offset=%lld",
- local_header_offset);
+ const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
+
+ if (lfh->lfh_signature != LocalFileHeader::kSignature) {
+ ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
+ static_cast<int64_t>(local_header_offset));
return kInvalidOffset;
}
// Paranoia: Match the values specified in the local file header
// to those specified in the central directory.
- const uint16_t lfhGpbFlags = get2LE(lfh_buf + kLFHGPBFlags);
- const uint16_t lfhNameLen = get2LE(lfh_buf + kLFHNameLen);
- const uint16_t lfhExtraLen = get2LE(lfh_buf + kLFHExtraLen);
-
- if ((lfhGpbFlags & kGPBDDFlagMask) == 0) {
- const uint32_t lfhCrc = get4LE(lfh_buf + kLFHCRC);
- const uint32_t lfhCompLen = get4LE(lfh_buf + kLFHCompLen);
- const uint32_t lfhUncompLen = get4LE(lfh_buf + kLFHUncompLen);
-
+ if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
data->has_data_descriptor = 0;
- if (data->compressed_length != lfhCompLen || data->uncompressed_length != lfhUncompLen
- || data->crc32 != lfhCrc) {
- ALOGW("Zip: size/crc32 mismatch. expected {%d, %d, %x}, was {%d, %d, %x}",
+ if (data->compressed_length != lfh->compressed_size
+ || data->uncompressed_length != lfh->uncompressed_size
+ || data->crc32 != lfh->crc32) {
+ ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
+ ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
data->compressed_length, data->uncompressed_length, data->crc32,
- lfhCompLen, lfhUncompLen, lfhCrc);
+ lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
return kInconsistentInformation;
}
} else {
@@ -727,24 +822,24 @@
// Check that the local file header name matches the declared
// name in the central directory.
- if (lfhNameLen == nameLen) {
- const off64_t name_offset = local_header_offset + kLFHLen;
- if (name_offset + lfhNameLen >= cd_offset) {
+ if (lfh->file_name_length == nameLen) {
+ const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
+ if (name_offset + lfh->file_name_length > cd_offset) {
ALOGW("Zip: Invalid declared length");
return kInvalidOffset;
}
- uint8_t* name_buf = (uint8_t*) malloc(nameLen);
+ uint8_t* name_buf = reinterpret_cast<uint8_t*>(malloc(nameLen));
ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
name_offset);
if (actual != nameLen) {
- ALOGW("Zip: failed reading lfh name from offset %lld", name_offset);
+ ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
free(name_buf);
return kIoError;
}
- if (memcmp(name, name_buf, nameLen)) {
+ if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
free(name_buf);
return kInconsistentInformation;
}
@@ -755,22 +850,24 @@
return kInconsistentInformation;
}
- const off64_t data_offset = local_header_offset + kLFHLen + lfhNameLen + lfhExtraLen;
+ const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
+ + lfh->file_name_length + lfh->extra_field_length;
if (data_offset > cd_offset) {
- ALOGW("Zip: bad data offset %lld in zip", (off64_t) data_offset);
+ ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
return kInvalidOffset;
}
- if ((off64_t)(data_offset + data->compressed_length) > cd_offset) {
- ALOGW("Zip: bad compressed length in zip (%lld + %zd > %lld)",
- data_offset, data->compressed_length, cd_offset);
+ if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
+ ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
+ static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
return kInvalidOffset;
}
if (data->method == kCompressStored &&
- (off64_t)(data_offset + data->uncompressed_length) > cd_offset) {
- ALOGW("Zip: bad uncompressed length in zip (%lld + %zd > %lld)",
- data_offset, data->uncompressed_length, cd_offset);
+ static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
+ ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
+ static_cast<int64_t>(data_offset), data->uncompressed_length,
+ static_cast<int64_t>(cd_offset));
return kInvalidOffset;
}
@@ -780,45 +877,61 @@
struct IterationHandle {
uint32_t position;
- const char* prefix;
+ // We're not using vector here because this code is used in the Windows SDK
+ // where the STL is not available.
+ const uint8_t* prefix;
uint16_t prefix_len;
ZipArchive* archive;
+
+ IterationHandle() : prefix(NULL), prefix_len(0) {}
+
+ IterationHandle(const ZipEntryName& prefix_name)
+ : prefix_len(prefix_name.name_length) {
+ uint8_t* prefix_copy = new uint8_t[prefix_len];
+ memcpy(prefix_copy, prefix_name.name, prefix_len);
+ prefix = prefix_copy;
+ }
+
+ ~IterationHandle() {
+ delete[] prefix;
+ }
};
-int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, const char* prefix) {
- ZipArchive* archive = (ZipArchive *) handle;
+int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
+ const ZipEntryName* optional_prefix) {
+ ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
if (archive == NULL || archive->hash_table == NULL) {
ALOGW("Zip: Invalid ZipArchiveHandle");
return kInvalidHandle;
}
- IterationHandle* cookie = (IterationHandle*) malloc(sizeof(IterationHandle));
+ IterationHandle* cookie =
+ optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
cookie->position = 0;
- cookie->prefix = prefix;
cookie->archive = archive;
- if (prefix != NULL) {
- cookie->prefix_len = strlen(prefix);
- }
*cookie_ptr = cookie ;
return 0;
}
-int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
+void EndIteration(void* cookie) {
+ delete reinterpret_cast<IterationHandle*>(cookie);
+}
+
+int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
ZipEntry* data) {
- const ZipArchive* archive = (ZipArchive*) handle;
- const int nameLen = strlen(entryName);
- if (nameLen == 0 || nameLen > 65535) {
- ALOGW("Zip: Invalid filename %s", entryName);
+ const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
+ if (entryName.name_length == 0) {
+ ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
return kInvalidEntryName;
}
const int64_t ent = EntryToIndex(archive->hash_table,
- archive->hash_table_size, entryName, nameLen);
+ archive->hash_table_size, entryName);
if (ent < 0) {
- ALOGV("Zip: Could not find entry %.*s", nameLen, entryName);
+ ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
return ent;
}
@@ -826,7 +939,7 @@
}
int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
- IterationHandle* handle = (IterationHandle *) cookie;
+ IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
if (handle == NULL) {
return kInvalidHandle;
}
@@ -843,7 +956,7 @@
for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
if (hash_table[i].name != NULL &&
- (handle->prefix == NULL ||
+ (handle->prefix_len == 0 ||
(memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
handle->position = (i + 1);
const int error = FindEntry(archive, i, data);
@@ -860,13 +973,20 @@
return kIterationEnd;
}
+// This method is using libz macros with old-style-casts
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
+ return inflateInit2(stream, window_bits);
+}
+#pragma GCC diagnostic pop
+
static int32_t InflateToFile(int fd, const ZipEntry* entry,
uint8_t* begin, uint32_t length,
uint64_t* crc_out) {
- int32_t result = -1;
- const uint32_t kBufSize = 32768;
- uint8_t read_buf[kBufSize];
- uint8_t write_buf[kBufSize];
+ const size_t kBufSize = 32768;
+ std::vector<uint8_t> read_buf(kBufSize);
+ std::vector<uint8_t> write_buf(kBufSize);
z_stream zstream;
int zerr;
@@ -879,7 +999,7 @@
zstream.opaque = Z_NULL;
zstream.next_in = NULL;
zstream.avail_in = 0;
- zstream.next_out = (Bytef*) write_buf;
+ zstream.next_out = &write_buf[0];
zstream.avail_out = kBufSize;
zstream.data_type = Z_UNKNOWN;
@@ -887,7 +1007,7 @@
* Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it.
*/
- zerr = inflateInit2(&zstream, -MAX_WBITS);
+ zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) {
ALOGE("Installed zlib is not compatible with linked version (%s)",
@@ -899,6 +1019,12 @@
return kZlibError;
}
+ auto zstream_deleter = [](z_stream* stream) {
+ inflateEnd(stream); /* free up any allocated structures */
+ };
+
+ std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
+
const uint32_t uncompressed_length = entry->uncompressed_length;
uint32_t compressed_length = entry->compressed_length;
@@ -906,17 +1032,16 @@
do {
/* read as much as we can */
if (zstream.avail_in == 0) {
- const ssize_t getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
- const ssize_t actual = TEMP_FAILURE_RETRY(read(fd, read_buf, getSize));
+ const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
+ const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, &read_buf[0], getSize));
if (actual != getSize) {
- ALOGW("Zip: inflate read failed (%d vs %zd)", actual, getSize);
- result = kIoError;
- goto z_bail;
+ ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
+ return kIoError;
}
compressed_length -= getSize;
- zstream.next_in = read_buf;
+ zstream.next_in = &read_buf[0];
zstream.avail_in = getSize;
}
@@ -926,22 +1051,21 @@
ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
zerr, zstream.next_in, zstream.avail_in,
zstream.next_out, zstream.avail_out);
- result = kZlibError;
- goto z_bail;
+ return kZlibError;
}
/* write when we're full or when we're done */
if (zstream.avail_out == 0 ||
(zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
- const size_t write_size = zstream.next_out - write_buf;
+ const size_t write_size = zstream.next_out - &write_buf[0];
// The file might have declared a bogus length.
if (write_size + write_count > length) {
- goto z_bail;
+ return -1;
}
- memcpy(begin + write_count, write_buf, write_size);
+ memcpy(begin + write_count, &write_buf[0], write_size);
write_count += write_size;
- zstream.next_out = write_buf;
+ zstream.next_out = &write_buf[0];
zstream.avail_out = kBufSize;
}
} while (zerr == Z_OK);
@@ -952,28 +1076,22 @@
*crc_out = zstream.adler;
if (zstream.total_out != uncompressed_length || compressed_length != 0) {
- ALOGW("Zip: size mismatch on inflated file (%ld vs %zd)",
+ ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
zstream.total_out, uncompressed_length);
- result = kInconsistentInformation;
- goto z_bail;
+ return kInconsistentInformation;
}
- result = 0;
-
-z_bail:
- inflateEnd(&zstream); /* free up any allocated structures */
-
- return result;
+ return 0;
}
int32_t ExtractToMemory(ZipArchiveHandle handle,
ZipEntry* entry, uint8_t* begin, uint32_t size) {
- ZipArchive* archive = (ZipArchive*) handle;
+ ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
const uint16_t method = entry->method;
off64_t data_offset = entry->offset;
if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
- ALOGW("Zip: lseek to data at %lld failed", (off64_t) data_offset);
+ ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
return kIoError;
}
@@ -996,7 +1114,7 @@
// TODO: Fix this check by passing the right flags to inflate2 so that
// it calculates the CRC for us.
if (entry->crc32 != crc && false) {
- ALOGW("Zip: crc mismatch: expected %u, was %llu", entry->crc32, crc);
+ ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
return kInconsistentInformation;
}
@@ -1016,8 +1134,8 @@
int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
if (result == -1) {
- ALOGW("Zip: unable to truncate file to %lld: %s", declared_length + current_offset,
- strerror(errno));
+ ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
+ static_cast<int64_t>(declared_length + current_offset), strerror(errno));
return kIoError;
}
@@ -1028,16 +1146,14 @@
return 0;
}
- android::FileMap* map = MapFileSegment(fd, current_offset, declared_length,
- false, kTempMappingFileName);
- if (map == NULL) {
+ android::FileMap map;
+ if (!map.create(kTempMappingFileName, fd, current_offset, declared_length, false)) {
return kMmapFailed;
}
const int32_t error = ExtractToMemory(handle, entry,
- reinterpret_cast<uint8_t*>(map->getDataPtr()),
- map->getDataLength());
- map->release();
+ reinterpret_cast<uint8_t*>(map.getDataPtr()),
+ map.getDataLength());
return error;
}
@@ -1050,6 +1166,6 @@
}
int GetFileDescriptor(const ZipArchiveHandle handle) {
- return ((ZipArchive*) handle)->fd;
+ return reinterpret_cast<ZipArchive*>(handle)->fd;
}
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc
index 3082216..64faa6d 100644
--- a/libziparchive/zip_archive_test.cc
+++ b/libziparchive/zip_archive_test.cc
@@ -17,6 +17,7 @@
#include "ziparchive/zip_archive.h"
#include <errno.h>
+#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
@@ -26,6 +27,7 @@
static std::string test_data_dir;
+static const std::string kMissingZip = "missing.zip";
static const std::string kValidZip = "valid.zip";
static const uint8_t kATxtContents[] = {
@@ -39,6 +41,27 @@
'\n'
};
+static const uint16_t kATxtNameLength = 5;
+static const uint16_t kBTxtNameLength = 5;
+static const uint16_t kNonexistentTxtNameLength = 15;
+static const uint16_t kEmptyTxtNameLength = 9;
+
+static const uint8_t kATxtName[kATxtNameLength] = {
+ 'a', '.', 't', 'x', 't'
+};
+
+static const uint8_t kBTxtName[kBTxtNameLength] = {
+ 'b', '.', 't', 'x', 't'
+};
+
+static const uint8_t kNonexistentTxtName[kNonexistentTxtNameLength] = {
+ 'n', 'o', 'n', 'e', 'x', 'i', 's', 't', 'e', 'n', 't', '.', 't', 'x' ,'t'
+};
+
+static const uint8_t kEmptyTxtName[kEmptyTxtNameLength] = {
+ 'e', 'm', 'p', 't', 'y', '.', 't', 'x', 't'
+};
+
static int32_t OpenArchiveWrapper(const std::string& name,
ZipArchiveHandle* handle) {
const std::string abs_path = test_data_dir + "/" + name;
@@ -58,6 +81,34 @@
CloseArchive(handle);
}
+TEST(ziparchive, OpenMissing) {
+ ZipArchiveHandle handle;
+ ASSERT_NE(0, OpenArchiveWrapper(kMissingZip, &handle));
+
+ // Confirm the file descriptor is not going to be mistaken for a valid one.
+ ASSERT_EQ(-1, GetFileDescriptor(handle));
+}
+
+TEST(ziparchive, OpenAssumeFdOwnership) {
+ int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
+ ASSERT_NE(-1, fd);
+ ZipArchiveHandle handle;
+ ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle));
+ CloseArchive(handle);
+ ASSERT_EQ(-1, lseek(fd, 0, SEEK_SET));
+ ASSERT_EQ(EBADF, errno);
+}
+
+TEST(ziparchive, OpenDoNotAssumeFdOwnership) {
+ int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
+ ASSERT_NE(-1, fd);
+ ZipArchiveHandle handle;
+ ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle, false));
+ CloseArchive(handle);
+ ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
+ close(fd);
+}
+
TEST(ziparchive, Iteration) {
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
@@ -99,7 +150,10 @@
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
ZipEntry data;
- ASSERT_EQ(0, FindEntry(handle, "a.txt", &data));
+ ZipEntryName name;
+ name.name = kATxtName;
+ name.name_length = kATxtNameLength;
+ ASSERT_EQ(0, FindEntry(handle, name, &data));
// Known facts about a.txt, from zipinfo -v.
ASSERT_EQ(63, data.offset);
@@ -109,7 +163,26 @@
ASSERT_EQ(0x950821c5, data.crc32);
// An entry that doesn't exist. Should be a negative return code.
- ASSERT_LT(FindEntry(handle, "nonexistent.txt", &data), 0);
+ ZipEntryName absent_name;
+ absent_name.name = kNonexistentTxtName;
+ absent_name.name_length = kNonexistentTxtNameLength;
+ ASSERT_LT(FindEntry(handle, absent_name, &data), 0);
+
+ CloseArchive(handle);
+}
+
+TEST(ziparchive, TestInvalidDeclaredLength) {
+ ZipArchiveHandle handle;
+ ASSERT_EQ(0, OpenArchiveWrapper("declaredlength.zip", &handle));
+
+ void* iteration_cookie;
+ ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL));
+
+ ZipEntryName name;
+ ZipEntry data;
+
+ ASSERT_EQ(Next(iteration_cookie, &data, &name), 0);
+ ASSERT_EQ(Next(iteration_cookie, &data, &name), 0);
CloseArchive(handle);
}
@@ -120,7 +193,10 @@
// An entry that's deflated.
ZipEntry data;
- ASSERT_EQ(0, FindEntry(handle, "a.txt", &data));
+ ZipEntryName a_name;
+ a_name.name = kATxtName;
+ a_name.name_length = kATxtNameLength;
+ ASSERT_EQ(0, FindEntry(handle, a_name, &data));
const uint32_t a_size = data.uncompressed_length;
ASSERT_EQ(a_size, sizeof(kATxtContents));
uint8_t* buffer = new uint8_t[a_size];
@@ -129,7 +205,10 @@
delete[] buffer;
// An entry that's stored.
- ASSERT_EQ(0, FindEntry(handle, "b.txt", &data));
+ ZipEntryName b_name;
+ b_name.name = kBTxtName;
+ b_name.name_length = kBTxtNameLength;
+ ASSERT_EQ(0, FindEntry(handle, b_name, &data));
const uint32_t b_size = data.uncompressed_length;
ASSERT_EQ(b_size, sizeof(kBTxtContents));
buffer = new uint8_t[b_size];
@@ -140,11 +219,7 @@
CloseArchive(handle);
}
-TEST(ziparchive, EmptyEntries) {
- char temp_file_pattern[] = "empty_entries_test_XXXXXX";
- int fd = mkstemp(temp_file_pattern);
- ASSERT_NE(-1, fd);
- const uint32_t data[] = {
+static const uint32_t kEmptyEntriesZip[] = {
0x04034b50, 0x0000000a, 0x63600000, 0x00004438, 0x00000000, 0x00000000,
0x00090000, 0x6d65001c, 0x2e797470, 0x55747874, 0x03000954, 0x52e25c13,
0x52e25c24, 0x000b7875, 0x42890401, 0x88040000, 0x50000013, 0x1e02014b,
@@ -152,20 +227,43 @@
0x00001800, 0x00000000, 0xa0000000, 0x00000081, 0x706d6500, 0x742e7974,
0x54557478, 0x13030005, 0x7552e25c, 0x01000b78, 0x00428904, 0x13880400,
0x4b500000, 0x00000605, 0x00010000, 0x004f0001, 0x00430000, 0x00000000 };
- const ssize_t file_size = 168;
- ASSERT_EQ(file_size, TEMP_FAILURE_RETRY(write(fd, data, file_size)));
+
+static int make_temporary_file(const char* file_name_pattern) {
+ char full_path[1024];
+ // Account for differences between the host and the target.
+ //
+ // TODO: Maybe reuse bionic/tests/TemporaryFile.h.
+ snprintf(full_path, sizeof(full_path), "/data/local/tmp/%s", file_name_pattern);
+ int fd = mkstemp(full_path);
+ if (fd == -1) {
+ snprintf(full_path, sizeof(full_path), "/tmp/%s", file_name_pattern);
+ fd = mkstemp(full_path);
+ }
+
+ return fd;
+}
+
+TEST(ziparchive, EmptyEntries) {
+ char temp_file_pattern[] = "empty_entries_test_XXXXXX";
+ int fd = make_temporary_file(temp_file_pattern);
+ ASSERT_NE(-1, fd);
+ const ssize_t file_size = sizeof(kEmptyEntriesZip);
+ ASSERT_EQ(file_size, TEMP_FAILURE_RETRY(write(fd, kEmptyEntriesZip, file_size)));
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle));
ZipEntry entry;
- ASSERT_EQ(0, FindEntry(handle, "empty.txt", &entry));
+ ZipEntryName empty_name;
+ empty_name.name = kEmptyTxtName;
+ empty_name.name_length = kEmptyTxtNameLength;
+ ASSERT_EQ(0, FindEntry(handle, empty_name, &entry));
ASSERT_EQ(static_cast<uint32_t>(0), entry.uncompressed_length);
uint8_t buffer[1];
ASSERT_EQ(0, ExtractToMemory(handle, &entry, buffer, 1));
char output_file_pattern[] = "empty_entries_output_XXXXXX";
- int output_fd = mkstemp(output_file_pattern);
+ int output_fd = make_temporary_file(output_file_pattern);
ASSERT_NE(-1, output_fd);
ASSERT_EQ(0, ExtractEntryToFile(handle, &entry, output_fd));
@@ -177,9 +275,25 @@
close(output_fd);
}
+TEST(ziparchive, TrailerAfterEOCD) {
+ char temp_file_pattern[] = "trailer_after_eocd_test_XXXXXX";
+ int fd = make_temporary_file(temp_file_pattern);
+ ASSERT_NE(-1, fd);
+
+ // Create a file with 8 bytes of random garbage.
+ static const uint8_t trailer[] = { 'A' ,'n', 'd', 'r', 'o', 'i', 'd', 'z' };
+ const ssize_t file_size = sizeof(kEmptyEntriesZip);
+ const ssize_t trailer_size = sizeof(trailer);
+ ASSERT_EQ(file_size, TEMP_FAILURE_RETRY(write(fd, kEmptyEntriesZip, file_size)));
+ ASSERT_EQ(trailer_size, TEMP_FAILURE_RETRY(write(fd, trailer, trailer_size)));
+
+ ZipArchiveHandle handle;
+ ASSERT_GT(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle));
+}
+
TEST(ziparchive, ExtractToFile) {
char kTempFilePattern[] = "zip_archive_input_XXXXXX";
- int fd = mkstemp(kTempFilePattern);
+ int fd = make_temporary_file(kTempFilePattern);
ASSERT_NE(-1, fd);
const uint8_t data[8] = { '1', '2', '3', '4', '5', '6', '7', '8' };
const ssize_t data_size = sizeof(data);
@@ -190,7 +304,10 @@
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
ZipEntry entry;
- ASSERT_EQ(0, FindEntry(handle, "a.txt", &entry));
+ ZipEntryName name;
+ name.name = kATxtName;
+ name.name_length = kATxtNameLength;
+ ASSERT_EQ(0, FindEntry(handle, name, &entry));
ASSERT_EQ(0, ExtractEntryToFile(handle, &entry, fd));
@@ -209,7 +326,8 @@
sizeof(kATxtContents)));
// Assert that the total length of the file is sane
- ASSERT_EQ(data_size + sizeof(kATxtContents), lseek64(fd, 0, SEEK_END));
+ ASSERT_EQ(data_size + static_cast<ssize_t>(sizeof(kATxtContents)),
+ lseek64(fd, 0, SEEK_END));
close(fd);
}
@@ -247,4 +365,3 @@
return RUN_ALL_TESTS();
}
-
diff --git a/libzipfile/Android.mk b/libzipfile/Android.mk
index d2d758c..f054e15 100644
--- a/libzipfile/Android.mk
+++ b/libzipfile/Android.mk
@@ -7,12 +7,13 @@
centraldir.c \
zipfile.c
-LOCAL_STATIC_LIBRARIES := \
- libunz
+LOCAL_STATIC_LIBRARIES := libz
LOCAL_MODULE:= libzipfile
-LOCAL_C_INCLUDES += external/zlib
+LOCAL_CFLAGS := -Werror
+
+LOCAL_MULTILIB := both
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -23,12 +24,11 @@
centraldir.c \
zipfile.c
-LOCAL_STATIC_LIBRARIES := \
- libunz
+LOCAL_STATIC_LIBRARIES := libz
LOCAL_MODULE:= libzipfile
-LOCAL_C_INCLUDES += external/zlib
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
@@ -39,10 +39,10 @@
LOCAL_SRC_FILES:= \
test_zipfile.c
-LOCAL_STATIC_LIBRARIES := libzipfile libunz
+LOCAL_STATIC_LIBRARIES := libzipfile libz
LOCAL_MODULE := test_zipfile
-LOCAL_C_INCLUDES += external/zlib
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
diff --git a/libzipfile/centraldir.c b/libzipfile/centraldir.c
index 911e2b9..69cf47a 100644
--- a/libzipfile/centraldir.c
+++ b/libzipfile/centraldir.c
@@ -3,6 +3,8 @@
#include <string.h>
#include <stdlib.h>
+#include <utils/Compat.h>
+
enum {
// finding the directory
CD_SIGNATURE = 0x06054b50,
@@ -66,24 +68,10 @@
{
const unsigned char* p;
- unsigned short versionMadeBy;
- unsigned short versionToExtract;
- unsigned short gpBitFlag;
- unsigned short compressionMethod;
- unsigned short lastModFileTime;
- unsigned short lastModFileDate;
- unsigned long crc32;
unsigned short extraFieldLength;
unsigned short fileCommentLength;
- unsigned short diskNumberStart;
- unsigned short internalAttrs;
- unsigned long externalAttrs;
unsigned long localHeaderRelOffset;
- const unsigned char* extraField;
- const unsigned char* fileComment;
unsigned int dataOffset;
- unsigned short lfhExtraFieldSize;
-
p = *buf;
@@ -97,21 +85,12 @@
return -1;
}
- versionMadeBy = read_le_short(&p[0x04]);
- versionToExtract = read_le_short(&p[0x06]);
- gpBitFlag = read_le_short(&p[0x08]);
entry->compressionMethod = read_le_short(&p[0x0a]);
- lastModFileTime = read_le_short(&p[0x0c]);
- lastModFileDate = read_le_short(&p[0x0e]);
- crc32 = read_le_int(&p[0x10]);
entry->compressedSize = read_le_int(&p[0x14]);
entry->uncompressedSize = read_le_int(&p[0x18]);
entry->fileNameLength = read_le_short(&p[0x1c]);
extraFieldLength = read_le_short(&p[0x1e]);
fileCommentLength = read_le_short(&p[0x20]);
- diskNumberStart = read_le_short(&p[0x22]);
- internalAttrs = read_le_short(&p[0x24]);
- externalAttrs = read_le_int(&p[0x26]);
localHeaderRelOffset = read_le_int(&p[0x2a]);
p += ENTRY_LEN;
@@ -125,19 +104,9 @@
p += entry->fileNameLength;
// extra field
- if (extraFieldLength != 0) {
- extraField = p;
- } else {
- extraField = NULL;
- }
p += extraFieldLength;
// comment, if any
- if (fileCommentLength != 0) {
- fileComment = p;
- } else {
- fileComment = NULL;
- }
p += fileCommentLength;
*buf = p;
@@ -183,7 +152,7 @@
int err;
const unsigned char* buf = file->buf;
- ssize_t bufsize = file->bufsize;
+ ZD_TYPE bufsize = file->bufsize;
const unsigned char* eocd;
const unsigned char* p;
const unsigned char* start;
@@ -192,7 +161,7 @@
// too small to be a ZIP archive?
if (bufsize < EOCD_LEN) {
- fprintf(stderr, "Length is %zd -- too small\n", bufsize);
+ fprintf(stderr, "Length is " ZD " -- too small\n", bufsize);
goto bail;
}
diff --git a/libzipfile/zipfile.c b/libzipfile/zipfile.c
index a401a9b..1032ecc 100644
--- a/libzipfile/zipfile.c
+++ b/libzipfile/zipfile.c
@@ -76,10 +76,9 @@
};
static int
-uninflate(unsigned char* out, int unlen, const unsigned char* in, int clen)
+inflate_wrapper(unsigned char* out, int unlen, const unsigned char* in, int clen)
{
z_stream zstream;
- unsigned long crc;
int err = 0;
int zerr;
@@ -122,7 +121,7 @@
memcpy(buf, entry->data, entry->uncompressedSize);
return 0;
case DEFLATED:
- return uninflate(buf, bufsize, entry->data, entry->compressedSize);
+ return inflate_wrapper(buf, bufsize, entry->data, entry->compressedSize);
default:
return -1;
}
diff --git a/lmkd/Android.mk b/lmkd/Android.mk
index 5d6d1d2..39081d6 100644
--- a/lmkd/Android.mk
+++ b/lmkd/Android.mk
@@ -2,8 +2,8 @@
include $(CLEAR_VARS)
LOCAL_SRC_FILES := lmkd.c
-LOCAL_STATIC_LIBRARIES := libcutils liblog libm libc
-LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_SHARED_LIBRARIES := liblog libm libc libprocessgroup
+LOCAL_CFLAGS := -Werror
LOCAL_MODULE := lmkd
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index e489e81..7bbc811 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -16,20 +16,27 @@
#define LOG_TAG "lowmemorykiller"
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include <unistd.h>
-#include <arpa/inet.h>
+#include <sys/cdefs.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
+#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
-#include <cutils/log.h>
+#include <unistd.h>
+
#include <cutils/sockets.h>
+#include <log/log.h>
+#include <processgroup/processgroup.h>
+
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
#define MEMCG_SYSFS_PATH "/dev/memcg/"
#define MEMPRESSURE_WATCH_LEVEL "medium"
@@ -98,6 +105,7 @@
struct proc {
struct adjslot_list asl;
int pid;
+ uid_t uid;
int oomadj;
struct proc *pidhash_next;
};
@@ -120,6 +128,26 @@
/* PAGE_SIZE / 1024 */
static long page_k;
+static ssize_t read_all(int fd, char *buf, size_t max_len)
+{
+ ssize_t ret = 0;
+
+ while (max_len > 0) {
+ ssize_t r = read(fd, buf, max_len);
+ if (r == 0) {
+ break;
+ }
+ if (r == -1) {
+ return -1;
+ }
+ ret += r;
+ buf += r;
+ max_len -= r;
+ }
+
+ return ret;
+}
+
static int lowmem_oom_adj_to_oom_score_adj(int oom_adj)
{
if (oom_adj == OOM_ADJUST_MAX)
@@ -221,7 +249,7 @@
close(fd);
}
-static void cmd_procprio(int pid, int oomadj) {
+static void cmd_procprio(int pid, int uid, int oomadj) {
struct proc *procp;
char path[80];
char val[20];
@@ -247,6 +275,7 @@
}
procp->pid = pid;
+ procp->uid = uid;
procp->oomadj = oomadj;
proc_insert(procp);
} else {
@@ -257,8 +286,6 @@
}
static void cmd_procremove(int pid) {
- struct proc *procp;
-
if (use_inkernel_interface)
return;
@@ -352,9 +379,9 @@
cmd_target(targets, &ibuf[1]);
break;
case LMK_PROCPRIO:
- if (nargs != 2)
+ if (nargs != 3)
goto wronglen;
- cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]));
+ cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
break;
case LMK_PROCREMOVE:
if (nargs != 1)
@@ -382,7 +409,7 @@
}
}
-static void ctrl_connect_handler(uint32_t events) {
+static void ctrl_connect_handler(uint32_t events __unused) {
struct sockaddr addr;
socklen_t alen;
struct epoll_event epev;
@@ -414,17 +441,13 @@
static int zoneinfo_parse_protection(char *cp) {
int max = 0;
int zoneval;
+ char *save_ptr;
- if (*cp++ != '(')
- return 0;
-
- do {
+ for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
zoneval = strtol(cp, &cp, 0);
- if ((*cp != ',') && (*cp != ')'))
- return 0;
if (zoneval > max)
max = zoneval;
- } while (cp = strtok(NULL, " "));
+ }
return max;
}
@@ -432,12 +455,13 @@
static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
char *cp = line;
char *ap;
+ char *save_ptr;
- cp = strtok(line, " ");
+ cp = strtok_r(line, " ", &save_ptr);
if (!cp)
return;
- ap = strtok(NULL, " ");
+ ap = strtok_r(NULL, " ", &save_ptr);
if (!ap)
return;
@@ -454,57 +478,74 @@
}
static int zoneinfo_parse(struct sysmeminfo *mip) {
- FILE *f;
- char *cp;
- char line[LINE_MAX];
+ int fd;
+ ssize_t size;
+ char buf[PAGE_SIZE];
+ char *save_ptr;
+ char *line;
memset(mip, 0, sizeof(struct sysmeminfo));
- f = fopen(ZONEINFO_PATH, "r");
- if (!f) {
+
+ fd = open(ZONEINFO_PATH, O_RDONLY);
+ if (fd == -1) {
ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
return -1;
}
- while (fgets(line, LINE_MAX, f))
+ size = read_all(fd, buf, sizeof(buf) - 1);
+ if (size < 0) {
+ ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
+ close(fd);
+ return -1;
+ }
+ ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
+ buf[size] = 0;
+
+ for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
zoneinfo_parse_line(line, mip);
- fclose(f);
+ close(fd);
return 0;
}
static int proc_get_size(int pid) {
char path[PATH_MAX];
char line[LINE_MAX];
- FILE *f;
+ int fd;
int rss = 0;
int total;
+ ssize_t ret;
snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
- f = fopen(path, "r");
- if (!f)
+ fd = open(path, O_RDONLY);
+ if (fd == -1)
return -1;
- if (!fgets(line, LINE_MAX, f)) {
- fclose(f);
+
+ ret = read_all(fd, line, sizeof(line) - 1);
+ if (ret < 0) {
+ close(fd);
return -1;
}
sscanf(line, "%d %d ", &total, &rss);
- fclose(f);
+ close(fd);
return rss;
}
static char *proc_get_name(int pid) {
char path[PATH_MAX];
static char line[LINE_MAX];
- FILE *f;
+ int fd;
char *cp;
+ ssize_t ret;
snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
- f = fopen(path, "r");
- if (!f)
+ fd = open(path, O_RDONLY);
+ if (fd == -1)
return NULL;
- if (!fgets(line, LINE_MAX, f)) {
- fclose(f);
+ ret = read_all(fd, line, sizeof(line) - 1);
+ close(fd);
+ if (ret < 0) {
return NULL;
}
@@ -519,29 +560,56 @@
return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
}
-static void mp_event(uint32_t events) {
+/* Kill one process specified by procp. Returns the size of the process killed */
+static int kill_one_process(struct proc *procp, int other_free, int other_file,
+ int minfree, int min_score_adj, bool first)
+{
+ int pid = procp->pid;
+ uid_t uid = procp->uid;
+ char *taskname;
+ int tasksize;
+ int r;
+
+ taskname = proc_get_name(pid);
+ if (!taskname) {
+ pid_remove(pid);
+ return -1;
+ }
+
+ tasksize = proc_get_size(pid);
+ if (tasksize <= 0) {
+ pid_remove(pid);
+ return -1;
+ }
+
+ ALOGI("Killing '%s' (%d), uid %d, adj %d\n"
+ " to free %ldkB because cache %s%ldkB is below limit %ldkB for oom_adj %d\n"
+ " Free memory is %s%ldkB %s reserved",
+ taskname, pid, uid, procp->oomadj, tasksize * page_k,
+ first ? "" : "~", other_file * page_k, minfree * page_k, min_score_adj,
+ first ? "" : "~", other_free * page_k, other_free >= 0 ? "above" : "below");
+ r = kill(pid, SIGKILL);
+ killProcessGroup(uid, pid, SIGKILL);
+ pid_remove(pid);
+
+ if (r) {
+ ALOGE("kill(%d): errno=%d", procp->pid, errno);
+ return -1;
+ } else {
+ return tasksize;
+ }
+}
+
+/*
+ * Find a process to kill based on the current (possibly estimated) free memory
+ * and cached memory sizes. Returns the size of the killed processes.
+ */
+static int find_and_kill_process(int other_free, int other_file, bool first)
+{
int i;
- int ret;
- unsigned long long evcount;
- struct sysmeminfo mi;
- int other_free;
- int other_file;
- int minfree = 0;
int min_score_adj = OOM_ADJUST_MAX + 1;
-
- ret = read(mpevfd, &evcount, sizeof(evcount));
- if (ret < 0)
- ALOGE("Error reading memory pressure event fd; errno=%d",
- errno);
-
- if (time(NULL) - kill_lasttime < KILL_TIMEOUT)
- return;
-
- if (zoneinfo_parse(&mi) < 0)
- return;
-
- other_free = mi.nr_free_pages - mi.totalreserve_pages;
- other_file = mi.nr_file_pages - mi.nr_shmem;
+ int minfree = 0;
+ int killed_size = 0;
for (i = 0; i < lowmem_targets_size; i++) {
minfree = lowmem_minfree[i];
@@ -552,50 +620,60 @@
}
if (min_score_adj == OOM_ADJUST_MAX + 1)
- return;
+ return 0;
for (i = OOM_ADJUST_MAX; i >= min_score_adj; i--) {
struct proc *procp;
- retry:
+retry:
procp = proc_adj_lru(i);
if (procp) {
- int pid = procp->pid;
- char *taskname;
- int tasksize;
- int r;
-
- taskname = proc_get_name(pid);
- if (!taskname) {
- pid_remove(pid);
- goto retry;
- }
-
- tasksize = proc_get_size(pid);
- if (tasksize < 0) {
- pid_remove(pid);
- goto retry;
- }
-
- ALOGI("Killing '%s' (%d), adj %d\n"
- " to free %ldkB because cache %ldkB is below limit %ldkB for oom_adj %d\n"
- " Free memory is %ldkB %s reserved",
- taskname, pid, procp->oomadj, tasksize * page_k,
- other_file * page_k, minfree * page_k, min_score_adj,
- other_free * page_k, other_free >= 0 ? "above" : "below");
- r = kill(pid, SIGKILL);
- pid_remove(pid);
-
- if (r) {
- ALOGE("kill(%d): errno=%d", procp->pid, errno);
+ killed_size = kill_one_process(procp, other_free, other_file, minfree, min_score_adj, first);
+ if (killed_size < 0) {
goto retry;
} else {
- time(&kill_lasttime);
- break;
+ return killed_size;
}
}
}
+
+ return 0;
+}
+
+static void mp_event(uint32_t events __unused) {
+ int ret;
+ unsigned long long evcount;
+ struct sysmeminfo mi;
+ int other_free;
+ int other_file;
+ int killed_size;
+ bool first = true;
+
+ ret = read(mpevfd, &evcount, sizeof(evcount));
+ if (ret < 0)
+ ALOGE("Error reading memory pressure event fd; errno=%d",
+ errno);
+
+ if (time(NULL) - kill_lasttime < KILL_TIMEOUT)
+ return;
+
+ while (zoneinfo_parse(&mi) < 0) {
+ // Failed to read /proc/zoneinfo, assume ENOMEM and kill something
+ find_and_kill_process(0, 0, true);
+ }
+
+ other_free = mi.nr_free_pages - mi.totalreserve_pages;
+ other_file = mi.nr_file_pages - mi.nr_shmem;
+
+ do {
+ killed_size = find_and_kill_process(other_free, other_file, first);
+ if (killed_size > 0) {
+ first = false;
+ other_free += killed_size;
+ other_file += killed_size;
+ }
+ } while (killed_size > 0);
}
static int init_mp(char *levelstr, void *event_handler)
@@ -738,7 +816,13 @@
}
}
-int main(int argc, char **argv) {
+int main(int argc __unused, char **argv __unused) {
+ struct sched_param param = {
+ .sched_priority = 1,
+ };
+
+ mlockall(MCL_FUTURE);
+ sched_setscheduler(0, SCHED_FIFO, ¶m);
if (!init())
mainloop();
diff --git a/logcat/Android.mk b/logcat/Android.mk
index b5e27eb..f46a4de 100644
--- a/logcat/Android.mk
+++ b/logcat/Android.mk
@@ -7,7 +7,9 @@
LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_MODULE:= logcat
+LOCAL_MODULE := logcat
+
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
diff --git a/logcat/event.logtags b/logcat/event.logtags
index a325692..1b5c6f4 100644
--- a/logcat/event.logtags
+++ b/logcat/event.logtags
@@ -134,5 +134,7 @@
# libcore failure logging
90100 exp_det_cert_pin_failure (certs|4)
+1397638484 snet_event_log (subtag|3) (uid|1) (message|3)
+
# NOTE - the range 1000000-2000000 is reserved for partners and others who
# want to define their own log tags without conflicting with the core platform.
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 3c33938..be96fc4 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -1,9 +1,10 @@
-// Copyright 2006-2014 The Android Open Source Project
+// Copyright 2006-2015 The Android Open Source Project
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -17,6 +18,7 @@
#include <cutils/sockets.h>
#include <log/log.h>
+#include <log/log_read.h>
#include <log/logger.h>
#include <log/logd.h>
#include <log/logprint.h>
@@ -36,14 +38,12 @@
struct logger *logger;
struct logger_list *logger_list;
bool printed;
- char label;
log_device_t* next;
- log_device_t(const char* d, bool b, char l) {
+ log_device_t(const char* d, bool b) {
device = d;
binary = b;
- label = l;
next = NULL;
printed = false;
}
@@ -59,9 +59,7 @@
static int g_outFD = -1;
static off_t g_outByteCount = 0;
static int g_printBinary = 0;
-static int g_devCount = 0;
-
-static EventTagMap* g_eventTagMap = NULL;
+static int g_devCount = 0; // >1 means multiple
static int openLogFile (const char *pathname)
{
@@ -79,15 +77,20 @@
close(g_outFD);
+ // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
+ // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
+ int maxRotationCountDigits =
+ (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
+
for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
char *file0, *file1;
- asprintf(&file1, "%s.%d", g_outputFileName, i);
+ asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
if (i - 1 == 0) {
asprintf(&file0, "%s", g_outputFileName);
} else {
- asprintf(&file0, "%s.%d", g_outputFileName, i - 1);
+ asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
}
err = rename (file0, file1);
@@ -126,8 +129,15 @@
char binaryMsgBuf[1024];
if (dev->binary) {
+ static bool hasOpenedEventTagMap = false;
+ static EventTagMap *eventTagMap = NULL;
+
+ if (!eventTagMap && !hasOpenedEventTagMap) {
+ eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
+ hasOpenedEventTagMap = true;
+ }
err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
- g_eventTagMap,
+ eventTagMap,
binaryMsgBuf,
sizeof(binaryMsgBuf));
//printf(">>> pri=%d len=%d msg='%s'\n",
@@ -140,16 +150,6 @@
}
if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
- if (false && g_devCount > 1) {
- binaryMsgBuf[0] = dev->label;
- binaryMsgBuf[1] = ' ';
- bytesWritten = write(g_outFD, binaryMsgBuf, 2);
- if (bytesWritten < 0) {
- perror("output error");
- exit(-1);
- }
- }
-
bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
if (bytesWritten < 0) {
@@ -171,18 +171,19 @@
return;
}
-static void maybePrintStart(log_device_t* dev) {
- if (!dev->printed) {
- dev->printed = true;
+static void maybePrintStart(log_device_t* dev, bool printDividers) {
+ if (!dev->printed || printDividers) {
if (g_devCount > 1 && !g_printBinary) {
char buf[1024];
- snprintf(buf, sizeof(buf), "--------- beginning of %s\n",
+ snprintf(buf, sizeof(buf), "--------- %s %s\n",
+ dev->printed ? "switch to" : "beginning of",
dev->device);
if (write(g_outFD, buf, strlen(buf)) < 0) {
perror("output error");
exit(-1);
}
}
+ dev->printed = true;
}
}
@@ -214,41 +215,54 @@
fprintf(stderr, "options include:\n"
" -s Set default filter to silent.\n"
- " Like specifying filterspec '*:s'\n"
+ " Like specifying filterspec '*:S'\n"
" -f <filename> Log to file. Default to stdout\n"
" -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
" -n <count> Sets max number of rotated logs to <count>, default 4\n"
- " -v <format> Sets the log print format, where <format> is one of:\n\n"
- " brief process tag thread raw time threadtime long\n\n"
+ " -v <format> Sets the log print format, where <format> is:\n\n"
+ " brief color long process raw tag thread threadtime time\n\n"
+ " -D print dividers between each log buffer\n"
" -c clear (flush) the entire log and exit\n"
" -d dump the log and then exit (don't block)\n"
" -t <count> print only the most recent <count> lines (implies -d)\n"
+ " -t '<time>' print most recent lines since specified time (implies -d)\n"
" -T <count> print only the most recent <count> lines (does not imply -d)\n"
+ " -T '<time>' print most recent lines since specified time (not imply -d)\n"
+ " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
" -g get the size of the log's ring buffer and exit\n"
- " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio'\n"
- " or 'events'. Multiple -b parameters are allowed and the\n"
- " results are interleaved. The default is -b main -b system.\n"
- " -B output the log in binary");
-
+ " -L dump logs from prior to last reboot\n"
+ " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
+ " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
+ " allowed and results are interleaved. The default is\n"
+ " -b main -b system -b crash.\n"
+ " -B output the log in binary.\n"
+ " -S output statistics.\n"
+ " -G <size> set size of log ring buffer, may suffix with K or M.\n"
+ " -p print prune white and ~black list. Service is specified as\n"
+ " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
+ " with ~, otherwise weighed for longevity if unadorned. All\n"
+ " other pruning activity is oldest first. Special case ~!\n"
+ " represents an automatic quicker pruning for the noisiest\n"
+ " UID as determined by the current statistics.\n"
+ " -P '<list> ...' set prune white and ~black list, using same format as\n"
+ " printed above. Must be quoted.\n");
fprintf(stderr,"\nfilterspecs are a series of \n"
" <tag>[:priority]\n\n"
"where <tag> is a log component tag (or * for all) and priority is:\n"
- " V Verbose\n"
- " D Debug\n"
+ " V Verbose (default for <tag>)\n"
+ " D Debug (default for '*')\n"
" I Info\n"
" W Warn\n"
" E Error\n"
" F Fatal\n"
- " S Silent (supress all output)\n"
- "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
- "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
- "If no filterspec is found, filter defaults to '*:I'\n"
- "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
- "or defaults to \"brief\"\n\n");
-
-
-
+ " S Silent (suppress all output)\n"
+ "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
+ "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
+ "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
+ "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
+ "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
+ "or defaults to \"threadtime\"\n\n");
}
@@ -270,7 +284,29 @@
return 0;
}
-extern "C" void logprint_run_tests(void);
+static const char multipliers[][2] = {
+ { "" },
+ { "K" },
+ { "M" },
+ { "G" }
+};
+
+static unsigned long value_of_size(unsigned long value)
+{
+ for (unsigned i = 0;
+ (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
+ value /= 1024, ++i) ;
+ return value;
+}
+
+static const char *multiplier_of_size(unsigned long value)
+{
+ unsigned i;
+ for (i = 0;
+ (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
+ value /= 1024, ++i) ;
+ return multipliers[i];
+}
int main(int argc, char **argv)
{
@@ -278,23 +314,23 @@
int hasSetLogFormat = 0;
int clearLog = 0;
int getLogSize = 0;
- int mode = O_RDONLY;
+ unsigned long setLogSize = 0;
+ int getPruneList = 0;
+ char *setPruneList = NULL;
+ int printStatistics = 0;
+ int mode = ANDROID_LOG_RDONLY;
const char *forceFilters = NULL;
log_device_t* devices = NULL;
log_device_t* dev;
- bool needBinary = false;
+ bool printDividers = false;
struct logger_list *logger_list;
- int tail_lines = 0;
+ unsigned int tail_lines = 0;
+ log_time tail_time(log_time::EPOCH);
signal(SIGPIPE, exit);
g_logformat = android_log_format_new();
- if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
- logprint_run_tests();
- exit(0);
- }
-
if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
android::show_help(argv[0]);
exit(0);
@@ -303,7 +339,7 @@
for (;;) {
int ret;
- ret = getopt(argc, argv, "cdt:T:gsQf:r::n:v:b:B");
+ ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
if (ret < 0) {
break;
@@ -317,38 +353,144 @@
case 'c':
clearLog = 1;
- mode = O_WRONLY;
+ mode |= ANDROID_LOG_WRONLY;
+ break;
+
+ case 'L':
+ mode |= ANDROID_LOG_PSTORE;
break;
case 'd':
- mode = O_RDONLY | O_NDELAY;
+ mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
break;
case 't':
- mode = O_RDONLY | O_NDELAY;
+ mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
/* FALLTHRU */
case 'T':
- tail_lines = atoi(optarg);
+ if (strspn(optarg, "0123456789") != strlen(optarg)) {
+ char *cp = tail_time.strptime(optarg,
+ log_time::default_format);
+ if (!cp) {
+ fprintf(stderr,
+ "ERROR: -%c \"%s\" not in \"%s\" time format\n",
+ ret, optarg, log_time::default_format);
+ exit(1);
+ }
+ if (*cp) {
+ char c = *cp;
+ *cp = '\0';
+ fprintf(stderr,
+ "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
+ ret, optarg, c, cp + 1);
+ *cp = c;
+ }
+ } else {
+ tail_lines = atoi(optarg);
+ if (!tail_lines) {
+ fprintf(stderr,
+ "WARNING: -%c %s invalid, setting to 1\n",
+ ret, optarg);
+ tail_lines = 1;
+ }
+ }
+ break;
+
+ case 'D':
+ printDividers = true;
break;
case 'g':
getLogSize = 1;
break;
- case 'b': {
- bool binary = strcmp(optarg, "events") == 0;
- if (binary) {
- needBinary = true;
+ case 'G': {
+ // would use atol if not for the multiplier
+ char *cp = optarg;
+ setLogSize = 0;
+ while (('0' <= *cp) && (*cp <= '9')) {
+ setLogSize *= 10;
+ setLogSize += *cp - '0';
+ ++cp;
}
+ switch(*cp) {
+ case 'g':
+ case 'G':
+ setLogSize *= 1024;
+ /* FALLTHRU */
+ case 'm':
+ case 'M':
+ setLogSize *= 1024;
+ /* FALLTHRU */
+ case 'k':
+ case 'K':
+ setLogSize *= 1024;
+ /* FALLTHRU */
+ case '\0':
+ break;
+
+ default:
+ setLogSize = 0;
+ }
+
+ if (!setLogSize) {
+ fprintf(stderr, "ERROR: -G <num><multiplier>\n");
+ exit(1);
+ }
+ }
+ break;
+
+ case 'p':
+ getPruneList = 1;
+ break;
+
+ case 'P':
+ setPruneList = optarg;
+ break;
+
+ case 'b': {
+ if (strcmp(optarg, "all") == 0) {
+ while (devices) {
+ dev = devices;
+ devices = dev->next;
+ delete dev;
+ }
+
+ devices = dev = NULL;
+ android::g_devCount = 0;
+ for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
+ const char *name = android_log_id_to_name((log_id_t)i);
+ log_id_t log_id = android_name_to_log_id(name);
+
+ if (log_id != (log_id_t)i) {
+ continue;
+ }
+
+ bool binary = strcmp(name, "events") == 0;
+ log_device_t* d = new log_device_t(name, binary);
+
+ if (dev) {
+ dev->next = d;
+ dev = d;
+ } else {
+ devices = dev = d;
+ }
+ android::g_devCount++;
+ }
+ break;
+ }
+
+ bool binary = strcmp(optarg, "events") == 0;
+
if (devices) {
dev = devices;
while (dev->next) {
dev = dev->next;
}
- dev->next = new log_device_t(optarg, binary, optarg[0]);
+ dev->next = new log_device_t(optarg, binary);
} else {
- devices = new log_device_t(optarg, binary, optarg[0]);
+ devices = new log_device_t(optarg, binary);
}
android::g_devCount++;
}
@@ -370,9 +512,6 @@
android::g_logRotateSizeKBytes
= DEFAULT_LOG_ROTATE_SIZE_KBYTES;
} else {
- long logRotateSize;
- char *lastDigit;
-
if (!isdigit(optarg[0])) {
fprintf(stderr,"Invalid parameter to -r\n");
android::show_help(argv[0]);
@@ -400,7 +539,9 @@
exit(-1);
}
- hasSetLogFormat = 1;
+ if (strcmp("color", optarg)) { // exception for modifiers
+ hasSetLogFormat = 1;
+ }
break;
case 'Q':
@@ -470,6 +611,10 @@
}
break;
+ case 'S':
+ printStatistics = 1;
+ break;
+
default:
fprintf(stderr,"Unrecognized Option\n");
android::show_help(argv[0]);
@@ -479,10 +624,14 @@
}
if (!devices) {
- devices = new log_device_t("main", false, 'm');
+ dev = devices = new log_device_t("main", false);
android::g_devCount = 1;
if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
- devices->next = new log_device_t("system", false, 's');
+ dev = dev->next = new log_device_t("system", false);
+ android::g_devCount++;
+ }
+ if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
+ dev = dev->next = new log_device_t("crash", false);
android::g_devCount++;
}
}
@@ -502,11 +651,12 @@
if (logFormat != NULL) {
err = setLogFormat(logFormat);
-
if (err < 0) {
fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
logFormat);
}
+ } else {
+ setLogFormat("threadtime");
}
}
@@ -544,7 +694,11 @@
}
dev = devices;
- logger_list = android_logger_list_alloc(mode, tail_lines, 0);
+ if (tail_time != log_time::EPOCH) {
+ logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
+ } else {
+ logger_list = android_logger_list_alloc(mode, tail_lines, 0);
+ }
while (dev) {
dev->logger_list = logger_list;
dev->logger = android_logger_open(logger_list,
@@ -558,38 +712,117 @@
int ret;
ret = android_logger_clear(dev->logger);
if (ret) {
- perror("clearLog");
+ perror("failed to clear the log");
exit(EXIT_FAILURE);
}
}
+ if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
+ perror("failed to set the log size");
+ exit(EXIT_FAILURE);
+ }
+
if (getLogSize) {
- int size, readable;
+ long size, readable;
size = android_logger_get_log_size(dev->logger);
if (size < 0) {
- perror("getLogSize");
+ perror("failed to get the log size");
exit(EXIT_FAILURE);
}
readable = android_logger_get_log_readable_size(dev->logger);
if (readable < 0) {
- perror("getLogReadableSize");
+ perror("failed to get the readable log size");
exit(EXIT_FAILURE);
}
- printf("%s: ring buffer is %dKb (%dKb consumed), "
+ printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
"max entry is %db, max payload is %db\n", dev->device,
- size / 1024, readable / 1024,
+ value_of_size(size), multiplier_of_size(size),
+ value_of_size(readable), multiplier_of_size(readable),
(int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
}
dev = dev->next;
}
+ if (setPruneList) {
+ size_t len = strlen(setPruneList) + 32; // margin to allow rc
+ char *buf = (char *) malloc(len);
+
+ strcpy(buf, setPruneList);
+ int ret = android_logger_set_prune_list(logger_list, buf, len);
+ free(buf);
+
+ if (ret) {
+ perror("failed to set the prune list");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ if (printStatistics || getPruneList) {
+ size_t len = 8192;
+ char *buf;
+
+ for(int retry = 32;
+ (retry >= 0) && ((buf = new char [len]));
+ delete [] buf, --retry) {
+ if (getPruneList) {
+ android_logger_get_prune_list(logger_list, buf, len);
+ } else {
+ android_logger_get_statistics(logger_list, buf, len);
+ }
+ buf[len-1] = '\0';
+ size_t ret = atol(buf) + 1;
+ if (ret < 4) {
+ delete [] buf;
+ buf = NULL;
+ break;
+ }
+ bool check = ret <= len;
+ len = ret;
+ if (check) {
+ break;
+ }
+ }
+
+ if (!buf) {
+ perror("failed to read data");
+ exit(EXIT_FAILURE);
+ }
+
+ // remove trailing FF
+ char *cp = buf + len - 1;
+ *cp = '\0';
+ bool truncated = *--cp != '\f';
+ if (!truncated) {
+ *cp = '\0';
+ }
+
+ // squash out the byte count
+ cp = buf;
+ if (!truncated) {
+ while (isdigit(*cp)) {
+ ++cp;
+ }
+ if (*cp == '\n') {
+ ++cp;
+ }
+ }
+
+ printf("%s", cp);
+ delete [] buf;
+ exit(0);
+ }
+
+
if (getLogSize) {
exit(0);
}
+ if (setLogSize || setPruneList) {
+ exit(0);
+ }
if (clearLog) {
exit(0);
}
@@ -598,15 +831,15 @@
//LOG_EVENT_LONG(11, 0x1122334455667788LL);
//LOG_EVENT_STRING(0, "whassup, doc?");
- if (needBinary)
- android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
-
+ dev = NULL;
+ log_device_t unexpected("unexpected", false);
while (1) {
struct log_msg log_msg;
+ log_device_t* d;
int ret = android_logger_list_read(logger_list, &log_msg);
if (ret == 0) {
- fprintf(stderr, "read: Unexpected EOF!\n");
+ fprintf(stderr, "read: unexpected EOF!\n");
exit(EXIT_FAILURE);
}
@@ -616,28 +849,32 @@
}
if (ret == -EIO) {
- fprintf(stderr, "read: Unexpected EOF!\n");
+ fprintf(stderr, "read: unexpected EOF!\n");
exit(EXIT_FAILURE);
}
if (ret == -EINVAL) {
fprintf(stderr, "read: unexpected length.\n");
exit(EXIT_FAILURE);
}
- perror("logcat read");
+ perror("logcat read failure");
exit(EXIT_FAILURE);
}
- for(dev = devices; dev; dev = dev->next) {
- if (android_name_to_log_id(dev->device) == log_msg.id()) {
+ for(d = devices; d; d = d->next) {
+ if (android_name_to_log_id(d->device) == log_msg.id()) {
break;
}
}
- if (!dev) {
- fprintf(stderr, "read: Unexpected log ID!\n");
- exit(EXIT_FAILURE);
+ if (!d) {
+ android::g_devCount = 2; // set to Multiple
+ d = &unexpected;
+ d->binary = log_msg.id() == LOG_ID_EVENTS;
}
- android::maybePrintStart(dev);
+ if (dev != d) {
+ dev = d;
+ android::maybePrintStart(dev, printDividers);
+ }
if (android::g_printBinary) {
android::printBinary(&log_msg);
} else {
diff --git a/logcat/tests/Android.mk b/logcat/tests/Android.mk
index bdaec14..015a23d 100644
--- a/logcat/tests/Android.mk
+++ b/logcat/tests/Android.mk
@@ -16,11 +16,7 @@
LOCAL_PATH := $(call my-dir)
-# -----------------------------------------------------------------------------
-# Unit tests.
-# -----------------------------------------------------------------------------
-
-test_module := logcat-unit-tests
+test_module_prefix := logcat-
test_tags := tests
test_c_flags := \
@@ -29,6 +25,28 @@
-Wall -Wextra \
-Werror \
-fno-builtin \
+ -std=gnu++11
+
+# -----------------------------------------------------------------------------
+# Benchmarks (actually a gTest where the result code does not matter)
+# ----------------------------------------------------------------------------
+
+benchmark_src_files := \
+ logcat_benchmark.cpp
+
+# Build benchmarks for the device. Run with:
+# adb shell /data/nativetest/logcat-benchmarks/logcat-benchmarks
+include $(CLEAR_VARS)
+LOCAL_MODULE := $(test_module_prefix)benchmarks
+LOCAL_MODULE_TAGS := $(test_tags)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_CFLAGS += $(test_c_flags)
+LOCAL_SRC_FILES := $(benchmark_src_files)
+include $(BUILD_NATIVE_TEST)
+
+# -----------------------------------------------------------------------------
+# Unit tests.
+# -----------------------------------------------------------------------------
test_src_files := \
logcat_test.cpp \
@@ -36,11 +54,10 @@
# Build tests for the device (with .so). Run with:
# adb shell /data/nativetest/logcat-unit-tests/logcat-unit-tests
include $(CLEAR_VARS)
-LOCAL_MODULE := $(test_module)
+LOCAL_MODULE := $(test_module_prefix)unit-tests
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CFLAGS += $(test_c_flags)
-LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)
diff --git a/logcat/tests/logcat_benchmark.cpp b/logcat/tests/logcat_benchmark.cpp
new file mode 100644
index 0000000..be815be
--- /dev/null
+++ b/logcat/tests/logcat_benchmark.cpp
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2013-2014 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 <gtest/gtest.h>
+
+static const char begin[] = "--------- beginning of ";
+
+TEST(logcat, sorted_order) {
+ FILE *fp;
+
+ ASSERT_TRUE(NULL != (fp = popen(
+ "logcat -v time -b radio -b events -b system -b main -d 2>/dev/null",
+ "r")));
+
+ class timestamp {
+ private:
+ int month;
+ int day;
+ int hour;
+ int minute;
+ int second;
+ int millisecond;
+ bool ok;
+
+ public:
+ void init(const char *buffer)
+ {
+ ok = false;
+ if (buffer != NULL) {
+ ok = sscanf(buffer, "%d-%d %d:%d:%d.%d ",
+ &month, &day, &hour, &minute, &second, &millisecond) == 6;
+ }
+ }
+
+ timestamp(const char *buffer)
+ {
+ init(buffer);
+ }
+
+ bool operator< (timestamp &T)
+ {
+ return !ok || !T.ok
+ || (month < T.month)
+ || ((month == T.month)
+ && ((day < T.day)
+ || ((day == T.day)
+ && ((hour < T.hour)
+ || ((hour == T.hour)
+ && ((minute < T.minute)
+ || ((minute == T.minute)
+ && ((second < T.second)
+ || ((second == T.second)
+ && (millisecond < T.millisecond))))))))));
+ }
+
+ bool valid(void)
+ {
+ return ok;
+ }
+ } last(NULL);
+
+ char *last_buffer = NULL;
+ char buffer[5120];
+
+ int count = 0;
+ int next_lt_last = 0;
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
+ continue;
+ }
+ if (!last.valid()) {
+ free(last_buffer);
+ last_buffer = strdup(buffer);
+ last.init(buffer);
+ }
+ timestamp next(buffer);
+ if (next < last) {
+ if (last_buffer) {
+ fprintf(stderr, "<%s", last_buffer);
+ }
+ fprintf(stderr, ">%s", buffer);
+ ++next_lt_last;
+ }
+ if (next.valid()) {
+ free(last_buffer);
+ last_buffer = strdup(buffer);
+ last.init(buffer);
+ }
+ ++count;
+ }
+ free(last_buffer);
+
+ pclose(fp);
+
+ static const int max_ok = 2;
+
+ // Allow few fails, happens with readers active
+ fprintf(stderr, "%s: %d/%d out of order entries\n",
+ (next_lt_last)
+ ? ((next_lt_last <= max_ok)
+ ? "WARNING"
+ : "ERROR")
+ : "INFO",
+ next_lt_last, count);
+
+ EXPECT_GE(max_ok, next_lt_last);
+
+ // sample statistically too small
+ EXPECT_LT(100, count);
+}
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index f963a3a..de2db67 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -14,8 +14,12 @@
* limitations under the License.
*/
+#include <ctype.h>
#include <signal.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
#include <gtest/gtest.h>
#include <log/log.h>
#include <log/logger.h>
@@ -38,88 +42,10 @@
static const char begin[] = "--------- beginning of ";
-TEST(logcat, sorted_order) {
- FILE *fp;
-
- ASSERT_EQ(0, NULL == (fp = popen(
- "logcat -v time -b radio -b events -b system -b main -d 2>/dev/null",
- "r")));
-
- class timestamp {
- private:
- int month;
- int day;
- int hour;
- int minute;
- int second;
- int millisecond;
- bool ok;
-
- public:
- void init(const char *buffer)
- {
- ok = false;
- if (buffer != NULL) {
- ok = sscanf(buffer, "%d-%d %d:%d:%d.%d ",
- &month, &day, &hour, &minute, &second, &millisecond) == 6;
- }
- }
-
- timestamp(const char *buffer)
- {
- init(buffer);
- }
-
- bool operator< (timestamp &T)
- {
- return !ok || !T.ok
- || (month < T.month)
- || ((month == T.month)
- && ((day < T.day)
- || ((day == T.day)
- && ((hour < T.hour)
- || ((hour == T.hour)
- && ((minute < T.minute)
- || ((minute == T.minute)
- && ((second < T.second)
- || ((second == T.second)
- && (millisecond < T.millisecond))))))))));
- }
-
- bool valid(void)
- {
- return ok;
- }
- } last(NULL);
-
- char buffer[5120];
-
- int count = 0;
-
- while (fgets(buffer, sizeof(buffer), fp)) {
- if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
- continue;
- }
- if (!last.valid()) {
- last.init(buffer);
- }
- timestamp next(buffer);
- ASSERT_EQ(0, next < last);
- if (next.valid()) {
- last.init(buffer);
- }
- ++count;
- }
-
- pclose(fp);
-
- ASSERT_LT(100, count);
-}
-
TEST(logcat, buckets) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
+ ASSERT_TRUE(NULL != (fp = popen(
"logcat -b radio -b events -b system -b main -d 2>/dev/null",
"r")));
@@ -141,15 +67,15 @@
pclose(fp);
- ASSERT_EQ(15, ids);
+ EXPECT_EQ(15, ids);
- ASSERT_EQ(4, count);
+ EXPECT_EQ(4, count);
}
TEST(logcat, tail_3) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
+ ASSERT_TRUE(NULL != (fp = popen(
"logcat -v long -b radio -b events -b system -b main -t 3 2>/dev/null",
"r")));
@@ -173,7 +99,7 @@
TEST(logcat, tail_10) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
+ ASSERT_TRUE(NULL != (fp = popen(
"logcat -v long -b radio -b events -b system -b main -t 10 2>/dev/null",
"r")));
@@ -197,7 +123,7 @@
TEST(logcat, tail_100) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
+ ASSERT_TRUE(NULL != (fp = popen(
"logcat -v long -b radio -b events -b system -b main -t 100 2>/dev/null",
"r")));
@@ -221,7 +147,7 @@
TEST(logcat, tail_1000) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
+ ASSERT_TRUE(NULL != (fp = popen(
"logcat -v long -b radio -b events -b system -b main -t 1000 2>/dev/null",
"r")));
@@ -242,6 +168,72 @@
ASSERT_EQ(1000, count);
}
+TEST(logcat, tail_time) {
+ FILE *fp;
+
+ ASSERT_TRUE(NULL != (fp = popen("logcat -v long -b all -t 10 2>&1", "r")));
+
+ char buffer[5120];
+ char *last_timestamp = NULL;
+ char *first_timestamp = NULL;
+ int count = 0;
+ const unsigned int time_length = 18;
+ const unsigned int time_offset = 2;
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ if ((buffer[0] == '[') && (buffer[1] == ' ')
+ && isdigit(buffer[time_offset]) && isdigit(buffer[time_offset + 1])
+ && (buffer[time_offset + 2] == '-')) {
+ ++count;
+ buffer[time_length + time_offset] = '\0';
+ if (!first_timestamp) {
+ first_timestamp = strdup(buffer + time_offset);
+ }
+ free(last_timestamp);
+ last_timestamp = strdup(buffer + time_offset);
+ }
+ }
+ pclose(fp);
+
+ EXPECT_EQ(10, count);
+ EXPECT_TRUE(last_timestamp != NULL);
+ EXPECT_TRUE(first_timestamp != NULL);
+
+ snprintf(buffer, sizeof(buffer), "logcat -v long -b all -t '%s' 2>&1",
+ first_timestamp);
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
+
+ int second_count = 0;
+ int last_timestamp_count = -1;
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ if ((buffer[0] == '[') && (buffer[1] == ' ')
+ && isdigit(buffer[time_offset]) && isdigit(buffer[time_offset + 1])
+ && (buffer[time_offset + 2] == '-')) {
+ ++second_count;
+ buffer[time_length + time_offset] = '\0';
+ if (first_timestamp) {
+ // we can get a transitory *extremely* rare failure if hidden
+ // underneath the time is *exactly* XX-XX XX:XX:XX.XXX000000
+ EXPECT_STREQ(buffer + time_offset, first_timestamp);
+ free(first_timestamp);
+ first_timestamp = NULL;
+ }
+ if (!strcmp(buffer + time_offset, last_timestamp)) {
+ last_timestamp_count = second_count;
+ }
+ }
+ }
+ pclose(fp);
+
+ free(last_timestamp);
+ last_timestamp = NULL;
+
+ EXPECT_TRUE(first_timestamp == NULL);
+ EXPECT_LE(count, second_count);
+ EXPECT_LE(count, last_timestamp_count);
+}
+
TEST(logcat, End_to_End) {
pid_t pid = getpid();
@@ -250,8 +242,8 @@
ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts)));
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
- "logcat -b events -t 100 2>/dev/null",
+ ASSERT_TRUE(NULL != (fp = popen(
+ "logcat -v brief -b events -t 100 2>/dev/null",
"r")));
char buffer[5120];
@@ -278,11 +270,12 @@
ASSERT_EQ(1, count);
}
-TEST(logcat, get_) {
+TEST(logcat, get_size) {
FILE *fp;
- ASSERT_EQ(0, NULL == (fp = popen(
- "logcat -b radio -b events -b system -b main -g 2>/dev/null",
+ // NB: crash log only available in user space
+ ASSERT_TRUE(NULL != (fp = popen(
+ "logcat -v brief -b radio -b events -b system -b main -g 2>/dev/null",
"r")));
char buffer[5120];
@@ -291,13 +284,53 @@
while (fgets(buffer, sizeof(buffer), fp)) {
int size, consumed, max, payload;
+ char size_mult[2], consumed_mult[2];
+ long full_size, full_consumed;
size = consumed = max = payload = 0;
- if ((4 == sscanf(buffer, "%*s ring buffer is %dKb (%dKb consumed),"
- " max entry is %db, max payload is %db",
- &size, &consumed, &max, &payload))
- && ((size * 3) >= consumed)
- && ((size * 1024) > max)
+ // NB: crash log can be very small, not hit a Kb of consumed space
+ // doubly lucky we are not including it.
+ if (6 != sscanf(buffer, "%*s ring buffer is %d%2s (%d%2s consumed),"
+ " max entry is %db, max payload is %db",
+ &size, size_mult, &consumed, consumed_mult,
+ &max, &payload)) {
+ fprintf(stderr, "WARNING: Parse error: %s", buffer);
+ continue;
+ }
+ full_size = size;
+ switch(size_mult[0]) {
+ case 'G':
+ full_size *= 1024;
+ /* FALLTHRU */
+ case 'M':
+ full_size *= 1024;
+ /* FALLTHRU */
+ case 'K':
+ full_size *= 1024;
+ /* FALLTHRU */
+ case 'b':
+ break;
+ }
+ full_consumed = consumed;
+ switch(consumed_mult[0]) {
+ case 'G':
+ full_consumed *= 1024;
+ /* FALLTHRU */
+ case 'M':
+ full_consumed *= 1024;
+ /* FALLTHRU */
+ case 'K':
+ full_consumed *= 1024;
+ /* FALLTHRU */
+ case 'b':
+ break;
+ }
+ EXPECT_GT((full_size * 9) / 4, full_consumed);
+ EXPECT_GT(full_size, max);
+ EXPECT_GT(max, payload);
+
+ if ((((full_size * 9) / 4) >= full_consumed)
+ && (full_size > max)
&& (max > payload)) {
++count;
}
@@ -308,7 +341,7 @@
ASSERT_EQ(4, count);
}
-static void caught_blocking(int signum)
+static void caught_blocking(int /*signum*/)
{
unsigned long long v = 0xDEADBEEFA55A0000ULL;
@@ -319,15 +352,19 @@
TEST(logcat, blocking) {
FILE *fp;
- unsigned long long v = 0xDEADBEEFA55A0000ULL;
+ unsigned long long v = 0xDEADBEEFA55F0000ULL;
pid_t pid = getpid();
v += pid & 0xFFFF;
- ASSERT_EQ(0, NULL == (fp = popen(
+ LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));
+
+ v &= 0xFFFFFFFFFFFAFFFFULL;
+
+ ASSERT_TRUE(NULL != (fp = popen(
"( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
- " logcat -b events 2>&1",
+ " logcat -v brief -b events 2>&1",
"r")));
char buffer[5120];
@@ -339,14 +376,13 @@
signal(SIGALRM, caught_blocking);
alarm(2);
while (fgets(buffer, sizeof(buffer), fp)) {
- alarm(2);
-
- ++count;
if (!strncmp(buffer, "DONE", 4)) {
break;
}
+ ++count;
+
int p;
unsigned long long l;
@@ -369,12 +405,12 @@
pclose(fp);
- ASSERT_LT(10, count);
+ EXPECT_LE(2, count);
- ASSERT_EQ(1, signals);
+ EXPECT_EQ(1, signals);
}
-static void caught_blocking_tail(int signum)
+static void caught_blocking_tail(int /*signum*/)
{
unsigned long long v = 0xA55ADEADBEEF0000ULL;
@@ -385,15 +421,19 @@
TEST(logcat, blocking_tail) {
FILE *fp;
- unsigned long long v = 0xA55ADEADBEEF0000ULL;
+ unsigned long long v = 0xA55FDEADBEEF0000ULL;
pid_t pid = getpid();
v += pid & 0xFFFF;
- ASSERT_EQ(0, NULL == (fp = popen(
+ LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));
+
+ v &= 0xFFFAFFFFFFFFFFFFULL;
+
+ ASSERT_TRUE(NULL != (fp = popen(
"( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
- " logcat -b events -T 5 2>&1",
+ " logcat -v brief -b events -T 5 2>&1",
"r")));
char buffer[5120];
@@ -405,14 +445,13 @@
signal(SIGALRM, caught_blocking_tail);
alarm(2);
while (fgets(buffer, sizeof(buffer), fp)) {
- alarm(2);
-
- ++count;
if (!strncmp(buffer, "DONE", 4)) {
break;
}
+ ++count;
+
int p;
unsigned long long l;
@@ -431,13 +470,284 @@
alarm(0);
signal(SIGALRM, SIG_DFL);
- /* Generate SIGPIPE */
+ // Generate SIGPIPE
fclose(fp);
caught_blocking_tail(0);
pclose(fp);
- ASSERT_LT(5, count);
+ EXPECT_LE(2, count);
- ASSERT_EQ(1, signals);
+ EXPECT_EQ(1, signals);
+}
+
+TEST(logcat, logrotate) {
+ static const char form[] = "/data/local/tmp/logcat.logrotate.XXXXXX";
+ char buf[sizeof(form)];
+ ASSERT_TRUE(NULL != mkdtemp(strcpy(buf, form)));
+
+ static const char comm[] = "logcat -b radio -b events -b system -b main"
+ " -d -f %s/log.txt -n 7 -r 1";
+ char command[sizeof(buf) + sizeof(comm)];
+ sprintf(command, comm, buf);
+
+ int ret;
+ EXPECT_FALSE((ret = system(command)));
+ if (!ret) {
+ sprintf(command, "ls -s %s 2>/dev/null", buf);
+
+ FILE *fp;
+ EXPECT_TRUE(NULL != (fp = popen(command, "r")));
+ if (fp) {
+ char buffer[5120];
+ int count = 0;
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ static const char match_1[] = "4 log.txt";
+ static const char match_2[] = "8 log.txt";
+ static const char match_3[] = "12 log.txt";
+ static const char match_4[] = "16 log.txt";
+ static const char total[] = "total ";
+
+ if (!strncmp(buffer, match_1, sizeof(match_1) - 1)
+ || !strncmp(buffer, match_2, sizeof(match_2) - 1)
+ || !strncmp(buffer, match_3, sizeof(match_3) - 1)
+ || !strncmp(buffer, match_4, sizeof(match_4) - 1)) {
+ ++count;
+ } else if (strncmp(buffer, total, sizeof(total) - 1)) {
+ fprintf(stderr, "WARNING: Parse error: %s", buffer);
+ }
+ }
+ pclose(fp);
+ EXPECT_TRUE(count == 7 || count == 8);
+ }
+ }
+ sprintf(command, "rm -rf %s", buf);
+ EXPECT_FALSE(system(command));
+}
+
+TEST(logcat, logrotate_suffix) {
+ static const char tmp_out_dir_form[] = "/data/local/tmp/logcat.logrotate.XXXXXX";
+ char tmp_out_dir[sizeof(tmp_out_dir_form)];
+ ASSERT_TRUE(NULL != mkdtemp(strcpy(tmp_out_dir, tmp_out_dir_form)));
+
+ static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main"
+ " -d -f %s/log.txt -n 10 -r 1";
+ char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)];
+ sprintf(command, logcat_cmd, tmp_out_dir);
+
+ int ret;
+ EXPECT_FALSE((ret = system(command)));
+ if (!ret) {
+ sprintf(command, "ls %s 2>/dev/null", tmp_out_dir);
+
+ FILE *fp;
+ EXPECT_TRUE(NULL != (fp = popen(command, "r")));
+ char buffer[5120];
+ int log_file_count = 0;
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ static const char rotated_log_filename_prefix[] = "log.txt.";
+ static const size_t rotated_log_filename_prefix_len =
+ strlen(rotated_log_filename_prefix);
+ static const char log_filename[] = "log.txt";
+
+ if (!strncmp(buffer, rotated_log_filename_prefix, rotated_log_filename_prefix_len)) {
+ // Rotated file should have form log.txt.##
+ char* rotated_log_filename_suffix = buffer + rotated_log_filename_prefix_len;
+ char* endptr;
+ const long int suffix_value = strtol(rotated_log_filename_suffix, &endptr, 10);
+ EXPECT_EQ(rotated_log_filename_suffix + 2, endptr);
+ EXPECT_LE(suffix_value, 10);
+ EXPECT_GT(suffix_value, 0);
+ ++log_file_count;
+ continue;
+ }
+
+ if (!strncmp(buffer, log_filename, strlen(log_filename))) {
+ ++log_file_count;
+ continue;
+ }
+
+ fprintf(stderr, "ERROR: Unexpected file: %s", buffer);
+ ADD_FAILURE();
+ }
+ pclose(fp);
+ EXPECT_EQ(11, log_file_count);
+ }
+ sprintf(command, "rm -rf %s", tmp_out_dir);
+ EXPECT_FALSE(system(command));
+}
+
+static void caught_blocking_clear(int /*signum*/)
+{
+ unsigned long long v = 0xDEADBEEFA55C0000ULL;
+
+ v += getpid() & 0xFFFF;
+
+ LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));
+}
+
+TEST(logcat, blocking_clear) {
+ FILE *fp;
+ unsigned long long v = 0xDEADBEEFA55C0000ULL;
+
+ pid_t pid = getpid();
+
+ v += pid & 0xFFFF;
+
+ // This test is racey; an event occurs between clear and dump.
+ // We accept that we will get a false positive, but never a false negative.
+ ASSERT_TRUE(NULL != (fp = popen(
+ "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
+ " logcat -b events -c 2>&1 ;"
+ " logcat -v brief -b events 2>&1",
+ "r")));
+
+ char buffer[5120];
+
+ int count = 0;
+
+ int signals = 0;
+
+ signal(SIGALRM, caught_blocking_clear);
+ alarm(2);
+ while (fgets(buffer, sizeof(buffer), fp)) {
+
+ if (!strncmp(buffer, "clearLog: ", 10)) {
+ fprintf(stderr, "WARNING: Test lacks permission to run :-(\n");
+ count = signals = 1;
+ break;
+ }
+
+ if (!strncmp(buffer, "DONE", 4)) {
+ break;
+ }
+
+ ++count;
+
+ int p;
+ unsigned long long l;
+
+ if ((2 != sscanf(buffer, "I/[0] ( %u): %lld", &p, &l))
+ || (p != pid)) {
+ continue;
+ }
+
+ if (l == v) {
+ if (count > 1) {
+ fprintf(stderr, "WARNING: Possible false positive\n");
+ }
+ ++signals;
+ break;
+ }
+ }
+ alarm(0);
+ signal(SIGALRM, SIG_DFL);
+
+ // Generate SIGPIPE
+ fclose(fp);
+ caught_blocking_clear(0);
+
+ pclose(fp);
+
+ EXPECT_LE(1, count);
+
+ EXPECT_EQ(1, signals);
+}
+
+static bool get_white_black(char **list) {
+ FILE *fp;
+
+ fp = popen("logcat -p 2>/dev/null", "r");
+ if (fp == NULL) {
+ fprintf(stderr, "ERROR: logcat -p 2>/dev/null\n");
+ return false;
+ }
+
+ char buffer[5120];
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ char *hold = *list;
+ char *buf = buffer;
+ while (isspace(*buf)) {
+ ++buf;
+ }
+ char *end = buf + strlen(buf);
+ while (isspace(*--end) && (end >= buf)) {
+ *end = '\0';
+ }
+ if (end < buf) {
+ continue;
+ }
+ if (hold) {
+ asprintf(list, "%s %s", hold, buf);
+ free(hold);
+ } else {
+ asprintf(list, "%s", buf);
+ }
+ }
+ pclose(fp);
+ return *list != NULL;
+}
+
+static bool set_white_black(const char *list) {
+ FILE *fp;
+
+ char buffer[5120];
+
+ snprintf(buffer, sizeof(buffer), "logcat -P '%s' 2>&1", list ? list : "");
+ fp = popen(buffer, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "ERROR: %s\n", buffer);
+ return false;
+ }
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ char *buf = buffer;
+ while (isspace(*buf)) {
+ ++buf;
+ }
+ char *end = buf + strlen(buf);
+ while ((end > buf) && isspace(*--end)) {
+ *end = '\0';
+ }
+ if (end <= buf) {
+ continue;
+ }
+ fprintf(stderr, "%s\n", buf);
+ pclose(fp);
+ return false;
+ }
+ return pclose(fp) == 0;
+}
+
+TEST(logcat, white_black_adjust) {
+ char *list = NULL;
+ char *adjust = NULL;
+
+ get_white_black(&list);
+
+ static const char adjustment[] = "~! 300/20 300/25 2000 ~1000/5 ~1000/30";
+ ASSERT_EQ(true, set_white_black(adjustment));
+ ASSERT_EQ(true, get_white_black(&adjust));
+ EXPECT_STREQ(adjustment, adjust);
+ free(adjust);
+ adjust = NULL;
+
+ static const char adjustment2[] = "300/20 300/21 2000 ~1000";
+ ASSERT_EQ(true, set_white_black(adjustment2));
+ ASSERT_EQ(true, get_white_black(&adjust));
+ EXPECT_STREQ(adjustment2, adjust);
+ free(adjust);
+ adjust = NULL;
+
+ ASSERT_EQ(true, set_white_black(list));
+ get_white_black(&adjust);
+ EXPECT_STREQ(list ? list : "", adjust ? adjust : "");
+ free(adjust);
+ adjust = NULL;
+
+ free(list);
+ list = NULL;
}
diff --git a/logd/Android.mk b/logd/Android.mk
new file mode 100644
index 0000000..127a66b
--- /dev/null
+++ b/logd/Android.mk
@@ -0,0 +1,42 @@
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= logd
+
+LOCAL_SRC_FILES := \
+ main.cpp \
+ LogCommand.cpp \
+ CommandListener.cpp \
+ LogListener.cpp \
+ LogReader.cpp \
+ FlushCommand.cpp \
+ LogBuffer.cpp \
+ LogBufferElement.cpp \
+ LogTimes.cpp \
+ LogStatistics.cpp \
+ LogWhiteBlackList.cpp \
+ libaudit.c \
+ LogAudit.cpp \
+ event.logtags
+
+LOCAL_SHARED_LIBRARIES := \
+ libsysutils \
+ liblog \
+ libcutils \
+ libutils
+
+# This is what we want to do:
+# event_logtags = $(shell \
+# sed -n \
+# "s/^\([0-9]*\)[ \t]*$1[ \t].*/-D`echo $1 | tr a-z A-Z`_LOG_TAG=\1/p" \
+# $(LOCAL_PATH)/$2/event.logtags)
+# event_flag := $(call event_logtags,auditd)
+# so make sure we do not regret hard-coding it as follows:
+event_flag := -DAUDITD_LOG_TAG=1003
+
+LOCAL_CFLAGS := -Werror $(event_flag)
+
+include $(BUILD_EXECUTABLE)
+
+include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/logd/CommandListener.cpp b/logd/CommandListener.cpp
new file mode 100644
index 0000000..561ea3e
--- /dev/null
+++ b/logd/CommandListener.cpp
@@ -0,0 +1,326 @@
+/*
+ * Copyright (C) 2012-2014 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 <arpa/inet.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/prctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <cutils/sockets.h>
+#include <private/android_filesystem_config.h>
+#include <sysutils/SocketClient.h>
+
+#include "CommandListener.h"
+#include "LogCommand.h"
+
+CommandListener::CommandListener(LogBuffer *buf, LogReader * /*reader*/,
+ LogListener * /*swl*/)
+ : FrameworkListener(getLogSocket())
+ , mBuf(*buf) {
+ // registerCmd(new ShutdownCmd(buf, writer, swl));
+ registerCmd(new ClearCmd(buf));
+ registerCmd(new GetBufSizeCmd(buf));
+ registerCmd(new SetBufSizeCmd(buf));
+ registerCmd(new GetBufSizeUsedCmd(buf));
+ registerCmd(new GetStatisticsCmd(buf));
+ registerCmd(new SetPruneListCmd(buf));
+ registerCmd(new GetPruneListCmd(buf));
+ registerCmd(new ReinitCmd());
+}
+
+CommandListener::ShutdownCmd::ShutdownCmd(LogBuffer *buf, LogReader *reader,
+ LogListener *swl)
+ : LogCommand("shutdown")
+ , mBuf(*buf)
+ , mReader(*reader)
+ , mSwl(*swl)
+{ }
+
+int CommandListener::ShutdownCmd::runCommand(SocketClient * /*cli*/,
+ int /*argc*/,
+ char ** /*argv*/) {
+ mSwl.stopListener();
+ mReader.stopListener();
+ exit(0);
+}
+
+CommandListener::ClearCmd::ClearCmd(LogBuffer *buf)
+ : LogCommand("clear")
+ , mBuf(*buf)
+{ }
+
+static void setname() {
+ prctl(PR_SET_NAME, "logd.control");
+}
+
+int CommandListener::ClearCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ uid_t uid = cli->getUid();
+ if (clientHasLogCredentials(cli)) {
+ uid = AID_ROOT;
+ }
+
+ if (argc < 2) {
+ cli->sendMsg("Missing Argument");
+ return 0;
+ }
+
+ int id = atoi(argv[1]);
+ if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+
+ mBuf.clear((log_id_t) id, uid);
+ cli->sendMsg("success");
+ return 0;
+}
+
+CommandListener::GetBufSizeCmd::GetBufSizeCmd(LogBuffer *buf)
+ : LogCommand("getLogSize")
+ , mBuf(*buf)
+{ }
+
+int CommandListener::GetBufSizeCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ if (argc < 2) {
+ cli->sendMsg("Missing Argument");
+ return 0;
+ }
+
+ int id = atoi(argv[1]);
+ if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+
+ unsigned long size = mBuf.getSize((log_id_t) id);
+ char buf[512];
+ snprintf(buf, sizeof(buf), "%lu", size);
+ cli->sendMsg(buf);
+ return 0;
+}
+
+CommandListener::SetBufSizeCmd::SetBufSizeCmd(LogBuffer *buf)
+ : LogCommand("setLogSize")
+ , mBuf(*buf)
+{ }
+
+int CommandListener::SetBufSizeCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ if (!clientHasLogCredentials(cli)) {
+ cli->sendMsg("Permission Denied");
+ return 0;
+ }
+
+ if (argc < 3) {
+ cli->sendMsg("Missing Argument");
+ return 0;
+ }
+
+ int id = atoi(argv[1]);
+ if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+
+ unsigned long size = atol(argv[2]);
+ if (mBuf.setSize((log_id_t) id, size)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+
+ cli->sendMsg("success");
+ return 0;
+}
+
+CommandListener::GetBufSizeUsedCmd::GetBufSizeUsedCmd(LogBuffer *buf)
+ : LogCommand("getLogSizeUsed")
+ , mBuf(*buf)
+{ }
+
+int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ if (argc < 2) {
+ cli->sendMsg("Missing Argument");
+ return 0;
+ }
+
+ int id = atoi(argv[1]);
+ if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+
+ unsigned long size = mBuf.getSizeUsed((log_id_t) id);
+ char buf[512];
+ snprintf(buf, sizeof(buf), "%lu", size);
+ cli->sendMsg(buf);
+ return 0;
+}
+
+CommandListener::GetStatisticsCmd::GetStatisticsCmd(LogBuffer *buf)
+ : LogCommand("getStatistics")
+ , mBuf(*buf)
+{ }
+
+static void package_string(char **strp) {
+ const char *a = *strp;
+ if (!a) {
+ a = "";
+ }
+
+ // Calculate total buffer size prefix, count is the string length w/o nul
+ char fmt[32];
+ for(size_t l = strlen(a), y = 0, x = 6; y != x; y = x, x = strlen(fmt) - 2) {
+ snprintf(fmt, sizeof(fmt), "%zu\n%%s\n\f", l + x);
+ }
+
+ char *b = *strp;
+ *strp = NULL;
+ asprintf(strp, fmt, a);
+ free(b);
+}
+
+int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ uid_t uid = cli->getUid();
+ if (clientHasLogCredentials(cli)) {
+ uid = AID_ROOT;
+ }
+
+ unsigned int logMask = -1;
+ if (argc > 1) {
+ logMask = 0;
+ for (int i = 1; i < argc; ++i) {
+ int id = atoi(argv[i]);
+ if ((id < LOG_ID_MIN) || (LOG_ID_MAX <= id)) {
+ cli->sendMsg("Range Error");
+ return 0;
+ }
+ logMask |= 1 << id;
+ }
+ }
+
+ char *buf = NULL;
+
+ mBuf.formatStatistics(&buf, uid, logMask);
+ if (!buf) {
+ cli->sendMsg("Failed");
+ } else {
+ package_string(&buf);
+ cli->sendMsg(buf);
+ free(buf);
+ }
+ return 0;
+}
+
+CommandListener::GetPruneListCmd::GetPruneListCmd(LogBuffer *buf)
+ : LogCommand("getPruneList")
+ , mBuf(*buf)
+{ }
+
+int CommandListener::GetPruneListCmd::runCommand(SocketClient *cli,
+ int /*argc*/, char ** /*argv*/) {
+ setname();
+ char *buf = NULL;
+ mBuf.formatPrune(&buf);
+ if (!buf) {
+ cli->sendMsg("Failed");
+ } else {
+ package_string(&buf);
+ cli->sendMsg(buf);
+ free(buf);
+ }
+ return 0;
+}
+
+CommandListener::SetPruneListCmd::SetPruneListCmd(LogBuffer *buf)
+ : LogCommand("setPruneList")
+ , mBuf(*buf)
+{ }
+
+int CommandListener::SetPruneListCmd::runCommand(SocketClient *cli,
+ int argc, char **argv) {
+ setname();
+ if (!clientHasLogCredentials(cli)) {
+ cli->sendMsg("Permission Denied");
+ return 0;
+ }
+
+ char *cp = NULL;
+ for (int i = 1; i < argc; ++i) {
+ char *p = cp;
+ if (p) {
+ cp = NULL;
+ asprintf(&cp, "%s %s", p, argv[i]);
+ free(p);
+ } else {
+ asprintf(&cp, "%s", argv[i]);
+ }
+ }
+
+ int ret = mBuf.initPrune(cp);
+ free(cp);
+
+ if (ret) {
+ cli->sendMsg("Invalid");
+ return 0;
+ }
+
+ cli->sendMsg("success");
+
+ return 0;
+}
+
+CommandListener::ReinitCmd::ReinitCmd()
+ : LogCommand("reinit")
+{ }
+
+int CommandListener::ReinitCmd::runCommand(SocketClient *cli,
+ int /*argc*/, char ** /*argv*/) {
+ setname();
+
+ reinit_signal_handler(SIGHUP);
+
+ cli->sendMsg("success");
+
+ return 0;
+}
+
+int CommandListener::getLogSocket() {
+ static const char socketName[] = "logd";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM);
+ }
+
+ return sock;
+}
diff --git a/logd/CommandListener.h b/logd/CommandListener.h
new file mode 100644
index 0000000..83e06b4
--- /dev/null
+++ b/logd/CommandListener.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2012-2014 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 _COMMANDLISTENER_H__
+#define _COMMANDLISTENER_H__
+
+#include <sysutils/FrameworkListener.h>
+#include "LogCommand.h"
+#include "LogBuffer.h"
+#include "LogReader.h"
+#include "LogListener.h"
+
+// See main.cpp for implementation
+void reinit_signal_handler(int /*signal*/);
+
+class CommandListener : public FrameworkListener {
+ LogBuffer &mBuf;
+
+public:
+ CommandListener(LogBuffer *buf, LogReader *reader, LogListener *swl);
+ virtual ~CommandListener() {}
+
+private:
+ static int getLogSocket();
+
+ class ShutdownCmd : public LogCommand {
+ LogBuffer &mBuf;
+ LogReader &mReader;
+ LogListener &mSwl;
+
+ public:
+ ShutdownCmd(LogBuffer *buf, LogReader *reader, LogListener *swl);
+ virtual ~ShutdownCmd() {}
+ int runCommand(SocketClient *c, int argc, char ** argv);
+ };
+
+#define LogBufferCmd(name) \
+ class name##Cmd : public LogCommand { \
+ LogBuffer &mBuf; \
+ public: \
+ name##Cmd(LogBuffer *buf); \
+ virtual ~name##Cmd() {} \
+ int runCommand(SocketClient *c, int argc, char ** argv); \
+ };
+
+ LogBufferCmd(Clear)
+ LogBufferCmd(GetBufSize)
+ LogBufferCmd(SetBufSize)
+ LogBufferCmd(GetBufSizeUsed)
+ LogBufferCmd(GetStatistics)
+ LogBufferCmd(GetPruneList)
+ LogBufferCmd(SetPruneList)
+
+ class ReinitCmd : public LogCommand {
+ public:
+ ReinitCmd();
+ virtual ~ReinitCmd() {}
+ int runCommand(SocketClient *c, int argc, char ** argv);
+ };
+
+};
+
+#endif
diff --git a/logd/FlushCommand.cpp b/logd/FlushCommand.cpp
new file mode 100644
index 0000000..26a1861
--- /dev/null
+++ b/logd/FlushCommand.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2012-2014 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 <stdlib.h>
+
+#include "FlushCommand.h"
+#include "LogBufferElement.h"
+#include "LogCommand.h"
+#include "LogReader.h"
+#include "LogTimes.h"
+
+FlushCommand::FlushCommand(LogReader &reader,
+ bool nonBlock,
+ unsigned long tail,
+ unsigned int logMask,
+ pid_t pid,
+ uint64_t start)
+ : mReader(reader)
+ , mNonBlock(nonBlock)
+ , mTail(tail)
+ , mLogMask(logMask)
+ , mPid(pid)
+ , mStart(start)
+{ }
+
+// runSocketCommand is called once for every open client on the
+// log reader socket. Here we manage and associated the reader
+// client tracking and log region locks LastLogTimes list of
+// LogTimeEntrys, and spawn a transitory per-client thread to
+// work at filing data to the socket.
+//
+// global LogTimeEntry::lock() is used to protect access,
+// reference counts are used to ensure that individual
+// LogTimeEntry lifetime is managed when not protected.
+void FlushCommand::runSocketCommand(SocketClient *client) {
+ LogTimeEntry *entry = NULL;
+ LastLogTimes × = mReader.logbuf().mTimes;
+
+ LogTimeEntry::lock();
+ LastLogTimes::iterator it = times.begin();
+ while(it != times.end()) {
+ entry = (*it);
+ if (entry->mClient == client) {
+ entry->triggerReader_Locked();
+ if (entry->runningReader_Locked()) {
+ LogTimeEntry::unlock();
+ return;
+ }
+ entry->incRef_Locked();
+ break;
+ }
+ it++;
+ }
+
+ if (it == times.end()) {
+ // Create LogTimeEntry in notifyNewLog() ?
+ if (mTail == (unsigned long) -1) {
+ LogTimeEntry::unlock();
+ return;
+ }
+ entry = new LogTimeEntry(mReader, client, mNonBlock, mTail, mLogMask, mPid, mStart);
+ times.push_back(entry);
+ }
+
+ client->incRef();
+
+ // release client and entry reference counts once done
+ entry->startReader_Locked();
+ LogTimeEntry::unlock();
+}
+
+bool FlushCommand::hasReadLogs(SocketClient *client) {
+ return clientHasLogCredentials(client);
+}
diff --git a/logd/FlushCommand.h b/logd/FlushCommand.h
new file mode 100644
index 0000000..61c6858
--- /dev/null
+++ b/logd/FlushCommand.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012-2013 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 _FLUSH_COMMAND_H
+#define _FLUSH_COMMAND_H
+
+#include <log/log_read.h>
+#include <sysutils/SocketClientCommand.h>
+
+class LogBufferElement;
+
+#include "LogTimes.h"
+
+class LogReader;
+
+class FlushCommand : public SocketClientCommand {
+ LogReader &mReader;
+ bool mNonBlock;
+ unsigned long mTail;
+ unsigned int mLogMask;
+ pid_t mPid;
+ uint64_t mStart;
+
+public:
+ FlushCommand(LogReader &mReader,
+ bool nonBlock = false,
+ unsigned long tail = -1,
+ unsigned int logMask = -1,
+ pid_t pid = 0,
+ uint64_t start = 1);
+ virtual void runSocketCommand(SocketClient *client);
+
+ static bool hasReadLogs(SocketClient *client);
+};
+
+#endif
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp
new file mode 100644
index 0000000..6b3e637
--- /dev/null
+++ b/logd/LogAudit.cpp
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2014 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 <ctype.h>
+#include <endian.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/prctl.h>
+#include <sys/uio.h>
+#include <syslog.h>
+
+#include <private/android_logger.h>
+
+#include "libaudit.h"
+#include "LogAudit.h"
+
+#define KMSG_PRIORITY(PRI) \
+ '<', \
+ '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
+ '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
+ '>'
+
+LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg)
+ : SocketListener(getLogSocket(), false)
+ , logbuf(buf)
+ , reader(reader)
+ , fdDmesg(fdDmesg)
+ , initialized(false) {
+ static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
+ 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
+ ' ', 's', 't', 'a', 'r', 't', '\n' };
+ write(fdDmesg, auditd_message, sizeof(auditd_message));
+}
+
+bool LogAudit::onDataAvailable(SocketClient *cli) {
+ if (!initialized) {
+ prctl(PR_SET_NAME, "logd.auditd");
+ initialized = true;
+ }
+
+ struct audit_message rep;
+
+ rep.nlh.nlmsg_type = 0;
+ rep.nlh.nlmsg_len = 0;
+ rep.data[0] = '\0';
+
+ if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
+ SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
+ return false;
+ }
+
+ logPrint("type=%d %.*s",
+ rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
+
+ return true;
+}
+
+int LogAudit::logPrint(const char *fmt, ...) {
+ if (fmt == NULL) {
+ return -EINVAL;
+ }
+
+ va_list args;
+
+ char *str = NULL;
+ va_start(args, fmt);
+ int rc = vasprintf(&str, fmt, args);
+ va_end(args);
+
+ if (rc < 0) {
+ return rc;
+ }
+
+ char *cp;
+ while ((cp = strstr(str, " "))) {
+ memmove(cp, cp + 1, strlen(cp + 1) + 1);
+ }
+
+ bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
+ if ((fdDmesg >= 0) && initialized) {
+ struct iovec iov[3];
+ static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
+ static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
+
+ iov[0].iov_base = info ? const_cast<char *>(log_info)
+ : const_cast<char *>(log_warning);
+ iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
+ iov[1].iov_base = str;
+ iov[1].iov_len = strlen(str);
+ iov[2].iov_base = const_cast<char *>("\n");
+ iov[2].iov_len = 1;
+
+ writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
+ }
+
+ pid_t pid = getpid();
+ pid_t tid = gettid();
+ uid_t uid = getuid();
+ log_time now;
+
+ static const char audit_str[] = " audit(";
+ char *timeptr = strstr(str, audit_str);
+ if (timeptr
+ && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
+ && (*cp == ':')) {
+ memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
+ memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
+ } else {
+ now.strptime("", ""); // side effect of setting CLOCK_REALTIME
+ }
+
+ static const char pid_str[] = " pid=";
+ char *pidptr = strstr(str, pid_str);
+ if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
+ cp = pidptr + sizeof(pid_str) - 1;
+ pid = 0;
+ while (isdigit(*cp)) {
+ pid = (pid * 10) + (*cp - '0');
+ ++cp;
+ }
+ tid = pid;
+ uid = logbuf->pidToUid(pid);
+ memmove(pidptr, cp, strlen(cp) + 1);
+ }
+
+ // log to events
+
+ size_t l = strlen(str);
+ size_t n = l + sizeof(android_log_event_string_t);
+
+ bool notify = false;
+
+ android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n));
+ if (!event) {
+ rc = -ENOMEM;
+ } else {
+ event->header.tag = htole32(AUDITD_LOG_TAG);
+ event->payload.type = EVENT_TYPE_STRING;
+ event->payload.length = htole32(l);
+ memcpy(event->payload.data, str, l);
+
+ logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
+ reinterpret_cast<char *>(event),
+ (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
+ free(event);
+
+ notify = true;
+ }
+
+ // log to main
+
+ static const char comm_str[] = " comm=\"";
+ const char *comm = strstr(str, comm_str);
+ const char *estr = str + strlen(str);
+ if (comm) {
+ estr = comm;
+ comm += sizeof(comm_str) - 1;
+ } else if (pid == getpid()) {
+ pid = tid;
+ comm = "auditd";
+ } else if (!(comm = logbuf->pidToName(pid))) {
+ comm = "unknown";
+ }
+
+ const char *ecomm = strchr(comm, '"');
+ if (ecomm) {
+ ++ecomm;
+ l = ecomm - comm;
+ } else {
+ l = strlen(comm) + 1;
+ ecomm = "";
+ }
+ n = (estr - str) + strlen(ecomm) + l + 2;
+
+ char *newstr = static_cast<char *>(malloc(n));
+ if (!newstr) {
+ rc = -ENOMEM;
+ } else {
+ *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
+ strlcpy(newstr + 1, comm, l);
+ strncpy(newstr + 1 + l, str, estr - str);
+ strcpy(newstr + 1 + l + (estr - str), ecomm);
+
+ logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
+ (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
+ free(newstr);
+
+ notify = true;
+ }
+
+ free(str);
+
+ if (notify) {
+ reader->notifyNewLog();
+ }
+
+ return rc;
+}
+
+int LogAudit::log(char *buf) {
+ char *audit = strstr(buf, " audit(");
+ if (!audit) {
+ return 0;
+ }
+
+ *audit = '\0';
+
+ int rc;
+ char *type = strstr(buf, "type=");
+ if (type) {
+ rc = logPrint("%s %s", type, audit + 1);
+ } else {
+ rc = logPrint("%s", audit + 1);
+ }
+ *audit = ' ';
+ return rc;
+}
+
+int LogAudit::getLogSocket() {
+ int fd = audit_open();
+ if (fd < 0) {
+ return fd;
+ }
+ if (audit_setup(fd, getpid()) < 0) {
+ audit_close(fd);
+ fd = -1;
+ }
+ return fd;
+}
diff --git a/logd/LogAudit.h b/logd/LogAudit.h
new file mode 100644
index 0000000..f977be9
--- /dev/null
+++ b/logd/LogAudit.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2014 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 _LOGD_LOG_AUDIT_H__
+#define _LOGD_LOG_AUDIT_H__
+
+#include <sysutils/SocketListener.h>
+#include "LogReader.h"
+
+class LogAudit : public SocketListener {
+ LogBuffer *logbuf;
+ LogReader *reader;
+ int fdDmesg;
+ bool initialized;
+
+public:
+ LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg);
+ int log(char *buf);
+
+protected:
+ virtual bool onDataAvailable(SocketClient *cli);
+
+private:
+ static int getLogSocket();
+ int logPrint(const char *fmt, ...)
+ __attribute__ ((__format__ (__printf__, 2, 3)));
+};
+
+#endif
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
new file mode 100644
index 0000000..2693583
--- /dev/null
+++ b/logd/LogBuffer.cpp
@@ -0,0 +1,508 @@
+/*
+ * Copyright (C) 2012-2014 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 <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/user.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <cutils/properties.h>
+#include <log/logger.h>
+
+#include "LogBuffer.h"
+#include "LogReader.h"
+#include "LogStatistics.h"
+#include "LogWhiteBlackList.h"
+
+// Default
+#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
+#define log_buffer_size(id) mMaxSize[id]
+#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
+#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
+
+static bool valid_size(unsigned long value) {
+ if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
+ return false;
+ }
+
+ long pages = sysconf(_SC_PHYS_PAGES);
+ if (pages < 1) {
+ return true;
+ }
+
+ long pagesize = sysconf(_SC_PAGESIZE);
+ if (pagesize <= 1) {
+ pagesize = PAGE_SIZE;
+ }
+
+ // maximum memory impact a somewhat arbitrary ~3%
+ pages = (pages + 31) / 32;
+ unsigned long maximum = pages * pagesize;
+
+ if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
+ return true;
+ }
+
+ return value <= maximum;
+}
+
+static unsigned long property_get_size(const char *key) {
+ char property[PROPERTY_VALUE_MAX];
+ property_get(key, property, "");
+
+ char *cp;
+ unsigned long value = strtoul(property, &cp, 10);
+
+ switch(*cp) {
+ case 'm':
+ case 'M':
+ value *= 1024;
+ /* FALLTHRU */
+ case 'k':
+ case 'K':
+ value *= 1024;
+ /* FALLTHRU */
+ case '\0':
+ break;
+
+ default:
+ value = 0;
+ }
+
+ if (!valid_size(value)) {
+ value = 0;
+ }
+
+ return value;
+}
+
+void LogBuffer::init() {
+ static const char global_tuneable[] = "persist.logd.size"; // Settings App
+ static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
+
+ unsigned long default_size = property_get_size(global_tuneable);
+ if (!default_size) {
+ default_size = property_get_size(global_default);
+ }
+
+ log_id_for_each(i) {
+ char key[PROP_NAME_MAX];
+
+ snprintf(key, sizeof(key), "%s.%s",
+ global_tuneable, android_log_id_to_name(i));
+ unsigned long property_size = property_get_size(key);
+
+ if (!property_size) {
+ snprintf(key, sizeof(key), "%s.%s",
+ global_default, android_log_id_to_name(i));
+ property_size = property_get_size(key);
+ }
+
+ if (!property_size) {
+ property_size = default_size;
+ }
+
+ if (!property_size) {
+ property_size = LOG_BUFFER_SIZE;
+ }
+
+ if (setSize(i, property_size)) {
+ setSize(i, LOG_BUFFER_MIN_SIZE);
+ }
+ }
+}
+
+LogBuffer::LogBuffer(LastLogTimes *times)
+ : mTimes(*times) {
+ pthread_mutex_init(&mLogElementsLock, NULL);
+
+ init();
+}
+
+void LogBuffer::log(log_id_t log_id, log_time realtime,
+ uid_t uid, pid_t pid, pid_t tid,
+ const char *msg, unsigned short len) {
+ if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
+ return;
+ }
+ LogBufferElement *elem = new LogBufferElement(log_id, realtime,
+ uid, pid, tid, msg, len);
+
+ pthread_mutex_lock(&mLogElementsLock);
+
+ // Insert elements in time sorted order if possible
+ // NB: if end is region locked, place element at end of list
+ LogBufferElementCollection::iterator it = mLogElements.end();
+ LogBufferElementCollection::iterator last = it;
+ while (last != mLogElements.begin()) {
+ --it;
+ if ((*it)->getRealTime() <= realtime) {
+ break;
+ }
+ last = it;
+ }
+
+ if (last == mLogElements.end()) {
+ mLogElements.push_back(elem);
+ } else {
+ uint64_t end = 1;
+ bool end_set = false;
+ bool end_always = false;
+
+ LogTimeEntry::lock();
+
+ LastLogTimes::iterator t = mTimes.begin();
+ while(t != mTimes.end()) {
+ LogTimeEntry *entry = (*t);
+ if (entry->owned_Locked()) {
+ if (!entry->mNonBlock) {
+ end_always = true;
+ break;
+ }
+ if (!end_set || (end <= entry->mEnd)) {
+ end = entry->mEnd;
+ end_set = true;
+ }
+ }
+ t++;
+ }
+
+ if (end_always
+ || (end_set && (end >= (*last)->getSequence()))) {
+ mLogElements.push_back(elem);
+ } else {
+ mLogElements.insert(last,elem);
+ }
+
+ LogTimeEntry::unlock();
+ }
+
+ stats.add(len, log_id, uid, pid);
+ maybePrune(log_id);
+ pthread_mutex_unlock(&mLogElementsLock);
+}
+
+// If we're using more than 256K of memory for log entries, prune
+// at least 10% of the log entries.
+//
+// mLogElementsLock must be held when this function is called.
+void LogBuffer::maybePrune(log_id_t id) {
+ size_t sizes = stats.sizes(id);
+ if (sizes > log_buffer_size(id)) {
+ size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
+ size_t elements = stats.elements(id);
+ unsigned long pruneRows = elements * sizeOver90Percent / sizes;
+ elements /= 10;
+ if (pruneRows <= elements) {
+ pruneRows = elements;
+ }
+ prune(id, pruneRows);
+ }
+}
+
+// prune "pruneRows" of type "id" from the buffer.
+//
+// mLogElementsLock must be held when this function is called.
+void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
+ LogTimeEntry *oldest = NULL;
+
+ LogTimeEntry::lock();
+
+ // Region locked?
+ LastLogTimes::iterator t = mTimes.begin();
+ while(t != mTimes.end()) {
+ LogTimeEntry *entry = (*t);
+ if (entry->owned_Locked() && entry->isWatching(id)
+ && (!oldest || (oldest->mStart > entry->mStart))) {
+ oldest = entry;
+ }
+ t++;
+ }
+
+ LogBufferElementCollection::iterator it;
+
+ if (caller_uid != AID_ROOT) {
+ for(it = mLogElements.begin(); it != mLogElements.end();) {
+ LogBufferElement *e = *it;
+
+ if (oldest && (oldest->mStart <= e->getSequence())) {
+ break;
+ }
+
+ if (e->getLogId() != id) {
+ ++it;
+ continue;
+ }
+
+ uid_t uid = e->getUid();
+
+ if (uid == caller_uid) {
+ it = mLogElements.erase(it);
+ stats.subtract(e->getMsgLen(), id, uid, e->getPid());
+ delete e;
+ pruneRows--;
+ if (pruneRows == 0) {
+ break;
+ }
+ } else {
+ ++it;
+ }
+ }
+ LogTimeEntry::unlock();
+ return;
+ }
+
+ // prune by worst offender by uid
+ while (pruneRows > 0) {
+ // recalculate the worst offender on every batched pass
+ uid_t worst = (uid_t) -1;
+ size_t worst_sizes = 0;
+ size_t second_worst_sizes = 0;
+
+ if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
+ LidStatistics &l = stats.id(id);
+ l.sort();
+ UidStatisticsCollection::iterator iu = l.begin();
+ if (iu != l.end()) {
+ UidStatistics *u = *iu;
+ worst = u->getUid();
+ worst_sizes = u->sizes();
+ if (++iu != l.end()) {
+ second_worst_sizes = (*iu)->sizes();
+ }
+ }
+ }
+
+ bool kick = false;
+ for(it = mLogElements.begin(); it != mLogElements.end();) {
+ LogBufferElement *e = *it;
+
+ if (oldest && (oldest->mStart <= e->getSequence())) {
+ break;
+ }
+
+ if (e->getLogId() != id) {
+ ++it;
+ continue;
+ }
+
+ uid_t uid = e->getUid();
+
+ if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed
+ it = mLogElements.erase(it);
+ unsigned short len = e->getMsgLen();
+ stats.subtract(len, id, uid, e->getPid());
+ delete e;
+ pruneRows--;
+ if (uid == worst) {
+ kick = true;
+ if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
+ break;
+ }
+ worst_sizes -= len;
+ } else if (pruneRows == 0) {
+ break;
+ }
+ } else {
+ ++it;
+ }
+ }
+
+ if (!kick || !mPrune.worstUidEnabled()) {
+ break; // the following loop will ask bad clients to skip/drop
+ }
+ }
+
+ bool whitelist = false;
+ it = mLogElements.begin();
+ while((pruneRows > 0) && (it != mLogElements.end())) {
+ LogBufferElement *e = *it;
+ if (e->getLogId() == id) {
+ if (oldest && (oldest->mStart <= e->getSequence())) {
+ if (!whitelist) {
+ if (stats.sizes(id) > (2 * log_buffer_size(id))) {
+ // kick a misbehaving log reader client off the island
+ oldest->release_Locked();
+ } else {
+ oldest->triggerSkip_Locked(id, pruneRows);
+ }
+ }
+ break;
+ }
+
+ if (mPrune.nice(e)) { // WhiteListed
+ whitelist = true;
+ it++;
+ continue;
+ }
+
+ it = mLogElements.erase(it);
+ stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
+ delete e;
+ pruneRows--;
+ } else {
+ it++;
+ }
+ }
+
+ if (whitelist && (pruneRows > 0)) {
+ it = mLogElements.begin();
+ while((it != mLogElements.end()) && (pruneRows > 0)) {
+ LogBufferElement *e = *it;
+ if (e->getLogId() == id) {
+ if (oldest && (oldest->mStart <= e->getSequence())) {
+ if (stats.sizes(id) > (2 * log_buffer_size(id))) {
+ // kick a misbehaving log reader client off the island
+ oldest->release_Locked();
+ } else {
+ oldest->triggerSkip_Locked(id, pruneRows);
+ }
+ break;
+ }
+ it = mLogElements.erase(it);
+ stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
+ delete e;
+ pruneRows--;
+ } else {
+ it++;
+ }
+ }
+ }
+
+ LogTimeEntry::unlock();
+}
+
+// clear all rows of type "id" from the buffer.
+void LogBuffer::clear(log_id_t id, uid_t uid) {
+ pthread_mutex_lock(&mLogElementsLock);
+ prune(id, ULONG_MAX, uid);
+ pthread_mutex_unlock(&mLogElementsLock);
+}
+
+// get the used space associated with "id".
+unsigned long LogBuffer::getSizeUsed(log_id_t id) {
+ pthread_mutex_lock(&mLogElementsLock);
+ size_t retval = stats.sizes(id);
+ pthread_mutex_unlock(&mLogElementsLock);
+ return retval;
+}
+
+// set the total space allocated to "id"
+int LogBuffer::setSize(log_id_t id, unsigned long size) {
+ // Reasonable limits ...
+ if (!valid_size(size)) {
+ return -1;
+ }
+ pthread_mutex_lock(&mLogElementsLock);
+ log_buffer_size(id) = size;
+ pthread_mutex_unlock(&mLogElementsLock);
+ return 0;
+}
+
+// get the total space allocated to "id"
+unsigned long LogBuffer::getSize(log_id_t id) {
+ pthread_mutex_lock(&mLogElementsLock);
+ size_t retval = log_buffer_size(id);
+ pthread_mutex_unlock(&mLogElementsLock);
+ return retval;
+}
+
+uint64_t LogBuffer::flushTo(
+ SocketClient *reader, const uint64_t start, bool privileged,
+ int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
+ LogBufferElementCollection::iterator it;
+ uint64_t max = start;
+ uid_t uid = reader->getUid();
+
+ pthread_mutex_lock(&mLogElementsLock);
+
+ if (start <= 1) {
+ // client wants to start from the beginning
+ it = mLogElements.begin();
+ } else {
+ // Client wants to start from some specified time. Chances are
+ // we are better off starting from the end of the time sorted list.
+ for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
+ --it;
+ LogBufferElement *element = *it;
+ if (element->getSequence() <= start) {
+ it++;
+ break;
+ }
+ }
+ }
+
+ for (; it != mLogElements.end(); ++it) {
+ LogBufferElement *element = *it;
+
+ if (!privileged && (element->getUid() != uid)) {
+ continue;
+ }
+
+ if (element->getSequence() <= start) {
+ continue;
+ }
+
+ // NB: calling out to another object with mLogElementsLock held (safe)
+ if (filter) {
+ int ret = (*filter)(element, arg);
+ if (ret == false) {
+ continue;
+ }
+ if (ret != true) {
+ break;
+ }
+ }
+
+ pthread_mutex_unlock(&mLogElementsLock);
+
+ // range locking in LastLogTimes looks after us
+ max = element->flushTo(reader);
+
+ if (max == element->FLUSH_ERROR) {
+ return max;
+ }
+
+ pthread_mutex_lock(&mLogElementsLock);
+ }
+ pthread_mutex_unlock(&mLogElementsLock);
+
+ return max;
+}
+
+void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
+ uint64_t oldest = UINT64_MAX;
+
+ pthread_mutex_lock(&mLogElementsLock);
+
+ // Find oldest element in the log(s)
+ LogBufferElementCollection::iterator it;
+ for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
+ LogBufferElement *element = *it;
+
+ if ((logMask & (1 << element->getLogId()))) {
+ oldest = element->getSequence();
+ break;
+ }
+ }
+
+ stats.format(strp, uid, logMask, oldest);
+
+ pthread_mutex_unlock(&mLogElementsLock);
+}
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
new file mode 100644
index 0000000..13e6aa8
--- /dev/null
+++ b/logd/LogBuffer.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2012-2014 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 _LOGD_LOG_BUFFER_H__
+#define _LOGD_LOG_BUFFER_H__
+
+#include <sys/types.h>
+
+#include <log/log.h>
+#include <sysutils/SocketClient.h>
+#include <utils/List.h>
+
+#include <private/android_filesystem_config.h>
+
+#include "LogBufferElement.h"
+#include "LogTimes.h"
+#include "LogStatistics.h"
+#include "LogWhiteBlackList.h"
+
+typedef android::List<LogBufferElement *> LogBufferElementCollection;
+
+class LogBuffer {
+ LogBufferElementCollection mLogElements;
+ pthread_mutex_t mLogElementsLock;
+
+ LogStatistics stats;
+
+ PruneList mPrune;
+
+ unsigned long mMaxSize[LOG_ID_MAX];
+
+public:
+ LastLogTimes &mTimes;
+
+ LogBuffer(LastLogTimes *times);
+ void init();
+
+ void log(log_id_t log_id, log_time realtime,
+ uid_t uid, pid_t pid, pid_t tid,
+ const char *msg, unsigned short len);
+ uint64_t flushTo(SocketClient *writer, const uint64_t start,
+ bool privileged,
+ int (*filter)(const LogBufferElement *element, void *arg) = NULL,
+ void *arg = NULL);
+
+ void clear(log_id_t id, uid_t uid = AID_ROOT);
+ unsigned long getSize(log_id_t id);
+ int setSize(log_id_t id, unsigned long size);
+ unsigned long getSizeUsed(log_id_t id);
+ // *strp uses malloc, use free to release.
+ void formatStatistics(char **strp, uid_t uid, unsigned int logMask);
+
+ void enableStatistics() {
+ stats.enableStatistics();
+ }
+
+ int initPrune(char *cp) { return mPrune.init(cp); }
+ // *strp uses malloc, use free to release.
+ void formatPrune(char **strp) { mPrune.format(strp); }
+
+ // helper
+ char *pidToName(pid_t pid) { return stats.pidToName(pid); }
+ uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
+
+private:
+ void maybePrune(log_id_t id);
+ void prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
+
+};
+
+#endif // _LOGD_LOG_BUFFER_H__
diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp
new file mode 100644
index 0000000..5e780b5
--- /dev/null
+++ b/logd/LogBufferElement.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2012-2014 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 <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <log/logger.h>
+
+#include "LogBufferElement.h"
+#include "LogReader.h"
+
+const uint64_t LogBufferElement::FLUSH_ERROR(0);
+atomic_int_fast64_t LogBufferElement::sequence;
+
+LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
+ uid_t uid, pid_t pid, pid_t tid,
+ const char *msg, unsigned short len)
+ : mLogId(log_id)
+ , mUid(uid)
+ , mPid(pid)
+ , mTid(tid)
+ , mMsgLen(len)
+ , mSequence(sequence.fetch_add(1, memory_order_relaxed))
+ , mRealTime(realtime) {
+ mMsg = new char[len];
+ memcpy(mMsg, msg, len);
+}
+
+LogBufferElement::~LogBufferElement() {
+ delete [] mMsg;
+}
+
+uint64_t LogBufferElement::flushTo(SocketClient *reader) {
+ struct logger_entry_v3 entry;
+ memset(&entry, 0, sizeof(struct logger_entry_v3));
+ entry.hdr_size = sizeof(struct logger_entry_v3);
+ entry.len = mMsgLen;
+ entry.lid = mLogId;
+ entry.pid = mPid;
+ entry.tid = mTid;
+ entry.sec = mRealTime.tv_sec;
+ entry.nsec = mRealTime.tv_nsec;
+
+ struct iovec iovec[2];
+ iovec[0].iov_base = &entry;
+ iovec[0].iov_len = sizeof(struct logger_entry_v3);
+ iovec[1].iov_base = mMsg;
+ iovec[1].iov_len = mMsgLen;
+ if (reader->sendDatav(iovec, 2)) {
+ return FLUSH_ERROR;
+ }
+
+ return mSequence;
+}
diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h
new file mode 100644
index 0000000..25f1450
--- /dev/null
+++ b/logd/LogBufferElement.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2012-2014 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 _LOGD_LOG_BUFFER_ELEMENT_H__
+#define _LOGD_LOG_BUFFER_ELEMENT_H__
+
+#include <sys/types.h>
+#include <stdatomic.h>
+#include <sysutils/SocketClient.h>
+#include <log/log.h>
+#include <log/log_read.h>
+
+class LogBufferElement {
+ const log_id_t mLogId;
+ const uid_t mUid;
+ const pid_t mPid;
+ const pid_t mTid;
+ char *mMsg;
+ const unsigned short mMsgLen;
+ const uint64_t mSequence;
+ const log_time mRealTime;
+ static atomic_int_fast64_t sequence;
+
+public:
+ LogBufferElement(log_id_t log_id, log_time realtime,
+ uid_t uid, pid_t pid, pid_t tid,
+ const char *msg, unsigned short len);
+ virtual ~LogBufferElement();
+
+ log_id_t getLogId() const { return mLogId; }
+ uid_t getUid(void) const { return mUid; }
+ pid_t getPid(void) const { return mPid; }
+ pid_t getTid(void) const { return mTid; }
+ unsigned short getMsgLen() const { return mMsgLen; }
+ uint64_t getSequence(void) const { return mSequence; }
+ static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
+ log_time getRealTime(void) const { return mRealTime; }
+
+ static const uint64_t FLUSH_ERROR;
+ uint64_t flushTo(SocketClient *writer);
+};
+
+#endif
diff --git a/logd/LogCommand.cpp b/logd/LogCommand.cpp
new file mode 100644
index 0000000..e4c138e
--- /dev/null
+++ b/logd/LogCommand.cpp
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2012-2014 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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <private/android_filesystem_config.h>
+
+#include "LogCommand.h"
+
+LogCommand::LogCommand(const char *cmd)
+ : FrameworkCommand(cmd) {
+}
+
+// gets a list of supplementary group IDs associated with
+// the socket peer. This is implemented by opening
+// /proc/PID/status and look for the "Group:" line.
+//
+// This function introduces races especially since status
+// can change 'shape' while reading, the net result is err
+// on lack of permission.
+//
+// Race-free alternative is to introduce pairs of sockets
+// and threads for each command and reading, one each that
+// has open permissions, and one that has restricted
+// permissions.
+
+static bool groupIsLog(char *buf) {
+ char *ptr;
+ static const char ws[] = " \n";
+ bool ret = false;
+
+ for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(NULL, ws, &ptr)) {
+ errno = 0;
+ gid_t Gid = strtol(buf, NULL, 10);
+ if (errno != 0) {
+ return false;
+ }
+ if (Gid == AID_LOG) {
+ ret = true;
+ }
+ }
+ return ret;
+}
+
+bool clientHasLogCredentials(SocketClient * cli) {
+ uid_t uid = cli->getUid();
+ if (uid == AID_ROOT) {
+ return true;
+ }
+
+ gid_t gid = cli->getGid();
+ if ((gid == AID_ROOT) || (gid == AID_SYSTEM) || (gid == AID_LOG)) {
+ return true;
+ }
+
+ // FYI We will typically be here for 'adb logcat'
+ bool ret = false;
+
+ char filename[1024];
+ snprintf(filename, sizeof(filename), "/proc/%d/status", cli->getPid());
+
+ FILE *file = fopen(filename, "r");
+ if (!file) {
+ return ret;
+ }
+
+ bool foundGid = false;
+ bool foundUid = false;
+
+ char line[1024];
+ while (fgets(line, sizeof(line), file)) {
+ static const char groups_string[] = "Groups:\t";
+ static const char uid_string[] = "Uid:\t";
+ static const char gid_string[] = "Gid:\t";
+
+ if (strncmp(groups_string, line, strlen(groups_string)) == 0) {
+ ret = groupIsLog(line + strlen(groups_string));
+ if (!ret) {
+ break;
+ }
+ } else if (strncmp(uid_string, line, strlen(uid_string)) == 0) {
+ uid_t u[4] = { (uid_t) -1, (uid_t) -1, (uid_t) -1, (uid_t) -1};
+
+ sscanf(line + strlen(uid_string), "%u\t%u\t%u\t%u",
+ &u[0], &u[1], &u[2], &u[3]);
+
+ // Protect against PID reuse by checking that the UID is the same
+ if ((uid != u[0]) || (uid != u[1]) || (uid != u[2]) || (uid != u[3])) {
+ ret = false;
+ break;
+ }
+ foundUid = true;
+ } else if (strncmp(gid_string, line, strlen(gid_string)) == 0) {
+ gid_t g[4] = { (gid_t) -1, (gid_t) -1, (gid_t) -1, (gid_t) -1};
+
+ sscanf(line + strlen(gid_string), "%u\t%u\t%u\t%u",
+ &g[0], &g[1], &g[2], &g[3]);
+
+ // Protect against PID reuse by checking that the GID is the same
+ if ((gid != g[0]) || (gid != g[1]) || (gid != g[2]) || (gid != g[3])) {
+ ret = false;
+ break;
+ }
+ foundGid = true;
+ }
+ }
+
+ fclose(file);
+
+ if (!foundGid || !foundUid) {
+ ret = false;
+ }
+
+ return ret;
+}
diff --git a/libnl_2/cache.c b/logd/LogCommand.h
similarity index 61%
copy from libnl_2/cache.c
copy to logd/LogCommand.h
index c21974d..e3b96a2 100644
--- a/libnl_2/cache.c
+++ b/logd/LogCommand.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011 The Android Open Source Project
+ * Copyright (C) 2012-2014 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.
@@ -14,24 +14,18 @@
* limitations under the License.
*/
-/* NOTICE: This is a clean room re-implementation of libnl */
+#ifndef _LOGD_COMMAND_H
+#define _LOGD_COMMAND_H
-#include "netlink/cache.h"
-#include "netlink/object.h"
+#include <sysutils/SocketClient.h>
+#include <sysutils/FrameworkCommand.h>
-void nl_cache_free(struct nl_cache *cache)
-{
+class LogCommand : public FrameworkCommand {
+public:
+ LogCommand(const char *cmd);
+ virtual ~LogCommand() {}
+};
-}
+bool clientHasLogCredentials(SocketClient * cli);
-void nl_cache_clear(struct nl_cache *cache)
-{
-
-}
-
-void nl_cache_remove(struct nl_object *obj)
-{
-
-}
-
-
+#endif
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
new file mode 100644
index 0000000..fc9e30f
--- /dev/null
+++ b/logd/LogListener.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2012-2014 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 <limits.h>
+#include <sys/prctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include <cutils/sockets.h>
+#include <log/logger.h>
+#include <private/android_logger.h>
+
+#include "LogListener.h"
+
+LogListener::LogListener(LogBuffer *buf, LogReader *reader)
+ : SocketListener(getLogSocket(), false)
+ , logbuf(buf)
+ , reader(reader)
+{ }
+
+bool LogListener::onDataAvailable(SocketClient *cli) {
+ prctl(PR_SET_NAME, "logd.writer");
+
+ char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
+ + LOGGER_ENTRY_MAX_PAYLOAD];
+ struct iovec iov = { buffer, sizeof(buffer) };
+ memset(buffer, 0, sizeof(buffer));
+
+ char control[CMSG_SPACE(sizeof(struct ucred))];
+ struct msghdr hdr = {
+ NULL,
+ 0,
+ &iov,
+ 1,
+ control,
+ sizeof(control),
+ 0,
+ };
+
+ int socket = cli->getSocket();
+
+ ssize_t n = recvmsg(socket, &hdr, 0);
+ if (n <= (ssize_t)(sizeof(android_log_header_t))) {
+ return false;
+ }
+
+ struct ucred *cred = NULL;
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
+ while (cmsg != NULL) {
+ if (cmsg->cmsg_level == SOL_SOCKET
+ && cmsg->cmsg_type == SCM_CREDENTIALS) {
+ cred = (struct ucred *)CMSG_DATA(cmsg);
+ break;
+ }
+ cmsg = CMSG_NXTHDR(&hdr, cmsg);
+ }
+
+ if (cred == NULL) {
+ return false;
+ }
+
+ if (cred->uid == getuid()) {
+ // ignore log messages we send to ourself.
+ // Such log messages are often generated by libraries we depend on
+ // which use standard Android logging.
+ return false;
+ }
+
+ android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
+ if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX) {
+ return false;
+ }
+
+ char *msg = ((char *)buffer) + sizeof(android_log_header_t);
+ n -= sizeof(android_log_header_t);
+
+ // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
+ // truncated message to the logs.
+
+ logbuf->log((log_id_t)header->id, header->realtime,
+ cred->uid, cred->pid, header->tid, msg,
+ ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
+ reader->notifyNewLog();
+
+ return true;
+}
+
+int LogListener::getLogSocket() {
+ static const char socketName[] = "logdw";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_DGRAM);
+ }
+
+ int on = 1;
+ if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
+ return -1;
+ }
+ return sock;
+}
diff --git a/logd/LogListener.h b/logd/LogListener.h
new file mode 100644
index 0000000..7099e13
--- /dev/null
+++ b/logd/LogListener.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2012-2013 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 _LOGD_LOG_LISTENER_H__
+#define _LOGD_LOG_LISTENER_H__
+
+#include <sysutils/SocketListener.h>
+#include "LogReader.h"
+
+class LogListener : public SocketListener {
+ LogBuffer *logbuf;
+ LogReader *reader;
+
+public:
+ LogListener(LogBuffer *buf, LogReader *reader);
+
+protected:
+ virtual bool onDataAvailable(SocketClient *cli);
+
+private:
+ static int getLogSocket();
+};
+
+#endif
diff --git a/logd/LogReader.cpp b/logd/LogReader.cpp
new file mode 100644
index 0000000..f7df275
--- /dev/null
+++ b/logd/LogReader.cpp
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2012-2013 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 <ctype.h>
+#include <poll.h>
+#include <sys/prctl.h>
+#include <sys/socket.h>
+
+#include <cutils/sockets.h>
+
+#include "LogReader.h"
+#include "FlushCommand.h"
+
+LogReader::LogReader(LogBuffer *logbuf)
+ : SocketListener(getLogSocket(), true)
+ , mLogbuf(*logbuf)
+{ }
+
+// When we are notified a new log entry is available, inform
+// all of our listening sockets.
+void LogReader::notifyNewLog() {
+ FlushCommand command(*this);
+ runOnEachSocket(&command);
+}
+
+bool LogReader::onDataAvailable(SocketClient *cli) {
+ prctl(PR_SET_NAME, "logd.reader");
+
+ char buffer[255];
+
+ int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
+ if (len <= 0) {
+ doSocketDelete(cli);
+ return false;
+ }
+ buffer[len] = '\0';
+
+ unsigned long tail = 0;
+ static const char _tail[] = " tail=";
+ char *cp = strstr(buffer, _tail);
+ if (cp) {
+ tail = atol(cp + sizeof(_tail) - 1);
+ }
+
+ log_time start(log_time::EPOCH);
+ static const char _start[] = " start=";
+ cp = strstr(buffer, _start);
+ if (cp) {
+ // Parse errors will result in current time
+ start.strptime(cp + sizeof(_start) - 1, "%s.%q");
+ }
+
+ unsigned int logMask = -1;
+ static const char _logIds[] = " lids=";
+ cp = strstr(buffer, _logIds);
+ if (cp) {
+ logMask = 0;
+ cp += sizeof(_logIds) - 1;
+ while (*cp && *cp != '\0') {
+ int val = 0;
+ while (isdigit(*cp)) {
+ val = val * 10 + *cp - '0';
+ ++cp;
+ }
+ logMask |= 1 << val;
+ if (*cp != ',') {
+ break;
+ }
+ ++cp;
+ }
+ }
+
+ pid_t pid = 0;
+ static const char _pid[] = " pid=";
+ cp = strstr(buffer, _pid);
+ if (cp) {
+ pid = atol(cp + sizeof(_pid) - 1);
+ }
+
+ bool nonBlock = false;
+ if (strncmp(buffer, "dumpAndClose", 12) == 0) {
+ // Allow writer to get some cycles, and wait for pending notifications
+ sched_yield();
+ LogTimeEntry::lock();
+ LogTimeEntry::unlock();
+ sched_yield();
+ nonBlock = true;
+ }
+
+ uint64_t sequence = 1;
+ // Convert realtime to sequence number
+ if (start != log_time::EPOCH) {
+ class LogFindStart {
+ const pid_t mPid;
+ const unsigned mLogMask;
+ bool startTimeSet;
+ log_time &start;
+ uint64_t &sequence;
+ uint64_t last;
+
+ public:
+ LogFindStart(unsigned logMask, pid_t pid, log_time &start, uint64_t &sequence)
+ : mPid(pid)
+ , mLogMask(logMask)
+ , startTimeSet(false)
+ , start(start)
+ , sequence(sequence)
+ , last(sequence)
+ { }
+
+ static int callback(const LogBufferElement *element, void *obj) {
+ LogFindStart *me = reinterpret_cast<LogFindStart *>(obj);
+ if ((!me->mPid || (me->mPid == element->getPid()))
+ && (me->mLogMask & (1 << element->getLogId()))) {
+ if (me->start == element->getRealTime()) {
+ me->sequence = element->getSequence();
+ me->startTimeSet = true;
+ return -1;
+ } else {
+ if (me->start < element->getRealTime()) {
+ me->sequence = me->last;
+ me->startTimeSet = true;
+ return -1;
+ }
+ me->last = element->getSequence();
+ }
+ }
+ return false;
+ }
+
+ bool found() { return startTimeSet; }
+ } logFindStart(logMask, pid, start, sequence);
+
+ logbuf().flushTo(cli, sequence, FlushCommand::hasReadLogs(cli),
+ logFindStart.callback, &logFindStart);
+
+ if (!logFindStart.found()) {
+ if (nonBlock) {
+ doSocketDelete(cli);
+ return false;
+ }
+ sequence = LogBufferElement::getCurrentSequence();
+ }
+ }
+
+ FlushCommand command(*this, nonBlock, tail, logMask, pid, sequence);
+ command.runSocketCommand(cli);
+ return true;
+}
+
+void LogReader::doSocketDelete(SocketClient *cli) {
+ LastLogTimes × = mLogbuf.mTimes;
+ LogTimeEntry::lock();
+ LastLogTimes::iterator it = times.begin();
+ while(it != times.end()) {
+ LogTimeEntry *entry = (*it);
+ if (entry->mClient == cli) {
+ times.erase(it);
+ entry->release_Locked();
+ break;
+ }
+ it++;
+ }
+ LogTimeEntry::unlock();
+}
+
+int LogReader::getLogSocket() {
+ static const char socketName[] = "logdr";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_SEQPACKET);
+ }
+
+ return sock;
+}
diff --git a/logd/LogReader.h b/logd/LogReader.h
new file mode 100644
index 0000000..91559a3
--- /dev/null
+++ b/logd/LogReader.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2012-2013 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 _LOGD_LOG_WRITER_H__
+#define _LOGD_LOG_WRITER_H__
+
+#include <sysutils/SocketListener.h>
+#include "LogBuffer.h"
+#include "LogTimes.h"
+
+class LogReader : public SocketListener {
+ LogBuffer &mLogbuf;
+
+public:
+ LogReader(LogBuffer *logbuf);
+ void notifyNewLog();
+
+ LogBuffer &logbuf(void) const { return mLogbuf; }
+
+protected:
+ virtual bool onDataAvailable(SocketClient *cli);
+
+private:
+ static int getLogSocket();
+
+ void doSocketDelete(SocketClient *cli);
+
+};
+
+#endif
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
new file mode 100644
index 0000000..5a70689
--- /dev/null
+++ b/logd/LogStatistics.cpp
@@ -0,0 +1,843 @@
+/*
+ * Copyright (C) 2014 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 <fcntl.h>
+#include <malloc.h>
+#include <stdarg.h>
+#include <time.h>
+
+#include <log/logger.h>
+#include <private/android_filesystem_config.h>
+#include <utils/String8.h>
+
+#include "LogStatistics.h"
+
+PidStatistics::PidStatistics(pid_t pid, char *name)
+ : pid(pid)
+ , mSizesTotal(0)
+ , mElementsTotal(0)
+ , mSizes(0)
+ , mElements(0)
+ , name(name)
+ , mGone(false)
+{ }
+
+#ifdef DO_NOT_ERROR_IF_PIDSTATISTICS_USES_A_COPY_CONSTRUCTOR
+PidStatistics::PidStatistics(const PidStatistics ©)
+ : pid(copy->pid)
+ , name(copy->name ? strdup(copy->name) : NULL)
+ , mSizesTotal(copy->mSizesTotal)
+ , mElementsTotal(copy->mElementsTotal)
+ , mSizes(copy->mSizes)
+ , mElements(copy->mElements)
+ , mGone(copy->mGone)
+{ }
+#endif
+
+PidStatistics::~PidStatistics() {
+ free(name);
+}
+
+bool PidStatistics::pidGone() {
+ if (mGone || (pid == gone)) {
+ return true;
+ }
+ if (pid == 0) {
+ return false;
+ }
+ if (kill(pid, 0) && (errno != EPERM)) {
+ mGone = true;
+ return true;
+ }
+ return false;
+}
+
+void PidStatistics::setName(char *new_name) {
+ free(name);
+ name = new_name;
+}
+
+void PidStatistics::add(unsigned short size) {
+ mSizesTotal += size;
+ ++mElementsTotal;
+ mSizes += size;
+ ++mElements;
+}
+
+bool PidStatistics::subtract(unsigned short size) {
+ mSizes -= size;
+ --mElements;
+ return (mElements == 0) && pidGone();
+}
+
+void PidStatistics::addTotal(size_t size, size_t element) {
+ if (pid == gone) {
+ mSizesTotal += size;
+ mElementsTotal += element;
+ }
+}
+
+// must call free to release return value
+// If only we could sniff our own logs for:
+// <time> <pid> <pid> E AndroidRuntime: Process: <name>, PID: <pid>
+// which debuggerd prints as a process is crashing.
+char *PidStatistics::pidToName(pid_t pid) {
+ char *retval = NULL;
+ if (pid == 0) { // special case from auditd for kernel
+ retval = strdup("logd.auditd");
+ } else if (pid != gone) {
+ char buffer[512];
+ snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid);
+ int fd = open(buffer, O_RDONLY);
+ if (fd >= 0) {
+ ssize_t ret = read(fd, buffer, sizeof(buffer));
+ if (ret > 0) {
+ buffer[sizeof(buffer)-1] = '\0';
+ // frameworks intermediate state
+ if (strcmp(buffer, "<pre-initialized>")) {
+ retval = strdup(buffer);
+ }
+ }
+ close(fd);
+ }
+ }
+ return retval;
+}
+
+UidStatistics::UidStatistics(uid_t uid)
+ : uid(uid)
+ , mSizes(0)
+ , mElements(0) {
+ Pids.clear();
+}
+
+UidStatistics::~UidStatistics() {
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end();) {
+ delete (*it);
+ it = erase(it);
+ }
+}
+
+void UidStatistics::add(unsigned short size, pid_t pid) {
+ mSizes += size;
+ ++mElements;
+
+ PidStatistics *p = NULL;
+ PidStatisticsCollection::iterator last;
+ PidStatisticsCollection::iterator it;
+ for (last = it = begin(); it != end(); last = it, ++it) {
+ p = *it;
+ if (pid == p->getPid()) {
+ p->add(size);
+ return;
+ }
+ }
+ // insert if the gone entry.
+ bool insert_before_last = (last != it) && p && (p->getPid() == p->gone);
+ p = new PidStatistics(pid, pidToName(pid));
+ if (insert_before_last) {
+ insert(last, p);
+ } else {
+ push_back(p);
+ }
+ p->add(size);
+}
+
+void UidStatistics::subtract(unsigned short size, pid_t pid) {
+ mSizes -= size;
+ --mElements;
+
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ PidStatistics *p = *it;
+ if (pid == p->getPid()) {
+ if (p->subtract(size)) {
+ size_t szsTotal = p->sizesTotal();
+ size_t elsTotal = p->elementsTotal();
+ delete p;
+ erase(it);
+ it = end();
+ --it;
+ if (it == end()) {
+ p = new PidStatistics(p->gone);
+ push_back(p);
+ } else {
+ p = *it;
+ if (p->getPid() != p->gone) {
+ p = new PidStatistics(p->gone);
+ push_back(p);
+ }
+ }
+ p->addTotal(szsTotal, elsTotal);
+ }
+ return;
+ }
+ }
+}
+
+void UidStatistics::sort() {
+ for (bool pass = true; pass;) {
+ pass = false;
+ PidStatisticsCollection::iterator it = begin();
+ if (it != end()) {
+ PidStatisticsCollection::iterator lt = it;
+ PidStatistics *l = (*lt);
+ while (++it != end()) {
+ PidStatistics *n = (*it);
+ if ((n->getPid() != n->gone) && (n->sizes() > l->sizes())) {
+ pass = true;
+ erase(it);
+ insert(lt, n);
+ it = lt;
+ n = l;
+ }
+ lt = it;
+ l = n;
+ }
+ }
+ }
+}
+
+size_t UidStatistics::sizes(pid_t pid) {
+ if (pid == pid_all) {
+ return sizes();
+ }
+
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ PidStatistics *p = *it;
+ if (pid == p->getPid()) {
+ return p->sizes();
+ }
+ }
+ return 0;
+}
+
+size_t UidStatistics::elements(pid_t pid) {
+ if (pid == pid_all) {
+ return elements();
+ }
+
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ PidStatistics *p = *it;
+ if (pid == p->getPid()) {
+ return p->elements();
+ }
+ }
+ return 0;
+}
+
+size_t UidStatistics::sizesTotal(pid_t pid) {
+ size_t sizes = 0;
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ PidStatistics *p = *it;
+ if ((pid == pid_all) || (pid == p->getPid())) {
+ sizes += p->sizesTotal();
+ }
+ }
+ return sizes;
+}
+
+size_t UidStatistics::elementsTotal(pid_t pid) {
+ size_t elements = 0;
+ PidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ PidStatistics *p = *it;
+ if ((pid == pid_all) || (pid == p->getPid())) {
+ elements += p->elementsTotal();
+ }
+ }
+ return elements;
+}
+
+LidStatistics::LidStatistics() {
+ Uids.clear();
+}
+
+LidStatistics::~LidStatistics() {
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end();) {
+ delete (*it);
+ it = Uids.erase(it);
+ }
+}
+
+void LidStatistics::add(unsigned short size, uid_t uid, pid_t pid) {
+ UidStatistics *u;
+ UidStatisticsCollection::iterator it;
+ UidStatisticsCollection::iterator last;
+
+ if (uid == (uid_t) -1) { // init
+ uid = (uid_t) AID_ROOT;
+ }
+
+ for (last = it = begin(); it != end(); last = it, ++it) {
+ u = *it;
+ if (uid == u->getUid()) {
+ u->add(size, pid);
+ if ((last != it) && ((*last)->sizesTotal() < u->sizesTotal())) {
+ Uids.erase(it);
+ Uids.insert(last, u);
+ }
+ return;
+ }
+ }
+ u = new UidStatistics(uid);
+ if ((last != it) && ((*last)->sizesTotal() < (size_t) size)) {
+ Uids.insert(last, u);
+ } else {
+ Uids.push_back(u);
+ }
+ u->add(size, pid);
+}
+
+void LidStatistics::subtract(unsigned short size, uid_t uid, pid_t pid) {
+ if (uid == (uid_t) -1) { // init
+ uid = (uid_t) AID_ROOT;
+ }
+
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ UidStatistics *u = *it;
+ if (uid == u->getUid()) {
+ u->subtract(size, pid);
+ return;
+ }
+ }
+}
+
+void LidStatistics::sort() {
+ for (bool pass = true; pass;) {
+ pass = false;
+ UidStatisticsCollection::iterator it = begin();
+ if (it != end()) {
+ UidStatisticsCollection::iterator lt = it;
+ UidStatistics *l = (*lt);
+ while (++it != end()) {
+ UidStatistics *n = (*it);
+ if (n->sizes() > l->sizes()) {
+ pass = true;
+ Uids.erase(it);
+ Uids.insert(lt, n);
+ it = lt;
+ n = l;
+ }
+ lt = it;
+ l = n;
+ }
+ }
+ }
+}
+
+size_t LidStatistics::sizes(uid_t uid, pid_t pid) {
+ size_t sizes = 0;
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ UidStatistics *u = *it;
+ if ((uid == uid_all) || (uid == u->getUid())) {
+ sizes += u->sizes(pid);
+ }
+ }
+ return sizes;
+}
+
+size_t LidStatistics::elements(uid_t uid, pid_t pid) {
+ size_t elements = 0;
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ UidStatistics *u = *it;
+ if ((uid == uid_all) || (uid == u->getUid())) {
+ elements += u->elements(pid);
+ }
+ }
+ return elements;
+}
+
+size_t LidStatistics::sizesTotal(uid_t uid, pid_t pid) {
+ size_t sizes = 0;
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ UidStatistics *u = *it;
+ if ((uid == uid_all) || (uid == u->getUid())) {
+ sizes += u->sizesTotal(pid);
+ }
+ }
+ return sizes;
+}
+
+size_t LidStatistics::elementsTotal(uid_t uid, pid_t pid) {
+ size_t elements = 0;
+ UidStatisticsCollection::iterator it;
+ for (it = begin(); it != end(); ++it) {
+ UidStatistics *u = *it;
+ if ((uid == uid_all) || (uid == u->getUid())) {
+ elements += u->elementsTotal(pid);
+ }
+ }
+ return elements;
+}
+
+LogStatistics::LogStatistics()
+ : mStatistics(false)
+ , start(CLOCK_MONOTONIC) {
+ log_id_for_each(i) {
+ mSizes[i] = 0;
+ mElements[i] = 0;
+ }
+}
+
+void LogStatistics::add(unsigned short size,
+ log_id_t log_id, uid_t uid, pid_t pid) {
+ mSizes[log_id] += size;
+ ++mElements[log_id];
+ if (!mStatistics) {
+ return;
+ }
+ id(log_id).add(size, uid, pid);
+}
+
+void LogStatistics::subtract(unsigned short size,
+ log_id_t log_id, uid_t uid, pid_t pid) {
+ mSizes[log_id] -= size;
+ --mElements[log_id];
+ if (!mStatistics) {
+ return;
+ }
+ id(log_id).subtract(size, uid, pid);
+}
+
+size_t LogStatistics::sizes(log_id_t log_id, uid_t uid, pid_t pid) {
+ if (log_id != log_id_all) {
+ return id(log_id).sizes(uid, pid);
+ }
+ size_t sizes = 0;
+ log_id_for_each(i) {
+ sizes += id(i).sizes(uid, pid);
+ }
+ return sizes;
+}
+
+size_t LogStatistics::elements(log_id_t log_id, uid_t uid, pid_t pid) {
+ if (log_id != log_id_all) {
+ return id(log_id).elements(uid, pid);
+ }
+ size_t elements = 0;
+ log_id_for_each(i) {
+ elements += id(i).elements(uid, pid);
+ }
+ return elements;
+}
+
+size_t LogStatistics::sizesTotal(log_id_t log_id, uid_t uid, pid_t pid) {
+ if (log_id != log_id_all) {
+ return id(log_id).sizesTotal(uid, pid);
+ }
+ size_t sizes = 0;
+ log_id_for_each(i) {
+ sizes += id(i).sizesTotal(uid, pid);
+ }
+ return sizes;
+}
+
+size_t LogStatistics::elementsTotal(log_id_t log_id, uid_t uid, pid_t pid) {
+ if (log_id != log_id_all) {
+ return id(log_id).elementsTotal(uid, pid);
+ }
+ size_t elements = 0;
+ log_id_for_each(i) {
+ elements += id(i).elementsTotal(uid, pid);
+ }
+ return elements;
+}
+
+void LogStatistics::format(char **buf,
+ uid_t uid, unsigned int logMask, log_time oldest) {
+ static const unsigned short spaces_current = 13;
+ static const unsigned short spaces_total = 19;
+
+ if (*buf) {
+ free(*buf);
+ *buf = NULL;
+ }
+
+ android::String8 string(" span -> size/num");
+ size_t oldLength;
+ short spaces = 2;
+
+ log_id_for_each(i) {
+ if (!(logMask & (1 << i))) {
+ continue;
+ }
+ oldLength = string.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s%s", spaces, "", android_log_id_to_name(i));
+ spaces += spaces_total + oldLength - string.length();
+
+ LidStatistics &l = id(i);
+ l.sort();
+
+ UidStatisticsCollection::iterator iu;
+ for (iu = l.begin(); iu != l.end(); ++iu) {
+ (*iu)->sort();
+ }
+ }
+
+ spaces = 1;
+ log_time t(CLOCK_MONOTONIC);
+ unsigned long long d;
+ if (mStatistics) {
+ d = t.nsec() - start.nsec();
+ string.appendFormat("\nTotal%4llu:%02llu:%02llu.%09llu",
+ d / NS_PER_SEC / 60 / 60, (d / NS_PER_SEC / 60) % 60,
+ (d / NS_PER_SEC) % 60, d % NS_PER_SEC);
+
+ log_id_for_each(i) {
+ if (!(logMask & (1 << i))) {
+ continue;
+ }
+ oldLength = string.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s%zu/%zu", spaces, "",
+ sizesTotal(i), elementsTotal(i));
+ spaces += spaces_total + oldLength - string.length();
+ }
+ spaces = 1;
+ }
+
+ d = t.nsec() - oldest.nsec();
+ string.appendFormat("\nNow%6llu:%02llu:%02llu.%09llu",
+ d / NS_PER_SEC / 60 / 60, (d / NS_PER_SEC / 60) % 60,
+ (d / NS_PER_SEC) % 60, d % NS_PER_SEC);
+
+ log_id_for_each(i) {
+ if (!(logMask & (1 << i))) {
+ continue;
+ }
+
+ size_t els = elements(i);
+ if (els) {
+ oldLength = string.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s%zu/%zu", spaces, "", sizes(i), els);
+ spaces -= string.length() - oldLength;
+ }
+ spaces += spaces_total;
+ }
+
+ // Construct list of worst spammers by Pid
+ static const unsigned char num_spammers = 10;
+ bool header = false;
+
+ log_id_for_each(i) {
+ if (!(logMask & (1 << i))) {
+ continue;
+ }
+
+ PidStatisticsCollection pids;
+ pids.clear();
+
+ LidStatistics &l = id(i);
+ UidStatisticsCollection::iterator iu;
+ for (iu = l.begin(); iu != l.end(); ++iu) {
+ UidStatistics &u = *(*iu);
+ PidStatisticsCollection::iterator ip;
+ for (ip = u.begin(); ip != u.end(); ++ip) {
+ PidStatistics *p = (*ip);
+ if (p->getPid() == p->gone) {
+ break;
+ }
+
+ size_t mySizes = p->sizes();
+
+ PidStatisticsCollection::iterator q;
+ unsigned char num = 0;
+ for (q = pids.begin(); q != pids.end(); ++q) {
+ if (mySizes > (*q)->sizes()) {
+ pids.insert(q, p);
+ break;
+ }
+ // do we need to traverse deeper in the list?
+ if (++num > num_spammers) {
+ break;
+ }
+ }
+ if (q == pids.end()) {
+ pids.push_back(p);
+ }
+ }
+ }
+
+ size_t threshold = sizes(i);
+ if (threshold < 65536) {
+ threshold = 65536;
+ }
+ threshold /= 100;
+
+ PidStatisticsCollection::iterator pt = pids.begin();
+
+ for(int line = 0;
+ (pt != pids.end()) && (line < num_spammers);
+ ++line, pt = pids.erase(pt)) {
+ PidStatistics *p = *pt;
+
+ size_t sizes = p->sizes();
+ if (sizes < threshold) {
+ break;
+ }
+
+ char *name = p->getName();
+ pid_t pid = p->getPid();
+ if (!name || !*name) {
+ name = pidToName(pid);
+ if (name) {
+ if (*name) {
+ p->setName(name);
+ } else {
+ free(name);
+ name = NULL;
+ }
+ }
+ }
+
+ if (!header) {
+ string.appendFormat("\n\nChattiest clients:\n"
+ "log id %-*s PID[?] name",
+ spaces_total, "size/total");
+ header = true;
+ }
+
+ size_t sizesTotal = p->sizesTotal();
+
+ android::String8 sz("");
+ if (sizes == sizesTotal) {
+ sz.appendFormat("%zu", sizes);
+ } else {
+ sz.appendFormat("%zu/%zu", sizes, sizesTotal);
+ }
+
+ android::String8 pd("");
+ pd.appendFormat("%u%c", pid, p->pidGone() ? '?' : ' ');
+
+ string.appendFormat("\n%-7s%-*s %-7s%s",
+ line ? "" : android_log_id_to_name(i),
+ spaces_total, sz.string(), pd.string(),
+ name ? name : "");
+ }
+
+ pids.clear();
+ }
+
+ log_id_for_each(i) {
+ if (!(logMask & (1 << i))) {
+ continue;
+ }
+
+ header = false;
+ bool first = true;
+
+ UidStatisticsCollection::iterator ut;
+ for(ut = id(i).begin(); ut != id(i).end(); ++ut) {
+ UidStatistics *up = *ut;
+ if ((uid != AID_ROOT) && (uid != up->getUid())) {
+ continue;
+ }
+
+ PidStatisticsCollection::iterator pt = up->begin();
+ if (pt == up->end()) {
+ continue;
+ }
+
+ android::String8 intermediate;
+
+ if (!header) {
+ // header below tuned to match spaces_total and spaces_current
+ spaces = 0;
+ intermediate = string.format("%s: UID/PID Total size/num",
+ android_log_id_to_name(i));
+ string.appendFormat("\n\n%-31sNow "
+ "UID/PID[?] Total Now",
+ intermediate.string());
+ intermediate.clear();
+ header = true;
+ }
+
+ bool oneline = ++pt == up->end();
+ --pt;
+
+ if (!oneline) {
+ first = true;
+ } else if (!first && (spaces > 0)) {
+ string.appendFormat("%*s", spaces, "");
+ }
+ spaces = 0;
+
+ uid_t u = up->getUid();
+ PidStatistics *pp = *pt;
+ pid_t p = pp->getPid();
+
+ if (!oneline) {
+ intermediate = string.format("%d", u);
+ } else if (p == PidStatistics::gone) {
+ intermediate = string.format("%d/?", u);
+ } else if (pp->pidGone()) {
+ intermediate = string.format("%d/%d?", u, p);
+ } else {
+ intermediate = string.format("%d/%d", u, p);
+ }
+ string.appendFormat(first ? "\n%-12s" : "%-12s",
+ intermediate.string());
+ intermediate.clear();
+
+ size_t elsTotal = up->elementsTotal();
+ oldLength = string.length();
+ string.appendFormat("%zu/%zu", up->sizesTotal(), elsTotal);
+ spaces += spaces_total + oldLength - string.length();
+
+ size_t els = up->elements();
+ if (els == elsTotal) {
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s=", spaces, "");
+ spaces = -1;
+ } else if (els) {
+ oldLength = string.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s%zu/%zu", spaces, "", up->sizes(), els);
+ spaces -= string.length() - oldLength;
+ }
+ spaces += spaces_current;
+
+ first = !first;
+
+ if (oneline) {
+ continue;
+ }
+
+ size_t gone_szs = 0;
+ size_t gone_els = 0;
+
+ for(; pt != up->end(); ++pt) {
+ pp = *pt;
+ p = pp->getPid();
+
+ // If a PID no longer has any current logs, and is not
+ // active anymore, skip & report totals for gone.
+ elsTotal = pp->elementsTotal();
+ size_t szsTotal = pp->sizesTotal();
+ if (p == pp->gone) {
+ gone_szs += szsTotal;
+ gone_els += elsTotal;
+ continue;
+ }
+ els = pp->elements();
+ bool gone = pp->pidGone();
+ if (gone && (els == 0)) {
+ // ToDo: garbage collection: move this statistical bucket
+ // from its current UID/PID to UID/? (races and
+ // wrap around are our achilles heel). Below is
+ // merely lipservice to catch PIDs that were still
+ // around when the stats were pruned to zero.
+ gone_szs += szsTotal;
+ gone_els += elsTotal;
+ continue;
+ }
+
+ if (!first && (spaces > 0)) {
+ string.appendFormat("%*s", spaces, "");
+ }
+ spaces = 0;
+
+ intermediate = string.format(gone ? "%d/%d?" : "%d/%d", u, p);
+ string.appendFormat(first ? "\n%-12s" : "%-12s",
+ intermediate.string());
+ intermediate.clear();
+
+ oldLength = string.length();
+ string.appendFormat("%zu/%zu", szsTotal, elsTotal);
+ spaces += spaces_total + oldLength - string.length();
+
+ if (els == elsTotal) {
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s=", spaces, "");
+ spaces = -1;
+ } else if (els) {
+ oldLength = string.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ string.appendFormat("%*s%zu/%zu", spaces, "",
+ pp->sizes(), els);
+ spaces -= string.length() - oldLength;
+ }
+ spaces += spaces_current;
+
+ first = !first;
+ }
+
+ if (gone_els) {
+ if (!first && (spaces > 0)) {
+ string.appendFormat("%*s", spaces, "");
+ }
+
+ intermediate = string.format("%d/?", u);
+ string.appendFormat(first ? "\n%-12s" : "%-12s",
+ intermediate.string());
+ intermediate.clear();
+
+ spaces = spaces_total + spaces_current;
+
+ oldLength = string.length();
+ string.appendFormat("%zu/%zu", gone_szs, gone_els);
+ spaces -= string.length() - oldLength;
+
+ first = !first;
+ }
+ }
+ }
+
+ *buf = strdup(string.string());
+}
+
+uid_t LogStatistics::pidToUid(pid_t pid) {
+ log_id_for_each(i) {
+ LidStatistics &l = id(i);
+ UidStatisticsCollection::iterator iu;
+ for (iu = l.begin(); iu != l.end(); ++iu) {
+ UidStatistics &u = *(*iu);
+ PidStatisticsCollection::iterator ip;
+ for (ip = u.begin(); ip != u.end(); ++ip) {
+ if ((*ip)->getPid() == pid) {
+ return u.getUid();
+ }
+ }
+ }
+ }
+ return getuid(); // associate this with the logger
+}
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
new file mode 100644
index 0000000..f892cd0
--- /dev/null
+++ b/logd/LogStatistics.h
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2014 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 _LOGD_LOG_STATISTICS_H__
+#define _LOGD_LOG_STATISTICS_H__
+
+#include <sys/types.h>
+
+#include <log/log.h>
+#include <log/log_read.h>
+#include <utils/List.h>
+
+#define log_id_for_each(i) \
+ for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1))
+
+class PidStatistics {
+ const pid_t pid;
+
+ // Total
+ size_t mSizesTotal;
+ size_t mElementsTotal;
+ // Current
+ size_t mSizes;
+ size_t mElements;
+
+ char *name;
+ bool mGone;
+
+public:
+ static const pid_t gone = (pid_t) -1;
+
+ PidStatistics(pid_t pid, char *name = NULL);
+ PidStatistics(const PidStatistics ©);
+ ~PidStatistics();
+
+ pid_t getPid() const { return pid; }
+ bool pidGone();
+ char *getName() const { return name; }
+ void setName(char *name);
+
+ void add(unsigned short size);
+ bool subtract(unsigned short size); // returns true if stats and PID gone
+ void addTotal(size_t size, size_t element);
+
+ size_t sizes() const { return mSizes; }
+ size_t elements() const { return mElements; }
+
+ size_t sizesTotal() const { return mSizesTotal; }
+ size_t elementsTotal() const { return mElementsTotal; }
+
+ // helper
+ static char *pidToName(pid_t pid);
+};
+
+typedef android::List<PidStatistics *> PidStatisticsCollection;
+
+class UidStatistics {
+ const uid_t uid;
+
+ PidStatisticsCollection Pids;
+
+ void insert(PidStatisticsCollection::iterator i, PidStatistics *p)
+ { Pids.insert(i, p); }
+ void push_back(PidStatistics *p) { Pids.push_back(p); }
+
+ size_t mSizes;
+ size_t mElements;
+
+public:
+ UidStatistics(uid_t uid);
+ ~UidStatistics();
+
+ PidStatisticsCollection::iterator begin() { return Pids.begin(); }
+ PidStatisticsCollection::iterator end() { return Pids.end(); }
+ PidStatisticsCollection::iterator erase(PidStatisticsCollection::iterator i)
+ { return Pids.erase(i); }
+
+ uid_t getUid() { return uid; }
+
+ void add(unsigned short size, pid_t pid);
+ void subtract(unsigned short size, pid_t pid);
+ void sort();
+
+ static const pid_t pid_all = (pid_t) -1;
+
+ // fast track current value
+ size_t sizes() const { return mSizes; };
+ size_t elements() const { return mElements; };
+
+ // statistical track
+ size_t sizes(pid_t pid);
+ size_t elements(pid_t pid);
+
+ size_t sizesTotal(pid_t pid = pid_all);
+ size_t elementsTotal(pid_t pid = pid_all);
+
+ // helper
+ static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); }
+};
+
+typedef android::List<UidStatistics *> UidStatisticsCollection;
+
+class LidStatistics {
+ UidStatisticsCollection Uids;
+
+public:
+ LidStatistics();
+ ~LidStatistics();
+
+ UidStatisticsCollection::iterator begin() { return Uids.begin(); }
+ UidStatisticsCollection::iterator end() { return Uids.end(); }
+
+ void add(unsigned short size, uid_t uid, pid_t pid);
+ void subtract(unsigned short size, uid_t uid, pid_t pid);
+ void sort();
+
+ static const pid_t pid_all = (pid_t) -1;
+ static const uid_t uid_all = (uid_t) -1;
+
+ size_t sizes(uid_t uid = uid_all, pid_t pid = pid_all);
+ size_t elements(uid_t uid = uid_all, pid_t pid = pid_all);
+
+ size_t sizesTotal(uid_t uid = uid_all, pid_t pid = pid_all);
+ size_t elementsTotal(uid_t uid = uid_all, pid_t pid = pid_all);
+};
+
+// Log Statistics
+class LogStatistics {
+ LidStatistics LogIds[LOG_ID_MAX];
+
+ size_t mSizes[LOG_ID_MAX];
+ size_t mElements[LOG_ID_MAX];
+
+ bool mStatistics;
+
+ static const unsigned short mBuckets[14];
+ log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])];
+
+public:
+ const log_time start;
+
+ LogStatistics();
+
+ LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; }
+
+ void enableStatistics() { mStatistics = true; }
+
+ void add(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid);
+ void subtract(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid);
+ void sort();
+
+ // fast track current value by id only
+ size_t sizes(log_id_t id) const { return mSizes[id]; }
+ size_t elements(log_id_t id) const { return mElements[id]; }
+
+ // statistical track
+ static const log_id_t log_id_all = (log_id_t) -1;
+ static const uid_t uid_all = (uid_t) -1;
+ static const pid_t pid_all = (pid_t) -1;
+
+ size_t sizes(log_id_t id, uid_t uid, pid_t pid = pid_all);
+ size_t elements(log_id_t id, uid_t uid, pid_t pid = pid_all);
+ size_t sizes() { return sizes(log_id_all, uid_all); }
+ size_t elements() { return elements(log_id_all, uid_all); }
+
+ size_t sizesTotal(log_id_t id = log_id_all,
+ uid_t uid = uid_all,
+ pid_t pid = pid_all);
+ size_t elementsTotal(log_id_t id = log_id_all,
+ uid_t uid = uid_all,
+ pid_t pid = pid_all);
+
+ // *strp = malloc, balance with free
+ void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest);
+
+ // helper
+ static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); }
+ uid_t pidToUid(pid_t pid);
+};
+
+#endif // _LOGD_LOG_STATISTICS_H__
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
new file mode 100644
index 0000000..1b60b7e
--- /dev/null
+++ b/logd/LogTimes.cpp
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2014 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 <sys/prctl.h>
+
+#include "FlushCommand.h"
+#include "LogBuffer.h"
+#include "LogTimes.h"
+#include "LogReader.h"
+
+pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
+
+LogTimeEntry::LogTimeEntry(LogReader &reader, SocketClient *client,
+ bool nonBlock, unsigned long tail,
+ unsigned int logMask, pid_t pid,
+ uint64_t start)
+ : mRefCount(1)
+ , mRelease(false)
+ , mError(false)
+ , threadRunning(false)
+ , mReader(reader)
+ , mLogMask(logMask)
+ , mPid(pid)
+ , mCount(0)
+ , mTail(tail)
+ , mIndex(0)
+ , mClient(client)
+ , mStart(start)
+ , mNonBlock(nonBlock)
+ , mEnd(LogBufferElement::getCurrentSequence())
+{
+ pthread_cond_init(&threadTriggeredCondition, NULL);
+ cleanSkip_Locked();
+}
+
+void LogTimeEntry::startReader_Locked(void) {
+ pthread_attr_t attr;
+
+ threadRunning = true;
+
+ if (!pthread_attr_init(&attr)) {
+ if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
+ if (!pthread_create(&mThread, &attr,
+ LogTimeEntry::threadStart, this)) {
+ pthread_attr_destroy(&attr);
+ return;
+ }
+ }
+ pthread_attr_destroy(&attr);
+ }
+ threadRunning = false;
+ if (mClient) {
+ mClient->decRef();
+ }
+ decRef_Locked();
+}
+
+void LogTimeEntry::threadStop(void *obj) {
+ LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
+
+ lock();
+
+ if (me->mNonBlock) {
+ me->error_Locked();
+ }
+
+ SocketClient *client = me->mClient;
+
+ if (me->isError_Locked()) {
+ LogReader &reader = me->mReader;
+ LastLogTimes × = reader.logbuf().mTimes;
+
+ LastLogTimes::iterator it = times.begin();
+ while(it != times.end()) {
+ if (*it == me) {
+ times.erase(it);
+ me->release_Locked();
+ break;
+ }
+ it++;
+ }
+
+ me->mClient = NULL;
+ reader.release(client);
+ }
+
+ if (client) {
+ client->decRef();
+ }
+
+ me->threadRunning = false;
+ me->decRef_Locked();
+
+ unlock();
+}
+
+void *LogTimeEntry::threadStart(void *obj) {
+ prctl(PR_SET_NAME, "logd.reader.per");
+
+ LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
+
+ pthread_cleanup_push(threadStop, obj);
+
+ SocketClient *client = me->mClient;
+ if (!client) {
+ me->error();
+ return NULL;
+ }
+
+ LogBuffer &logbuf = me->mReader.logbuf();
+
+ bool privileged = FlushCommand::hasReadLogs(client);
+
+ lock();
+
+ while (me->threadRunning && !me->isError_Locked()) {
+ uint64_t start = me->mStart;
+
+ unlock();
+
+ if (me->mTail) {
+ logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
+ }
+ start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
+
+ lock();
+
+ if (start == LogBufferElement::FLUSH_ERROR) {
+ me->error_Locked();
+ }
+
+ if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
+ break;
+ }
+
+ me->cleanSkip_Locked();
+
+ pthread_cond_wait(&me->threadTriggeredCondition, ×Lock);
+ }
+
+ unlock();
+
+ pthread_cleanup_pop(true);
+
+ return NULL;
+}
+
+// A first pass to count the number of elements
+int LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
+ LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
+
+ LogTimeEntry::lock();
+
+ if (me->mCount == 0) {
+ me->mStart = element->getSequence();
+ }
+
+ if ((!me->mPid || (me->mPid == element->getPid()))
+ && (me->isWatching(element->getLogId()))) {
+ ++me->mCount;
+ }
+
+ LogTimeEntry::unlock();
+
+ return false;
+}
+
+// A second pass to send the selected elements
+int LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
+ LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
+
+ LogTimeEntry::lock();
+
+ me->mStart = element->getSequence();
+
+ if (me->skipAhead[element->getLogId()]) {
+ me->skipAhead[element->getLogId()]--;
+ goto skip;
+ }
+
+ // Truncate to close race between first and second pass
+ if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
+ goto stop;
+ }
+
+ if (!me->isWatching(element->getLogId())) {
+ goto skip;
+ }
+
+ if (me->mPid && (me->mPid != element->getPid())) {
+ goto skip;
+ }
+
+ if (me->isError_Locked()) {
+ goto stop;
+ }
+
+ if (!me->mTail) {
+ goto ok;
+ }
+
+ ++me->mIndex;
+
+ if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
+ goto skip;
+ }
+
+ if (!me->mNonBlock) {
+ me->mTail = 0;
+ }
+
+ok:
+ if (!me->skipAhead[element->getLogId()]) {
+ LogTimeEntry::unlock();
+ return true;
+ }
+ // FALLTHRU
+
+skip:
+ LogTimeEntry::unlock();
+ return false;
+
+stop:
+ LogTimeEntry::unlock();
+ return -1;
+}
+
+void LogTimeEntry::cleanSkip_Locked(void) {
+ for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) {
+ skipAhead[i] = 0;
+ }
+}
diff --git a/logd/LogTimes.h b/logd/LogTimes.h
new file mode 100644
index 0000000..ae2f92b
--- /dev/null
+++ b/logd/LogTimes.h
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2012-2013 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 _LOGD_LOG_TIMES_H__
+#define _LOGD_LOG_TIMES_H__
+
+#include <pthread.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sysutils/SocketClient.h>
+#include <utils/List.h>
+#include <log/log.h>
+
+class LogReader;
+
+class LogTimeEntry {
+ static pthread_mutex_t timesLock;
+ unsigned int mRefCount;
+ bool mRelease;
+ bool mError;
+ bool threadRunning;
+ pthread_cond_t threadTriggeredCondition;
+ pthread_t mThread;
+ LogReader &mReader;
+ static void *threadStart(void *me);
+ static void threadStop(void *me);
+ const unsigned int mLogMask;
+ const pid_t mPid;
+ unsigned int skipAhead[LOG_ID_MAX];
+ unsigned long mCount;
+ unsigned long mTail;
+ unsigned long mIndex;
+
+public:
+ LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
+ unsigned long tail, unsigned int logMask, pid_t pid,
+ uint64_t start);
+
+ SocketClient *mClient;
+ uint64_t mStart;
+ const bool mNonBlock;
+ const uint64_t mEnd; // only relevant if mNonBlock
+
+ // Protect List manipulations
+ static void lock(void) { pthread_mutex_lock(×Lock); }
+ static void unlock(void) { pthread_mutex_unlock(×Lock); }
+
+ void startReader_Locked(void);
+
+ bool runningReader_Locked(void) const {
+ return threadRunning || mRelease || mError || mNonBlock;
+ }
+ void triggerReader_Locked(void) {
+ pthread_cond_signal(&threadTriggeredCondition);
+ }
+
+ void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; }
+ void cleanSkip_Locked(void);
+
+ // Called after LogTimeEntry removed from list, lock implicitly held
+ void release_Locked(void) {
+ mRelease = true;
+ pthread_cond_signal(&threadTriggeredCondition);
+ if (mRefCount || threadRunning) {
+ return;
+ }
+ // No one else is holding a reference to this
+ delete this;
+ }
+
+ // Called to mark socket in jeopardy
+ void error_Locked(void) { mError = true; }
+ void error(void) { lock(); error_Locked(); unlock(); }
+
+ bool isError_Locked(void) const { return mRelease || mError; }
+
+ // Mark Used
+ // Locking implied, grabbed when protection around loop iteration
+ void incRef_Locked(void) { ++mRefCount; }
+
+ bool owned_Locked(void) const { return mRefCount != 0; }
+
+ void decRef_Locked(void) {
+ if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
+ return;
+ }
+ // No one else is holding a reference to this
+ delete this;
+ }
+ bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; }
+ // flushTo filter callbacks
+ static int FilterFirstPass(const LogBufferElement *element, void *me);
+ static int FilterSecondPass(const LogBufferElement *element, void *me);
+};
+
+typedef android::List<LogTimeEntry *> LastLogTimes;
+
+#endif
diff --git a/logd/LogWhiteBlackList.cpp b/logd/LogWhiteBlackList.cpp
new file mode 100644
index 0000000..6910854
--- /dev/null
+++ b/logd/LogWhiteBlackList.cpp
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2014 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 <ctype.h>
+#include <malloc.h>
+
+#include <utils/String8.h>
+
+#include "LogWhiteBlackList.h"
+
+// White and Black list
+
+Prune::Prune(uid_t uid, pid_t pid)
+ : mUid(uid)
+ , mPid(pid)
+{ }
+
+int Prune::cmp(uid_t uid, pid_t pid) const {
+ if ((mUid == uid_all) || (mUid == uid)) {
+ if (mPid == pid_all) {
+ return 0;
+ }
+ return pid - mPid;
+ }
+ return uid - mUid;
+}
+
+void Prune::format(char **strp) {
+ if (mUid != uid_all) {
+ if (mPid != pid_all) {
+ asprintf(strp, "%u/%u", mUid, mPid);
+ } else {
+ asprintf(strp, "%u", mUid);
+ }
+ } else if (mPid != pid_all) {
+ asprintf(strp, "/%u", mPid);
+ } else { // NB: mPid == pid_all can not happen if mUid == uid_all
+ asprintf(strp, "/");
+ }
+}
+
+PruneList::PruneList()
+ : mWorstUidEnabled(false) {
+ mNaughty.clear();
+ mNice.clear();
+}
+
+PruneList::~PruneList() {
+ PruneCollection::iterator it;
+ for (it = mNice.begin(); it != mNice.end();) {
+ delete (*it);
+ it = mNice.erase(it);
+ }
+ for (it = mNaughty.begin(); it != mNaughty.end();) {
+ delete (*it);
+ it = mNaughty.erase(it);
+ }
+}
+
+int PruneList::init(char *str) {
+ mWorstUidEnabled = false;
+ PruneCollection::iterator it;
+ for (it = mNice.begin(); it != mNice.end();) {
+ delete (*it);
+ it = mNice.erase(it);
+ }
+ for (it = mNaughty.begin(); it != mNaughty.end();) {
+ delete (*it);
+ it = mNaughty.erase(it);
+ }
+
+ if (!str) {
+ return 0;
+ }
+
+ mWorstUidEnabled = false;
+
+ for(; *str; ++str) {
+ if (isspace(*str)) {
+ continue;
+ }
+
+ PruneCollection *list;
+ if ((*str == '~') || (*str == '!')) { // ~ supported, ! undocumented
+ ++str;
+ // special case, translates to worst UID at priority in blacklist
+ if (*str == '!') {
+ mWorstUidEnabled = true;
+ ++str;
+ if (!*str) {
+ break;
+ }
+ if (!isspace(*str)) {
+ return 1;
+ }
+ continue;
+ }
+ if (!*str) {
+ return 1;
+ }
+ list = &mNaughty;
+ } else {
+ list = &mNice;
+ }
+
+ uid_t uid = Prune::uid_all;
+ if (isdigit(*str)) {
+ uid = 0;
+ do {
+ uid = uid * 10 + *str++ - '0';
+ } while (isdigit(*str));
+ }
+
+ pid_t pid = Prune::pid_all;
+ if (*str == '/') {
+ ++str;
+ if (isdigit(*str)) {
+ pid = 0;
+ do {
+ pid = pid * 10 + *str++ - '0';
+ } while (isdigit(*str));
+ }
+ }
+
+ if ((uid == Prune::uid_all) && (pid == Prune::pid_all)) {
+ return 1;
+ }
+
+ if (*str && !isspace(*str)) {
+ return 1;
+ }
+
+ // insert sequentially into list
+ PruneCollection::iterator it = list->begin();
+ while (it != list->end()) {
+ Prune *p = *it;
+ int m = uid - p->mUid;
+ if (m == 0) {
+ if (p->mPid == p->pid_all) {
+ break;
+ }
+ if ((pid == p->pid_all) && (p->mPid != p->pid_all)) {
+ it = list->erase(it);
+ continue;
+ }
+ m = pid - p->mPid;
+ }
+ if (m <= 0) {
+ if (m < 0) {
+ list->insert(it, new Prune(uid,pid));
+ }
+ break;
+ }
+ ++it;
+ }
+ if (it == list->end()) {
+ list->push_back(new Prune(uid,pid));
+ }
+ if (!*str) {
+ break;
+ }
+ }
+
+ return 0;
+}
+
+void PruneList::format(char **strp) {
+ if (*strp) {
+ free(*strp);
+ *strp = NULL;
+ }
+
+ static const char nice_format[] = " %s";
+ const char *fmt = nice_format + 1;
+
+ android::String8 string;
+
+ if (mWorstUidEnabled) {
+ string.setTo("~!");
+ fmt = nice_format;
+ }
+
+ PruneCollection::iterator it;
+
+ for (it = mNice.begin(); it != mNice.end(); ++it) {
+ char *a = NULL;
+ (*it)->format(&a);
+
+ string.appendFormat(fmt, a);
+ fmt = nice_format;
+
+ free(a);
+ }
+
+ static const char naughty_format[] = " ~%s";
+ fmt = naughty_format + (*fmt != ' ');
+ for (it = mNaughty.begin(); it != mNaughty.end(); ++it) {
+ char *a = NULL;
+ (*it)->format(&a);
+
+ string.appendFormat(fmt, a);
+ fmt = naughty_format;
+
+ free(a);
+ }
+
+ *strp = strdup(string.string());
+}
+
+// ToDo: Lists are in sorted order, Prune->cmp() returns + or -
+// If there is scaling issues, resort to a better algorithm than linear
+// based on these assumptions.
+
+bool PruneList::naughty(LogBufferElement *element) {
+ PruneCollection::iterator it;
+ for (it = mNaughty.begin(); it != mNaughty.end(); ++it) {
+ if (!(*it)->cmp(element)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool PruneList::nice(LogBufferElement *element) {
+ PruneCollection::iterator it;
+ for (it = mNice.begin(); it != mNice.end(); ++it) {
+ if (!(*it)->cmp(element)) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/logd/LogWhiteBlackList.h b/logd/LogWhiteBlackList.h
new file mode 100644
index 0000000..769d651
--- /dev/null
+++ b/logd/LogWhiteBlackList.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2014 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 _LOGD_LOG_WHITE_BLACK_LIST_H__
+#define _LOGD_LOG_WHITE_BLACK_LIST_H__
+
+#include <sys/types.h>
+
+#include <utils/List.h>
+
+#include <LogBufferElement.h>
+
+// White and Blacklist
+
+class Prune {
+ friend class PruneList;
+
+ const uid_t mUid;
+ const pid_t mPid;
+ int cmp(uid_t uid, pid_t pid) const;
+
+public:
+ static const uid_t uid_all = (uid_t) -1;
+ static const pid_t pid_all = (pid_t) -1;
+
+ Prune(uid_t uid, pid_t pid);
+
+ uid_t getUid() const { return mUid; }
+ pid_t getPid() const { return mPid; }
+
+ int cmp(LogBufferElement *e) const { return cmp(e->getUid(), e->getPid()); }
+
+ // *strp is malloc'd, use free to release
+ void format(char **strp);
+};
+
+typedef android::List<Prune *> PruneCollection;
+
+class PruneList {
+ PruneCollection mNaughty;
+ PruneCollection mNice;
+ bool mWorstUidEnabled;
+
+public:
+ PruneList();
+ ~PruneList();
+
+ int init(char *str);
+
+ bool naughty(LogBufferElement *element);
+ bool nice(LogBufferElement *element);
+ bool worstUidEnabled() const { return mWorstUidEnabled; }
+
+ // *strp is malloc'd, use free to release
+ void format(char **strp);
+};
+
+#endif // _LOGD_LOG_WHITE_BLACK_LIST_H__
diff --git a/logd/README.auditd b/logd/README.auditd
new file mode 100644
index 0000000..3f614a3
--- /dev/null
+++ b/logd/README.auditd
@@ -0,0 +1,17 @@
+Auditd Daemon
+
+The audit daemon is a simplified version of its desktop
+counterpart designed to gather the audit logs from the
+audit kernel subsystem. The audit subsystem of the kernel
+includes Linux Security Modules (LSM) messages as well.
+
+To enable the audit subsystem, you must add this to your
+kernel config:
+CONFIG_AUDIT=y
+
+To enable a LSM, you must consult that LSM's documentation, the
+example below is for SELinux:
+CONFIG_SECURITY_SELINUX=y
+
+This does not include possible dependencies that may need to be
+satisfied for that particular LSM.
diff --git a/logd/README.property b/logd/README.property
new file mode 100644
index 0000000..60542b2
--- /dev/null
+++ b/logd/README.property
@@ -0,0 +1,22 @@
+The properties that logd responds to are:
+
+name type default description
+logd.auditd bool true Enable selinux audit daemon
+logd.auditd.dmesg bool true selinux audit messages duplicated and
+ sent on to dmesg log
+logd.statistics bool depends Enable logcat -S statistics.
+ro.config.low_ram bool false if true, logd.statistics default false
+ro.build.type string if user, logd.statistics default false
+persist.logd.size number 256K default size of the buffer for all
+ log ids at initial startup, at runtime
+ use: logcat -b all -G <value>
+persist.logd.size.main number 256K Size of the buffer for the main log
+persist.logd.size.system number 256K Size of the buffer for the system log
+persist.logd.size.radio number 256K Size of the buffer for the radio log
+persist.logd.size.event number 256K Size of the buffer for the event log
+persist.logd.size.crash number 256K Size of the buffer for the crash log
+
+NB:
+- number support multipliers (K or M) for convenience. Range is limited
+ to between 64K and 256M for log buffer sizes. Individual logs override the
+ global default.
diff --git a/logd/event.logtags b/logd/event.logtags
new file mode 100644
index 0000000..a63f034
--- /dev/null
+++ b/logd/event.logtags
@@ -0,0 +1,36 @@
+# The entries in this file map a sparse set of log tag numbers to tag names.
+# This is installed on the device, in /system/etc, and parsed by logcat.
+#
+# Tag numbers are decimal integers, from 0 to 2^31. (Let's leave the
+# negative values alone for now.)
+#
+# Tag names are one or more ASCII letters and numbers or underscores, i.e.
+# "[A-Z][a-z][0-9]_". Do not include spaces or punctuation (the former
+# impacts log readability, the latter makes regex searches more annoying).
+#
+# Tag numbers and names are separated by whitespace. Blank lines and lines
+# starting with '#' are ignored.
+#
+# Optionally, after the tag names can be put a description for the value(s)
+# of the tag. Description are in the format
+# (<name>|data type[|data unit])
+# Multiple values are separated by commas.
+#
+# The data type is a number from the following values:
+# 1: int
+# 2: long
+# 3: string
+# 4: list
+#
+# The data unit is a number taken from the following list:
+# 1: Number of objects
+# 2: Number of bytes
+# 3: Number of milliseconds
+# 4: Number of allocations
+# 5: Id
+# 6: Percent
+# Default value for data of type int/long is 2 (bytes).
+#
+# TODO: generate ".java" and ".h" files with integer constants from this file.
+
+1003 auditd (avc|3)
diff --git a/logd/libaudit.c b/logd/libaudit.c
new file mode 100644
index 0000000..d00d579
--- /dev/null
+++ b/logd/libaudit.c
@@ -0,0 +1,274 @@
+/*
+ * Copyright 2012, Samsung Telecommunications of America
+ * Copyright (C) 2014 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.
+ *
+ * Written by William Roberts <w.roberts@sta.samsung.com>
+ *
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LOG_TAG "libaudit"
+#include <log/log.h>
+
+#include "libaudit.h"
+
+/**
+ * Waits for an ack from the kernel
+ * @param fd
+ * The netlink socket fd
+ * @param seq
+ * The current sequence number were acking on
+ * @return
+ * This function returns 0 on success, else -errno.
+ */
+static int get_ack(int fd, int16_t seq)
+{
+ int rc;
+ struct audit_message rep;
+
+ /* Sanity check, this is an internal interface this shouldn't happen */
+ if (fd < 0) {
+ return -EINVAL;
+ }
+
+ rc = audit_get_reply(fd, &rep, GET_REPLY_BLOCKING, MSG_PEEK);
+ if (rc < 0) {
+ return rc;
+ }
+
+ if (rep.nlh.nlmsg_type == NLMSG_ERROR) {
+ audit_get_reply(fd, &rep, GET_REPLY_BLOCKING, 0);
+ rc = ((struct nlmsgerr *)rep.data)->error;
+ if (rc) {
+ return -rc;
+ }
+ }
+
+ if ((int16_t)rep.nlh.nlmsg_seq != seq) {
+ SLOGW("Expected sequence number between user space and kernel space is out of skew, "
+ "expected %u got %u", seq, rep.nlh.nlmsg_seq);
+ }
+
+ return 0;
+}
+
+/**
+ *
+ * @param fd
+ * The netlink socket fd
+ * @param type
+ * The type of netlink message
+ * @param data
+ * The data to send
+ * @param size
+ * The length of the data in bytes
+ * @return
+ * This function returns a positive sequence number on success, else -errno.
+ */
+static int audit_send(int fd, int type, const void *data, size_t size)
+{
+ int rc;
+ static int16_t sequence = 0;
+ struct audit_message req;
+ struct sockaddr_nl addr;
+
+ memset(&req, 0, sizeof(req));
+ memset(&addr, 0, sizeof(addr));
+
+ /* We always send netlink messaged */
+ addr.nl_family = AF_NETLINK;
+
+ /* Set up the netlink headers */
+ req.nlh.nlmsg_type = type;
+ req.nlh.nlmsg_len = NLMSG_SPACE(size);
+ req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+
+ /*
+ * Check for a valid fd, even though sendto would catch this, its easier
+ * to always blindly increment the sequence number
+ */
+ if (fd < 0) {
+ return -EBADF;
+ }
+
+ /* Ensure the message is not too big */
+ if (NLMSG_SPACE(size) > MAX_AUDIT_MESSAGE_LENGTH) {
+ SLOGE("netlink message is too large");
+ return -EINVAL;
+ }
+
+ /* Only memcpy in the data if it was specified */
+ if (size && data) {
+ memcpy(NLMSG_DATA(&req.nlh), data, size);
+ }
+
+ /*
+ * Only increment the sequence number on a guarantee
+ * you will send it to the kernel.
+ *
+ * Also, the sequence is defined as a u32 in the kernel
+ * struct. Using an int here might not work on 32/64 bit splits. A
+ * signed 64 bit value can overflow a u32..but a u32
+ * might not fit in the response, so we need to use s32.
+ * Which is still kind of hackish since int could be 16 bits
+ * in size. The only safe type to use here is a signed 16
+ * bit value.
+ */
+ req.nlh.nlmsg_seq = ++sequence;
+
+ /* While failing and its due to interrupts */
+
+ rc = TEMP_FAILURE_RETRY(sendto(fd, &req, req.nlh.nlmsg_len, 0,
+ (struct sockaddr*) &addr, sizeof(addr)));
+
+ /* Not all the bytes were sent */
+ if (rc < 0) {
+ rc = -errno;
+ SLOGE("Error sending data over the netlink socket: %s", strerror(-errno));
+ goto out;
+ } else if ((uint32_t) rc != req.nlh.nlmsg_len) {
+ rc = -EPROTO;
+ goto out;
+ }
+
+ /* We sent all the bytes, get the ack */
+ rc = get_ack(fd, sequence);
+
+ /* If the ack failed, return the error, else return the sequence number */
+ rc = (rc == 0) ? (int) sequence : rc;
+
+out:
+ /* Don't let sequence roll to negative */
+ if (sequence < 0) {
+ SLOGW("Auditd to Kernel sequence number has rolled over");
+ sequence = 0;
+ }
+
+ return rc;
+}
+
+int audit_setup(int fd, uint32_t pid)
+{
+ int rc;
+ struct audit_message rep;
+ struct audit_status status;
+
+ memset(&status, 0, sizeof(status));
+
+ /*
+ * In order to set the auditd PID we send an audit message over the netlink
+ * socket with the pid field of the status struct set to our current pid,
+ * and the the mask set to AUDIT_STATUS_PID
+ */
+ status.pid = pid;
+ status.mask = AUDIT_STATUS_PID | AUDIT_STATUS_RATE_LIMIT;
+ status.rate_limit = 20; // audit entries per second
+
+ /* Let the kernel know this pid will be registering for audit events */
+ rc = audit_send(fd, AUDIT_SET, &status, sizeof(status));
+ if (rc < 0) {
+ SLOGE("Could net set pid for audit events, error: %s", strerror(-rc));
+ return rc;
+ }
+
+ /*
+ * In a request where we need to wait for a response, wait for the message
+ * and discard it. This message confirms and sync's us with the kernel.
+ * This daemon is now registered as the audit logger.
+ *
+ * TODO
+ * If the daemon dies and restarts the message didn't come back,
+ * so I went to non-blocking and it seemed to fix the bug.
+ * Need to investigate further.
+ */
+ audit_get_reply(fd, &rep, GET_REPLY_NONBLOCKING, 0);
+
+ return 0;
+}
+
+int audit_open()
+{
+ return socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_AUDIT);
+}
+
+int audit_get_reply(int fd, struct audit_message *rep, reply_t block, int peek)
+{
+ ssize_t len;
+ int flags;
+ int rc = 0;
+
+ struct sockaddr_nl nladdr;
+ socklen_t nladdrlen = sizeof(nladdr);
+
+ if (fd < 0) {
+ return -EBADF;
+ }
+
+ /* Set up the flags for recv from */
+ flags = (block == GET_REPLY_NONBLOCKING) ? MSG_DONTWAIT : 0;
+ flags |= peek;
+
+ /*
+ * Get the data from the netlink socket but on error we need to be carefull,
+ * the interface shows that EINTR can never be returned, other errors,
+ * however, can be returned.
+ */
+ len = TEMP_FAILURE_RETRY(recvfrom(fd, rep, sizeof(*rep), flags,
+ (struct sockaddr*) &nladdr, &nladdrlen));
+
+ /*
+ * EAGAIN should be re-tried until success or another error manifests.
+ */
+ if (len < 0) {
+ rc = -errno;
+ if (block == GET_REPLY_NONBLOCKING && rc == -EAGAIN) {
+ /* If request is non blocking and errno is EAGAIN, just return 0 */
+ return 0;
+ }
+ SLOGE("Error receiving from netlink socket, error: %s", strerror(-rc));
+ return rc;
+ }
+
+ if (nladdrlen != sizeof(nladdr)) {
+ SLOGE("Protocol fault, error: %s", strerror(EPROTO));
+ return -EPROTO;
+ }
+
+ /* Make sure the netlink message was not spoof'd */
+ if (nladdr.nl_pid) {
+ SLOGE("Invalid netlink pid received, expected 0 got: %d", nladdr.nl_pid);
+ return -EINVAL;
+ }
+
+ /* Check if the reply from the kernel was ok */
+ if (!NLMSG_OK(&rep->nlh, (size_t)len)) {
+ rc = (len == sizeof(*rep)) ? -EFBIG : -EBADE;
+ SLOGE("Bad kernel response %s", strerror(-rc));
+ }
+
+ return rc;
+}
+
+void audit_close(int fd)
+{
+ int rc = close(fd);
+ if (rc < 0) {
+ SLOGE("Attempting to close invalid fd %d, error: %s", fd, strerror(errno));
+ }
+ return;
+}
diff --git a/logd/libaudit.h b/logd/libaudit.h
new file mode 100644
index 0000000..b9e330d
--- /dev/null
+++ b/logd/libaudit.h
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2012, Samsung Telecommunications of America
+ * Copyright (C) 2014 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.
+ *
+ * Written by William Roberts <w.roberts@sta.samsung.com>
+ */
+
+#ifndef _LIBAUDIT_H_
+#define _LIBAUDIT_H_
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <linux/netlink.h>
+#include <linux/audit.h>
+
+__BEGIN_DECLS
+
+#define MAX_AUDIT_MESSAGE_LENGTH 8970
+
+typedef enum {
+ GET_REPLY_BLOCKING=0,
+ GET_REPLY_NONBLOCKING
+} reply_t;
+
+/* type == AUDIT_SIGNAL_INFO */
+struct audit_sig_info {
+ uid_t uid;
+ pid_t pid;
+ char ctx[0];
+};
+
+struct audit_message {
+ struct nlmsghdr nlh;
+ char data[MAX_AUDIT_MESSAGE_LENGTH];
+};
+
+/**
+ * Opens a connection to the Audit netlink socket
+ * @return
+ * A valid fd on success or < 0 on error with errno set.
+ * Returns the same errors as man 2 socket.
+ */
+extern int audit_open(void);
+
+/**
+ * Closes the fd returned from audit_open()
+ * @param fd
+ * The fd to close
+ */
+extern void audit_close(int fd);
+
+/**
+ *
+ * @param fd
+ * The fd returned by a call to audit_open()
+ * @param rep
+ * The response struct to store the response in.
+ * @param block
+ * Whether or not to block on IO
+ * @param peek
+ * Whether or not we are to remove the message from
+ * the queue when we do a read on the netlink socket.
+ * @return
+ * This function returns 0 on success, else -errno.
+ */
+extern int audit_get_reply(int fd, struct audit_message *rep, reply_t block,
+ int peek);
+
+/**
+ * Sets a pid to recieve audit netlink events from the kernel
+ * @param fd
+ * The fd returned by a call to audit_open()
+ * @param pid
+ * The pid whom to set as the reciever of audit messages
+ * @return
+ * This function returns 0 on success, -errno on error.
+ */
+extern int audit_setup(int fd, uint32_t pid);
+
+__END_DECLS
+
+#endif
diff --git a/logd/main.cpp b/logd/main.cpp
new file mode 100644
index 0000000..a61beff
--- /dev/null
+++ b/logd/main.cpp
@@ -0,0 +1,333 @@
+/*
+ * Copyright (C) 2012-2013 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 <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <sched.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/capability.h>
+#include <sys/klog.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include <cutils/properties.h>
+#include <cutils/sched_policy.h>
+#include <cutils/sockets.h>
+
+#include "private/android_filesystem_config.h"
+#include "CommandListener.h"
+#include "LogBuffer.h"
+#include "LogListener.h"
+#include "LogAudit.h"
+
+#define KMSG_PRIORITY(PRI) \
+ '<', \
+ '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
+ '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
+ '>'
+
+//
+// The service is designed to be run by init, it does not respond well
+// to starting up manually. When starting up manually the sockets will
+// fail to open typically for one of the following reasons:
+// EADDRINUSE if logger is running.
+// EACCESS if started without precautions (below)
+//
+// Here is a cookbook procedure for starting up logd manually assuming
+// init is out of the way, pedantically all permissions and selinux
+// security is put back in place:
+//
+// setenforce 0
+// rm /dev/socket/logd*
+// chmod 777 /dev/socket
+// # here is where you would attach the debugger or valgrind for example
+// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
+// sleep 1
+// chmod 755 /dev/socket
+// chown logd.logd /dev/socket/logd*
+// restorecon /dev/socket/logd*
+// setenforce 1
+//
+// If minimalism prevails, typical for debugging and security is not a concern:
+//
+// setenforce 0
+// chmod 777 /dev/socket
+// logd
+//
+
+static int drop_privs() {
+ struct sched_param param;
+ memset(¶m, 0, sizeof(param));
+
+ if (set_sched_policy(0, SP_BACKGROUND) < 0) {
+ return -1;
+ }
+
+ if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) {
+ return -1;
+ }
+
+ if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
+ return -1;
+ }
+
+ if (setgid(AID_LOGD) != 0) {
+ return -1;
+ }
+
+ if (setuid(AID_LOGD) != 0) {
+ return -1;
+ }
+
+ struct __user_cap_header_struct capheader;
+ struct __user_cap_data_struct capdata[2];
+ memset(&capheader, 0, sizeof(capheader));
+ memset(&capdata, 0, sizeof(capdata));
+ capheader.version = _LINUX_CAPABILITY_VERSION_3;
+ capheader.pid = 0;
+
+ capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
+ capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
+
+ capdata[0].effective = capdata[0].permitted;
+ capdata[1].effective = capdata[1].permitted;
+ capdata[0].inheritable = 0;
+ capdata[1].inheritable = 0;
+
+ if (capset(&capheader, &capdata[0]) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+// Property helper
+static bool property_get_bool(const char *key, bool def) {
+ char property[PROPERTY_VALUE_MAX];
+ property_get(key, property, "");
+
+ if (!strcasecmp(property, "true")) {
+ return true;
+ }
+ if (!strcasecmp(property, "false")) {
+ return false;
+ }
+
+ return def;
+}
+
+// Remove the static, and use this variable
+// globally for debugging if necessary. eg:
+// write(fdDmesg, "I am here\n", 10);
+static int fdDmesg = -1;
+
+static sem_t reinit;
+static bool reinit_running = false;
+static LogBuffer *logBuf = NULL;
+
+static void *reinit_thread_start(void * /*obj*/) {
+ prctl(PR_SET_NAME, "logd.daemon");
+ set_sched_policy(0, SP_BACKGROUND);
+
+ setgid(AID_LOGD);
+ setuid(AID_LOGD);
+
+ while (reinit_running && !sem_wait(&reinit) && reinit_running) {
+ if (fdDmesg >= 0) {
+ static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
+ 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
+ ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
+ write(fdDmesg, reinit_message, sizeof(reinit_message));
+ }
+
+ // Anything that reads persist.<property>
+ if (logBuf) {
+ logBuf->init();
+ }
+ }
+
+ return NULL;
+}
+
+// Serves as a global method to trigger reinitialization
+// and as a function that can be provided to signal().
+void reinit_signal_handler(int /*signal*/) {
+ sem_post(&reinit);
+}
+
+// Foreground waits for exit of the main persistent threads
+// that are started here. The threads are created to manage
+// UNIX domain client sockets for writing, reading and
+// controlling the user space logger, and for any additional
+// logging plugins like auditd and restart control. Additional
+// transitory per-client threads are created for each reader.
+int main(int argc, char *argv[]) {
+ fdDmesg = open("/dev/kmsg", O_WRONLY);
+
+ // issue reinit command. KISS argument parsing.
+ if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
+ int sock = TEMP_FAILURE_RETRY(
+ socket_local_client("logd",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM));
+ if (sock < 0) {
+ return -errno;
+ }
+ static const char reinit[] = "reinit";
+ ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
+ if (ret < 0) {
+ return -errno;
+ }
+ struct pollfd p;
+ memset(&p, 0, sizeof(p));
+ p.fd = sock;
+ p.events = POLLIN;
+ ret = TEMP_FAILURE_RETRY(poll(&p, 1, 100));
+ if (ret < 0) {
+ return -errno;
+ }
+ if ((ret == 0) || !(p.revents & POLLIN)) {
+ return -ETIME;
+ }
+ static const char success[] = "success";
+ char buffer[sizeof(success) - 1];
+ memset(buffer, 0, sizeof(buffer));
+ ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
+ if (ret < 0) {
+ return -errno;
+ }
+ return strncmp(buffer, success, sizeof(success) - 1) != 0;
+ }
+
+ // Reinit Thread
+ sem_init(&reinit, 0, 0);
+ pthread_attr_t attr;
+ if (!pthread_attr_init(&attr)) {
+ struct sched_param param;
+
+ memset(¶m, 0, sizeof(param));
+ pthread_attr_setschedparam(&attr, ¶m);
+ pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
+ if (!pthread_attr_setdetachstate(&attr,
+ PTHREAD_CREATE_DETACHED)) {
+ pthread_t thread;
+ reinit_running = true;
+ if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
+ reinit_running = false;
+ }
+ }
+ pthread_attr_destroy(&attr);
+ }
+
+ if (drop_privs() != 0) {
+ return -1;
+ }
+
+ // Serves the purpose of managing the last logs times read on a
+ // socket connection, and as a reader lock on a range of log
+ // entries.
+
+ LastLogTimes *times = new LastLogTimes();
+
+ // LogBuffer is the object which is responsible for holding all
+ // log entries.
+
+ logBuf = new LogBuffer(times);
+
+ signal(SIGHUP, reinit_signal_handler);
+
+ {
+ char property[PROPERTY_VALUE_MAX];
+ property_get("ro.build.type", property, "");
+ if (property_get_bool("logd.statistics",
+ !!strcmp(property, "user")
+ && !property_get_bool("ro.config.low_ram", false))) {
+ logBuf->enableStatistics();
+ }
+ }
+
+ // LogReader listens on /dev/socket/logdr. When a client
+ // connects, log entries in the LogBuffer are written to the client.
+
+ LogReader *reader = new LogReader(logBuf);
+ if (reader->startListener()) {
+ exit(1);
+ }
+
+ // LogListener listens on /dev/socket/logdw for client
+ // initiated log messages. New log entries are added to LogBuffer
+ // and LogReader is notified to send updates to connected clients.
+
+ LogListener *swl = new LogListener(logBuf, reader);
+ // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
+ if (swl->startListener(300)) {
+ exit(1);
+ }
+
+ // Command listener listens on /dev/socket/logd for incoming logd
+ // administrative commands.
+
+ CommandListener *cl = new CommandListener(logBuf, reader, swl);
+ if (cl->startListener()) {
+ exit(1);
+ }
+
+ // LogAudit listens on NETLINK_AUDIT socket for selinux
+ // initiated log messages. New log entries are added to LogBuffer
+ // and LogReader is notified to send updates to connected clients.
+
+ bool auditd = property_get_bool("logd.auditd", true);
+
+ if (auditd) {
+ bool dmesg = property_get_bool("logd.auditd.dmesg", true);
+
+ // failure is an option ... messages are in dmesg (required by standard)
+ LogAudit *al = new LogAudit(logBuf, reader, dmesg ? fdDmesg : -1);
+
+ int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
+ if (len > 0) {
+ len++;
+ char buf[len];
+
+ int rc = klogctl(KLOG_READ_ALL, buf, len);
+
+ buf[len - 1] = '\0';
+
+ for(char *ptr, *tok = buf;
+ (rc >= 0) && ((tok = strtok_r(tok, "\r\n", &ptr)));
+ tok = NULL) {
+ rc = al->log(tok);
+ }
+ }
+
+ if (al->startListener()) {
+ delete al;
+ }
+ }
+
+ TEMP_FAILURE_RETRY(pause());
+
+ exit(0);
+}
diff --git a/logd/tests/Android.mk b/logd/tests/Android.mk
new file mode 100644
index 0000000..f851288
--- /dev/null
+++ b/logd/tests/Android.mk
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2014 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)
+
+# -----------------------------------------------------------------------------
+# Benchmarks. (see ../../liblog/tests)
+# -----------------------------------------------------------------------------
+
+test_module_prefix := logd-
+test_tags := tests
+
+# -----------------------------------------------------------------------------
+# Unit tests.
+# -----------------------------------------------------------------------------
+
+test_c_flags := \
+ -fstack-protector-all \
+ -g \
+ -Wall -Wextra \
+ -Werror \
+ -fno-builtin \
+
+ifneq ($(TARGET_USES_LOGD),false)
+test_c_flags += -DTARGET_USES_LOGD=1
+endif
+
+test_src_files := \
+ logd_test.cpp
+
+# Build tests for the logger. Run with:
+# adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
+include $(CLEAR_VARS)
+LOCAL_MODULE := $(test_module_prefix)unit-tests
+LOCAL_MODULE_TAGS := $(test_tags)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_CFLAGS += $(test_c_flags)
+LOCAL_SHARED_LIBRARIES := libcutils
+LOCAL_SRC_FILES := $(test_src_files)
+include $(BUILD_NATIVE_TEST)
diff --git a/logd/tests/logd_test.cpp b/logd/tests/logd_test.cpp
new file mode 100644
index 0000000..46bd9c0
--- /dev/null
+++ b/logd/tests/logd_test.cpp
@@ -0,0 +1,547 @@
+/*
+ * Copyright (C) 2014 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 <fcntl.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <gtest/gtest.h>
+
+#include "cutils/sockets.h"
+#include "log/log.h"
+#include "log/logger.h"
+
+#define __unused __attribute__((__unused__))
+
+/*
+ * returns statistics
+ */
+static void my_android_logger_get_statistics(char *buf, size_t len)
+{
+ snprintf(buf, len, "getStatistics 0 1 2 3 4");
+ int sock = socket_local_client("logd",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM);
+ if (sock >= 0) {
+ if (write(sock, buf, strlen(buf) + 1) > 0) {
+ ssize_t ret;
+ while ((ret = read(sock, buf, len)) > 0) {
+ if ((size_t)ret == len) {
+ break;
+ }
+ len -= ret;
+ buf += ret;
+
+ struct pollfd p = {
+ .fd = sock,
+ .events = POLLIN,
+ .revents = 0
+ };
+
+ ret = poll(&p, 1, 20);
+ if ((ret <= 0) || !(p.revents & POLLIN)) {
+ break;
+ }
+ }
+ }
+ close(sock);
+ }
+}
+
+static void alloc_statistics(char **buffer, size_t *length)
+{
+ size_t len = 8192;
+ char *buf;
+
+ for(int retry = 32; (retry >= 0); delete [] buf, --retry) {
+ buf = new char [len];
+ my_android_logger_get_statistics(buf, len);
+
+ buf[len-1] = '\0';
+ size_t ret = atol(buf) + 1;
+ if (ret < 4) {
+ delete [] buf;
+ buf = NULL;
+ break;
+ }
+ bool check = ret <= len;
+ len = ret;
+ if (check) {
+ break;
+ }
+ len += len / 8; // allow for some slop
+ }
+ *buffer = buf;
+ *length = len;
+}
+
+static char *find_benchmark_spam(char *cp)
+{
+ // liblog_benchmarks has been run designed to SPAM. The signature of
+ // a noisiest UID statistics is one of the following:
+ //
+ // main: UID/PID Total size/num Now UID/PID[?] Total
+ // 0 7500306/304207 71608/3183 0/4225? 7454388/303656
+ // <wrap> 93432/1012
+ // -or-
+ // 0/gone 7454388/303656 93432/1012
+ //
+ // basically if we see a *large* number of 0/????? entries
+ unsigned long value;
+ do {
+ char *benchmark = strstr(cp, " 0/");
+ char *benchmark_newline = strstr(cp, "\n0/");
+ if (!benchmark) {
+ benchmark = benchmark_newline;
+ }
+ if (benchmark_newline && (benchmark > benchmark_newline)) {
+ benchmark = benchmark_newline;
+ }
+ cp = benchmark;
+ if (!cp) {
+ break;
+ }
+ cp += 3;
+ while (isdigit(*cp) || (*cp == 'g') || (*cp == 'o') || (*cp == 'n')) {
+ ++cp;
+ }
+ value = 0;
+ // ###? or gone
+ if ((*cp == '?') || (*cp == 'e')) {
+ while (*++cp == ' ');
+ while (isdigit(*cp)) {
+ value = value * 10ULL + *cp - '0';
+ ++cp;
+ }
+ if (*cp != '/') {
+ value = 0;
+ continue;
+ }
+ while (isdigit(*++cp));
+ while (*cp == ' ') ++cp;
+ if (!isdigit(*cp)) {
+ value = 0;
+ }
+ }
+ } while ((value < 900000ULL) && *cp);
+ return cp;
+}
+
+TEST(logd, statistics) {
+ size_t len;
+ char *buf;
+
+ alloc_statistics(&buf, &len);
+
+#ifdef TARGET_USES_LOGD
+ ASSERT_TRUE(NULL != buf);
+#else
+ if (!buf) {
+ return;
+ }
+#endif
+
+ // remove trailing FF
+ char *cp = buf + len - 1;
+ *cp = '\0';
+ bool truncated = *--cp != '\f';
+ if (!truncated) {
+ *cp = '\0';
+ }
+
+ // squash out the byte count
+ cp = buf;
+ if (!truncated) {
+ while (isdigit(*cp) || (*cp == '\n')) {
+ ++cp;
+ }
+ }
+
+ fprintf(stderr, "%s", cp);
+
+ EXPECT_LT((size_t)64, strlen(cp));
+
+ EXPECT_EQ(0, truncated);
+
+#ifdef TARGET_USES_LOGD
+ char *main_logs = strstr(cp, "\nmain:");
+ EXPECT_TRUE(NULL != main_logs);
+
+ char *radio_logs = strstr(cp, "\nradio:");
+ EXPECT_TRUE(NULL != radio_logs);
+
+ char *system_logs = strstr(cp, "\nsystem:");
+ EXPECT_TRUE(NULL != system_logs);
+
+ char *events_logs = strstr(cp, "\nevents:");
+ EXPECT_TRUE(NULL != events_logs);
+#endif
+
+ delete [] buf;
+}
+
+static void caught_signal(int signum __unused) { }
+
+static void dump_log_msg(const char *prefix,
+ log_msg *msg, unsigned int version, int lid) {
+ switch(msg->entry.hdr_size) {
+ case 0:
+ version = 1;
+ break;
+
+ case sizeof(msg->entry_v2):
+ if (version == 0) {
+ version = 2;
+ }
+ break;
+ }
+
+ fprintf(stderr, "%s: v%u[%u] ", prefix, version, msg->len());
+ if (version != 1) {
+ fprintf(stderr, "hdr_size=%u ", msg->entry.hdr_size);
+ }
+ fprintf(stderr, "pid=%u tid=%u %u.%09u ",
+ msg->entry.pid, msg->entry.tid, msg->entry.sec, msg->entry.nsec);
+ switch(version) {
+ case 1:
+ break;
+ case 2:
+ fprintf(stderr, "euid=%u ", msg->entry_v2.euid);
+ break;
+ case 3:
+ default:
+ lid = msg->entry.lid;
+ break;
+ }
+
+ switch(lid) {
+ case 0:
+ fprintf(stderr, "lid=main ");
+ break;
+ case 1:
+ fprintf(stderr, "lid=radio ");
+ break;
+ case 2:
+ fprintf(stderr, "lid=events ");
+ break;
+ case 3:
+ fprintf(stderr, "lid=system ");
+ break;
+ default:
+ if (lid >= 0) {
+ fprintf(stderr, "lid=%d ", lid);
+ }
+ }
+
+ unsigned int len = msg->entry.len;
+ fprintf(stderr, "msg[%u]={", len);
+ unsigned char *cp = reinterpret_cast<unsigned char *>(msg->msg());
+ while(len) {
+ unsigned char *p = cp;
+ while (*p && (((' ' <= *p) && (*p < 0x7F)) || (*p == '\n'))) {
+ ++p;
+ }
+ if (((p - cp) > 3) && !*p && ((unsigned int)(p - cp) < len)) {
+ fprintf(stderr, "\"");
+ while (*cp) {
+ if (*cp != '\n') {
+ fprintf(stderr, "%c", *cp);
+ } else {
+ fprintf(stderr, "\\n");
+ }
+ ++cp;
+ --len;
+ }
+ fprintf(stderr, "\"");
+ } else {
+ fprintf(stderr, "%02x", *cp);
+ }
+ ++cp;
+ if (--len) {
+ fprintf(stderr, ", ");
+ }
+ }
+ fprintf(stderr, "}\n");
+}
+
+TEST(logd, both) {
+ log_msg msg;
+
+ // check if we can read any logs from logd
+ bool user_logger_available = false;
+ bool user_logger_content = false;
+
+ int fd = socket_local_client("logdr",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_SEQPACKET);
+ if (fd >= 0) {
+ struct sigaction ignore, old_sigaction;
+ memset(&ignore, 0, sizeof(ignore));
+ ignore.sa_handler = caught_signal;
+ sigemptyset(&ignore.sa_mask);
+ sigaction(SIGALRM, &ignore, &old_sigaction);
+ unsigned int old_alarm = alarm(10);
+
+ static const char ask[] = "dumpAndClose lids=0,1,2,3";
+ user_logger_available = write(fd, ask, sizeof(ask)) == sizeof(ask);
+
+ user_logger_content = recv(fd, msg.buf, sizeof(msg), 0) > 0;
+
+ if (user_logger_content) {
+ dump_log_msg("user", &msg, 3, -1);
+ }
+
+ alarm(old_alarm);
+ sigaction(SIGALRM, &old_sigaction, NULL);
+
+ close(fd);
+ }
+
+ // check if we can read any logs from kernel logger
+ bool kernel_logger_available = false;
+ bool kernel_logger_content = false;
+
+ static const char *loggers[] = {
+ "/dev/log/main", "/dev/log_main",
+ "/dev/log/radio", "/dev/log_radio",
+ "/dev/log/events", "/dev/log_events",
+ "/dev/log/system", "/dev/log_system",
+ };
+
+ for (unsigned int i = 0; i < (sizeof(loggers) / sizeof(loggers[0])); ++i) {
+ fd = open(loggers[i], O_RDONLY);
+ if (fd < 0) {
+ continue;
+ }
+ kernel_logger_available = true;
+ fcntl(fd, F_SETFL, O_RDONLY | O_NONBLOCK);
+ int result = TEMP_FAILURE_RETRY(read(fd, msg.buf, sizeof(msg)));
+ if (result > 0) {
+ kernel_logger_content = true;
+ dump_log_msg("kernel", &msg, 0, i / 2);
+ }
+ close(fd);
+ }
+
+ static const char yes[] = "\xE2\x9C\x93";
+ static const char no[] = "\xE2\x9c\x98";
+ fprintf(stderr,
+ "LOGGER Available Content\n"
+ "user %-13s%s\n"
+ "kernel %-13s%s\n"
+ " status %-11s%s\n",
+ (user_logger_available) ? yes : no,
+ (user_logger_content) ? yes : no,
+ (kernel_logger_available) ? yes : no,
+ (kernel_logger_content) ? yes : no,
+ (user_logger_available && kernel_logger_available) ? "ERROR" : "ok",
+ (user_logger_content && kernel_logger_content) ? "ERROR" : "ok");
+
+ EXPECT_EQ(0, user_logger_available && kernel_logger_available);
+ EXPECT_EQ(0, !user_logger_available && !kernel_logger_available);
+ EXPECT_EQ(0, user_logger_content && kernel_logger_content);
+ EXPECT_EQ(0, !user_logger_content && !kernel_logger_content);
+}
+
+// BAD ROBOT
+// Benchmark threshold are generally considered bad form unless there is
+// is some human love applied to the continued maintenance and whether the
+// thresholds are tuned on a per-target basis. Here we check if the values
+// are more than double what is expected. Doubling will not prevent failure
+// on busy or low-end systems that could have a tendency to stretch values.
+//
+// The primary goal of this test is to simulate a spammy app (benchmark
+// being the worst) and check to make sure the logger can deal with it
+// appropriately by checking all the statistics are in an expected range.
+//
+TEST(logd, benchmark) {
+ size_t len;
+ char *buf;
+
+ alloc_statistics(&buf, &len);
+ bool benchmark_already_run = buf && find_benchmark_spam(buf);
+ delete [] buf;
+
+ if (benchmark_already_run) {
+ fprintf(stderr, "WARNING: spam already present and too much history\n"
+ " false OK for prune by worst UID check\n");
+ }
+
+ FILE *fp;
+
+ // Introduce some extreme spam for the worst UID filter
+ ASSERT_TRUE(NULL != (fp = popen(
+ "/data/nativetest/liblog-benchmarks/liblog-benchmarks",
+ "r")));
+
+ char buffer[5120];
+
+ static const char *benchmarks[] = {
+ "BM_log_maximum_retry ",
+ "BM_log_maximum ",
+ "BM_clock_overhead ",
+ "BM_log_overhead ",
+ "BM_log_latency ",
+ "BM_log_delay "
+ };
+ static const unsigned int log_maximum_retry = 0;
+ static const unsigned int log_maximum = 1;
+ static const unsigned int clock_overhead = 2;
+ static const unsigned int log_overhead = 3;
+ static const unsigned int log_latency = 4;
+ static const unsigned int log_delay = 5;
+
+ unsigned long ns[sizeof(benchmarks) / sizeof(benchmarks[0])];
+
+ memset(ns, 0, sizeof(ns));
+
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ for (unsigned i = 0; i < sizeof(ns) / sizeof(ns[0]); ++i) {
+ char *cp = strstr(buffer, benchmarks[i]);
+ if (!cp) {
+ continue;
+ }
+ sscanf(cp, "%*s %lu %lu", &ns[i], &ns[i]);
+ fprintf(stderr, "%-22s%8lu\n", benchmarks[i], ns[i]);
+ }
+ }
+ int ret = pclose(fp);
+
+ if (!WIFEXITED(ret) || (WEXITSTATUS(ret) == 127)) {
+ fprintf(stderr,
+ "WARNING: "
+ "/data/nativetest/liblog-benchmarks/liblog-benchmarks missing\n"
+ " can not perform test\n");
+ return;
+ }
+
+#ifdef TARGET_USES_LOGD
+ EXPECT_GE(100000UL, ns[log_maximum_retry]); // 42777 user
+#else
+ EXPECT_GE(10000UL, ns[log_maximum_retry]); // 5636 kernel
+#endif
+
+#ifdef TARGET_USES_LOGD
+ EXPECT_GE(30000UL, ns[log_maximum]); // 27305 user
+#else
+ EXPECT_GE(10000UL, ns[log_maximum]); // 5637 kernel
+#endif
+
+ EXPECT_GE(4096UL, ns[clock_overhead]); // 4095
+
+#ifdef TARGET_USES_LOGD
+ EXPECT_GE(250000UL, ns[log_overhead]); // 121876 user
+#else
+ EXPECT_GE(100000UL, ns[log_overhead]); // 50945 kernel
+#endif
+
+#ifdef TARGET_USES_LOGD
+ EXPECT_GE(7500UL, ns[log_latency]); // 3718 user space
+#else
+ EXPECT_GE(500000UL, ns[log_latency]); // 254200 kernel
+#endif
+
+#ifdef TARGET_USES_LOGD
+ EXPECT_GE(20000000UL, ns[log_delay]); // 10500289 user
+#else
+ EXPECT_GE(55000UL, ns[log_delay]); // 27341 kernel
+#endif
+
+ for (unsigned i = 0; i < sizeof(ns) / sizeof(ns[0]); ++i) {
+ EXPECT_NE(0UL, ns[i]);
+ }
+
+ alloc_statistics(&buf, &len);
+
+#ifdef TARGET_USES_LOGD
+ bool collected_statistics = !!buf;
+ EXPECT_EQ(true, collected_statistics);
+#else
+ if (!buf) {
+ return;
+ }
+#endif
+
+ ASSERT_TRUE(NULL != buf);
+
+ char *benchmark_statistics_found = find_benchmark_spam(buf);
+ ASSERT_TRUE(benchmark_statistics_found != NULL);
+
+ // Check how effective the SPAM filter is, parse out Now size.
+ // Total Now
+ // 0/4225? 7454388/303656 31488/755
+ // ^-- benchmark_statistics_found
+
+ unsigned long nowSpamSize = atol(benchmark_statistics_found);
+
+ delete [] buf;
+
+ ASSERT_NE(0UL, nowSpamSize);
+
+ // Determine if we have the spam filter enabled
+ int sock = socket_local_client("logd",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM);
+
+ ASSERT_TRUE(sock >= 0);
+
+ static const char getPruneList[] = "getPruneList";
+ if (write(sock, getPruneList, sizeof(getPruneList)) > 0) {
+ char buffer[80];
+ memset(buffer, 0, sizeof(buffer));
+ read(sock, buffer, sizeof(buffer));
+ char *cp = strchr(buffer, '\n');
+ if (!cp || (cp[1] != '~') || (cp[2] != '!')) {
+ close(sock);
+ fprintf(stderr,
+ "WARNING: "
+ "Logger has SPAM filtration turned off \"%s\"\n", buffer);
+ return;
+ }
+ } else {
+ int save_errno = errno;
+ close(sock);
+ FAIL() << "Can not send " << getPruneList << " to logger -- " << strerror(save_errno);
+ }
+
+ static const unsigned long expected_absolute_minimum_log_size = 65536UL;
+ unsigned long totalSize = expected_absolute_minimum_log_size;
+ static const char getSize[] = {
+ 'g', 'e', 't', 'L', 'o', 'g', 'S', 'i', 'z', 'e', ' ',
+ LOG_ID_MAIN + '0', '\0'
+ };
+ if (write(sock, getSize, sizeof(getSize)) > 0) {
+ char buffer[80];
+ memset(buffer, 0, sizeof(buffer));
+ read(sock, buffer, sizeof(buffer));
+ totalSize = atol(buffer);
+ if (totalSize < expected_absolute_minimum_log_size) {
+ fprintf(stderr,
+ "WARNING: "
+ "Logger had unexpected referenced size \"%s\"\n", buffer);
+ totalSize = expected_absolute_minimum_log_size;
+ }
+ }
+ close(sock);
+
+ // logd allows excursions to 110% of total size
+ totalSize = (totalSize * 11 ) / 10;
+
+ // 50% threshold for SPAM filter (<20% typical, lots of engineering margin)
+ ASSERT_GT(totalSize, nowSpamSize * 2);
+}
diff --git a/logwrapper/Android.mk b/logwrapper/Android.mk
index 917bf37..61b4659 100644
--- a/logwrapper/Android.mk
+++ b/logwrapper/Android.mk
@@ -11,6 +11,7 @@
LOCAL_SHARED_LIBRARIES := libcutils liblog
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
# ========================================================
@@ -22,6 +23,7 @@
LOCAL_WHOLE_STATIC_LIBRARIES := liblogwrap
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
# ========================================================
@@ -31,4 +33,5 @@
LOCAL_SRC_FILES:= logwrapper.c
LOCAL_MODULE := logwrapper
LOCAL_STATIC_LIBRARIES := liblog liblogwrap libcutils
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index 4ca1db4..83576fb 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -104,8 +104,6 @@
static int add_line_to_linear_buf(struct beginning_buf *b_buf,
char *line, ssize_t line_len)
{
- size_t new_len;
- char *new_buf;
int full = 0;
if ((line_len + b_buf->used_len) > b_buf->buf_size) {
@@ -124,7 +122,6 @@
{
ssize_t free_len;
ssize_t needed_space;
- char *new_buf;
int cnt;
if (e_buf->buf == NULL) {
@@ -192,7 +189,6 @@
{
char *line_start;
char c;
- int line_len;
int i;
line_start = buf;
@@ -329,7 +325,7 @@
if (log_target & LOG_KLOG) {
snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
- "<6>%.*s: %%s", MAX_KLOG_TAG, log_info.btag);
+ "<6>%.*s: %%s\n", MAX_KLOG_TAG, log_info.btag);
}
if ((log_target & LOG_FILE) && !file_path) {
@@ -481,7 +477,6 @@
pid_t pid;
int parent_ptty;
int child_ptty;
- char *child_devname = NULL;
struct sigaction intact;
struct sigaction quitact;
sigset_t blockset;
@@ -502,8 +497,9 @@
goto err_open;
}
+ char child_devname[64];
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
- ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
+ ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
ERROR("Problem with /dev/ptmx\n");
rc = -1;
goto err_ptty;
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
index 2a97c26..0c9b0c6 100644
--- a/mkbootimg/Android.mk
+++ b/mkbootimg/Android.mk
@@ -4,6 +4,7 @@
LOCAL_SRC_FILES := mkbootimg.c
LOCAL_STATIC_LIBRARIES := libmincrypt
+LOCAL_CFLAGS := -Werror
LOCAL_MODULE := mkbootimg
diff --git a/mkbootimg/mkbootimg.c b/mkbootimg/mkbootimg.c
index d598f03..fc92b4d 100644
--- a/mkbootimg/mkbootimg.c
+++ b/mkbootimg/mkbootimg.c
@@ -77,7 +77,7 @@
int write_padding(int fd, unsigned pagesize, unsigned itemsize)
{
unsigned pagemask = pagesize - 1;
- unsigned count;
+ ssize_t count;
if((itemsize & pagemask) == 0) {
return 0;
@@ -108,7 +108,7 @@
unsigned pagesize = 2048;
int fd;
SHA_CTX ctx;
- uint8_t* sha;
+ const uint8_t* sha;
unsigned base = 0x10000000;
unsigned kernel_offset = 0x00008000;
unsigned ramdisk_offset = 0x01000000;
@@ -189,7 +189,7 @@
return usage();
}
- strcpy(hdr.name, board);
+ strcpy((char *) hdr.name, board);
memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
@@ -255,15 +255,15 @@
if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
- if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
+ if(write(fd, kernel_data, hdr.kernel_size) != (ssize_t) hdr.kernel_size) goto fail;
if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
- if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
+ if(write(fd, ramdisk_data, hdr.ramdisk_size) != (ssize_t) hdr.ramdisk_size) goto fail;
if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
if(second_data) {
- if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
- if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
+ if(write(fd, second_data, hdr.second_size) != (ssize_t) hdr.second_size) goto fail;
+ if(write_padding(fd, pagesize, hdr.second_size)) goto fail;
}
return 0;
diff --git a/netcfg/Android.mk b/netcfg/Android.mk
deleted file mode 100644
index 949f417..0000000
--- a/netcfg/Android.mk
+++ /dev/null
@@ -1,16 +0,0 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= netcfg.c
-LOCAL_MODULE:= netcfg
-
-#LOCAL_FORCE_STATIC_EXECUTABLE := true
-#LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
-#LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
-#LOCAL_STATIC_LIBRARIES := libcutils libc
-
-LOCAL_SHARED_LIBRARIES := libc libnetutils
-
-include $(BUILD_EXECUTABLE)
-endif
diff --git a/netcfg/MODULE_LICENSE_APACHE2 b/netcfg/MODULE_LICENSE_APACHE2
deleted file mode 100644
index e69de29..0000000
--- a/netcfg/MODULE_LICENSE_APACHE2
+++ /dev/null
diff --git a/netcfg/NOTICE b/netcfg/NOTICE
deleted file mode 100644
index c5b1efa..0000000
--- a/netcfg/NOTICE
+++ /dev/null
@@ -1,190 +0,0 @@
-
- Copyright (c) 2005-2008, 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.
-
- 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.
-
-
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
diff --git a/netcfg/netcfg.c b/netcfg/netcfg.c
deleted file mode 100644
index 3738f24..0000000
--- a/netcfg/netcfg.c
+++ /dev/null
@@ -1,188 +0,0 @@
-/* system/bin/netcfg/netcfg.c
-**
-** Copyright 2006, 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 <errno.h>
-#include <dirent.h>
-#include <netinet/ether.h>
-#include <netinet/if_ether.h>
-
-#include <netutils/ifc.h>
-#include <netutils/dhcp.h>
-
-static int verbose = 0;
-
-
-void die(const char *reason)
-{
- perror(reason);
- exit(1);
-}
-
-const char *ipaddr(in_addr_t addr)
-{
- struct in_addr in_addr;
-
- in_addr.s_addr = addr;
- return inet_ntoa(in_addr);
-}
-
-void usage(void)
-{
- fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
- exit(1);
-}
-
-int dump_interface(const char *name)
-{
- unsigned addr, flags;
- unsigned char hwbuf[ETH_ALEN];
- int prefixLength;
-
- if(ifc_get_info(name, &addr, &prefixLength, &flags)) {
- return 0;
- }
-
- printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN");
- printf("%40s", ipaddr(addr));
- printf("/%-4d", prefixLength);
- printf("0x%08x ", flags);
- if (!ifc_get_hwaddr(name, hwbuf)) {
- int i;
- for(i=0; i < (ETH_ALEN-1); i++)
- printf("%02x:", hwbuf[i]);
- printf("%02x\n", hwbuf[i]);
- } else {
- printf("\n");
- }
- return 0;
-}
-
-int dump_interfaces(void)
-{
- DIR *d;
- struct dirent *de;
-
- d = opendir("/sys/class/net");
- if(d == 0) return -1;
-
- while((de = readdir(d))) {
- if(de->d_name[0] == '.') continue;
- dump_interface(de->d_name);
- }
- closedir(d);
- return 0;
-}
-
-int set_hwaddr(const char *name, const char *asc) {
- struct ether_addr *addr = ether_aton(asc);
- if (!addr) {
- printf("Failed to parse '%s'\n", asc);
- return -1;
- }
- return ifc_set_hwaddr(name, addr->ether_addr_octet);
-}
-
-struct
-{
- const char *name;
- int nargs;
- void *func;
-} CMDS[] = {
- { "dhcp", 1, do_dhcp },
- { "up", 1, ifc_up },
- { "down", 1, ifc_down },
- { "flhosts", 1, ifc_remove_host_routes },
- { "deldefault", 1, ifc_remove_default_route },
- { "hwaddr", 2, set_hwaddr },
- { 0, 0, 0 },
-};
-
-static int call_func(void *_func, unsigned nargs, char **args)
-{
- switch(nargs){
- case 1: {
- int (*func)(char *a0) = _func;
- return func(args[0]);
- }
- case 2: {
- int (*func)(char *a0, char *a1) = _func;
- return func(args[0], args[1]);
- }
- case 3: {
- int (*func)(char *a0, char *a1, char *a2) = _func;
- return func(args[0], args[1], args[2]);
- }
- default:
- return -1;
- }
-}
-
-int main(int argc, char **argv)
-{
- char *iname;
- int n;
-
- if(ifc_init()) {
- die("Cannot perform requested operation");
- }
-
- if(argc == 1) {
- int result = dump_interfaces();
- ifc_close();
- return result;
- }
-
- if(argc < 3) usage();
-
- iname = argv[1];
- if(strlen(iname) > 16) usage();
-
- argc -= 2;
- argv += 2;
- while(argc > 0) {
- for(n = 0; CMDS[n].name; n++){
- if(!strcmp(argv[0], CMDS[n].name)) {
- char *cmdname = argv[0];
- int nargs = CMDS[n].nargs;
-
- argv[0] = iname;
- if(argc < nargs) {
- fprintf(stderr, "not enough arguments for '%s'\n", cmdname);
- ifc_close();
- exit(1);
- }
- if(call_func(CMDS[n].func, nargs, argv)) {
- fprintf(stderr, "action '%s' failed (%s)\n", cmdname, strerror(errno));
- ifc_close();
- exit(1);
- }
- argc -= nargs;
- argv += nargs;
- goto done;
- }
- }
- fprintf(stderr,"no such action '%s'\n", argv[0]);
- usage();
- done:
- ;
- }
- ifc_close();
-
- return 0;
-}
diff --git a/reboot/Android.mk b/reboot/Android.mk
index 4db0c1e..7a24f99 100644
--- a/reboot/Android.mk
+++ b/reboot/Android.mk
@@ -1,12 +1,14 @@
# Copyright 2013 The Android Open Source Project
-LOCAL_PATH:= $(call my-dir)
+LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= reboot.c
+LOCAL_SRC_FILES := reboot.c
-LOCAL_SHARED_LIBRARIES:= libcutils
+LOCAL_SHARED_LIBRARIES := libcutils
-LOCAL_MODULE:= reboot
+LOCAL_MODULE := reboot
+
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
diff --git a/reboot/reboot.c b/reboot/reboot.c
index d9a4227..007dfba 100644
--- a/reboot/reboot.c
+++ b/reboot/reboot.c
@@ -35,7 +35,7 @@
c = getopt(argc, argv, "p");
- if (c == EOF) {
+ if (c == -1) {
break;
}
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 2c16084..3ecb1db 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -30,9 +30,18 @@
include $(BUILD_SYSTEM)/base_rules.mk
-$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/init.environ.rc.in
+# Regenerate init.environ.rc if PRODUCT_BOOTCLASSPATH has changed.
+bcp_md5 := $(word 1, $(shell echo $(PRODUCT_BOOTCLASSPATH) $(PRODUCT_SYSTEM_SERVER_CLASSPATH) | $(MD5SUM)))
+bcp_dep := $(intermediates)/$(bcp_md5).bcp.dep
+$(bcp_dep) :
+ $(hide) mkdir -p $(dir $@) && rm -rf $(dir $@)*.bcp.dep && touch $@
+
+$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/init.environ.rc.in $(bcp_dep)
@echo "Generate: $< -> $@"
@mkdir -p $(dir $@)
$(hide) sed -e 's?%BOOTCLASSPATH%?$(PRODUCT_BOOTCLASSPATH)?g' $< >$@
+ $(hide) sed -i -e 's?%SYSTEMSERVERCLASSPATH%?$(PRODUCT_SYSTEM_SERVER_CLASSPATH)?g' $@
+bcp_md5 :=
+bcp_dep :=
#######################################
diff --git a/rootdir/etc/hosts b/rootdir/etc/hosts
index 99848f6..649151c 100644
--- a/rootdir/etc/hosts
+++ b/rootdir/etc/hosts
@@ -1 +1,2 @@
-127.0.0.1 localhost
+127.0.0.1 localhost
+::1 ip6-localhost
diff --git a/rootdir/etc/init.testmenu b/rootdir/etc/init.testmenu
deleted file mode 100755
index 7ae16d5..0000000
--- a/rootdir/etc/init.testmenu
+++ /dev/null
@@ -1,322 +0,0 @@
-#!/system/bin/sh
-
-atdev=/dev/omap_csmi_tty0
-pppdev=/dev/omap_csmi_tty1
-
-n1=`cat /data/phoneentry1 2>/dev/null`
-n2=`cat /data/phoneentry2 2>/dev/null`
-n3=`cat /data/phoneentry3 2>/dev/null`
-n1=${n1:-"*#06#"}
-n2=${n2:-"*#06#"}
-n3=${n3:-"*#06#"}
-phoneoutputpid=
-eventoutputpid=
-notifypid=
-notifytoggle=false
-pppdpid=
-powerdidletime=120
-
-# map phone specific keys
-setkey -k 0xe4 -v 0x23 # map #
-setkey -k 0xe3 -v 0x2a # map *
-setkey -k 231 -v 513 # map send to newline
-#setkey -k 0x67 -v 0x20b # map up to scroll back
-#setkey -k 0x6c -v 0x20a # map down to scroll forward
-setkey -k 0x73 -v 0x20b # map volume up to scroll back
-setkey -k 0x72 -v 0x20a # map volume down to scroll forward
-setkey -k 0x60 -v 0x211 # map PoC to next console
-
-# tuttle keys
-setkey -k 0x38 -v 0x703 # map leftalt to alt
-setkey -k 0x9b -v 0x703 # map mail to alt
-setkey -t 8 -k 0x9b -v 0x703 # map alt-mail to alt
-setkey -t 8 -k 0x10 -v 0x21 # map alt-q to !
-setkey -t 8 -k 0x11 -v 0x31 # map alt-w to 1
-setkey -t 8 -k 0x12 -v 0x32 # map alt-e to 2
-setkey -t 8 -k 0x13 -v 0x33 # map alt-r to 3
-setkey -t 8 -k 0x14 -v 0x2b # map alt-t to +
-setkey -t 8 -k 0x15 -v 0x28 # map alt-y to (
-setkey -t 8 -k 0x16 -v 0x29 # map alt-u to )
-setkey -t 8 -k 0x17 -v 0x2d # map alt-i to -
-setkey -t 8 -k 0x18 -v 0x5f # map alt-o to _
-setkey -t 8 -k 0x19 -v 0x22 # map alt-p to "
-setkey -t 8 -k 0x1e -v 0x23 # map alt-a to #
-setkey -t 8 -k 0x1f -v 0x34 # map alt-s to 4
-setkey -t 8 -k 0x20 -v 0x35 # map alt-d to 5
-setkey -t 8 -k 0x21 -v 0x36 # map alt-f to 6
-setkey -t 8 -k 0x22 -v 0x2f # map alt-g to /
-setkey -t 8 -k 0x23 -v 0x3f # map alt-h to ?
-setkey -t 8 -k 0x24 -v 0xa3 # map alt-j to pound
-setkey -t 8 -k 0x25 -v 0x24 # map alt-k to $
-setkey -t 8 -k 0x2c -v 0x2a # map alt-z to *
-setkey -t 8 -k 0x2d -v 0x37 # map alt-x to 7
-setkey -t 8 -k 0x2e -v 0x38 # map alt-c to 8
-setkey -t 8 -k 0x2f -v 0x39 # map alt-v to 9
-setkey -t 8 -k 0x30 -v 0x7c # map alt-b to |
-setkey -t 8 -k 0x31 -v 0x40 # map alt-n to @
-setkey -t 8 -k 0x32 -v 0x3d # map alt-m to =
-setkey -t 8 -k 0x33 -v 0x3b # map alt-, to ;
-setkey -t 8 -k 0x34 -v 0x3a # map alt-. to :
-setkey -t 8 -k 0x0f -v 0x30 # map alt-tab to 0
-setkey -t 8 -k 0x67 -v 0x20b # map alt-up to scroll back
-setkey -t 8 -k 0x6c -v 0x20a # map alt-down to scroll forward
-
-while true
-do
- echo
- echo "------------------------------"
- echo " 1: init commands"
- echo " 2: call commands"
- echo " 3: misc phone"
- echo " 4: phone debug output"
- echo " 5: test data connection"
- echo " 6: start runtime"
- echo " 7: start runtime w/output"
- echo " 8: stop runtime"
- echo " 9: misc"
- echo -n ": "
- while true
- do
- c=`readtty -t 50 -f -a 1234567890#`
- case "$c" in
- "" ) ;;
- * ) break;
- esac
- done
- echo Got key -$c-
- case $c in
- "1" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Print phone output"
- echo " 2: ATQ0V1E1+CMEE=2;+CREG=0"
- echo " 3: AT+CFUN=1"
- echo " 4: AT+COPS=0"
- echo " 5: AT+CREG?"
- echo " 6: Stop phone output"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 1234560#`
- echo Got key -$c-
- case "$c" in
- "1" ) kill $phoneoutputpid; cat $atdev & phoneoutputpid=$! ;;
- "2" ) echo -e "ATQ0V1E1+CMEE=2;+CREG=0\r" >$atdev;;
- "3" ) echo -e "AT+CFUN=1\r" >$atdev;;
- "4" ) echo -e "AT+COPS=0\r" >$atdev;;
- "5" ) echo -e "AT+CREG?\r" >$atdev;;
- "6" ) kill $phoneoutputpid; phoneoutputpid= ;;
- "0" ) break;;
- esac
- done
- ;;
- "2" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Dial: ATD $n1;"
- echo " 2: Dial: ATD $n2;"
- echo " 3: Dial: ATD $n3;"
- echo " 4: Set number for 1"
- echo " 5: Set number for 2"
- echo " 6: Set number for 3"
- echo " 7: Dial: ATD ...;"
- echo " 8: Hang up: ATH"
- echo " 9: Answer: ATA"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 1234567890#`
- echo Got key -$c-
- case "$c" in
- "1" ) echo "Dialing $n1"; echo -e "ATD $n1;\r" >$atdev;;
- "2" ) echo "Dialing $n2"; echo -e "ATD $n2;\r" >$atdev;;
- "3" ) echo "Dialing $n3"; echo -e "ATD $n3;\r" >$atdev;;
- "4" ) echo -n "Number: "; read n1; echo $n1 >/data/phoneentry1;;
- "5" ) echo -n "Number: "; read n2; echo $n2 >/data/phoneentry2;;
- "6" ) echo -n "Number: "; read n3; echo $n3 >/data/phoneentry3;;
- "7" ) echo -n "Number: "; read n; echo "Dialing $n"; echo -e "ATD $n;\r" >$atdev;;
- "8" ) echo -e "ATH\r" >$atdev;;
- "9" ) echo -e "ATA\r" >$atdev;;
- "0" ) break;;
- esac
- done
- ;;
- "3" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Save FFS data"
- echo " 2: Load user FFS data"
- echo " 3: Load system FFS data"
- echo " 4: Reset FFS data"
- echo " 5: Set uplink gain"
- echo " 6: Set echo"
- echo " 7: cat /dev/omap_csmi_battery_t"
- echo " 8: cat /dev/omap_csmi_htc"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 123456780#`
- echo Got key -$c-
- case "$c" in
- "1" ) cat /dev/omap_csmi_ffs >/data/ffsdata;;
- "2" ) cat /data/ffsdata >/dev/omap_csmi_ffs;;
- "3" ) cat /system/ffsdata >/dev/omap_csmi_ffs;;
- "4" ) echo - >/dev/omap_csmi_ffs;;
- "5" )
- echo -n "Gain: "; read g;
- echo gu$g >/tmp/gain;
- cat /tmp/gain 2>/dev/null >/dev/omap_csmi_audio_tes
- ;;
- "6" )
- echo -n "Echo param (hex): "; read e;
- echo "e0x$e" >/tmp/echo;
- cat /tmp/echo 2>/dev/null >/dev/omap_csmi_audio_tes
- ;;
- "7" ) cat /dev/omap_csmi_battery_t;;
- "8" ) cat /dev/omap_csmi_htc;;
- "0" ) break;;
- esac
- done
- ;;
- "4" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Toggle debug I/O"
- echo " 2: Toggle debug Flow"
- echo " 3: Toggle debug Interrupt"
- echo " 4: Toggle debug Info"
- echo " 5: Toggle GSM run state"
- echo " 6: Clear GSM data area"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 1234560#`
- echo Got key -$c-
- case "$c" in
- "1" ) echo -n "i" >/sys/devices/system/omap_csmi/debug;;
- "2" ) echo -n "f" >/sys/devices/system/omap_csmi/debug;;
- "3" ) echo -n "I" >/sys/devices/system/omap_csmi/debug;;
- "4" ) echo -n "F" >/sys/devices/system/omap_csmi/debug;;
- "5" ) echo -n "s" >/sys/devices/system/omap_csmi/debug;;
- "6" ) echo -n "c" >/sys/devices/system/omap_csmi/debug;;
- "0" ) break;;
- esac
- done
- ;;
- "5" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Start pppd - userspace"
- echo " 2: Start pppd - kernel"
- echo " 3: Start pppd - kernel <at1"
- echo " 4: Configure ppp data to at2"
- echo " 5: Test with HTTP GET"
- echo " 6: Kill pppd"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 1234560#`
- echo Got key -$c-
- case "$c" in
- "1" ) kill $pppdpid; pppd notty < $pppdev > $pppdev & pppdpid=$!;;
- "2" ) kill $pppdpid; pppd nodetach $pppdev & pppdpid=$!;;
- "3" ) kill &pppdpid; pppd nodetach $pppdev connect "sh -c \"chat -v -f /etc/ppp/connect-data <$atdev >$atdev\"" & pppdpid=$!;;
- "4" ) echo -e 'AT%DATA=2,"UART",1,,"SER","UART",0\r' >$atdev;;
- "5" ) test-data-connection;;
- "6" ) kill $pppdpid; pppdpid=;;
- "0" ) break;;
- esac
- done
- ;;
- "6" )
- echo
- echo ------------------------
- echo Starting android runtime
- echo ------------------------
- start
- ;;
- "7" )
- echo
- echo ------------------------
- echo Starting android runtime
- echo ------------------------
- if exists /data/singleproc
- then
- single_process="-s"
- else
- single_process=""
- fi
- start runtime $single_process
- ;;
- "8" )
- stop
- ;;
- "9" )
- while true; do
- echo
- echo "------------------------------"
- echo " 1: Print events"
- echo " 2: Stop event output"
- if $notifytoggle
- then
- echo " 3: stop notify"
- else
- echo " 3: notify /sys/android_power"
- fi
- echo " 4: start powerd"
- echo " 5: start powerd verbose"
- echo " 6: stop powerd"
- echo " 7: set powerd idletime ($powerdidletime)"
- echo " 8: start multitap shell"
- if exists /data/singleproc
- then
- echo " 9: enable multiprocess"
- else
- echo " 9: disable multiprocess"
- fi
- echo " c: start shell"
- echo " 0: back"
- echo -n ": "
- c=`readtty -f -a 1234567890c#`
- echo Got key -$c-
- case "$c" in
- "1" ) kill $eventoutputpid; getevent & eventoutputpid=$! ;;
- "2" ) kill $eventoutputpid; eventoutputpid= ;;
- "3" )
- if $notifytoggle
- then
- kill $notifypid
- notifypid=
- notifytoggle=false
- else
- kill $notifypid
- notify -m 0x00000002 -c 0 -p -v 0 -w 30 /sys/android_power &
- notifypid=$!
- notifytoggle=true
- fi
- ;;
- "4" ) start powerd -i $powerdidletime ;;
- "5" ) start powerd -i $powerdidletime -v ;;
- "6" ) stop powerd ;;
- "7" ) echo -n "Idle time (seconds): "; read powerdidletime ;;
- "8" )
- readtty -f -p -t 10 -e "[ ~" | sh -i
- ;;
- "9" )
- if exists /data/singleproc
- then
- echo "Enabling multiprocess environment."
- rm /data/singleproc
- else
- echo "Disabling multiprocess environment."
- echo >/data/singleproc "true"
- fi
- ;;
- "c" ) sh -i <>/dev/tty0 1>&0 2>&1 ;;
- "0" ) break;;
- esac
- done
- ;;
- esac
-done
-
diff --git a/rootdir/init.environ.rc.in b/rootdir/init.environ.rc.in
index 927c33d..0064790 100644
--- a/rootdir/init.environ.rc.in
+++ b/rootdir/init.environ.rc.in
@@ -1,6 +1,5 @@
# set up the global environment
on init
- export PATH /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin
export ANDROID_BOOTLOGO 1
export ANDROID_ROOT /system
export ANDROID_ASSETS /system/app
@@ -9,3 +8,4 @@
export ASEC_MOUNTPOINT /mnt/asec
export LOOP_MOUNTPOINT /mnt/obb
export BOOTCLASSPATH %BOOTCLASSPATH%
+ export SYSTEMSERVERCLASSPATH %SYSTEMSERVERCLASSPATH%
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 51246fb..2ff0d04 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -7,6 +7,7 @@
import /init.environ.rc
import /init.usb.rc
import /init.${ro.hardware}.rc
+import /init.${ro.zygote}.rc
import /init.trace.rc
on early-init
@@ -25,29 +26,27 @@
start ueventd
-# create mountpoints
+ # create mountpoints
mkdir /mnt 0775 root system
on init
+ sysclktz 0
-sysclktz 0
+ loglevel 3
-loglevel 3
-
-# Backward compatibility
+ # Backward compatibility.
symlink /system/etc /etc
symlink /sys/kernel/debug /d
-# Right now vendor lives on the same filesystem as system,
-# but someday that may change.
+ # Link /vendor to /system/vendor for devices without a vendor partition.
symlink /system/vendor /vendor
-# Create cgroup mount point for cpu accounting
+ # Create cgroup mount point for cpu accounting
mkdir /acct
mount cgroup none /acct cpuacct
mkdir /acct/uid
-# Create cgroup mount point for memory
+ # Create cgroup mount point for memory
mount tmpfs none /sys/fs/cgroup mode=0750,uid=0,gid=1000
mkdir /sys/fs/cgroup/memory 0750 root system
mount cgroup none /sys/fs/cgroup/memory memory
@@ -101,55 +100,98 @@
write /proc/sys/kernel/sched_child_runs_first 0
write /proc/sys/kernel/randomize_va_space 2
write /proc/sys/kernel/kptr_restrict 2
- write /proc/sys/kernel/dmesg_restrict 1
write /proc/sys/vm/mmap_min_addr 32768
write /proc/sys/net/ipv4/ping_group_range "0 2147483647"
+ write /proc/sys/net/unix/max_dgram_qlen 300
write /proc/sys/kernel/sched_rt_runtime_us 950000
write /proc/sys/kernel/sched_rt_period_us 1000000
-# Create cgroup mount points for process groups
+ # reflect fwmark from incoming packets onto generated replies
+ write /proc/sys/net/ipv4/fwmark_reflect 1
+ write /proc/sys/net/ipv6/fwmark_reflect 1
+
+ # set fwmark on accepted sockets
+ write /proc/sys/net/ipv4/tcp_fwmark_accept 1
+
+ # disable icmp redirects
+ write /proc/sys/net/ipv4/conf/all/accept_redirects 0
+ write /proc/sys/net/ipv6/conf/all/accept_redirects 0
+
+ # Create cgroup mount points for process groups
mkdir /dev/cpuctl
mount cgroup none /dev/cpuctl cpu
chown system system /dev/cpuctl
chown system system /dev/cpuctl/tasks
- chmod 0660 /dev/cpuctl/tasks
+ chmod 0666 /dev/cpuctl/tasks
write /dev/cpuctl/cpu.shares 1024
- write /dev/cpuctl/cpu.rt_runtime_us 950000
+ write /dev/cpuctl/cpu.rt_runtime_us 800000
write /dev/cpuctl/cpu.rt_period_us 1000000
- mkdir /dev/cpuctl/apps
- chown system system /dev/cpuctl/apps/tasks
- chmod 0666 /dev/cpuctl/apps/tasks
- write /dev/cpuctl/apps/cpu.shares 1024
- write /dev/cpuctl/apps/cpu.rt_runtime_us 800000
- write /dev/cpuctl/apps/cpu.rt_period_us 1000000
-
- mkdir /dev/cpuctl/apps/bg_non_interactive
- chown system system /dev/cpuctl/apps/bg_non_interactive/tasks
- chmod 0666 /dev/cpuctl/apps/bg_non_interactive/tasks
+ mkdir /dev/cpuctl/bg_non_interactive
+ chown system system /dev/cpuctl/bg_non_interactive/tasks
+ chmod 0666 /dev/cpuctl/bg_non_interactive/tasks
# 5.0 %
- write /dev/cpuctl/apps/bg_non_interactive/cpu.shares 52
- write /dev/cpuctl/apps/bg_non_interactive/cpu.rt_runtime_us 700000
- write /dev/cpuctl/apps/bg_non_interactive/cpu.rt_period_us 1000000
+ write /dev/cpuctl/bg_non_interactive/cpu.shares 52
+ write /dev/cpuctl/bg_non_interactive/cpu.rt_runtime_us 700000
+ write /dev/cpuctl/bg_non_interactive/cpu.rt_period_us 1000000
-# qtaguid will limit access to specific data based on group memberships.
-# net_bw_acct grants impersonation of socket owners.
-# net_bw_stats grants access to other apps' detailed tagged-socket stats.
+ # qtaguid will limit access to specific data based on group memberships.
+ # net_bw_acct grants impersonation of socket owners.
+ # net_bw_stats grants access to other apps' detailed tagged-socket stats.
chown root net_bw_acct /proc/net/xt_qtaguid/ctrl
chown root net_bw_stats /proc/net/xt_qtaguid/stats
-# Allow everybody to read the xt_qtaguid resource tracking misc dev.
-# This is needed by any process that uses socket tagging.
+ # Allow everybody to read the xt_qtaguid resource tracking misc dev.
+ # This is needed by any process that uses socket tagging.
chmod 0644 /dev/xt_qtaguid
-# Create location for fs_mgr to store abbreviated output from filesystem
-# checker programs.
+ # Create location for fs_mgr to store abbreviated output from filesystem
+ # checker programs.
mkdir /dev/fscklogs 0770 root system
-# pstore/ramoops previous console log
+ # pstore/ramoops previous console log
mount pstore pstore /sys/fs/pstore
chown system log /sys/fs/pstore/console-ramoops
chmod 0440 /sys/fs/pstore/console-ramoops
+ chown system log /sys/fs/pstore/pmsg-ramoops-0
+ chmod 0440 /sys/fs/pstore/pmsg-ramoops-0
+
+ # enable armv8_deprecated instruction hooks
+ write /proc/sys/abi/swp 1
+
+# Healthd can trigger a full boot from charger mode by signaling this
+# property when the power button is held.
+on property:sys.boot_from_charger_mode=1
+ class_stop charger
+ trigger late-init
+
+# Load properties from /system/ + /factory after fs mount.
+on load_all_props_action
+ load_all_props
+ start logd-reinit
+
+# Indicate to fw loaders that the relevant mounts are up.
+on firmware_mounts_complete
+ rm /dev/.booting
+
+# Mount filesystems and start core system services.
+on late-init
+ trigger early-fs
+ trigger fs
+ trigger post-fs
+ trigger post-fs-data
+
+ # Load properties from /system/ + /factory after fs mount. Place
+ # this in another action so that the load will be scheduled after the prior
+ # issued fs triggers have completed.
+ trigger load_all_props_action
+
+ # Remove a file to wake up anything waiting for firmware.
+ trigger firmware_mounts_complete
+
+ trigger early-boot
+ trigger boot
+
on post-fs
# once everything is setup, no need to modify /
@@ -161,13 +203,11 @@
chown system cache /cache
chmod 0770 /cache
# We restorecon /cache in case the cache partition has been reset.
- restorecon /cache
+ restorecon_recursive /cache
# This may have been created by the recovery system with odd permissions
chown system cache /cache/recovery
chmod 0770 /cache/recovery
- # This may have been created by the recovery system with the wrong context.
- restorecon /cache/recovery
#change permissions on vmallocinfo so we can grab it from bugreports
chown root log /proc/vmallocinfo
@@ -184,6 +224,9 @@
chown system log /proc/last_kmsg
chmod 0440 /proc/last_kmsg
+ # make the selinux kernel policy world-readable
+ chmod 0444 /sys/fs/selinux/policy
+
# create the lost+found directories, so as to enforce our permissions
mkdir /cache/lost+found 0770 root root
@@ -194,25 +237,14 @@
# We restorecon /data in case the userdata partition has been reset.
restorecon /data
+ # Start bootcharting as soon as possible after the data partition is
+ # mounted to collect more data.
+ mkdir /data/bootchart 0755 shell shell
+ bootchart_init
+
# Avoid predictable entropy pool. Carry over entropy from previous boot.
copy /data/system/entropy.dat /dev/urandom
- # Create dump dir and collect dumps.
- # Do this before we mount cache so eventually we can use cache for
- # storing dumps on platforms which do not have a dedicated dump partition.
- mkdir /data/dontpanic 0750 root log
-
- # Collect apanic data, free resources and re-arm trigger
- copy /proc/apanic_console /data/dontpanic/apanic_console
- chown root log /data/dontpanic/apanic_console
- chmod 0640 /data/dontpanic/apanic_console
-
- copy /proc/apanic_threads /data/dontpanic/apanic_threads
- chown root log /data/dontpanic/apanic_threads
- chmod 0640 /data/dontpanic/apanic_threads
-
- write /proc/apanic_console 1
-
# create basic filesystem structure
mkdir /data/misc 01771 system misc
mkdir /data/misc/adb 02750 system shell
@@ -220,26 +252,23 @@
mkdir /data/misc/bluetooth 0770 system system
mkdir /data/misc/keystore 0700 keystore keystore
mkdir /data/misc/keychain 0771 system system
+ mkdir /data/misc/net 0750 root shell
mkdir /data/misc/radio 0770 system radio
mkdir /data/misc/sms 0770 system radio
mkdir /data/misc/zoneinfo 0775 system system
- restorecon_recursive /data/misc/zoneinfo
mkdir /data/misc/vpn 0770 system vpn
+ mkdir /data/misc/shared_relro 0771 shared_relro shared_relro
mkdir /data/misc/systemkeys 0700 system system
mkdir /data/misc/wifi 0770 wifi wifi
mkdir /data/misc/wifi/sockets 0770 wifi wifi
- restorecon_recursive /data/misc/wifi/sockets
mkdir /data/misc/wifi/wpa_supplicant 0770 wifi wifi
+ mkdir /data/misc/ethernet 0770 system system
mkdir /data/misc/dhcp 0770 dhcp dhcp
+ mkdir /data/misc/user 0771 root root
# give system access to wpa_supplicant.conf for backup and restore
chmod 0660 /data/misc/wifi/wpa_supplicant.conf
mkdir /data/local 0751 root root
mkdir /data/misc/media 0700 media media
- restorecon_recursive /data/misc/media
-
- # Set security context of any pre-existing /data/misc/adb/adb_keys file.
- restorecon /data/misc/adb
- restorecon /data/misc/adb/adb_keys
# For security reasons, /data/local/tmp should always be empty.
# Do not place files or directories in /data/local/tmp
@@ -250,11 +279,11 @@
mkdir /data/app-lib 0771 system system
mkdir /data/app 0771 system system
mkdir /data/property 0700 root root
- mkdir /data/ssh 0750 root shell
- mkdir /data/ssh/empty 0700 root root
+ mkdir /data/tombstones 0771 system system
# create dalvik-cache, so as to enforce our permissions
- mkdir /data/dalvik-cache 0771 system system
+ mkdir /data/dalvik-cache 0771 root root
+ mkdir /data/dalvik-cache/profiles 0711 system system
# create resource-cache and double-check the perms
mkdir /data/resource-cache 0771 system system
@@ -271,7 +300,8 @@
# create directory for MediaDrm plug-ins - give drm the read/write access to
# the following directory.
mkdir /data/mediadrm 0770 mediadrm mediadrm
- restorecon_recursive /data/mediadrm
+
+ mkdir /data/adb 0700 root root
# symlink to bugreport storage location
symlink /data/data/com.android.shell/files/bugreports /data/bugreports
@@ -282,6 +312,9 @@
# Reload policy from /data/security if present.
setprop selinux.reload_policy 1
+ # Set SELinux security contexts on upgrade or policy update.
+ restorecon_recursive /data
+
# If there is no fs-post-data action in the init.<device>.rc file, you
# must uncomment this line, otherwise encrypted filesystems
# won't work.
@@ -289,17 +322,17 @@
#setprop vold.post_fs_data_done 1
on boot
-# basic network init
+ # basic network init
ifup lo
hostname localhost
domainname localdomain
-# set RLIMIT_NICE to allow priorities from 19 to -20
+ # set RLIMIT_NICE to allow priorities from 19 to -20
setrlimit 13 40 40
-# Memory management. Basic kernel parameters, and allow the high
-# level system server to be able to adjust the kernel OOM driver
-# parameters to match how it is managing things.
+ # Memory management. Basic kernel parameters, and allow the high
+ # level system server to be able to adjust the kernel OOM driver
+ # parameters to match how it is managing things.
write /proc/sys/vm/overcommit_memory 1
write /proc/sys/vm/min_free_order_shift 4
chown root system /sys/module/lowmemorykiller/parameters/adj
@@ -375,27 +408,25 @@
chown system system /sys/kernel/ipv4/tcp_rmem_max
chown root radio /proc/cmdline
-# Define TCP buffer sizes for various networks
-# ReadMin, ReadInitial, ReadMax, WriteMin, WriteInitial, WriteMax,
- setprop net.tcp.buffersize.default 4096,87380,110208,4096,16384,110208
- setprop net.tcp.buffersize.wifi 524288,1048576,2097152,262144,524288,1048576
- setprop net.tcp.buffersize.ethernet 524288,1048576,3145728,524288,1048576,2097152
- setprop net.tcp.buffersize.lte 524288,1048576,2097152,262144,524288,1048576
- setprop net.tcp.buffersize.umts 4094,87380,110208,4096,16384,110208
- setprop net.tcp.buffersize.hspa 4094,87380,262144,4096,16384,262144
- setprop net.tcp.buffersize.hsupa 4094,87380,262144,4096,16384,262144
- setprop net.tcp.buffersize.hsdpa 4094,87380,262144,4096,16384,262144
- setprop net.tcp.buffersize.hspap 4094,87380,1220608,4096,16384,1220608
- setprop net.tcp.buffersize.edge 4093,26280,35040,4096,16384,35040
- setprop net.tcp.buffersize.gprs 4092,8760,11680,4096,8760,11680
- setprop net.tcp.buffersize.evdo 4094,87380,262144,4096,16384,262144
+ # Define default initial receive window size in segments.
+ setprop net.tcp.default_init_rwnd 60
class_start core
- class_start main
on nonencrypted
+ class_start main
class_start late_start
+on property:vold.decrypt=trigger_default_encryption
+ start defaultcrypto
+
+on property:vold.decrypt=trigger_encryption
+ start surfaceflinger
+ start encrypt
+
+on property:sys.init_log_level=*
+ loglevel ${sys.init_log_level}
+
on charger
class_start charger
@@ -404,6 +435,7 @@
on property:vold.decrypt=trigger_load_persist_props
load_persist_props
+ start logd-reinit
on property:vold.decrypt=trigger_post_fs_data
trigger post-fs-data
@@ -422,10 +454,17 @@
on property:sys.powerctl=*
powerctl ${sys.powerctl}
-# system server cannot write to /proc/sys files, so proxy it through init
+# system server cannot write to /proc/sys files,
+# and chown/chmod does not work for /proc/sys/ entries.
+# So proxy writes through init.
on property:sys.sysctl.extra_free_kbytes=*
write /proc/sys/vm/extra_free_kbytes ${sys.sysctl.extra_free_kbytes}
+# "tcp_default_init_rwnd" Is too long!
+on property:sys.sysctl.tcp_def_init_rwnd=*
+ write /proc/sys/net/ipv4/tcp_default_init_rwnd ${sys.sysctl.tcp_def_init_rwnd}
+
+
## Daemon processes to be run by init.
##
service ueventd /sbin/ueventd
@@ -433,6 +472,16 @@
critical
seclabel u:r:ueventd:s0
+service logd /system/bin/logd
+ class core
+ socket logd stream 0666 logd logd
+ socket logdr seqpacket 0666 logd logd
+ socket logdw dgram 0222 logd logd
+
+service logd-reinit /system/bin/logd --reinit
+ oneshot
+ disabled
+
service healthd /sbin/healthd
class core
critical
@@ -443,7 +492,7 @@
console
disabled
user shell
- group log
+ group shell log
seclabel u:r:shell:s0
on property:ro.debuggable=1
@@ -474,7 +523,6 @@
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
- onrestart restart inputflinger
onrestart restart drm
service vold /system/bin/vold
@@ -487,10 +535,14 @@
socket netd stream 0660 root system
socket dnsproxyd stream 0660 root inet
socket mdns stream 0660 root system
+ socket fwmarkd stream 0660 root inet
service debuggerd /system/bin/debuggerd
class main
+service debuggerd64 /system/bin/debuggerd64
+ class main
+
service ril-daemon /system/bin/rild
class main
socket rild stream 660 root radio
@@ -499,25 +551,11 @@
group radio cache inet misc audio log
service surfaceflinger /system/bin/surfaceflinger
- class main
+ class core
user system
group graphics drmrpc
onrestart restart zygote
-service inputflinger /system/bin/inputflinger
- class main
- user system
- group input
- onrestart restart zygote
-
-service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
- class main
- socket zygote stream 660 root system
- onrestart write /sys/android_power/request_state wake
- onrestart write /sys/power/state on
- onrestart restart media
- onrestart restart netd
-
service drm /system/bin/drmserver
class main
user drm
@@ -529,10 +567,24 @@
group audio camera inet net_bt net_bt_admin net_bw_acct drmrpc mediadrm
ioprio rt 4
+# One shot invocation to deal with encrypted volume.
+service defaultcrypto /system/bin/vdc --wait cryptfs mountdefaultencrypted
+ disabled
+ oneshot
+ # vold will set vold.decrypt to trigger_restart_framework (default
+ # encryption) or trigger_restart_min_framework (other encryption)
+
+# One shot invocation to encrypt unencrypted volumes
+service encrypt /system/bin/vdc --wait cryptfs enablecrypto inplace default
+ disabled
+ oneshot
+ # vold will set vold.decrypt to trigger_restart_framework (default
+ # encryption)
+
service bootanim /system/bin/bootanimation
- class main
+ class core
user graphics
- group graphics
+ group graphics audio
disabled
oneshot
@@ -571,10 +623,6 @@
disabled
oneshot
-service sshd /system/bin/start-ssh
- class main
- disabled
-
service mdnsd /system/bin/mdnsd
class main
user mdnsr
diff --git a/rootdir/init.trace.rc b/rootdir/init.trace.rc
index 50944e6..cd8d350 100644
--- a/rootdir/init.trace.rc
+++ b/rootdir/init.trace.rc
@@ -1,6 +1,6 @@
## Permissions to allow system-wide tracing to the kernel trace buffer.
##
-on boot
+on early-boot
# Allow writing to the kernel trace log.
chmod 0222 /sys/kernel/debug/tracing/trace_marker
diff --git a/rootdir/init.usb.rc b/rootdir/init.usb.rc
index 15467cc..e290ca4 100644
--- a/rootdir/init.usb.rc
+++ b/rootdir/init.usb.rc
@@ -17,12 +17,12 @@
setprop sys.usb.state ${sys.usb.config}
# adb only USB configuration
-# This should only be used during device bringup
-# and as a fallback if the USB manager fails to set a standard configuration
+# This is the fallback configuration if the
+# USB manager fails to set a standard configuration
on property:sys.usb.config=adb
write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 18d1
- write /sys/class/android_usb/android0/idProduct D002
+ write /sys/class/android_usb/android0/idProduct 4EE7
write /sys/class/android_usb/android0/functions ${sys.usb.config}
write /sys/class/android_usb/android0/enable 1
start adbd
diff --git a/rootdir/init.zygote32.rc b/rootdir/init.zygote32.rc
new file mode 100644
index 0000000..75961e6
--- /dev/null
+++ b/rootdir/init.zygote32.rc
@@ -0,0 +1,8 @@
+service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
+ class main
+ socket zygote stream 660 root system
+ onrestart write /sys/android_power/request_state wake
+ onrestart write /sys/power/state on
+ onrestart restart media
+ onrestart restart netd
+
diff --git a/rootdir/init.zygote32_64.rc b/rootdir/init.zygote32_64.rc
new file mode 100644
index 0000000..68c0668
--- /dev/null
+++ b/rootdir/init.zygote32_64.rc
@@ -0,0 +1,12 @@
+service zygote /system/bin/app_process32 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote
+ class main
+ socket zygote stream 660 root system
+ onrestart write /sys/android_power/request_state wake
+ onrestart write /sys/power/state on
+ onrestart restart media
+ onrestart restart netd
+
+service zygote_secondary /system/bin/app_process64 -Xzygote /system/bin --zygote --socket-name=zygote_secondary
+ class main
+ socket zygote_secondary stream 660 root system
+ onrestart restart zygote
diff --git a/rootdir/init.zygote64.rc b/rootdir/init.zygote64.rc
new file mode 100644
index 0000000..afb6d63
--- /dev/null
+++ b/rootdir/init.zygote64.rc
@@ -0,0 +1,8 @@
+service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server
+ class main
+ socket zygote stream 660 root system
+ onrestart write /sys/android_power/request_state wake
+ onrestart write /sys/power/state on
+ onrestart restart media
+ onrestart restart netd
+
diff --git a/rootdir/init.zygote64_32.rc b/rootdir/init.zygote64_32.rc
new file mode 100644
index 0000000..979ab3b
--- /dev/null
+++ b/rootdir/init.zygote64_32.rc
@@ -0,0 +1,12 @@
+service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote
+ class main
+ socket zygote stream 660 root system
+ onrestart write /sys/android_power/request_state wake
+ onrestart write /sys/power/state on
+ onrestart restart media
+ onrestart restart netd
+
+service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary
+ class main
+ socket zygote_secondary stream 660 root system
+ onrestart restart zygote
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 9bf17e2..9cf9ed9 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -16,6 +16,7 @@
# Anyone can read the logs, but if they're not in the "logs"
# group, then they'll only see log entries for their UID.
/dev/log/* 0666 root log
+/dev/pmsg0 0222 root log
# the msm hw3d client device node is world writable/readable.
/dev/msm_hw3dc 0666 root root
@@ -35,7 +36,7 @@
/dev/uhid 0660 system net_bt_stack
/dev/uinput 0660 system net_bt_stack
/dev/alarm 0664 system radio
-/dev/rtc0 0664 system radio
+/dev/rtc0 0640 system system
/dev/tty0 0660 root system
/dev/graphics/* 0660 root graphics
/dev/msm_hw3dm 0660 system graphics
@@ -88,6 +89,9 @@
/dev/ppp 0660 radio vpn
# sysfs properties
+/sys/devices/platform/trusty.* trusty_version 0440 root log
/sys/devices/virtual/input/input* enable 0660 root input
/sys/devices/virtual/input/input* poll_delay 0660 root input
/sys/devices/virtual/usb_composite/* enable 0664 root system
+/sys/devices/system/cpu/cpu* cpufreq/scaling_max_freq 0664 system system
+/sys/devices/system/cpu/cpu* cpufreq/scaling_min_freq 0664 system system
diff --git a/run-as/Android.mk b/run-as/Android.mk
index a8f2885..3774acc 100644
--- a/run-as/Android.mk
+++ b/run-as/Android.mk
@@ -1,10 +1,12 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= run-as.c package.c
+LOCAL_SRC_FILES := run-as.c package.c
LOCAL_SHARED_LIBRARIES := libselinux
-LOCAL_MODULE:= run-as
+LOCAL_MODULE := run-as
+
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
diff --git a/run-as/package.c b/run-as/package.c
index 901e9e3..4f8f3a7 100644
--- a/run-as/package.c
+++ b/run-as/package.c
@@ -128,7 +128,9 @@
}
/* Memory-map the file now */
- address = TEMP_FAILURE_RETRY(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0));
+ do {
+ address = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
+ } while (address == MAP_FAILED && errno == EINTR);
if (address == MAP_FAILED) {
address = NULL;
goto EXIT;
@@ -408,10 +410,6 @@
value = -1;
}
return value;
-
-BAD:
- *pp = p;
- return -1;
}
/* Read the system's package database and extract information about
diff --git a/sdcard/Android.mk b/sdcard/Android.mk
index 4630db9..cb3a8fb 100644
--- a/sdcard/Android.mk
+++ b/sdcard/Android.mk
@@ -1,11 +1,11 @@
-LOCAL_PATH:= $(call my-dir)
+LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= sdcard.c
-LOCAL_MODULE:= sdcard
-LOCAL_CFLAGS := -Wall -Wno-unused-parameter
+LOCAL_SRC_FILES := sdcard.c
+LOCAL_MODULE := sdcard
+LOCAL_CFLAGS := -Wall -Wno-unused-parameter -Werror
-LOCAL_SHARED_LIBRARIES := libc libcutils
+LOCAL_SHARED_LIBRARIES := libcutils
include $(BUILD_EXECUTABLE)
diff --git a/sdcard/fuse.h b/sdcard/fuse.h
deleted file mode 100644
index 3138da9..0000000
--- a/sdcard/fuse.h
+++ /dev/null
@@ -1,578 +0,0 @@
-/*
- FUSE: Filesystem in Userspace
- Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
-
- This program can be distributed under the terms of the GNU GPL.
- See the file COPYING.
-*/
-
-/*
- * from the libfuse FAQ (and consistent with the Linux Kernel license):
- *
- * Under what conditions may I distribute a filesystem that uses the
- * raw kernel interface of FUSE?
- *
- * There are no restrictions whatsoever for using the raw kernel interface.
- *
- */
-
-/*
- * This file defines the kernel interface of FUSE
- *
- * Protocol changelog:
- *
- * 7.9:
- * - new fuse_getattr_in input argument of GETATTR
- * - add lk_flags in fuse_lk_in
- * - add lock_owner field to fuse_setattr_in, fuse_read_in and fuse_write_in
- * - add blksize field to fuse_attr
- * - add file flags field to fuse_read_in and fuse_write_in
- *
- * 7.10
- * - add nonseekable open flag
- *
- * 7.11
- * - add IOCTL message
- * - add unsolicited notification support
- * - add POLL message and NOTIFY_POLL notification
- *
- * 7.12
- * - add umask flag to input argument of open, mknod and mkdir
- * - add notification messages for invalidation of inodes and
- * directory entries
- *
- * 7.13
- * - make max number of background requests and congestion threshold
- * tunables
- */
-
-#ifndef _LINUX_FUSE_H
-#define _LINUX_FUSE_H
-
-#include <linux/types.h>
-
-/*
- * Version negotiation:
- *
- * Both the kernel and userspace send the version they support in the
- * INIT request and reply respectively.
- *
- * If the major versions match then both shall use the smallest
- * of the two minor versions for communication.
- *
- * If the kernel supports a larger major version, then userspace shall
- * reply with the major version it supports, ignore the rest of the
- * INIT message and expect a new INIT message from the kernel with a
- * matching major version.
- *
- * If the library supports a larger major version, then it shall fall
- * back to the major protocol version sent by the kernel for
- * communication and reply with that major version (and an arbitrary
- * supported minor version).
- */
-
-/** Version number of this interface */
-#define FUSE_KERNEL_VERSION 7
-
-/** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 13
-
-/** The node ID of the root inode */
-#define FUSE_ROOT_ID 1
-
-/* Make sure all structures are padded to 64bit boundary, so 32bit
- userspace works under 64bit kernels */
-
-struct fuse_attr {
- __u64 ino;
- __u64 size;
- __u64 blocks;
- __u64 atime;
- __u64 mtime;
- __u64 ctime;
- __u32 atimensec;
- __u32 mtimensec;
- __u32 ctimensec;
- __u32 mode;
- __u32 nlink;
- __u32 uid;
- __u32 gid;
- __u32 rdev;
- __u32 blksize;
- __u32 padding;
-};
-
-struct fuse_kstatfs {
- __u64 blocks;
- __u64 bfree;
- __u64 bavail;
- __u64 files;
- __u64 ffree;
- __u32 bsize;
- __u32 namelen;
- __u32 frsize;
- __u32 padding;
- __u32 spare[6];
-};
-
-struct fuse_file_lock {
- __u64 start;
- __u64 end;
- __u32 type;
- __u32 pid; /* tgid */
-};
-
-/**
- * Bitmasks for fuse_setattr_in.valid
- */
-#define FATTR_MODE (1 << 0)
-#define FATTR_UID (1 << 1)
-#define FATTR_GID (1 << 2)
-#define FATTR_SIZE (1 << 3)
-#define FATTR_ATIME (1 << 4)
-#define FATTR_MTIME (1 << 5)
-#define FATTR_FH (1 << 6)
-#define FATTR_ATIME_NOW (1 << 7)
-#define FATTR_MTIME_NOW (1 << 8)
-#define FATTR_LOCKOWNER (1 << 9)
-
-/**
- * Flags returned by the OPEN request
- *
- * FOPEN_DIRECT_IO: bypass page cache for this open file
- * FOPEN_KEEP_CACHE: don't invalidate the data cache on open
- * FOPEN_NONSEEKABLE: the file is not seekable
- */
-#define FOPEN_DIRECT_IO (1 << 0)
-#define FOPEN_KEEP_CACHE (1 << 1)
-#define FOPEN_NONSEEKABLE (1 << 2)
-
-/**
- * INIT request/reply flags
- *
- * FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".."
- * FUSE_DONT_MASK: don't apply umask to file mode on create operations
- */
-#define FUSE_ASYNC_READ (1 << 0)
-#define FUSE_POSIX_LOCKS (1 << 1)
-#define FUSE_FILE_OPS (1 << 2)
-#define FUSE_ATOMIC_O_TRUNC (1 << 3)
-#define FUSE_EXPORT_SUPPORT (1 << 4)
-#define FUSE_BIG_WRITES (1 << 5)
-#define FUSE_DONT_MASK (1 << 6)
-
-/**
- * CUSE INIT request/reply flags
- *
- * CUSE_UNRESTRICTED_IOCTL: use unrestricted ioctl
- */
-#define CUSE_UNRESTRICTED_IOCTL (1 << 0)
-
-/**
- * Release flags
- */
-#define FUSE_RELEASE_FLUSH (1 << 0)
-
-/**
- * Getattr flags
- */
-#define FUSE_GETATTR_FH (1 << 0)
-
-/**
- * Lock flags
- */
-#define FUSE_LK_FLOCK (1 << 0)
-
-/**
- * WRITE flags
- *
- * FUSE_WRITE_CACHE: delayed write from page cache, file handle is guessed
- * FUSE_WRITE_LOCKOWNER: lock_owner field is valid
- */
-#define FUSE_WRITE_CACHE (1 << 0)
-#define FUSE_WRITE_LOCKOWNER (1 << 1)
-
-/**
- * Read flags
- */
-#define FUSE_READ_LOCKOWNER (1 << 1)
-
-/**
- * Ioctl flags
- *
- * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine
- * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed
- * FUSE_IOCTL_RETRY: retry with new iovecs
- *
- * FUSE_IOCTL_MAX_IOV: maximum of in_iovecs + out_iovecs
- */
-#define FUSE_IOCTL_COMPAT (1 << 0)
-#define FUSE_IOCTL_UNRESTRICTED (1 << 1)
-#define FUSE_IOCTL_RETRY (1 << 2)
-
-#define FUSE_IOCTL_MAX_IOV 256
-
-/**
- * Poll flags
- *
- * FUSE_POLL_SCHEDULE_NOTIFY: request poll notify
- */
-#define FUSE_POLL_SCHEDULE_NOTIFY (1 << 0)
-
-enum fuse_opcode {
- FUSE_LOOKUP = 1,
- FUSE_FORGET = 2, /* no reply */
- FUSE_GETATTR = 3,
- FUSE_SETATTR = 4,
- FUSE_READLINK = 5,
- FUSE_SYMLINK = 6,
- FUSE_MKNOD = 8,
- FUSE_MKDIR = 9,
- FUSE_UNLINK = 10,
- FUSE_RMDIR = 11,
- FUSE_RENAME = 12,
- FUSE_LINK = 13,
- FUSE_OPEN = 14,
- FUSE_READ = 15,
- FUSE_WRITE = 16,
- FUSE_STATFS = 17,
- FUSE_RELEASE = 18,
- FUSE_FSYNC = 20,
- FUSE_SETXATTR = 21,
- FUSE_GETXATTR = 22,
- FUSE_LISTXATTR = 23,
- FUSE_REMOVEXATTR = 24,
- FUSE_FLUSH = 25,
- FUSE_INIT = 26,
- FUSE_OPENDIR = 27,
- FUSE_READDIR = 28,
- FUSE_RELEASEDIR = 29,
- FUSE_FSYNCDIR = 30,
- FUSE_GETLK = 31,
- FUSE_SETLK = 32,
- FUSE_SETLKW = 33,
- FUSE_ACCESS = 34,
- FUSE_CREATE = 35,
- FUSE_INTERRUPT = 36,
- FUSE_BMAP = 37,
- FUSE_DESTROY = 38,
- FUSE_IOCTL = 39,
- FUSE_POLL = 40,
-
- /* CUSE specific operations */
- CUSE_INIT = 4096,
-};
-
-enum fuse_notify_code {
- FUSE_NOTIFY_POLL = 1,
- FUSE_NOTIFY_INVAL_INODE = 2,
- FUSE_NOTIFY_INVAL_ENTRY = 3,
- FUSE_NOTIFY_CODE_MAX,
-};
-
-/* The read buffer is required to be at least 8k, but may be much larger */
-#define FUSE_MIN_READ_BUFFER 8192
-
-#define FUSE_COMPAT_ENTRY_OUT_SIZE 120
-
-struct fuse_entry_out {
- __u64 nodeid; /* Inode ID */
- __u64 generation; /* Inode generation: nodeid:gen must
- be unique for the fs's lifetime */
- __u64 entry_valid; /* Cache timeout for the name */
- __u64 attr_valid; /* Cache timeout for the attributes */
- __u32 entry_valid_nsec;
- __u32 attr_valid_nsec;
- struct fuse_attr attr;
-};
-
-struct fuse_forget_in {
- __u64 nlookup;
-};
-
-struct fuse_getattr_in {
- __u32 getattr_flags;
- __u32 dummy;
- __u64 fh;
-};
-
-#define FUSE_COMPAT_ATTR_OUT_SIZE 96
-
-struct fuse_attr_out {
- __u64 attr_valid; /* Cache timeout for the attributes */
- __u32 attr_valid_nsec;
- __u32 dummy;
- struct fuse_attr attr;
-};
-
-#define FUSE_COMPAT_MKNOD_IN_SIZE 8
-
-struct fuse_mknod_in {
- __u32 mode;
- __u32 rdev;
- __u32 umask;
- __u32 padding;
-};
-
-struct fuse_mkdir_in {
- __u32 mode;
- __u32 umask;
-};
-
-struct fuse_rename_in {
- __u64 newdir;
-};
-
-struct fuse_link_in {
- __u64 oldnodeid;
-};
-
-struct fuse_setattr_in {
- __u32 valid;
- __u32 padding;
- __u64 fh;
- __u64 size;
- __u64 lock_owner;
- __u64 atime;
- __u64 mtime;
- __u64 unused2;
- __u32 atimensec;
- __u32 mtimensec;
- __u32 unused3;
- __u32 mode;
- __u32 unused4;
- __u32 uid;
- __u32 gid;
- __u32 unused5;
-};
-
-struct fuse_open_in {
- __u32 flags;
- __u32 unused;
-};
-
-struct fuse_create_in {
- __u32 flags;
- __u32 mode;
- __u32 umask;
- __u32 padding;
-};
-
-struct fuse_open_out {
- __u64 fh;
- __u32 open_flags;
- __u32 padding;
-};
-
-struct fuse_release_in {
- __u64 fh;
- __u32 flags;
- __u32 release_flags;
- __u64 lock_owner;
-};
-
-struct fuse_flush_in {
- __u64 fh;
- __u32 unused;
- __u32 padding;
- __u64 lock_owner;
-};
-
-struct fuse_read_in {
- __u64 fh;
- __u64 offset;
- __u32 size;
- __u32 read_flags;
- __u64 lock_owner;
- __u32 flags;
- __u32 padding;
-};
-
-#define FUSE_COMPAT_WRITE_IN_SIZE 24
-
-struct fuse_write_in {
- __u64 fh;
- __u64 offset;
- __u32 size;
- __u32 write_flags;
- __u64 lock_owner;
- __u32 flags;
- __u32 padding;
-};
-
-struct fuse_write_out {
- __u32 size;
- __u32 padding;
-};
-
-#define FUSE_COMPAT_STATFS_SIZE 48
-
-struct fuse_statfs_out {
- struct fuse_kstatfs st;
-};
-
-struct fuse_fsync_in {
- __u64 fh;
- __u32 fsync_flags;
- __u32 padding;
-};
-
-struct fuse_setxattr_in {
- __u32 size;
- __u32 flags;
-};
-
-struct fuse_getxattr_in {
- __u32 size;
- __u32 padding;
-};
-
-struct fuse_getxattr_out {
- __u32 size;
- __u32 padding;
-};
-
-struct fuse_lk_in {
- __u64 fh;
- __u64 owner;
- struct fuse_file_lock lk;
- __u32 lk_flags;
- __u32 padding;
-};
-
-struct fuse_lk_out {
- struct fuse_file_lock lk;
-};
-
-struct fuse_access_in {
- __u32 mask;
- __u32 padding;
-};
-
-struct fuse_init_in {
- __u32 major;
- __u32 minor;
- __u32 max_readahead;
- __u32 flags;
-};
-
-struct fuse_init_out {
- __u32 major;
- __u32 minor;
- __u32 max_readahead;
- __u32 flags;
- __u16 max_background;
- __u16 congestion_threshold;
- __u32 max_write;
-};
-
-#define CUSE_INIT_INFO_MAX 4096
-
-struct cuse_init_in {
- __u32 major;
- __u32 minor;
- __u32 unused;
- __u32 flags;
-};
-
-struct cuse_init_out {
- __u32 major;
- __u32 minor;
- __u32 unused;
- __u32 flags;
- __u32 max_read;
- __u32 max_write;
- __u32 dev_major; /* chardev major */
- __u32 dev_minor; /* chardev minor */
- __u32 spare[10];
-};
-
-struct fuse_interrupt_in {
- __u64 unique;
-};
-
-struct fuse_bmap_in {
- __u64 block;
- __u32 blocksize;
- __u32 padding;
-};
-
-struct fuse_bmap_out {
- __u64 block;
-};
-
-struct fuse_ioctl_in {
- __u64 fh;
- __u32 flags;
- __u32 cmd;
- __u64 arg;
- __u32 in_size;
- __u32 out_size;
-};
-
-struct fuse_ioctl_out {
- __s32 result;
- __u32 flags;
- __u32 in_iovs;
- __u32 out_iovs;
-};
-
-struct fuse_poll_in {
- __u64 fh;
- __u64 kh;
- __u32 flags;
- __u32 padding;
-};
-
-struct fuse_poll_out {
- __u32 revents;
- __u32 padding;
-};
-
-struct fuse_notify_poll_wakeup_out {
- __u64 kh;
-};
-
-struct fuse_in_header {
- __u32 len;
- __u32 opcode;
- __u64 unique;
- __u64 nodeid;
- __u32 uid;
- __u32 gid;
- __u32 pid;
- __u32 padding;
-};
-
-struct fuse_out_header {
- __u32 len;
- __s32 error;
- __u64 unique;
-};
-
-struct fuse_dirent {
- __u64 ino;
- __u64 off;
- __u32 namelen;
- __u32 type;
- char name[0];
-};
-
-#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
-#define FUSE_DIRENT_ALIGN(x) (((x) + sizeof(__u64) - 1) & ~(sizeof(__u64) - 1))
-#define FUSE_DIRENT_SIZE(d) \
- FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + (d)->namelen)
-
-struct fuse_notify_inval_inode_out {
- __u64 ino;
- __s64 off;
- __s64 len;
-};
-
-struct fuse_notify_inval_entry_out {
- __u64 parent;
- __u32 namelen;
- __u32 padding;
-};
-
-#endif /* _LINUX_FUSE_H */
diff --git a/sdcard/sdcard.c b/sdcard/sdcard.c
index 4722708..4d50bf0 100644
--- a/sdcard/sdcard.c
+++ b/sdcard/sdcard.c
@@ -14,39 +14,43 @@
* limitations under the License.
*/
+#define LOG_TAG "sdcard"
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <linux/fuse.h>
+#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
+#include <sys/inotify.h>
#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/statfs.h>
-#include <sys/uio.h>
-#include <dirent.h>
-#include <limits.h>
-#include <ctype.h>
-#include <pthread.h>
#include <sys/time.h>
-#include <sys/resource.h>
-#include <sys/inotify.h>
+#include <sys/uio.h>
+#include <unistd.h>
#include <cutils/fs.h>
#include <cutils/hashmap.h>
+#include <cutils/log.h>
#include <cutils/multiuser.h>
#include <private/android_filesystem_config.h>
-#include "fuse.h"
-
/* README
*
* What is this?
- *
+ *
* sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
- * directory permissions (all files are given fixed owner, group, and
- * permissions at creation, owner, group, and permissions are not
+ * directory permissions (all files are given fixed owner, group, and
+ * permissions at creation, owner, group, and permissions are not
* changeable, symlinks and hardlinks are not createable, etc.
*
* See usage() for command line options.
@@ -91,12 +95,12 @@
#define FUSE_TRACE 0
#if FUSE_TRACE
-#define TRACE(x...) fprintf(stderr,x)
+#define TRACE(x...) ALOGD(x)
#else
#define TRACE(x...) do {} while (0)
#endif
-#define ERROR(x...) fprintf(stderr,x)
+#define ERROR(x...) ALOGE(x)
#define FUSE_UNKNOWN_INO 0xffffffff
@@ -140,6 +144,8 @@
PERM_ANDROID_DATA,
/* This node is "/Android/obb" */
PERM_ANDROID_OBB,
+ /* This node is "/Android/media" */
+ PERM_ANDROID_MEDIA,
/* This node is "/Android/user" */
PERM_ANDROID_USER,
} perm_t;
@@ -163,6 +169,11 @@
__u32 refcount;
__u64 nid;
__u64 gen;
+ /*
+ * The inode number for this FUSE node. Note that this isn't stable across
+ * multiple invocations of the FUSE daemon.
+ */
+ __u32 ino;
/* State derived based on current position in hierarchy. */
perm_t perm;
@@ -219,6 +230,25 @@
struct node root;
char obbpath[PATH_MAX];
+ /* Used to allocate unique inode numbers for fuse nodes. We use
+ * a simple counter based scheme where inode numbers from deleted
+ * nodes aren't reused. Note that inode allocations are not stable
+ * across multiple invocation of the sdcard daemon, but that shouldn't
+ * be a huge problem in practice.
+ *
+ * Note that we restrict inodes to 32 bit unsigned integers to prevent
+ * truncation on 32 bit processes when unsigned long long stat.st_ino is
+ * assigned to an unsigned long ino_t type in an LP32 process.
+ *
+ * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
+ * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
+ * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
+ * in fs/fuse/inode.c).
+ *
+ * Accesses must be guarded by |lock|.
+ */
+ __u32 inode_ctr;
+
Hashmap* package_to_appid;
Hashmap* appid_with_rw;
};
@@ -232,7 +262,7 @@
* buffer at the same time. This allows us to share the underlying storage. */
union {
__u8 request_buffer[MAX_REQUEST_SIZE];
- __u8 read_buffer[MAX_READ];
+ __u8 read_buffer[MAX_READ + PAGESIZE];
};
};
@@ -382,15 +412,15 @@
static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
{
- attr->ino = node->nid;
+ attr->ino = node->ino;
attr->size = s->st_size;
attr->blocks = s->st_blocks;
- attr->atime = s->st_atime;
- attr->mtime = s->st_mtime;
- attr->ctime = s->st_ctime;
- attr->atimensec = s->st_atime_nsec;
- attr->mtimensec = s->st_mtime_nsec;
- attr->ctimensec = s->st_ctime_nsec;
+ attr->atime = s->st_atim.tv_sec;
+ attr->mtime = s->st_mtim.tv_sec;
+ attr->ctime = s->st_ctim.tv_sec;
+ attr->atimensec = s->st_atim.tv_nsec;
+ attr->mtimensec = s->st_mtim.tv_nsec;
+ attr->ctimensec = s->st_ctim.tv_nsec;
attr->mode = s->st_mode;
attr->nlink = s->st_nlink;
@@ -476,6 +506,10 @@
/* Single OBB directory is always shared */
node->graft_path = fuse->obbpath;
node->graft_pathlen = strlen(fuse->obbpath);
+ } else if (!strcasecmp(node->name, "media")) {
+ /* App-specific directories inside; let anyone traverse */
+ node->perm = PERM_ANDROID_MEDIA;
+ node->mode = 0771;
} else if (!strcasecmp(node->name, "user")) {
/* User directories must only be accessible to system, protected
* by sdcard_all. Zygote will bind mount the appropriate user-
@@ -487,6 +521,7 @@
break;
case PERM_ANDROID_DATA:
case PERM_ANDROID_OBB:
+ case PERM_ANDROID_MEDIA:
appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
if (appid != 0) {
node->uid = multiuser_get_uid(parent->userid, appid);
@@ -565,6 +600,13 @@
struct node *node;
size_t namelen = strlen(name);
+ // Detect overflows in the inode counter. "4 billion nodes should be enough
+ // for everybody".
+ if (fuse->inode_ctr == 0) {
+ ERROR("No more inode numbers available");
+ return NULL;
+ }
+
node = calloc(1, sizeof(struct node));
if (!node) {
return NULL;
@@ -586,6 +628,7 @@
}
node->namelen = namelen;
node->nid = ptr_to_id(node);
+ node->ino = fuse->inode_ctr++;
node->gen = fuse->next_generation++;
derive_permissions_locked(fuse, parent, node);
@@ -690,6 +733,7 @@
fuse->derive = derive;
fuse->split_perms = split_perms;
fuse->write_gid = write_gid;
+ fuse->inode_ctr = 1;
memset(&fuse->root, 0, sizeof(fuse->root));
fuse->root.nid = FUSE_ROOT_ID; /* 1 */
@@ -819,7 +863,7 @@
pthread_mutex_lock(&fuse->lock);
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
parent_path, sizeof(parent_path));
- TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
+ TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
parent_node ? parent_node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -841,7 +885,7 @@
pthread_mutex_lock(&fuse->lock);
node = lookup_node_by_id_locked(fuse, hdr->nodeid);
- TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
+ TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
hdr->nodeid, node ? node->name : "?");
if (node) {
__u64 n = req->nlookup;
@@ -861,7 +905,7 @@
pthread_mutex_lock(&fuse->lock);
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
- TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
+ TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -886,21 +930,23 @@
pthread_mutex_lock(&fuse->lock);
has_rw = get_caller_has_rw_locked(fuse, hdr);
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
- TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
+ TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
pthread_mutex_unlock(&fuse->lock);
if (!node) {
return -ENOENT;
}
- if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
+
+ if (!(req->valid & FATTR_FH) &&
+ !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
return -EACCES;
}
/* XXX: incomplete implementation on purpose.
* chmod/chown should NEVER be implemented.*/
- if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
+ if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
return -errno;
}
@@ -951,7 +997,7 @@
has_rw = get_caller_has_rw_locked(fuse, hdr);
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
parent_path, sizeof(parent_path));
- TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
+ TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -982,7 +1028,7 @@
has_rw = get_caller_has_rw_locked(fuse, hdr);
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
parent_path, sizeof(parent_path));
- TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
+ TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -1031,7 +1077,7 @@
has_rw = get_caller_has_rw_locked(fuse, hdr);
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
parent_path, sizeof(parent_path));
- TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
+ TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
name, hdr->nodeid, parent_node ? parent_node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -1060,7 +1106,7 @@
has_rw = get_caller_has_rw_locked(fuse, hdr);
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
parent_path, sizeof(parent_path));
- TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
+ TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
name, hdr->nodeid, parent_node ? parent_node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -1098,7 +1144,7 @@
old_parent_path, sizeof(old_parent_path));
new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
new_parent_path, sizeof(new_parent_path));
- TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
+ TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
old_name, new_name,
hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
req->newdir, new_parent_node ? new_parent_node->name : "?");
@@ -1182,7 +1228,7 @@
pthread_mutex_lock(&fuse->lock);
has_rw = get_caller_has_rw_locked(fuse, hdr);
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
- TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
+ TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
req->flags, hdr->nodeid, node ? node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -1218,21 +1264,22 @@
__u32 size = req->size;
__u64 offset = req->offset;
int res;
+ __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
/* Don't access any other fields of hdr or req beyond this point, the read buffer
* overlaps the request buffer and will clobber data in the request. This
* saves us 128KB per request handler thread at the cost of this scary comment. */
- TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
- h, h->fd, size, offset);
- if (size > sizeof(handler->read_buffer)) {
+ TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
+ h, h->fd, size, (uint64_t) offset);
+ if (size > MAX_READ) {
return -EINVAL;
}
- res = pread64(h->fd, handler->read_buffer, size, offset);
+ res = pread64(h->fd, read_buffer, size, offset);
if (res < 0) {
return -errno;
}
- fuse_reply(fuse, unique, handler->read_buffer, res);
+ fuse_reply(fuse, unique, read_buffer, res);
return NO_STATUS;
}
@@ -1243,14 +1290,21 @@
struct fuse_write_out out;
struct handle *h = id_to_ptr(req->fh);
int res;
+ __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
- TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
+ if (req->flags & O_DIRECT) {
+ memcpy(aligned_buffer, buffer, req->size);
+ buffer = (const __u8*) aligned_buffer;
+ }
+
+ TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
h, h->fd, req->size, req->offset);
res = pwrite64(h->fd, buffer, req->size, req->offset);
if (res < 0) {
return -errno;
}
out.size = res;
+ out.padding = 0;
fuse_reply(fuse, hdr->unique, &out, sizeof(out));
return NO_STATUS;
}
@@ -1300,14 +1354,23 @@
static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
{
- int is_data_sync = req->fsync_flags & 1;
- struct handle *h = id_to_ptr(req->fh);
- int res;
+ bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
+ bool is_data_sync = req->fsync_flags & 1;
- TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
- h, h->fd, is_data_sync);
- res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
- if (res < 0) {
+ int fd = -1;
+ if (is_dir) {
+ struct dirhandle *dh = id_to_ptr(req->fh);
+ fd = dirfd(dh->d);
+ } else {
+ struct handle *h = id_to_ptr(req->fh);
+ fd = h->fd;
+ }
+
+ TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
+ is_dir ? "FSYNCDIR" : "FSYNC",
+ id_to_ptr(req->fh), fd, is_data_sync);
+ int res = is_data_sync ? fdatasync(fd) : fsync(fd);
+ if (res == -1) {
return -errno;
}
return 0;
@@ -1330,7 +1393,7 @@
pthread_mutex_lock(&fuse->lock);
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
- TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
+ TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
hdr->nodeid, node ? node->name : "?");
pthread_mutex_unlock(&fuse->lock);
@@ -1401,17 +1464,42 @@
const struct fuse_in_header* hdr, const struct fuse_init_in* req)
{
struct fuse_init_out out;
+ size_t fuse_struct_size;
TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
handler->token, req->major, req->minor, req->max_readahead, req->flags);
+
+ /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
+ * defined (fuse version 7.6). The structure is the same from 7.6 through
+ * 7.22. Beginning with 7.23, the structure increased in size and added
+ * new parameters.
+ */
+ if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
+ ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
+ req->major, req->minor, FUSE_KERNEL_VERSION);
+ return -1;
+ }
+
+ out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
+ fuse_struct_size = sizeof(out);
+#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
+ /* FUSE_KERNEL_VERSION >= 23. */
+
+ /* If the kernel only works on minor revs older than or equal to 22,
+ * then use the older structure size since this code only uses the 7.22
+ * version of the structure. */
+ if (req->minor <= 22) {
+ fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
+ }
+#endif
+
out.major = FUSE_KERNEL_VERSION;
- out.minor = FUSE_KERNEL_MINOR_VERSION;
out.max_readahead = req->max_readahead;
out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
out.max_background = 32;
out.congestion_threshold = 32;
out.max_write = MAX_WRITE;
- fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+ fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
return NO_STATUS;
}
@@ -1496,7 +1584,8 @@
return handle_release(fuse, handler, hdr, req);
}
- case FUSE_FSYNC: {
+ case FUSE_FSYNC:
+ case FUSE_FSYNCDIR: {
const struct fuse_fsync_in *req = data;
return handle_fsync(fuse, handler, hdr, req);
}
@@ -1524,14 +1613,13 @@
return handle_releasedir(fuse, handler, hdr, req);
}
-// case FUSE_FSYNCDIR:
case FUSE_INIT: { /* init_in -> init_out */
const struct fuse_init_in *req = data;
return handle_init(fuse, handler, hdr, req);
}
default: {
- TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
+ TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
return -ENOSYS;
}
@@ -1634,7 +1722,7 @@
}
}
- TRACE("read_package_list: found %d packages, %d with write_gid\n",
+ TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
hashmapSize(fuse->package_to_appid),
hashmapSize(fuse->appid_with_rw));
fclose(file);
@@ -1781,7 +1869,7 @@
"fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
fd, uid, gid);
- res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
+ res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC, opts);
if (res < 0) {
ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
goto error;
@@ -1831,6 +1919,7 @@
bool split_perms = false;
int i;
struct rlimit rlim;
+ int fs_version;
int opt;
while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
@@ -1905,6 +1994,11 @@
ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
}
+ while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
+ ERROR("installd fs upgrade not yet complete. Waiting...\n");
+ sleep(1);
+ }
+
res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
return res < 0 ? 1 : 0;
}
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index cdde20d..959dc22 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -1,105 +1,94 @@
LOCAL_PATH:= $(call my-dir)
+
+
+common_cflags := \
+ -std=gnu99 \
+ -Werror -Wno-unused-parameter \
+ -I$(LOCAL_PATH)/upstream-netbsd/include/ \
+ -include bsd-compatibility.h \
+
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ upstream-netbsd/bin/dd/args.c \
+ upstream-netbsd/bin/dd/conv.c \
+ upstream-netbsd/bin/dd/dd.c \
+ upstream-netbsd/bin/dd/dd_hostops.c \
+ upstream-netbsd/bin/dd/misc.c \
+ upstream-netbsd/bin/dd/position.c \
+ upstream-netbsd/lib/libc/gen/getbsize.c \
+ upstream-netbsd/lib/libc/gen/humanize_number.c \
+ upstream-netbsd/lib/libc/stdlib/strsuftoll.c \
+ upstream-netbsd/lib/libc/string/swab.c \
+ upstream-netbsd/lib/libutil/raise_default_signal.c
+LOCAL_CFLAGS += $(common_cflags) -Dmain=dd_main -DNO_CONV
+LOCAL_MODULE := libtoolbox_dd
+LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := upstream-netbsd/usr.bin/du/du.c
+LOCAL_CFLAGS += $(common_cflags) -Dmain=du_main
+LOCAL_MODULE := libtoolbox_du
+LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+include $(BUILD_STATIC_LIBRARY)
+
+
include $(CLEAR_VARS)
-TOOLS := \
- ls \
- mount \
- cat \
- ps \
- kill \
- ln \
- insmod \
- rmmod \
- lsmod \
- ifconfig \
- rm \
- mkdir \
- rmdir \
- getevent \
- sendevent \
- date \
- wipe \
- sync \
- umount \
- start \
- stop \
- notify \
- cmp \
- dmesg \
- route \
- hd \
- dd \
- df \
- getprop \
- setprop \
- watchprops \
- log \
- sleep \
- renice \
- printenv \
- smd \
- chmod \
- chown \
- newfs_msdos \
- netstat \
- ioctl \
- mv \
- schedtop \
- top \
- iftop \
- id \
- uptime \
- vmstat \
- nandread \
- ionice \
- touch \
- lsof \
- du \
- md5 \
- clear \
- getenforce \
- setenforce \
- chcon \
- restorecon \
- runcon \
- getsebool \
- setsebool \
- load_policy \
- swapon \
- swapoff \
- mkswap \
- readlink \
- mknod \
- nohup
+BSD_TOOLS := \
+ dd \
+ du \
-ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-TOOLS += r
-endif
+OUR_TOOLS := \
+ df \
+ getevent \
+ getprop \
+ iftop \
+ ioctl \
+ ionice \
+ load_policy \
+ log \
+ ls \
+ lsof \
+ mount \
+ nandread \
+ newfs_msdos \
+ ps \
+ prlimit \
+ renice \
+ restorecon \
+ route \
+ runcon \
+ sendevent \
+ setprop \
+ start \
+ stop \
+ top \
+ umount \
+ uptime \
+ watchprops \
-ALL_TOOLS = $(TOOLS)
-ALL_TOOLS += \
- cp \
- grep
+ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS)
LOCAL_SRC_FILES := \
- dynarray.c \
- toolbox.c \
- $(patsubst %,%.c,$(TOOLS)) \
- cp/cp.c cp/utils.c \
- grep/grep.c grep/fastgrep.c grep/file.c grep/queue.c grep/util.c
+ dynarray.c \
+ toolbox.c \
+ $(patsubst %,%.c,$(OUR_TOOLS)) \
-LOCAL_C_INCLUDES := bionic/libc/bionic
-
-LOCAL_CFLAGS += -Wno-unused-parameter
+LOCAL_CFLAGS += $(common_cflags)
LOCAL_SHARED_LIBRARIES := \
- libcutils \
- liblog \
- libc \
- libusbhost \
- libselinux
+ libcutils \
+ libselinux \
+
+LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS))
LOCAL_MODULE := toolbox
+LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+
+# Install the symlinks.
+LOCAL_POST_INSTALL_CMD := $(hide) $(foreach t,$(ALL_TOOLS),ln -sf toolbox $(TARGET_OUT)/bin/$(t);)
# Including this will define $(intermediates).
#
@@ -114,19 +103,26 @@
$(TOOLS_H):
$(transform-generated-source)
-# Make #!/system/bin/toolbox launchers for each tool.
-#
-SYMLINKS := $(addprefix $(TARGET_OUT)/bin/,$(ALL_TOOLS))
-$(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE)
-$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
- @echo "Symlink: $@ -> $(TOOLBOX_BINARY)"
- @mkdir -p $(dir $@)
- @rm -rf $@
- $(hide) ln -sf $(TOOLBOX_BINARY) $@
-ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
+# We only want 'r' on userdebug and eng builds.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := r.c
+LOCAL_CFLAGS += $(common_cflags)
+LOCAL_MODULE := r
+LOCAL_MODULE_TAGS := debug
+LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+include $(BUILD_EXECUTABLE)
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
- $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
+
+# We build BSD grep separately, so it can provide egrep and fgrep too.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ upstream-netbsd/usr.bin/grep/fastgrep.c \
+ upstream-netbsd/usr.bin/grep/file.c \
+ upstream-netbsd/usr.bin/grep/grep.c \
+ upstream-netbsd/usr.bin/grep/queue.c \
+ upstream-netbsd/usr.bin/grep/util.c
+LOCAL_CFLAGS += $(common_cflags)
+LOCAL_MODULE := grep
+LOCAL_POST_INSTALL_CMD := $(hide) $(foreach t,egrep fgrep,ln -sf grep $(TARGET_OUT)/bin/$(t);)
+include $(BUILD_EXECUTABLE)
diff --git a/toolbox/NOTICE b/toolbox/NOTICE
index 895b49a..e9ab58d 100644
--- a/toolbox/NOTICE
+++ b/toolbox/NOTICE
@@ -1,5 +1,20 @@
+Copyright (C) 2010 The Android Open Source Project
-Copyright (c) 2010, 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.
+
+-------------------------------------------------------------------
+
+Copyright (C) 2014, The Android Open Source Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -9,12 +24,8 @@
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
+ the documentation and/or other materials provided with the
distribution.
- * Neither the name of The Android Open Source Project nor the names
- of its contributors may be used to endorse or promote products
- derived from this software without specific prior written
- permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -23,86 +34,16 @@
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
+-------------------------------------------------------------------
-Copyright (c) 2009, The Android Open Source Project.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of The Android Open Source Project nor the names
- of its contributors may be used to endorse or promote products
- derived from this software without specific prior written
- permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
-
-Copyright (c) 2008, The Android Open Source Project.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of The Android Open Source Project nor the names
- of its contributors may be used to endorse or promote products
- derived from this software without specific prior written
- permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
-
-Copyright (c) 1998 Robert Nordier
-Copyright (c) 1989, 1993
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Kevin Fall.
-This code is derived from software contributed to Berkeley by
-Keith Muller of the University of California, San Diego and Lance
-Visser of Convex Computer Corporation.
-This code is derived from software contributed to Berkeley by
-Mike Muuss.
+Copyright (c) 1987, 1993
+ The Regents of the University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -128,66 +69,919 @@
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
+-------------------------------------------------------------------
- Copyright (c) 1989, 1993
- The Regents of the University of California. All rights reserved.
+Copyright (c) 1987, 1993, 1994
+ The Regents of the University of California. All rights reserved.
- This code is derived from software contributed to Berkeley by
- Kevin Fall.
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
- THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
+-------------------------------------------------------------------
+Copyright (c) 1988, 1993
+ The Regents of the University of California. All rights reserved.
- Copyright (c) 1991, 1993, 1994
- The Regents of the University of California. All rights reserved.
+This code is derived from software contributed to Berkeley by
+Jeffrey Mogul.
- This code is derived from software contributed to Berkeley by
- Keith Muller of the University of California, San Diego and Lance
- Visser of Convex Computer Corporation.
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
- THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
+-------------------------------------------------------------------
+
+Copyright (c) 1988, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1988, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+David Hitz of Auspex Systems Inc.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1988, 1993, 1994, 2003
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1989, 1993
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1989, 1993
+ The Regents of the University of California. All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+Kevin Fall.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1989, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+Chris Newcomb.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1989, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+Ken Smith of The State University of New York at Buffalo.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1990, 1993, 1994, 2003
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1991, 1993
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1991, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1991, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+Keith Muller of the University of California, San Diego and Lance
+Visser of Convex Computer Corporation.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1992, 1993, 1994
+ The Regents of the University of California. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
+All rights reserved.
+
+This code is derived from software contributed to The NetBSD Foundation
+by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1998 Robert Nordier
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
+OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+Copyright (C) 2008 Gabor Kovesdan <gabor@FreeBSD.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
+Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
+Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
+All rights reserved.
+
+This code is derived from software contributed to The NetBSD Foundation
+by Luke Mewburn.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2007 The NetBSD Foundation, Inc.
+All rights reserved.
+
+This code is derived from software contributed to The NetBSD Foundation
+by Luke Mewburn.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2008, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2009, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2010 The NetBSD Foundation, Inc.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2010, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2012, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2013, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
+Copyright (c) 2014, The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google, Inc. nor the names of its contributors
+ may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
diff --git a/toolbox/alarm.c b/toolbox/alarm.c
deleted file mode 100644
index 9bd58aa..0000000
--- a/toolbox/alarm.c
+++ /dev/null
@@ -1,190 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <asm/ioctl.h>
-//#include <linux/rtc.h>
-#include <linux/android_alarm.h>
-
-int alarm_main(int argc, char *argv[])
-{
- int c;
- int res;
- struct tm tm;
- time_t t;
- struct timespec ts;
-// struct rtc_time rtc_time;
- char strbuf[26];
- int afd;
- int nfd;
-// struct timeval timeout = { 0, 0 };
- int wait = 0;
- fd_set rfds;
- const char wake_lock_id[] = "alarm_test";
- int waitalarmmask = 0;
-
- int useutc = 0;
- android_alarm_type_t alarmtype_low = ANDROID_ALARM_RTC_WAKEUP;
- android_alarm_type_t alarmtype_high = ANDROID_ALARM_RTC_WAKEUP;
- android_alarm_type_t alarmtype = 0;
-
- do {
- //c = getopt(argc, argv, "uw:");
- c = getopt(argc, argv, "uwat:");
- if (c == EOF)
- break;
- switch (c) {
- case 'u':
- useutc = 1;
- break;
- case 't':
- alarmtype_low = alarmtype_high = strtol(optarg, NULL, 0);
- break;
- case 'a':
- alarmtype_low = ANDROID_ALARM_RTC_WAKEUP;
- alarmtype_high = ANDROID_ALARM_TYPE_COUNT - 1;
- break;
- case 'w':
- //timeout.tv_sec = strtol(optarg, NULL, 0);
- wait = 1;
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
- if(optind + 2 < argc) {
- fprintf(stderr,"%s [-uwa] [-t type] [seconds]\n", argv[0]);
- return 1;
- }
-
- afd = open("/dev/alarm", O_RDWR);
- if(afd < 0) {
- fprintf(stderr, "Unable to open rtc: %s\n", strerror(errno));
- return 1;
- }
-
- if(optind == argc) {
- for(alarmtype = alarmtype_low; alarmtype <= alarmtype_high; alarmtype++) {
- waitalarmmask |= 1U << alarmtype;
- }
-#if 0
- res = ioctl(fd, RTC_ALM_READ, &tm);
- if(res < 0) {
- fprintf(stderr, "Unable to read alarm: %s\n", strerror(errno));
- return 1;
- }
-#endif
-#if 0
- t = timegm(&tm);
- if(useutc)
- gmtime_r(&t, &tm);
- else
- localtime_r(&t, &tm);
-#endif
-#if 0
- asctime_r(&tm, strbuf);
- printf("%s", strbuf);
-#endif
- }
- else if(optind + 1 == argc) {
-#if 0
- res = ioctl(fd, RTC_RD_TIME, &tm);
- if(res < 0) {
- fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
- return 1;
- }
- asctime_r(&tm, strbuf);
- printf("Now: %s", strbuf);
- time(&tv.tv_sec);
-#endif
-#if 0
- time(&ts.tv_sec);
- ts.tv_nsec = 0;
-
- //strptime(argv[optind], NULL, &tm);
- //tv.tv_sec = mktime(&tm);
- //tv.tv_usec = 0;
-#endif
- for(alarmtype = alarmtype_low; alarmtype <= alarmtype_high; alarmtype++) {
- waitalarmmask |= 1U << alarmtype;
- res = ioctl(afd, ANDROID_ALARM_GET_TIME(alarmtype), &ts);
- if(res < 0) {
- fprintf(stderr, "Unable to get current time: %s\n", strerror(errno));
- return 1;
- }
- ts.tv_sec += strtol(argv[optind], NULL, 0);
- //strtotimeval(argv[optind], &tv);
- gmtime_r(&ts.tv_sec, &tm);
- printf("time %s -> %ld.%09ld\n", argv[optind], ts.tv_sec, ts.tv_nsec);
- asctime_r(&tm, strbuf);
- printf("Requested %s", strbuf);
-
- res = ioctl(afd, ANDROID_ALARM_SET(alarmtype), &ts);
- if(res < 0) {
- fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
- return 1;
- }
- }
-#if 0
- res = ioctl(fd, RTC_ALM_SET, &tm);
- if(res < 0) {
- fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
- return 1;
- }
- res = ioctl(fd, RTC_AIE_ON);
- if(res < 0) {
- fprintf(stderr, "Unable to enable alarm: %s\n", strerror(errno));
- return 1;
- }
-#endif
- }
- else {
- fprintf(stderr,"%s [-u] [date]\n", argv[0]);
- return 1;
- }
-
- if(wait) {
- while(waitalarmmask) {
- printf("wait for alarm %x\n", waitalarmmask);
- res = ioctl(afd, ANDROID_ALARM_WAIT);
- if(res < 0) {
- fprintf(stderr, "alarm wait failed\n");
- }
- printf("got alarm %x\n", res);
- waitalarmmask &= ~res;
- nfd = open("/sys/android_power/acquire_full_wake_lock", O_RDWR);
- write(nfd, wake_lock_id, sizeof(wake_lock_id) - 1);
- close(nfd);
- //sleep(5);
- nfd = open("/sys/android_power/release_wake_lock", O_RDWR);
- write(nfd, wake_lock_id, sizeof(wake_lock_id) - 1);
- close(nfd);
- }
- printf("done\n");
- }
-#if 0
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- res = select(fd + 1, &rfds, NULL, NULL, &timeout);
- if(res < 0) {
- fprintf(stderr, "select failed: %s\n", strerror(errno));
- return 1;
- }
- if(res > 0) {
- int event;
- read(fd, &event, sizeof(event));
- fprintf(stderr, "got %x\n", event);
- }
- else {
- fprintf(stderr, "timeout waiting for alarm\n");
- }
-#endif
-
- close(afd);
-
- return 0;
-}
diff --git a/toolbox/bsd-compatibility.h b/toolbox/bsd-compatibility.h
new file mode 100644
index 0000000..434d370
--- /dev/null
+++ b/toolbox/bsd-compatibility.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2014, The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+/* We want chown to support user.group as well as user:group. */
+#define SUPPORT_DOT
+
+/* We don't localize /system/bin! */
+#define WITHOUT_NLS
+
+// NetBSD uses _DIAGASSERT to null-check arguments and the like.
+#include <assert.h>
+#define _DIAGASSERT(e) ((e) ? (void) 0 : __assert2(__FILE__, __LINE__, __func__, #e))
+
+// TODO: update our <sys/cdefs.h> to support this properly.
+#define __type_fit(t, a) (0 == 0)
+
+// TODO: should this be in our <sys/cdefs.h>?
+#define __arraycount(a) (sizeof(a) / sizeof(a[0]))
+
+// This at least matches GNU dd(1) behavior.
+#define SIGINFO SIGUSR1
+
+#define S_ISWHT(x) false
+
+__BEGIN_DECLS
+
+/* From NetBSD <stdlib.h>. */
+#define HN_DECIMAL 0x01
+#define HN_NOSPACE 0x02
+#define HN_B 0x04
+#define HN_DIVISOR_1000 0x08
+#define HN_GETSCALE 0x10
+#define HN_AUTOSCALE 0x20
+int humanize_number(char *, size_t, int64_t, const char *, int, int);
+int dehumanize_number(const char *, int64_t *);
+char *getbsize(int *, long *);
+long long strsuftoll(const char *, const char *, long long, long long);
+long long strsuftollx(const char *, const char *, long long, long long,
+ char *, size_t);
+
+/* From NetBSD <string.h>. */
+void strmode(mode_t, char*);
+
+/* From NetBSD <sys/param.h>. */
+#define MAXBSIZE 65536
+
+/* From NetBSD <sys/stat.h>. */
+#define DEFFILEMODE (S_IRUSR | S_IWUSR)
+
+/* From NetBSD <unistd.h>. */
+void swab(const void * __restrict, void * __restrict, ssize_t);
+
+/* From NetBSD <util.h>. */
+int raise_default_signal(int);
+
+__END_DECLS
diff --git a/toolbox/cat.c b/toolbox/cat.c
deleted file mode 100644
index 6ac31f8..0000000
--- a/toolbox/cat.c
+++ /dev/null
@@ -1,291 +0,0 @@
-/* $NetBSD: cat.c,v 1.43 2004/01/04 03:31:28 jschauma Exp $ */
-
-/*
- * Copyright (c) 1989, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Kevin Fall.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <ctype.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#define CAT_BUFSIZ (4096)
-
-static int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
-static int rval;
-static const char *filename;
-
-static void
-cook_buf(FILE *fp)
-{
- int ch, gobble, line, prev;
- int stdout_err = 0;
-
- line = gobble = 0;
- for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
- if (prev == '\n') {
- if (ch == '\n') {
- if (sflag) {
- if (!gobble && putchar(ch) == EOF)
- break;
- gobble = 1;
- continue;
- }
- if (nflag) {
- if (!bflag) {
- if (fprintf(stdout,
- "%6d\t", ++line) < 0) {
- stdout_err++;
- break;
- }
- } else if (eflag) {
- if (fprintf(stdout,
- "%6s\t", "") < 0) {
- stdout_err++;
- break;
- }
- }
- }
- } else if (nflag) {
- if (fprintf(stdout, "%6d\t", ++line) < 0) {
- stdout_err++;
- break;
- }
- }
- }
- gobble = 0;
- if (ch == '\n') {
- if (eflag)
- if (putchar('$') == EOF)
- break;
- } else if (ch == '\t') {
- if (tflag) {
- if (putchar('^') == EOF || putchar('I') == EOF)
- break;
- continue;
- }
- } else if (vflag) {
- if (!isascii(ch)) {
- if (putchar('M') == EOF || putchar('-') == EOF)
- break;
- ch = (ch) & 0x7f;
- }
- if (iscntrl(ch)) {
- if (putchar('^') == EOF ||
- putchar(ch == '\177' ? '?' :
- ch | 0100) == EOF)
- break;
- continue;
- }
- }
- if (putchar(ch) == EOF)
- break;
- }
- if (stdout_err) {
- perror(filename);
- rval = 1;
- }
-}
-
-static void
-cook_args(char **argv)
-{
- FILE *fp;
-
- fp = stdin;
- filename = "stdin";
- do {
- if (*argv) {
- if (!strcmp(*argv, "-"))
- fp = stdin;
- else if ((fp = fopen(*argv,
- fflag ? "rf" : "r")) == NULL) {
- perror("fopen");
- rval = 1;
- ++argv;
- continue;
- }
- filename = *argv++;
- }
- cook_buf(fp);
- if (fp != stdin)
- fclose(fp);
- } while (*argv);
-}
-
-static void
-raw_cat(int rfd)
-{
- static char *buf;
- static char fb_buf[CAT_BUFSIZ];
- static size_t bsize;
-
- struct stat sbuf;
- ssize_t nr, nw, off;
- int wfd;
-
- wfd = fileno(stdout);
- if (buf == NULL) {
- if (fstat(wfd, &sbuf) == 0) {
- bsize = sbuf.st_blksize > CAT_BUFSIZ ?
- sbuf.st_blksize : CAT_BUFSIZ;
- buf = malloc(bsize);
- }
- if (buf == NULL) {
- buf = fb_buf;
- bsize = CAT_BUFSIZ;
- }
- }
- while ((nr = read(rfd, buf, bsize)) > 0)
- for (off = 0; nr; nr -= nw, off += nw)
- if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
- {
- perror("write");
- exit(EXIT_FAILURE);
- }
- if (nr < 0) {
- fprintf(stderr,"%s: invalid length\n", filename);
- rval = 1;
- }
-}
-
-static void
-raw_args(char **argv)
-{
- int fd;
-
- fd = fileno(stdin);
- filename = "stdin";
- do {
- if (*argv) {
- if (!strcmp(*argv, "-"))
- fd = fileno(stdin);
- else if (fflag) {
- struct stat st;
- fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
- if (fd < 0)
- goto skip;
-
- if (fstat(fd, &st) == -1) {
- close(fd);
- goto skip;
- }
- if (!S_ISREG(st.st_mode)) {
- close(fd);
- errno = EINVAL;
- goto skipnomsg;
- }
- }
- else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
-skip:
- perror(*argv);
-skipnomsg:
- rval = 1;
- ++argv;
- continue;
- }
- filename = *argv++;
- }
- raw_cat(fd);
- if (fd != fileno(stdin))
- close(fd);
- } while (*argv);
-}
-
-int
-cat_main(int argc, char *argv[])
-{
- int ch;
- struct flock stdout_lock;
-
- while ((ch = getopt(argc, argv, "beflnstv")) != -1)
- switch (ch) {
- case 'b':
- bflag = nflag = 1; /* -b implies -n */
- break;
- case 'e':
- eflag = vflag = 1; /* -e implies -v */
- break;
- case 'f':
- fflag = 1;
- break;
- case 'l':
- lflag = 1;
- break;
- case 'n':
- nflag = 1;
- break;
- case 's':
- sflag = 1;
- break;
- case 't':
- tflag = vflag = 1; /* -t implies -v */
- break;
- case 'v':
- vflag = 1;
- break;
- default:
- case '?':
- fprintf(stderr,
- "usage: cat [-beflnstv] [-] [file ...]\n");
- exit(EXIT_FAILURE);
- }
- argv += optind;
-
- if (lflag) {
- stdout_lock.l_len = 0;
- stdout_lock.l_start = 0;
- stdout_lock.l_type = F_WRLCK;
- stdout_lock.l_whence = SEEK_SET;
- if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
- {
- perror("fcntl");
- exit(EXIT_FAILURE);
- }
- }
-
- if (bflag || eflag || nflag || sflag || tflag || vflag)
- cook_args(argv);
- else
- raw_args(argv);
- if (fclose(stdout))
- {
- perror("fclose");
- exit(EXIT_FAILURE);
- }
- exit(rval);
-}
diff --git a/toolbox/chcon.c b/toolbox/chcon.c
deleted file mode 100644
index d594b9b..0000000
--- a/toolbox/chcon.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int chcon_main(int argc, char **argv)
-{
- int rc, i;
-
- if (argc < 3) {
- fprintf(stderr, "usage: %s context path...\n", argv[0]);
- exit(1);
- }
-
- for (i = 2; i < argc; i++) {
- rc = setfilecon(argv[i], argv[1]);
- if (rc < 0) {
- fprintf(stderr, "%s: Could not label %s with %s: %s\n",
- argv[0], argv[i], argv[1], strerror(errno));
- exit(2);
- }
- }
- exit(0);
-}
diff --git a/toolbox/chmod.c b/toolbox/chmod.c
deleted file mode 100644
index 2a524e9..0000000
--- a/toolbox/chmod.c
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
-#include <sys/limits.h>
-#include <sys/stat.h>
-
-#include <unistd.h>
-#include <time.h>
-
-void recurse_chmod(char* path, int mode)
-{
- struct dirent *dp;
- DIR *dir = opendir(path);
- if (dir == NULL) {
- // not a directory, carry on
- return;
- }
- char *subpath = malloc(sizeof(char)*PATH_MAX);
- int pathlen = strlen(path);
-
- while ((dp = readdir(dir)) != NULL) {
- if (strcmp(dp->d_name, ".") == 0 ||
- strcmp(dp->d_name, "..") == 0) continue;
-
- if (strlen(dp->d_name) + pathlen + 2/*NUL and slash*/ > PATH_MAX) {
- fprintf(stderr, "Invalid path specified: too long\n");
- exit(1);
- }
-
- strcpy(subpath, path);
- strcat(subpath, "/");
- strcat(subpath, dp->d_name);
-
- if (chmod(subpath, mode) < 0) {
- fprintf(stderr, "Unable to chmod %s: %s\n", subpath, strerror(errno));
- exit(1);
- }
-
- recurse_chmod(subpath, mode);
- }
- free(subpath);
- closedir(dir);
-}
-
-static int usage()
-{
- fprintf(stderr, "Usage: chmod [OPTION] <MODE> <FILE>\n");
- fprintf(stderr, " -R, --recursive change files and directories recursively\n");
- fprintf(stderr, " --help display this help and exit\n");
-
- return 10;
-}
-
-int chmod_main(int argc, char **argv)
-{
- int i;
-
- if (argc < 3 || strcmp(argv[1], "--help") == 0) {
- return usage();
- }
-
- int recursive = (strcmp(argv[1], "-R") == 0 ||
- strcmp(argv[1], "--recursive") == 0) ? 1 : 0;
-
- if (recursive && argc < 4) {
- return usage();
- }
-
- if (recursive) {
- argc--;
- argv++;
- }
-
- int mode = 0;
- const char* s = argv[1];
- while (*s) {
- if (*s >= '0' && *s <= '7') {
- mode = (mode<<3) | (*s-'0');
- }
- else {
- fprintf(stderr, "Bad mode\n");
- return 10;
- }
- s++;
- }
-
- for (i = 2; i < argc; i++) {
- if (chmod(argv[i], mode) < 0) {
- fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
- return 10;
- }
- if (recursive) {
- recurse_chmod(argv[i], mode);
- }
- }
- return 0;
-}
-
diff --git a/toolbox/chown.c b/toolbox/chown.c
deleted file mode 100644
index 92efee6..0000000
--- a/toolbox/chown.c
+++ /dev/null
@@ -1,73 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-
-#include <unistd.h>
-#include <time.h>
-
-int chown_main(int argc, char **argv)
-{
- int i;
-
- if (argc < 3) {
- fprintf(stderr, "Usage: chown <USER>[:GROUP] <FILE1> [FILE2] ...\n");
- return 10;
- }
-
- // Copy argv[1] to 'user' so we can truncate it at the period
- // if a group id specified.
- char user[32];
- char *group = NULL;
- strncpy(user, argv[1], sizeof(user));
- if ((group = strchr(user, ':')) != NULL) {
- *group++ = '\0';
- } else if ((group = strchr(user, '.')) != NULL) {
- *group++ = '\0';
- }
-
- // Lookup uid (and gid if specified)
- struct passwd *pw;
- struct group *grp = NULL;
- uid_t uid;
- gid_t gid = -1; // passing -1 to chown preserves current group
-
- pw = getpwnam(user);
- if (pw != NULL) {
- uid = pw->pw_uid;
- } else {
- char* endptr;
- uid = (int) strtoul(user, &endptr, 0);
- if (endptr == user) { // no conversion
- fprintf(stderr, "No such user '%s'\n", user);
- return 10;
- }
- }
-
- if (group != NULL) {
- grp = getgrnam(group);
- if (grp != NULL) {
- gid = grp->gr_gid;
- } else {
- char* endptr;
- gid = (int) strtoul(group, &endptr, 0);
- if (endptr == group) { // no conversion
- fprintf(stderr, "No such group '%s'\n", group);
- return 10;
- }
- }
- }
-
- for (i = 2; i < argc; i++) {
- if (chown(argv[i], uid, gid) < 0) {
- fprintf(stderr, "Unable to chown %s: %s\n", argv[i], strerror(errno));
- return 10;
- }
- }
-
- return 0;
-}
diff --git a/toolbox/clear.c b/toolbox/clear.c
deleted file mode 100644
index df46ad2..0000000
--- a/toolbox/clear.c
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2012, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdio.h>
-
-int clear_main(int argc, char **argv) {
- /* This prints the clear screen and move cursor to top-left corner control
- * characters for VT100 terminals. This means it will not work on
- * non-VT100 compliant terminals, namely Windows' cmd.exe, but should
- * work on anything unix-y. */
- fputs("\x1b[2J\x1b[H", stdout);
- return 0;
-}
diff --git a/toolbox/cmp.c b/toolbox/cmp.c
deleted file mode 100644
index 80635ad..0000000
--- a/toolbox/cmp.c
+++ /dev/null
@@ -1,91 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-
-int cmp_main(int argc, char *argv[])
-{
- int c;
- int fd1, fd2;
- char buf1[4096], buf2[4096];
- int res, res1, res2;
- int rv = 0;
- int i;
- int filepos = 0;
-
- int show_byte = 0;
- int show_all = 0;
- int limit = 0;
-
- do {
- c = getopt(argc, argv, "bln:");
- if (c == EOF)
- break;
- switch (c) {
- case 'b':
- show_byte = 1;
- break;
- case 'l':
- show_all = 1;
- break;
- case 'n':
- limit = atoi(optarg);
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
-
- if (optind + 2 != argc) {
- fprintf(stderr, "Usage: %s [-b] [-l] [-n count] file1 file2\n", argv[0]);
- exit(1);
- }
-
- fd1 = open(argv[optind], O_RDONLY);
- if(fd1 < 0) {
- fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
- return 1;
- }
-
- fd2 = open(argv[optind+1], O_RDONLY);
- if(fd2 < 0) {
- fprintf(stderr, "could not open %s, %s\n", argv[optind+1], strerror(errno));
- return 1;
- }
-
- while(1) {
- res1 = read(fd1, &buf1, sizeof(buf1));
- res2 = read(fd2, &buf2, sizeof(buf2));
- res = res1 < res2 ? res1 : res2;
- if(res1 == 0 && res2 == 0) {
- return rv;
- }
- for(i = 0; i < res; i++) {
- if(buf1[i] != buf2[i]) {
- printf("%s %s differ byte %d", argv[optind], argv[optind+1], filepos + i);
- if(show_byte)
- printf(" 0x%02x 0x%02x", buf1[i], buf2[i]);
- printf("\n");
- if(!show_all)
- return 1;
- rv = 1;
- }
- if(limit) {
- limit--;
- if(limit == 0)
- return rv;
- }
- }
- if(res1 != res2 || res < 0) {
- printf("%s on %s\n", res < 0 ? "Read error" : "EOF", res1 < res2 ? argv[optind] : argv[optind+1]);
- return 1;
- }
- filepos += res;
- }
-}
diff --git a/toolbox/cp/cp.c b/toolbox/cp/cp.c
deleted file mode 100644
index bd3c70e..0000000
--- a/toolbox/cp/cp.c
+++ /dev/null
@@ -1,552 +0,0 @@
-/* $NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $ */
-
-/*
- * Copyright (c) 1988, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * David Hitz of Auspex Systems Inc.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT(
-"@(#) Copyright (c) 1988, 1993, 1994\
- The Regents of the University of California. All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
-#else
-__RCSID("$NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $");
-#endif
-#endif /* not lint */
-
-/*
- * Cp copies source files to target files.
- *
- * The global PATH_T structure "to" always contains the path to the
- * current target file. Since fts(3) does not change directories,
- * this path can be either absolute or dot-relative.
- *
- * The basic algorithm is to initialize "to" and use fts(3) to traverse
- * the file hierarchy rooted in the argument list. A trivial case is the
- * case of 'cp file1 file2'. The more interesting case is the case of
- * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
- * path (relative to the root of the traversal) is appended to dir (stored
- * in "to") to form the final target path.
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <assert.h>
-#include <err.h>
-#include <errno.h>
-#include <fts.h>
-#include <locale.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "extern.h"
-
-#define STRIP_TRAILING_SLASH(p) { \
- while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
- *--(p).p_end = '\0'; \
-}
-
-static char empty[] = "";
-PATH_T to = { .p_end = to.p_path, .target_end = empty };
-
-uid_t myuid;
-int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag, vflag, Nflag;
-mode_t myumask;
-sig_atomic_t pinfo;
-
-enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
-
-static int copy(char *[], enum op, int);
-
-static void
-progress(int sig __unused)
-{
-
- pinfo++;
-}
-
-int
-cp_main(int argc, char *argv[])
-{
- struct stat to_stat, tmp_stat;
- enum op type;
- int ch, fts_options, r, have_trailing_slash;
- char *target, **src;
-
-#ifndef ANDROID
- setprogname(argv[0]);
-#endif
- (void)setlocale(LC_ALL, "");
-
- Hflag = Lflag = Pflag = Rflag = 0;
- while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1)
- switch (ch) {
- case 'H':
- Hflag = 1;
- Lflag = Pflag = 0;
- break;
- case 'L':
- Lflag = 1;
- Hflag = Pflag = 0;
- break;
- case 'N':
- Nflag = 1;
- break;
- case 'P':
- Pflag = 1;
- Hflag = Lflag = 0;
- break;
- case 'R':
- Rflag = 1;
- break;
- case 'a':
- Pflag = 1;
- pflag = 1;
- Rflag = 1;
- Hflag = Lflag = 0;
- break;
- case 'f':
- fflag = 1;
- iflag = 0;
- break;
- case 'i':
- iflag = isatty(fileno(stdin));
- fflag = 0;
- break;
- case 'l':
- lflag = 1;
- break;
- case 'p':
- pflag = 1;
- break;
- case 'r':
- rflag = 1;
- break;
- case 'v':
- vflag = 1;
- break;
- case '?':
- default:
- cp_usage();
- /* NOTREACHED */
- }
- argc -= optind;
- argv += optind;
-
- if (argc < 2)
- cp_usage();
-
- fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
- if (rflag) {
- if (Rflag) {
- errx(EXIT_FAILURE,
- "the -R and -r options may not be specified together.");
- /* NOTREACHED */
- }
- if (Hflag || Lflag || Pflag) {
- errx(EXIT_FAILURE,
- "the -H, -L, and -P options may not be specified with the -r option.");
- /* NOTREACHED */
- }
- fts_options &= ~FTS_PHYSICAL;
- fts_options |= FTS_LOGICAL;
- }
-
- if (Rflag) {
- if (Hflag)
- fts_options |= FTS_COMFOLLOW;
- if (Lflag) {
- fts_options &= ~FTS_PHYSICAL;
- fts_options |= FTS_LOGICAL;
- }
- } else if (!Pflag) {
- fts_options &= ~FTS_PHYSICAL;
- fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
- }
-
- myuid = getuid();
-
- /* Copy the umask for explicit mode setting. */
- myumask = umask(0);
- (void)umask(myumask);
-
- /* Save the target base in "to". */
- target = argv[--argc];
- if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
- errx(EXIT_FAILURE, "%s: name too long", target);
- to.p_end = to.p_path + strlen(to.p_path);
- have_trailing_slash = (to.p_end[-1] == '/');
- if (have_trailing_slash)
- STRIP_TRAILING_SLASH(to);
- to.target_end = to.p_end;
-
- /* Set end of argument list for fts(3). */
- argv[argc] = NULL;
-
-#ifndef ANDROID
- (void)signal(SIGINFO, progress);
-#endif
-
- /*
- * Cp has two distinct cases:
- *
- * cp [-R] source target
- * cp [-R] source1 ... sourceN directory
- *
- * In both cases, source can be either a file or a directory.
- *
- * In (1), the target becomes a copy of the source. That is, if the
- * source is a file, the target will be a file, and likewise for
- * directories.
- *
- * In (2), the real target is not directory, but "directory/source".
- */
- if (Pflag)
- r = lstat(to.p_path, &to_stat);
- else
- r = stat(to.p_path, &to_stat);
- if (r == -1 && errno != ENOENT) {
- err(EXIT_FAILURE, "%s", to.p_path);
- /* NOTREACHED */
- }
- if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
- /*
- * Case (1). Target is not a directory.
- */
- if (argc > 1)
- cp_usage();
- /*
- * Need to detect the case:
- * cp -R dir foo
- * Where dir is a directory and foo does not exist, where
- * we want pathname concatenations turned on but not for
- * the initial mkdir().
- */
- if (r == -1) {
- if (rflag || (Rflag && (Lflag || Hflag)))
- r = stat(*argv, &tmp_stat);
- else
- r = lstat(*argv, &tmp_stat);
- if (r == -1) {
- err(EXIT_FAILURE, "%s", *argv);
- /* NOTREACHED */
- }
-
- if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
- type = DIR_TO_DNE;
- else
- type = FILE_TO_FILE;
- } else
- type = FILE_TO_FILE;
-
- if (have_trailing_slash && type == FILE_TO_FILE) {
- if (r == -1)
- errx(1, "directory %s does not exist",
- to.p_path);
- else
- errx(1, "%s is not a directory", to.p_path);
- }
- } else {
- /*
- * Case (2). Target is a directory.
- */
- type = FILE_TO_DIR;
- }
-
- /*
- * make "cp -rp src/ dst" behave like "cp -rp src dst" not
- * like "cp -rp src/. dst"
- */
- for (src = argv; *src; src++) {
- size_t len = strlen(*src);
- while (len-- > 1 && (*src)[len] == '/')
- (*src)[len] = '\0';
- }
-
- exit(copy(argv, type, fts_options));
- /* NOTREACHED */
-}
-
-static int dnestack[MAXPATHLEN]; /* unlikely we'll have more nested dirs */
-static ssize_t dnesp;
-static void
-pushdne(int dne)
-{
-
- dnestack[dnesp++] = dne;
- assert(dnesp < MAXPATHLEN);
-}
-
-static int
-popdne(void)
-{
- int rv;
-
- rv = dnestack[--dnesp];
- assert(dnesp >= 0);
- return rv;
-}
-
-static int
-copy(char *argv[], enum op type, int fts_options)
-{
- struct stat to_stat;
- FTS *ftsp;
- FTSENT *curr;
- int base, dne, sval;
- int this_failed, any_failed;
- size_t nlen;
- char *p, *target_mid;
-
- base = 0; /* XXX gcc -Wuninitialized (see comment below) */
-
- if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
- err(EXIT_FAILURE, "%s", argv[0]);
- /* NOTREACHED */
- for (any_failed = 0; (curr = fts_read(ftsp)) != NULL;) {
- this_failed = 0;
- switch (curr->fts_info) {
- case FTS_NS:
- case FTS_DNR:
- case FTS_ERR:
- warnx("%s: %s", curr->fts_path,
- strerror(curr->fts_errno));
- this_failed = any_failed = 1;
- continue;
- case FTS_DC: /* Warn, continue. */
- warnx("%s: directory causes a cycle", curr->fts_path);
- this_failed = any_failed = 1;
- continue;
- }
-
- /*
- * If we are in case (2) or (3) above, we need to append the
- * source name to the target name.
- */
- if (type != FILE_TO_FILE) {
- if ((curr->fts_namelen +
- to.target_end - to.p_path + 1) > MAXPATHLEN) {
- warnx("%s/%s: name too long (not copied)",
- to.p_path, curr->fts_name);
- this_failed = any_failed = 1;
- continue;
- }
-
- /*
- * Need to remember the roots of traversals to create
- * correct pathnames. If there's a directory being
- * copied to a non-existent directory, e.g.
- * cp -R a/dir noexist
- * the resulting path name should be noexist/foo, not
- * noexist/dir/foo (where foo is a file in dir), which
- * is the case where the target exists.
- *
- * Also, check for "..". This is for correct path
- * concatentation for paths ending in "..", e.g.
- * cp -R .. /tmp
- * Paths ending in ".." are changed to ".". This is
- * tricky, but seems the easiest way to fix the problem.
- *
- * XXX
- * Since the first level MUST be FTS_ROOTLEVEL, base
- * is always initialized.
- */
- if (curr->fts_level == FTS_ROOTLEVEL) {
- if (type != DIR_TO_DNE) {
- p = strrchr(curr->fts_path, '/');
- base = (p == NULL) ? 0 :
- (int)(p - curr->fts_path + 1);
-
- if (!strcmp(&curr->fts_path[base],
- ".."))
- base += 1;
- } else
- base = curr->fts_pathlen;
- }
-
- p = &curr->fts_path[base];
- nlen = curr->fts_pathlen - base;
- target_mid = to.target_end;
- if (*p != '/' && target_mid[-1] != '/')
- *target_mid++ = '/';
- *target_mid = 0;
-
- if (target_mid - to.p_path + nlen >= PATH_MAX) {
- warnx("%s%s: name too long (not copied)",
- to.p_path, p);
- this_failed = any_failed = 1;
- continue;
- }
- (void)strncat(target_mid, p, nlen);
- to.p_end = target_mid + nlen;
- *to.p_end = 0;
- STRIP_TRAILING_SLASH(to);
- }
-
- sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat);
- /* Not an error but need to remember it happened */
- if (sval == -1)
- dne = 1;
- else {
- if (to_stat.st_dev == curr->fts_statp->st_dev &&
- to_stat.st_ino == curr->fts_statp->st_ino) {
- warnx("%s and %s are identical (not copied).",
- to.p_path, curr->fts_path);
- this_failed = any_failed = 1;
- if (S_ISDIR(curr->fts_statp->st_mode))
- (void)fts_set(ftsp, curr, FTS_SKIP);
- continue;
- }
- if (!S_ISDIR(curr->fts_statp->st_mode) &&
- S_ISDIR(to_stat.st_mode)) {
- warnx("cannot overwrite directory %s with non-directory %s",
- to.p_path, curr->fts_path);
- this_failed = any_failed = 1;
- continue;
- }
- dne = 0;
- }
-
- switch (curr->fts_statp->st_mode & S_IFMT) {
- case S_IFLNK:
- /* Catch special case of a non dangling symlink */
- if((fts_options & FTS_LOGICAL) ||
- ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
- if (copy_file(curr, dne))
- this_failed = any_failed = 1;
- } else {
- if (copy_link(curr, !dne))
- this_failed = any_failed = 1;
- }
- break;
- case S_IFDIR:
- if (!Rflag && !rflag) {
- if (curr->fts_info == FTS_D)
- warnx("%s is a directory (not copied).",
- curr->fts_path);
- (void)fts_set(ftsp, curr, FTS_SKIP);
- this_failed = any_failed = 1;
- break;
- }
-
- /*
- * Directories get noticed twice:
- * In the first pass, create it if needed.
- * In the second pass, after the children have been copied, set the permissions.
- */
- if (curr->fts_info == FTS_D) /* First pass */
- {
- /*
- * If the directory doesn't exist, create the new
- * one with the from file mode plus owner RWX bits,
- * modified by the umask. Trade-off between being
- * able to write the directory (if from directory is
- * 555) and not causing a permissions race. If the
- * umask blocks owner writes, we fail..
- */
- pushdne(dne);
- if (dne) {
- if (mkdir(to.p_path,
- curr->fts_statp->st_mode | S_IRWXU) < 0)
- err(EXIT_FAILURE, "%s",
- to.p_path);
- /* NOTREACHED */
- } else if (!S_ISDIR(to_stat.st_mode)) {
- errno = ENOTDIR;
- err(EXIT_FAILURE, "%s",
- to.p_path);
- /* NOTREACHED */
- }
- }
- else if (curr->fts_info == FTS_DP) /* Second pass */
- {
- /*
- * If not -p and directory didn't exist, set it to be
- * the same as the from directory, umodified by the
- * umask; arguably wrong, but it's been that way
- * forever.
- */
- if (pflag && setfile(curr->fts_statp, 0))
- this_failed = any_failed = 1;
- else if ((dne = popdne()))
- (void)chmod(to.p_path,
- curr->fts_statp->st_mode);
- }
- else
- {
- warnx("directory %s encountered when not expected.",
- curr->fts_path);
- this_failed = any_failed = 1;
- break;
- }
-
- break;
- case S_IFBLK:
- case S_IFCHR:
- if (Rflag) {
- if (copy_special(curr->fts_statp, !dne))
- this_failed = any_failed = 1;
- } else
- if (copy_file(curr, dne))
- this_failed = any_failed = 1;
- break;
- case S_IFIFO:
- if (Rflag) {
- if (copy_fifo(curr->fts_statp, !dne))
- this_failed = any_failed = 1;
- } else
- if (copy_file(curr, dne))
- this_failed = any_failed = 1;
- break;
- default:
- if (copy_file(curr, dne))
- this_failed = any_failed = 1;
- break;
- }
- if (vflag && !this_failed)
- (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
- }
- if (errno) {
- err(EXIT_FAILURE, "fts_read");
- /* NOTREACHED */
- }
- (void)fts_close(ftsp);
- return (any_failed);
-}
diff --git a/toolbox/cp/utils.c b/toolbox/cp/utils.c
deleted file mode 100644
index b682bbe..0000000
--- a/toolbox/cp/utils.c
+++ /dev/null
@@ -1,446 +0,0 @@
-/* $NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
-#else
-__RCSID("$NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/mman.h>
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#ifndef ANDROID
-#include <sys/extattr.h>
-#endif
-
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <fts.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "extern.h"
-
-#ifdef ANDROID
-#define MAXBSIZE 65536
-#endif
-
-#define MMAP_MAX_SIZE (8 * 1048576)
-#define MMAP_MAX_WRITE (64 * 1024)
-
-int
-set_utimes(const char *file, struct stat *fs)
-{
- static struct timeval tv[2];
-
-#ifdef ANDROID
- tv[0].tv_sec = fs->st_atime;
- tv[0].tv_usec = 0;
- tv[1].tv_sec = fs->st_mtime;
- tv[1].tv_usec = 0;
-
- if (utimes(file, tv)) {
- warn("utimes: %s", file);
- return 1;
- }
-#else
- TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
- TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
-
- if (lutimes(file, tv)) {
- warn("lutimes: %s", file);
- return (1);
- }
-#endif
- return (0);
-}
-
-struct finfo {
- const char *from;
- const char *to;
- size_t size;
-};
-
-static void
-progress(const struct finfo *fi, size_t written)
-{
- int pcent = (int)((100.0 * written) / fi->size);
-
- pinfo = 0;
- (void)fprintf(stderr, "%s => %s %zu/%zu bytes %d%% written\n",
- fi->from, fi->to, written, fi->size, pcent);
-}
-
-int
-copy_file(FTSENT *entp, int dne)
-{
- static char buf[MAXBSIZE];
- struct stat to_stat, *fs;
- int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
- char *p;
- size_t ptotal = 0;
-
- if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
- warn("%s", entp->fts_path);
- return (1);
- }
-
- to_fd = -1;
- fs = entp->fts_statp;
- tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
-
- /*
- * If the file exists and we're interactive, verify with the user.
- * If the file DNE, set the mode to be the from file, minus setuid
- * bits, modified by the umask; arguably wrong, but it makes copying
- * executables work right and it's been that way forever. (The
- * other choice is 666 or'ed with the execute bits on the from file
- * modified by the umask.)
- */
- if (!dne) {
- struct stat sb;
- int sval;
-
- if (iflag) {
- (void)fprintf(stderr, "overwrite %s? ", to.p_path);
- checkch = ch = getchar();
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (checkch != 'y' && checkch != 'Y') {
- (void)close(from_fd);
- return (0);
- }
- }
-
- sval = tolnk ?
- lstat(to.p_path, &sb) : stat(to.p_path, &sb);
- if (sval == -1) {
- warn("stat: %s", to.p_path);
- (void)close(from_fd);
- return (1);
- }
-
- if (!(tolnk && S_ISLNK(sb.st_mode)))
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
- } else
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
- fs->st_mode & ~(S_ISUID | S_ISGID));
-
- if (to_fd == -1 && (fflag || tolnk)) {
- /*
- * attempt to remove existing destination file name and
- * create a new file
- */
- (void)unlink(to.p_path);
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
- fs->st_mode & ~(S_ISUID | S_ISGID));
- }
-
- if (to_fd == -1) {
- warn("%s", to.p_path);
- (void)close(from_fd);
- return (1);
- }
-
- rval = 0;
-
- /* if hard linking then simply close the open fds, link and return */
- if (lflag) {
- (void)close(from_fd);
- (void)close(to_fd);
- (void)unlink(to.p_path);
- if (link(entp->fts_path, to.p_path)) {
- warn("%s", to.p_path);
- return (1);
- }
- return (0);
- }
- /* NOTREACHED */
-
- /*
- * There's no reason to do anything other than close the file
- * now if it's empty, so let's not bother.
- */
- if (fs->st_size > 0) {
- struct finfo fi;
-
- fi.from = entp->fts_path;
- fi.to = to.p_path;
- fi.size = (size_t)fs->st_size;
-
- /*
- * Mmap and write if less than 8M (the limit is so
- * we don't totally trash memory on big files).
- * This is really a minor hack, but it wins some CPU back.
- */
- bool use_read;
-
- use_read = true;
- if (fs->st_size <= MMAP_MAX_SIZE) {
- size_t fsize = (size_t)fs->st_size;
- p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
- from_fd, (off_t)0);
- if (p != MAP_FAILED) {
- size_t remainder;
-
- use_read = false;
-
- (void) madvise(p, (size_t)fs->st_size,
- MADV_SEQUENTIAL);
-
- /*
- * Write out the data in small chunks to
- * avoid locking the output file for a
- * long time if the reading the data from
- * the source is slow.
- */
- remainder = fsize;
- do {
- ssize_t chunk;
-
- chunk = (remainder > MMAP_MAX_WRITE) ?
- MMAP_MAX_WRITE : remainder;
- if (write(to_fd, &p[fsize - remainder],
- chunk) != chunk) {
- warn("%s", to.p_path);
- rval = 1;
- break;
- }
- remainder -= chunk;
- ptotal += chunk;
- if (pinfo)
- progress(&fi, ptotal);
- } while (remainder > 0);
-
- if (munmap(p, fsize) < 0) {
- warn("%s", entp->fts_path);
- rval = 1;
- }
- }
- }
-
- if (use_read) {
- while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
- wcount = write(to_fd, buf, (size_t)rcount);
- if (rcount != wcount || wcount == -1) {
- warn("%s", to.p_path);
- rval = 1;
- break;
- }
- ptotal += wcount;
- if (pinfo)
- progress(&fi, ptotal);
- }
- if (rcount < 0) {
- warn("%s", entp->fts_path);
- rval = 1;
- }
- }
- }
-
-#ifndef ANDROID
- if (pflag && (fcpxattr(from_fd, to_fd) != 0))
- warn("%s: error copying extended attributes", to.p_path);
-#endif
-
- (void)close(from_fd);
-
- if (rval == 1) {
- (void)close(to_fd);
- return (1);
- }
-
- if (pflag && setfile(fs, to_fd))
- rval = 1;
- /*
- * If the source was setuid or setgid, lose the bits unless the
- * copy is owned by the same user and group.
- */
-#define RETAINBITS \
- (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
- if (!pflag && dne
- && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
- if (fstat(to_fd, &to_stat)) {
- warn("%s", to.p_path);
- rval = 1;
- } else if (fs->st_gid == to_stat.st_gid &&
- fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
- warn("%s", to.p_path);
- rval = 1;
- }
- }
- if (close(to_fd)) {
- warn("%s", to.p_path);
- rval = 1;
- }
- /* set the mod/access times now after close of the fd */
- if (pflag && set_utimes(to.p_path, fs)) {
- rval = 1;
- }
- return (rval);
-}
-
-int
-copy_link(FTSENT *p, int exists)
-{
- int len;
- char target[MAXPATHLEN];
-
- if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
- warn("readlink: %s", p->fts_path);
- return (1);
- }
- target[len] = '\0';
- if (exists && unlink(to.p_path)) {
- warn("unlink: %s", to.p_path);
- return (1);
- }
- if (symlink(target, to.p_path)) {
- warn("symlink: %s", target);
- return (1);
- }
- return (pflag ? setfile(p->fts_statp, 0) : 0);
-}
-
-int
-copy_fifo(struct stat *from_stat, int exists)
-{
- if (exists && unlink(to.p_path)) {
- warn("unlink: %s", to.p_path);
- return (1);
- }
- if (mkfifo(to.p_path, from_stat->st_mode)) {
- warn("mkfifo: %s", to.p_path);
- return (1);
- }
- return (pflag ? setfile(from_stat, 0) : 0);
-}
-
-int
-copy_special(struct stat *from_stat, int exists)
-{
- if (exists && unlink(to.p_path)) {
- warn("unlink: %s", to.p_path);
- return (1);
- }
- if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
- warn("mknod: %s", to.p_path);
- return (1);
- }
- return (pflag ? setfile(from_stat, 0) : 0);
-}
-
-
-/*
- * Function: setfile
- *
- * Purpose:
- * Set the owner/group/permissions for the "to" file to the information
- * in the stat structure. If fd is zero, also call set_utimes() to set
- * the mod/access times. If fd is non-zero, the caller must do a utimes
- * itself after close(fd).
- */
-int
-setfile(struct stat *fs, int fd)
-{
- int rval, islink;
-
- rval = 0;
- islink = S_ISLNK(fs->st_mode);
- fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
-
- /*
- * Changing the ownership probably won't succeed, unless we're root
- * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
- * the mode; current BSD behavior is to remove all setuid bits on
- * chown. If chown fails, lose setuid/setgid bits.
- */
- if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
- lchown(to.p_path, fs->st_uid, fs->st_gid)) {
- if (errno != EPERM) {
- warn("chown: %s", to.p_path);
- rval = 1;
- }
- fs->st_mode &= ~(S_ISUID | S_ISGID);
- }
-#ifdef ANDROID
- if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
-#else
- if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
-#endif
- warn("chmod: %s", to.p_path);
- rval = 1;
- }
-
-#ifndef ANDROID
- if (!islink && !Nflag) {
- unsigned long fflags = fs->st_flags;
- /*
- * XXX
- * NFS doesn't support chflags; ignore errors unless
- * there's reason to believe we're losing bits.
- * (Note, this still won't be right if the server
- * supports flags and we were trying to *remove* flags
- * on a file that we copied, i.e., that we didn't create.)
- */
- errno = 0;
- if ((fd ? fchflags(fd, fflags) :
- chflags(to.p_path, fflags)) == -1)
- if (errno != EOPNOTSUPP || fs->st_flags != 0) {
- warn("chflags: %s", to.p_path);
- rval = 1;
- }
- }
-#endif
- /* if fd is non-zero, caller must call set_utimes() after close() */
- if (fd == 0 && set_utimes(to.p_path, fs))
- rval = 1;
- return (rval);
-}
-
-void
-cp_usage(void)
-{
- (void)fprintf(stderr,
- "usage: cp [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
- " cp [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n");
- exit(1);
- /* NOTREACHED */
-}
diff --git a/toolbox/date.c b/toolbox/date.c
deleted file mode 100644
index d6c9052..0000000
--- a/toolbox/date.c
+++ /dev/null
@@ -1,200 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <linux/android_alarm.h>
-#include <linux/rtc.h>
-#include <sys/ioctl.h>
-
-static int settime_alarm(struct timespec *ts) {
- int fd, ret;
-
- fd = open("/dev/alarm", O_RDWR);
- if (fd < 0)
- return fd;
-
- ret = ioctl(fd, ANDROID_ALARM_SET_RTC, ts);
- close(fd);
- return ret;
-}
-
-static int settime_alarm_tm(struct tm *tm) {
- time_t t;
- struct timespec ts;
-
- t = mktime(tm);
- ts.tv_sec = t;
- ts.tv_nsec = 0;
- return settime_alarm(&ts);
-}
-
-static int settime_alarm_timeval(struct timeval *tv) {
- struct timespec ts;
-
- ts.tv_sec = tv->tv_sec;
- ts.tv_nsec = tv->tv_usec * 1000;
- return settime_alarm(&ts);
-}
-
-static int settime_rtc_tm(struct tm *tm) {
- int fd, ret;
- struct timeval tv;
- struct rtc_time rtc;
-
- fd = open("/dev/rtc0", O_RDWR);
- if (fd < 0)
- return fd;
-
- tv.tv_sec = mktime(tm);
- tv.tv_usec = 0;
-
- ret = settimeofday(&tv, NULL);
- if (ret < 0)
- goto done;
-
- memset(&rtc, 0, sizeof(rtc));
- rtc.tm_sec = tm->tm_sec;
- rtc.tm_min = tm->tm_min;
- rtc.tm_hour = tm->tm_hour;
- rtc.tm_mday = tm->tm_mday;
- rtc.tm_mon = tm->tm_mon;
- rtc.tm_year = tm->tm_year;
- rtc.tm_wday = tm->tm_wday;
- rtc.tm_yday = tm->tm_yday;
- rtc.tm_isdst = tm->tm_isdst;
-
- ret = ioctl(fd, RTC_SET_TIME, rtc);
-done:
- close(fd);
- return ret;
-}
-
-static int settime_rtc_timeval(struct timeval *tv) {
- struct tm tm, *err;
- time_t t = tv->tv_sec;
-
- err = gmtime_r(&t, &tm);
- if (!err)
- return -1;
-
- return settime_rtc_tm(&tm);
-}
-
-static void settime(char *s) {
- struct tm tm;
- int day = atoi(s);
- int hour;
-
- while (*s && *s != '.')
- s++;
-
- if (*s)
- s++;
-
- hour = atoi(s);
-
- tm.tm_year = day / 10000 - 1900;
- tm.tm_mon = (day % 10000) / 100 - 1;
- tm.tm_mday = (day % 100);
- tm.tm_hour = hour / 10000;
- tm.tm_min = (hour % 10000) / 100;
- tm.tm_sec = (hour % 100);
- tm.tm_isdst = -1;
-
- if (settime_alarm_tm(&tm) < 0)
- settime_rtc_tm(&tm);
-}
-
-int date_main(int argc, char *argv[])
-{
- int c;
- int res;
- struct tm tm;
- time_t t;
- struct timeval tv;
- struct timespec ts;
- char strbuf[260];
- int fd;
-
- int useutc = 0;
-
- tzset();
-
- do {
- c = getopt(argc, argv, "us:");
- if (c == EOF)
- break;
- switch (c) {
- case 'u':
- useutc = 1;
- break;
- case 's':
- settime(optarg);
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
- if(optind + 2 < argc) {
- fprintf(stderr,"%s [-u] [date]\n", argv[0]);
- return 1;
- }
-
- int hasfmt = argc == optind + 1 && argv[optind][0] == '+';
- if(optind == argc || hasfmt) {
- char buf[2000];
- time(&t);
- if (useutc) {
- gmtime_r(&t, &tm);
- strftime(strbuf, sizeof(strbuf),
- (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S GMT %Y"),
- &tm);
- } else {
- localtime_r(&t, &tm);
- strftime(strbuf, sizeof(strbuf),
- (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S %Z %Y"),
- &tm);
- }
- printf("%s\n", strbuf);
- }
- else if(optind + 1 == argc) {
-#if 0
- struct tm *tmptr;
- tmptr = getdate(argv[optind]);
- if(tmptr == NULL) {
- fprintf(stderr,"getdate_r failed\n");
- return 1;
- }
- tm = *tmptr;
-#if 0
- if(getdate_r(argv[optind], &tm) < 0) {
- fprintf(stderr,"getdate_r failed %s\n", strerror(errno));
- return 1;
- }
-#endif
-#endif
- //strptime(argv[optind], NULL, &tm);
- //tv.tv_sec = mktime(&tm);
- //tv.tv_usec = 0;
- strtotimeval(argv[optind], &tv);
- printf("time %s -> %lu.%lu\n", argv[optind], tv.tv_sec, tv.tv_usec);
- res = settime_alarm_timeval(&tv);
- if (res < 0)
- res = settime_rtc_timeval(&tv);
- if(res < 0) {
- fprintf(stderr,"settimeofday failed %s\n", strerror(errno));
- return 1;
- }
- }
- else {
- fprintf(stderr,"%s [-s 20070325.123456] [-u] [date]\n", argv[0]);
- return 1;
- }
-
- return 0;
-}
diff --git a/toolbox/dd.c b/toolbox/dd.c
deleted file mode 100644
index 6b61ffb..0000000
--- a/toolbox/dd.c
+++ /dev/null
@@ -1,1313 +0,0 @@
-/* $NetBSD: dd.c,v 1.37 2004/01/17 21:00:16 dbj Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
- The Regents of the University of California. All rights reserved.\n");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: dd.c,v 1.37 2004/01/17 21:00:16 dbj Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "dd.h"
-
-//#define NO_CONV
-
-//#include "extern.h"
-void block(void);
-void block_close(void);
-void dd_out(int);
-void def(void);
-void def_close(void);
-void jcl(char **);
-void pos_in(void);
-void pos_out(void);
-void summary(void);
-void summaryx(int);
-void terminate(int);
-void unblock(void);
-void unblock_close(void);
-ssize_t bwrite(int, const void *, size_t);
-
-extern IO in, out;
-extern STAT st;
-extern void (*cfunc)(void);
-extern uint64_t cpy_cnt;
-extern uint64_t cbsz;
-extern u_int ddflags;
-extern u_int files_cnt;
-extern int progress;
-extern const u_char *ctab;
-
-#define DEFFILEMODE (S_IRUSR | S_IWUSR)
-
-static void dd_close(void);
-static void dd_in(void);
-static void getfdtype(IO *);
-static int redup_clean_fd(int);
-static void setup(void);
-
-
-IO in, out; /* input/output state */
-STAT st; /* statistics */
-void (*cfunc)(void); /* conversion function */
-uint64_t cpy_cnt; /* # of blocks to copy */
-static off_t pending = 0; /* pending seek if sparse */
-u_int ddflags; /* conversion options */
-uint64_t cbsz; /* conversion block size */
-u_int files_cnt = 1; /* # of files to copy */
-int progress = 0; /* display sign of life */
-const u_char *ctab; /* conversion table */
-sigset_t infoset; /* a set blocking SIGINFO */
-
-int
-dd_main(int argc, char *argv[])
-{
- int ch;
-
- while ((ch = getopt(argc, argv, "")) != -1) {
- switch (ch) {
- default:
- fprintf(stderr, "usage: dd [operand ...]\n");
- exit(1);
- /* NOTREACHED */
- }
- }
- argc -= (optind - 1);
- argv += (optind - 1);
-
- jcl(argv);
- setup();
-
-// (void)signal(SIGINFO, summaryx);
- (void)signal(SIGINT, terminate);
- (void)sigemptyset(&infoset);
-// (void)sigaddset(&infoset, SIGINFO);
-
- (void)atexit(summary);
-
- while (files_cnt--)
- dd_in();
-
- dd_close();
- exit(0);
- /* NOTREACHED */
-}
-
-static void
-setup(void)
-{
-
- if (in.name == NULL) {
- in.name = "stdin";
- in.fd = STDIN_FILENO;
- } else {
- in.fd = open(in.name, O_RDONLY, 0);
- if (in.fd < 0) {
- fprintf(stderr, "%s: cannot open for read: %s\n",
- in.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-
- /* Ensure in.fd is outside the stdio descriptor range */
- in.fd = redup_clean_fd(in.fd);
- }
-
- getfdtype(&in);
-
- if (files_cnt > 1 && !(in.flags & ISTAPE)) {
- fprintf(stderr,
- "files is not supported for non-tape devices\n");
- exit(1);
- /* NOTREACHED */
- }
-
- if (out.name == NULL) {
- /* No way to check for read access here. */
- out.fd = STDOUT_FILENO;
- out.name = "stdout";
- } else {
-#define OFLAGS \
- (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
- out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
- /*
- * May not have read access, so try again with write only.
- * Without read we may have a problem if output also does
- * not support seeks.
- */
- if (out.fd < 0) {
- out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
- out.flags |= NOREAD;
- }
- if (out.fd < 0) {
- fprintf(stderr, "%s: cannot open for write: %s\n",
- out.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-
- /* Ensure out.fd is outside the stdio descriptor range */
- out.fd = redup_clean_fd(out.fd);
- }
-
- getfdtype(&out);
-
- /*
- * Allocate space for the input and output buffers. If not doing
- * record oriented I/O, only need a single buffer.
- */
- if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
- if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) {
- exit(1);
- /* NOTREACHED */
- }
- out.db = in.db;
- } else if ((in.db =
- malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
- (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
- exit(1);
- /* NOTREACHED */
- }
- in.dbp = in.db;
- out.dbp = out.db;
-
- /* Position the input/output streams. */
- if (in.offset)
- pos_in();
- if (out.offset)
- pos_out();
-
- /*
- * Truncate the output file; ignore errors because it fails on some
- * kinds of output files, tapes, for example.
- */
- if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
- (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
-
- (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
-}
-
-static void
-getfdtype(IO *io)
-{
-// struct mtget mt;
- struct stat sb;
-
- if (fstat(io->fd, &sb)) {
- fprintf(stderr, "%s: cannot fstat: %s\n",
- io->name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
- if (S_ISCHR(sb.st_mode))
- io->flags |= /*ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; */ ISCHR;
- else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
- io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
-}
-
-/*
- * Move the parameter file descriptor to a descriptor that is outside the
- * stdio descriptor range, if necessary. This is required to avoid
- * accidentally outputting completion or error messages into the
- * output file that were intended for the tty.
- */
-static int
-redup_clean_fd(int fd)
-{
- int newfd;
-
- if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
- fd != STDERR_FILENO)
- /* File descriptor is ok, return immediately. */
- return fd;
-
- /*
- * 3 is the first descriptor greater than STD*_FILENO. Any
- * free descriptor valued 3 or above is acceptable...
- */
- newfd = fcntl(fd, F_DUPFD, 3);
- if (newfd < 0) {
- fprintf(stderr, "dupfd IO: %s\n", strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-
- close(fd);
-
- return newfd;
-}
-
-static void
-dd_in(void)
-{
- int flags;
- int64_t n;
-
- for (flags = ddflags;;) {
- if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
- return;
-
- /*
- * Clear the buffer first if doing "sync" on input.
- * If doing block operations use spaces. This will
- * affect not only the C_NOERROR case, but also the
- * last partial input block which should be padded
- * with zero and not garbage.
- */
- if (flags & C_SYNC) {
- if (flags & (C_BLOCK|C_UNBLOCK))
- (void)memset(in.dbp, ' ', in.dbsz);
- else
- (void)memset(in.dbp, 0, in.dbsz);
- }
-
- n = read(in.fd, in.dbp, in.dbsz);
- if (n == 0) {
- in.dbrcnt = 0;
- return;
- }
-
- /* Read error. */
- if (n < 0) {
-
- /*
- * If noerror not specified, die. POSIX requires that
- * the warning message be followed by an I/O display.
- */
- fprintf(stderr, "%s: read error: %s\n",
- in.name, strerror(errno));
- if (!(flags & C_NOERROR)) {
- exit(1);
- /* NOTREACHED */
- }
- summary();
-
- /*
- * If it's not a tape drive or a pipe, seek past the
- * error. If your OS doesn't do the right thing for
- * raw disks this section should be modified to re-read
- * in sector size chunks.
- */
- if (!(in.flags & (ISPIPE|ISTAPE)) &&
- lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
- fprintf(stderr, "%s: seek error: %s\n",
- in.name, strerror(errno));
-
- /* If sync not specified, omit block and continue. */
- if (!(ddflags & C_SYNC))
- continue;
-
- /* Read errors count as full blocks. */
- in.dbcnt += in.dbrcnt = in.dbsz;
- ++st.in_full;
-
- /* Handle full input blocks. */
- } else if (n == in.dbsz) {
- in.dbcnt += in.dbrcnt = n;
- ++st.in_full;
-
- /* Handle partial input blocks. */
- } else {
- /* If sync, use the entire block. */
- if (ddflags & C_SYNC)
- in.dbcnt += in.dbrcnt = in.dbsz;
- else
- in.dbcnt += in.dbrcnt = n;
- ++st.in_part;
- }
-
- /*
- * POSIX states that if bs is set and no other conversions
- * than noerror, notrunc or sync are specified, the block
- * is output without buffering as it is read.
- */
- if (ddflags & C_BS) {
- out.dbcnt = in.dbcnt;
- dd_out(1);
- in.dbcnt = 0;
- continue;
- }
-
-/* if (ddflags & C_SWAB) {
- if ((n = in.dbrcnt) & 1) {
- ++st.swab;
- --n;
- }
- swab(in.dbp, in.dbp, n);
- }
-*/
- in.dbp += in.dbrcnt;
- (*cfunc)();
- }
-}
-
-/*
- * Cleanup any remaining I/O and flush output. If necesssary, output file
- * is truncated.
- */
-static void
-dd_close(void)
-{
-
- if (cfunc == def)
- def_close();
- else if (cfunc == block)
- block_close();
- else if (cfunc == unblock)
- unblock_close();
- if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
- (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
- out.dbcnt = out.dbsz;
- }
- /* If there are pending sparse blocks, make sure
- * to write out the final block un-sparse
- */
- if ((out.dbcnt == 0) && pending) {
- memset(out.db, 0, out.dbsz);
- out.dbcnt = out.dbsz;
- out.dbp = out.db + out.dbcnt;
- pending -= out.dbsz;
- }
- if (out.dbcnt)
- dd_out(1);
-
- /*
- * Reporting nfs write error may be defered until next
- * write(2) or close(2) system call. So, we need to do an
- * extra check. If an output is stdout, the file structure
- * may be shared among with other processes and close(2) just
- * decreases the reference count.
- */
- if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL) {
- fprintf(stderr, "fsync stdout: %s\n", strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
- if (close(out.fd) == -1) {
- fprintf(stderr, "close: %s\n", strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-}
-
-void
-dd_out(int force)
-{
- static int warned;
- int64_t cnt, n, nw;
- u_char *outp;
-
- /*
- * Write one or more blocks out. The common case is writing a full
- * output block in a single write; increment the full block stats.
- * Otherwise, we're into partial block writes. If a partial write,
- * and it's a character device, just warn. If a tape device, quit.
- *
- * The partial writes represent two cases. 1: Where the input block
- * was less than expected so the output block was less than expected.
- * 2: Where the input block was the right size but we were forced to
- * write the block in multiple chunks. The original versions of dd(1)
- * never wrote a block in more than a single write, so the latter case
- * never happened.
- *
- * One special case is if we're forced to do the write -- in that case
- * we play games with the buffer size, and it's usually a partial write.
- */
- outp = out.db;
- for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
- for (cnt = n;; cnt -= nw) {
-
- if (!force && ddflags & C_SPARSE) {
- int sparse, i;
- sparse = 1; /* Is buffer sparse? */
- for (i = 0; i < cnt; i++)
- if (outp[i] != 0) {
- sparse = 0;
- break;
- }
- if (sparse) {
- pending += cnt;
- outp += cnt;
- nw = 0;
- break;
- }
- }
- if (pending != 0) {
- if (lseek(out.fd, pending, SEEK_CUR) ==
- -1) {
- fprintf(stderr,
- "%s: seek error creating "
- "sparse file: %s\n",
- out.name, strerror(errno));
- exit(1);
- }
- }
- nw = bwrite(out.fd, outp, cnt);
- if (nw <= 0) {
- if (nw == 0) {
- fprintf(stderr, "%s: end of device\n",
- out.name);
- exit(1);
- /* NOTREACHED */
- }
- if (errno != EINTR) {
- fprintf(stderr, "%s: write error: %s\n",
- out.name, strerror(errno));
- /* NOTREACHED */
- exit(1);
- }
- nw = 0;
- }
- if (pending) {
- st.bytes += pending;
- st.sparse += pending/out.dbsz;
- st.out_full += pending/out.dbsz;
- pending = 0;
- }
- outp += nw;
- st.bytes += nw;
- if (nw == n) {
- if (n != out.dbsz)
- ++st.out_part;
- else
- ++st.out_full;
- break;
- }
- ++st.out_part;
- if (nw == cnt)
- break;
- if (out.flags & ISCHR && !warned) {
- warned = 1;
- fprintf(stderr, "%s: short write on character "
- "device\n", out.name);
- }
- if (out.flags & ISTAPE) {
- fprintf(stderr,
- "%s: short write on tape device",
- out.name);
- exit(1);
- /* NOTREACHED */
- }
- }
- if ((out.dbcnt -= n) < out.dbsz)
- break;
- }
-
- /* Reassemble the output block. */
- if (out.dbcnt)
- (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
- out.dbp = out.db + out.dbcnt;
-
- if (progress)
- (void)write(STDERR_FILENO, ".", 1);
-}
-
-/*
- * A protected against SIGINFO write
- */
-ssize_t
-bwrite(int fd, const void *buf, size_t len)
-{
- sigset_t oset;
- ssize_t rv;
- int oerrno;
-
- (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
- rv = write(fd, buf, len);
- oerrno = errno;
- (void)sigprocmask(SIG_SETMASK, &oset, NULL);
- errno = oerrno;
- return (rv);
-}
-
-/*
- * Position input/output data streams before starting the copy. Device type
- * dependent. Seekable devices use lseek, and the rest position by reading.
- * Seeking past the end of file can cause null blocks to be written to the
- * output.
- */
-void
-pos_in(void)
-{
- int bcnt, cnt, nr, warned;
-
- /* If not a pipe or tape device, try to seek on it. */
- if (!(in.flags & (ISPIPE|ISTAPE))) {
- if (lseek64(in.fd,
- (off64_t)in.offset * (off64_t)in.dbsz, SEEK_CUR) == -1) {
- fprintf(stderr, "%s: seek error: %s",
- in.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
- return;
- /* NOTREACHED */
- }
-
- /*
- * Read the data. If a pipe, read until satisfy the number of bytes
- * being skipped. No differentiation for reading complete and partial
- * blocks for other devices.
- */
- for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
- if ((nr = read(in.fd, in.db, bcnt)) > 0) {
- if (in.flags & ISPIPE) {
- if (!(bcnt -= nr)) {
- bcnt = in.dbsz;
- --cnt;
- }
- } else
- --cnt;
- continue;
- }
-
- if (nr == 0) {
- if (files_cnt > 1) {
- --files_cnt;
- continue;
- }
- fprintf(stderr, "skip reached end of input\n");
- exit(1);
- /* NOTREACHED */
- }
-
- /*
- * Input error -- either EOF with no more files, or I/O error.
- * If noerror not set die. POSIX requires that the warning
- * message be followed by an I/O display.
- */
- if (ddflags & C_NOERROR) {
- if (!warned) {
-
- fprintf(stderr, "%s: error occurred\n",
- in.name);
- warned = 1;
- summary();
- }
- continue;
- }
- fprintf(stderr, "%s: read error: %s", in.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-}
-
-void
-pos_out(void)
-{
-// struct mtop t_op;
- int cnt, n;
-
- /*
- * If not a tape, try seeking on the file. Seeking on a pipe is
- * going to fail, but don't protect the user -- they shouldn't
- * have specified the seek operand.
- */
- if (!(out.flags & ISTAPE)) {
- if (lseek64(out.fd,
- (off64_t)out.offset * (off64_t)out.dbsz, SEEK_SET) == -1) {
- fprintf(stderr, "%s: seek error: %s\n",
- out.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
- return;
- }
-
- /* If no read access, try using mtio. */
- if (out.flags & NOREAD) {
-/* t_op.mt_op = MTFSR;
- t_op.mt_count = out.offset;
-
- if (ioctl(out.fd, MTIOCTOP, &t_op) < 0)*/
- fprintf(stderr, "%s: cannot read", out.name);
- exit(1);
- /* NOTREACHED */
- return;
- }
-
- /* Read it. */
- for (cnt = 0; cnt < out.offset; ++cnt) {
- if ((n = read(out.fd, out.db, out.dbsz)) > 0)
- continue;
-
- if (n < 0) {
- fprintf(stderr, "%s: cannot position by reading: %s\n",
- out.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
-
- /*
- * If reach EOF, fill with NUL characters; first, back up over
- * the EOF mark. Note, cnt has not yet been incremented, so
- * the EOF read does not count as a seek'd block.
- */
-/* t_op.mt_op = MTBSR;
- t_op.mt_count = 1;
- if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) */ {
- fprintf(stderr, "%s: cannot position\n", out.name);
- exit(1);
- /* NOTREACHED */
- }
-
- while (cnt++ < out.offset)
- if ((n = bwrite(out.fd, out.db, out.dbsz)) != out.dbsz) {
- fprintf(stderr, "%s: cannot position "
- "by writing: %s\n",
- out.name, strerror(errno));
- exit(1);
- /* NOTREACHED */
- }
- break;
- }
-}
-
-/*
- * def --
- * Copy input to output. Input is buffered until reaches obs, and then
- * output until less than obs remains. Only a single buffer is used.
- * Worst case buffer calculation is (ibs + obs - 1).
- */
-void
-def(void)
-{
- uint64_t cnt;
- u_char *inp;
- const u_char *t;
-
- if ((t = ctab) != NULL)
- for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
- *inp = t[*inp];
-
- /* Make the output buffer look right. */
- out.dbp = in.dbp;
- out.dbcnt = in.dbcnt;
-
- if (in.dbcnt >= out.dbsz) {
- /* If the output buffer is full, write it. */
- dd_out(0);
-
- /*
- * Ddout copies the leftover output to the beginning of
- * the buffer and resets the output buffer. Reset the
- * input buffer to match it.
- */
- in.dbp = out.dbp;
- in.dbcnt = out.dbcnt;
- }
-}
-
-void
-def_close(void)
-{
- if (ddflags & C_FDATASYNC) {
- fdatasync(out.fd);
- }
-
- /* Just update the count, everything is already in the buffer. */
- if (in.dbcnt)
- out.dbcnt = in.dbcnt;
-}
-
-#ifdef NO_CONV
-/* Build a smaller version (i.e. for a miniroot) */
-/* These can not be called, but just in case... */
-static const char no_block[] = "unblock and -DNO_CONV?\n";
-void block(void) { fprintf(stderr, "%s", no_block + 2); exit(1); }
-void block_close(void) { fprintf(stderr, "%s", no_block + 2); exit(1); }
-void unblock(void) { fprintf(stderr, "%s", no_block); exit(1); }
-void unblock_close(void) { fprintf(stderr, "%s", no_block); exit(1); }
-#else /* NO_CONV */
-
-/*
- * Copy variable length newline terminated records with a max size cbsz
- * bytes to output. Records less than cbs are padded with spaces.
- *
- * max in buffer: MAX(ibs, cbsz)
- * max out buffer: obs + cbsz
- */
-void
-block(void)
-{
- static int intrunc;
- int ch = 0; /* pacify gcc */
- uint64_t cnt, maxlen;
- u_char *inp, *outp;
- const u_char *t;
-
- /*
- * Record truncation can cross block boundaries. If currently in a
- * truncation state, keep tossing characters until reach a newline.
- * Start at the beginning of the buffer, as the input buffer is always
- * left empty.
- */
- if (intrunc) {
- for (inp = in.db, cnt = in.dbrcnt;
- cnt && *inp++ != '\n'; --cnt);
- if (!cnt) {
- in.dbcnt = 0;
- in.dbp = in.db;
- return;
- }
- intrunc = 0;
- /* Adjust the input buffer numbers. */
- in.dbcnt = cnt - 1;
- in.dbp = inp + cnt - 1;
- }
-
- /*
- * Copy records (max cbsz size chunks) into the output buffer. The
- * translation is done as we copy into the output buffer.
- */
- for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
- maxlen = MIN(cbsz, in.dbcnt);
- if ((t = ctab) != NULL)
- for (cnt = 0;
- cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
- *outp++ = t[ch];
- else
- for (cnt = 0;
- cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
- *outp++ = ch;
- /*
- * Check for short record without a newline. Reassemble the
- * input block.
- */
- if (ch != '\n' && in.dbcnt < cbsz) {
- (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
- break;
- }
-
- /* Adjust the input buffer numbers. */
- in.dbcnt -= cnt;
- if (ch == '\n')
- --in.dbcnt;
-
- /* Pad short records with spaces. */
- if (cnt < cbsz)
- (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
- else {
- /*
- * If the next character wouldn't have ended the
- * block, it's a truncation.
- */
- if (!in.dbcnt || *inp != '\n')
- ++st.trunc;
-
- /* Toss characters to a newline. */
- for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
- if (!in.dbcnt)
- intrunc = 1;
- else
- --in.dbcnt;
- }
-
- /* Adjust output buffer numbers. */
- out.dbp += cbsz;
- if ((out.dbcnt += cbsz) >= out.dbsz)
- dd_out(0);
- outp = out.dbp;
- }
- in.dbp = in.db + in.dbcnt;
-}
-
-void
-block_close(void)
-{
-
- /*
- * Copy any remaining data into the output buffer and pad to a record.
- * Don't worry about truncation or translation, the input buffer is
- * always empty when truncating, and no characters have been added for
- * translation. The bottom line is that anything left in the input
- * buffer is a truncated record. Anything left in the output buffer
- * just wasn't big enough.
- */
- if (in.dbcnt) {
- ++st.trunc;
- (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
- (void)memset(out.dbp + in.dbcnt,
- ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
- out.dbcnt += cbsz;
- }
-}
-
-/*
- * Convert fixed length (cbsz) records to variable length. Deletes any
- * trailing blanks and appends a newline.
- *
- * max in buffer: MAX(ibs, cbsz) + cbsz
- * max out buffer: obs + cbsz
- */
-void
-unblock(void)
-{
- uint64_t cnt;
- u_char *inp;
- const u_char *t;
-
- /* Translation and case conversion. */
- if ((t = ctab) != NULL)
- for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
- *inp = t[*inp];
- /*
- * Copy records (max cbsz size chunks) into the output buffer. The
- * translation has to already be done or we might not recognize the
- * spaces.
- */
- for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
- for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
- if (t >= inp) {
- cnt = t - inp + 1;
- (void)memmove(out.dbp, inp, cnt);
- out.dbp += cnt;
- out.dbcnt += cnt;
- }
- ++out.dbcnt;
- *out.dbp++ = '\n';
- if (out.dbcnt >= out.dbsz)
- dd_out(0);
- }
- if (in.dbcnt)
- (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
- in.dbp = in.db + in.dbcnt;
-}
-
-void
-unblock_close(void)
-{
- uint64_t cnt;
- u_char *t;
-
- if (in.dbcnt) {
- warnx("%s: short input record", in.name);
- for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
- if (t >= in.db) {
- cnt = t - in.db + 1;
- (void)memmove(out.dbp, in.db, cnt);
- out.dbp += cnt;
- out.dbcnt += cnt;
- }
- ++out.dbcnt;
- *out.dbp++ = '\n';
- }
-}
-
-#endif /* NO_CONV */
-
-#define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
-
-void
-summary(void)
-{
- char buf[100];
- int64_t mS;
- struct timeval tv;
-
- if (progress)
- (void)write(STDERR_FILENO, "\n", 1);
-
- (void)gettimeofday(&tv, NULL);
- mS = tv2mS(tv) - tv2mS(st.start);
- if (mS == 0)
- mS = 1;
- /* Use snprintf(3) so that we don't reenter stdio(3). */
- (void)snprintf(buf, sizeof(buf),
- "%llu+%llu records in\n%llu+%llu records out\n",
- (unsigned long long)st.in_full, (unsigned long long)st.in_part,
- (unsigned long long)st.out_full, (unsigned long long)st.out_part);
- (void)write(STDERR_FILENO, buf, strlen(buf));
- if (st.swab) {
- (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
- (unsigned long long)st.swab,
- (st.swab == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- if (st.trunc) {
- (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
- (unsigned long long)st.trunc,
- (st.trunc == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- if (st.sparse) {
- (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
- (unsigned long long)st.sparse,
- (st.sparse == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- (void)snprintf(buf, sizeof(buf),
- "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
- (unsigned long long) st.bytes,
- (long) (mS / 1000),
- (int) (mS % 1000),
- (unsigned long long) (st.bytes * 1000LL / mS));
- (void)write(STDERR_FILENO, buf, strlen(buf));
-}
-
-void
-terminate(int notused)
-{
-
- exit(0);
- /* NOTREACHED */
-}
-
-static int c_arg(const void *, const void *);
-#ifndef NO_CONV
-static int c_conv(const void *, const void *);
-#endif
-static void f_bs(char *);
-static void f_cbs(char *);
-static void f_conv(char *);
-static void f_count(char *);
-static void f_files(char *);
-static void f_ibs(char *);
-static void f_if(char *);
-static void f_obs(char *);
-static void f_of(char *);
-static void f_seek(char *);
-static void f_skip(char *);
-static void f_progress(char *);
-
-static const struct arg {
- const char *name;
- void (*f)(char *);
- u_int set, noset;
-} args[] = {
- /* the array needs to be sorted by the first column so
- bsearch() can be used to find commands quickly */
- { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
- { "cbs", f_cbs, C_CBS, C_CBS },
- { "conv", f_conv, 0, 0 },
- { "count", f_count, C_COUNT, C_COUNT },
- { "files", f_files, C_FILES, C_FILES },
- { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
- { "if", f_if, C_IF, C_IF },
- { "obs", f_obs, C_OBS, C_BS|C_OBS },
- { "of", f_of, C_OF, C_OF },
- { "progress", f_progress, 0, 0 },
- { "seek", f_seek, C_SEEK, C_SEEK },
- { "skip", f_skip, C_SKIP, C_SKIP },
-};
-
-/*
- * args -- parse JCL syntax of dd.
- */
-void
-jcl(char **argv)
-{
- struct arg *ap, tmp;
- char *oper, *arg;
-
- in.dbsz = out.dbsz = 512;
-
- while ((oper = *++argv) != NULL) {
- if ((arg = strchr(oper, '=')) == NULL) {
- fprintf(stderr, "unknown operand %s\n", oper);
- exit(1);
- /* NOTREACHED */
- }
- *arg++ = '\0';
- if (!*arg) {
- fprintf(stderr, "no value specified for %s\n", oper);
- exit(1);
- /* NOTREACHED */
- }
- tmp.name = oper;
- if (!(ap = (struct arg *)bsearch(&tmp, args,
- sizeof(args)/sizeof(struct arg), sizeof(struct arg),
- c_arg))) {
- fprintf(stderr, "unknown operand %s\n", tmp.name);
- exit(1);
- /* NOTREACHED */
- }
- if (ddflags & ap->noset) {
- fprintf(stderr,
- "%s: illegal argument combination or already set\n",
- tmp.name);
- exit(1);
- /* NOTREACHED */
- }
- ddflags |= ap->set;
- ap->f(arg);
- }
-
- /* Final sanity checks. */
-
- if (ddflags & C_BS) {
- /*
- * Bs is turned off by any conversion -- we assume the user
- * just wanted to set both the input and output block sizes
- * and didn't want the bs semantics, so we don't warn.
- */
- if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
- C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
- ddflags &= ~C_BS;
- ddflags |= C_IBS|C_OBS;
- }
-
- /* Bs supersedes ibs and obs. */
- if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
- fprintf(stderr, "bs supersedes ibs and obs\n");
- }
-
- /*
- * Ascii/ebcdic and cbs implies block/unblock.
- * Block/unblock requires cbs and vice-versa.
- */
- if (ddflags & (C_BLOCK|C_UNBLOCK)) {
- if (!(ddflags & C_CBS)) {
- fprintf(stderr, "record operations require cbs\n");
- exit(1);
- /* NOTREACHED */
- }
- cfunc = ddflags & C_BLOCK ? block : unblock;
- } else if (ddflags & C_CBS) {
- if (ddflags & (C_ASCII|C_EBCDIC)) {
- if (ddflags & C_ASCII) {
- ddflags |= C_UNBLOCK;
- cfunc = unblock;
- } else {
- ddflags |= C_BLOCK;
- cfunc = block;
- }
- } else {
- fprintf(stderr,
- "cbs meaningless if not doing record operations\n");
- exit(1);
- /* NOTREACHED */
- }
- } else
- cfunc = def;
-
- /* Read, write and seek calls take off_t as arguments.
- *
- * The following check is not done because an off_t is a quad
- * for current NetBSD implementations.
- *
- * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
- * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
- */
-}
-
-static int
-c_arg(const void *a, const void *b)
-{
-
- return (strcmp(((const struct arg *)a)->name,
- ((const struct arg *)b)->name));
-}
-
-static long long strsuftoll(const char* name, const char* arg, int def, unsigned int max)
-{
- long long result;
-
- if (sscanf(arg, "%lld", &result) == 0)
- result = def;
- return result;
-}
-
-static void
-f_bs(char *arg)
-{
-
- in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_cbs(char *arg)
-{
-
- cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
-}
-
-static void
-f_count(char *arg)
-{
-
- cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
- if (!cpy_cnt)
- terminate(0);
-}
-
-static void
-f_files(char *arg)
-{
-
- files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
- if (!files_cnt)
- terminate(0);
-}
-
-static void
-f_ibs(char *arg)
-{
-
- if (!(ddflags & C_BS))
- in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_if(char *arg)
-{
-
- in.name = arg;
-}
-
-static void
-f_obs(char *arg)
-{
-
- if (!(ddflags & C_BS))
- out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_of(char *arg)
-{
-
- out.name = arg;
-}
-
-static void
-f_seek(char *arg)
-{
-
- out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
-}
-
-static void
-f_skip(char *arg)
-{
-
- in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
-}
-
-static void
-f_progress(char *arg)
-{
-
- if (*arg != '0')
- progress = 1;
-}
-
-#ifdef NO_CONV
-/* Build a small version (i.e. for a ramdisk root) */
-static void
-f_conv(char *arg)
-{
-
- fprintf(stderr, "conv option disabled\n");
- exit(1);
- /* NOTREACHED */
-}
-#else /* NO_CONV */
-
-static const struct conv {
- const char *name;
- u_int set, noset;
- const u_char *ctab;
-} clist[] = {
- { "block", C_BLOCK, C_UNBLOCK, NULL },
- { "fdatasync", C_FDATASYNC, 0, NULL },
- { "noerror", C_NOERROR, 0, NULL },
- { "notrunc", C_NOTRUNC, 0, NULL },
- { "osync", C_OSYNC, C_BS, NULL },
- { "sparse", C_SPARSE, 0, NULL },
- { "swab", C_SWAB, 0, NULL },
- { "sync", C_SYNC, 0, NULL },
- { "unblock", C_UNBLOCK, C_BLOCK, NULL },
- /* If you add items to this table, be sure to add the
- * conversions to the C_BS check in the jcl routine above.
- */
-};
-
-static void
-f_conv(char *arg)
-{
- struct conv *cp, tmp;
-
- while (arg != NULL) {
- tmp.name = strsep(&arg, ",");
- if (!(cp = (struct conv *)bsearch(&tmp, clist,
- sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
- c_conv))) {
- errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
- /* NOTREACHED */
- }
- if (ddflags & cp->noset) {
- errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name);
- /* NOTREACHED */
- }
- ddflags |= cp->set;
- if (cp->ctab)
- ctab = cp->ctab;
- }
-}
-
-static int
-c_conv(const void *a, const void *b)
-{
-
- return (strcmp(((const struct conv *)a)->name,
- ((const struct conv *)b)->name));
-}
-
-#endif /* NO_CONV */
-
-
diff --git a/toolbox/dmesg.c b/toolbox/dmesg.c
deleted file mode 100644
index 9c73b00..0000000
--- a/toolbox/dmesg.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/klog.h>
-#include <string.h>
-
-#define FALLBACK_KLOG_BUF_SHIFT 17 /* CONFIG_LOG_BUF_SHIFT from our kernel */
-#define FALLBACK_KLOG_BUF_LEN (1 << FALLBACK_KLOG_BUF_SHIFT)
-
-int dmesg_main(int argc, char **argv)
-{
- char *buffer;
- char *p;
- ssize_t ret;
- int n, op, klog_buf_len;
-
- klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
-
- if (klog_buf_len <= 0) {
- klog_buf_len = FALLBACK_KLOG_BUF_LEN;
- }
-
- buffer = (char *)malloc(klog_buf_len + 1);
-
- if (!buffer) {
- perror("malloc");
- return EXIT_FAILURE;
- }
-
- p = buffer;
-
- if((argc == 2) && (!strcmp(argv[1],"-c"))) {
- op = KLOG_READ_CLEAR;
- } else {
- op = KLOG_READ_ALL;
- }
-
- n = klogctl(op, buffer, klog_buf_len);
- if (n < 0) {
- perror("klogctl");
- return EXIT_FAILURE;
- }
- buffer[n] = '\0';
-
- while((ret = write(STDOUT_FILENO, p, n))) {
- if (ret == -1) {
- if (errno == EINTR)
- continue;
- perror("write");
- return EXIT_FAILURE;
- }
- p += ret;
- n -= ret;
- }
-
- return 0;
-}
diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c
index e9b7b03..47594e0 100644
--- a/toolbox/dynarray.c
+++ b/toolbox/dynarray.c
@@ -1,6 +1,7 @@
#include "dynarray.h"
-#include <stdlib.h>
#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
void
dynarray_init( dynarray_t *a )
diff --git a/toolbox/exists.c b/toolbox/exists.c
deleted file mode 100644
index e348668..0000000
--- a/toolbox/exists.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-int exists_main(int argc, char *argv[])
-{
- struct stat s;
-
- if(argc < 2) return 1;
-
- if(stat(argv[1], &s)) {
- return 1;
- } else {
- return 0;
- }
-}
diff --git a/toolbox/getenforce.c b/toolbox/getenforce.c
deleted file mode 100644
index 9e7589a..0000000
--- a/toolbox/getenforce.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int getenforce_main(int argc, char **argv)
-{
- int rc;
-
- rc = is_selinux_enabled();
- if (rc <= 0) {
- printf("Disabled\n");
- return 0;
- }
-
- rc = security_getenforce();
- if (rc < 0) {
- fprintf(stderr, "Could not get enforcing status: %s\n",
- strerror(errno));
- return 2;
- }
-
- if (rc)
- printf("Enforcing\n");
- else
- printf("Permissive\n");
-
- return 0;
-}
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index c979c99..c58eb5d 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -10,6 +10,7 @@
#include <sys/poll.h>
#include <linux/input.h>
#include <errno.h>
+#include <unistd.h>
#include "getevent.h"
@@ -498,13 +499,11 @@
int c;
int i;
int res;
- int pollres;
int get_time = 0;
int print_device = 0;
char *newline = "\n";
uint16_t get_switch = 0;
struct input_event event;
- int version;
int print_flags = 0;
int print_flags_set = 0;
int dont_block = -1;
@@ -635,7 +634,8 @@
return 0;
while(1) {
- pollres = poll(ufds, nfds, -1);
+ //int pollres =
+ poll(ufds, nfds, -1);
//printf("poll %d, returned %d\n", nfds, pollres);
if(ufds[0].revents & POLLIN) {
read_notify(device_path, ufds[0].fd, print_flags);
diff --git a/toolbox/getevent.h b/toolbox/getevent.h
index 2b76209..0482d04 100644
--- a/toolbox/getevent.h
+++ b/toolbox/getevent.h
@@ -652,6 +652,7 @@
LABEL_END,
};
+#if 0
static struct label id_labels[] = {
LABEL(ID_BUS),
LABEL(ID_VENDOR),
@@ -682,6 +683,7 @@
LABEL(BUS_SPI),
LABEL_END,
};
+#endif
static struct label mt_tool_labels[] = {
LABEL(MT_TOOL_FINGER),
diff --git a/toolbox/getprop.c b/toolbox/getprop.c
index c001fda..dcc0ea0 100644
--- a/toolbox/getprop.c
+++ b/toolbox/getprop.c
@@ -3,7 +3,6 @@
#include <cutils/properties.h>
-#include <sys/system_properties.h>
#include "dynarray.h"
static void record_prop(const char* key, const char* name, void* opaque)
@@ -31,12 +30,8 @@
strlist_done(list);
}
-int __system_property_wait(prop_info *pi);
-
int getprop_main(int argc, char *argv[])
{
- int n = 0;
-
if (argc == 1) {
list_properties();
} else {
diff --git a/toolbox/getsebool.c b/toolbox/getsebool.c
deleted file mode 100644
index aab5200..0000000
--- a/toolbox/getsebool.c
+++ /dev/null
@@ -1,104 +0,0 @@
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <getopt.h>
-#include <errno.h>
-#include <string.h>
-#include <selinux/selinux.h>
-
-static void usage(const char *progname)
-{
- fprintf(stderr, "usage: %s -a or %s boolean...\n", progname, progname);
- exit(1);
-}
-
-int getsebool_main(int argc, char **argv)
-{
- int i, get_all = 0, rc = 0, active, pending, len = 0, opt;
- char **names;
-
- while ((opt = getopt(argc, argv, "a")) > 0) {
- switch (opt) {
- case 'a':
- if (argc > 2)
- usage(argv[0]);
- if (is_selinux_enabled() <= 0) {
- fprintf(stderr, "%s: SELinux is disabled\n",
- argv[0]);
- return 1;
- }
- errno = 0;
- rc = security_get_boolean_names(&names, &len);
- if (rc) {
- fprintf(stderr,
- "%s: Unable to get boolean names: %s\n",
- argv[0], strerror(errno));
- return 1;
- }
- if (!len) {
- printf("No booleans\n");
- return 0;
- }
- get_all = 1;
- break;
- default:
- usage(argv[0]);
- }
- }
-
- if (is_selinux_enabled() <= 0) {
- fprintf(stderr, "%s: SELinux is disabled\n", argv[0]);
- return 1;
- }
- if (!len) {
- if (argc < 2)
- usage(argv[0]);
- len = argc - 1;
- names = malloc(sizeof(char *) * len);
- if (!names) {
- fprintf(stderr, "%s: out of memory\n", argv[0]);
- return 2;
- }
- for (i = 0; i < len; i++) {
- names[i] = strdup(argv[i + 1]);
- if (!names[i]) {
- fprintf(stderr, "%s: out of memory\n",
- argv[0]);
- return 2;
- }
- }
- }
-
- for (i = 0; i < len; i++) {
- active = security_get_boolean_active(names[i]);
- if (active < 0) {
- if (get_all && errno == EACCES)
- continue;
- fprintf(stderr, "Error getting active value for %s\n",
- names[i]);
- rc = -1;
- goto out;
- }
- pending = security_get_boolean_pending(names[i]);
- if (pending < 0) {
- fprintf(stderr, "Error getting pending value for %s\n",
- names[i]);
- rc = -1;
- goto out;
- }
- if (pending != active) {
- printf("%s --> %s pending: %s\n", names[i],
- (active ? "on" : "off"),
- (pending ? "on" : "off"));
- } else {
- printf("%s --> %s\n", names[i],
- (active ? "on" : "off"));
- }
- }
-
-out:
- for (i = 0; i < len; i++)
- free(names[i]);
- free(names);
- return rc;
-}
diff --git a/toolbox/hd.c b/toolbox/hd.c
deleted file mode 100644
index 0d2f96a..0000000
--- a/toolbox/hd.c
+++ /dev/null
@@ -1,98 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-
-int hd_main(int argc, char *argv[])
-{
- int c;
- int fd;
- unsigned char buf[4096];
- int res;
- int read_len;
- int rv = 0;
- int i;
- int filepos = 0;
- int sum;
- int lsum;
-
- int base = -1;
- int count = 0;
- int repeat = 0;
-
- do {
- c = getopt(argc, argv, "b:c:r:");
- if (c == EOF)
- break;
- switch (c) {
- case 'b':
- base = strtol(optarg, NULL, 0);
- break;
- case 'c':
- count = strtol(optarg, NULL, 0);
- break;
- case 'r':
- repeat = strtol(optarg, NULL, 0);
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
-
- if (optind + 1 != argc) {
- fprintf(stderr, "Usage: %s [-b base] [-c count] [-r delay] file\n", argv[0]);
- exit(1);
- }
-
- fd = open(argv[optind], O_RDONLY);
- if(fd < 0) {
- fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
- return 1;
- }
-
- do {
- if(base >= 0) {
- lseek(fd, base, SEEK_SET);
- filepos = base;
- }
- sum = 0;
- lsum = 0;
- while(1) {
- read_len = sizeof(buf);
- if(count > 0 && base + count - filepos < read_len)
- read_len = base + count - filepos;
- res = read(fd, &buf, read_len);
- if(res == 0)
- break;
- for(i = 0; i < res; i++) {
- if((i & 15) == 0) {
- printf("%08x: ", filepos + i);
- }
- lsum += buf[i];
- sum += buf[i];
- printf("%02x ", buf[i]);
- if(((i & 15) == 15) || (i == res - 1)) {
- printf("s %x\n", lsum);
- lsum = 0;
- }
- }
- if(res < 0) {
- printf("Read error on %s, offset %d len %d, %s\n", argv[optind], filepos, read_len, strerror(errno));
- return 1;
- }
- filepos += res;
- if(filepos == base + count)
- break;
- }
- printf("sum %x\n", sum);
- if(repeat)
- sleep(repeat);
- } while(repeat);
- return 0;
-}
diff --git a/toolbox/id.c b/toolbox/id.c
deleted file mode 100644
index 8ec79c1..0000000
--- a/toolbox/id.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#include <grp.h>
-#include <selinux/selinux.h>
-
-static void print_uid(uid_t uid)
-{
- struct passwd *pw = getpwuid(uid);
-
- if (pw) {
- printf("%d(%s)", uid, pw->pw_name);
- } else {
- printf("%d",uid);
- }
-}
-
-static void print_gid(gid_t gid)
-{
- struct group *gr = getgrgid(gid);
- if (gr) {
- printf("%d(%s)", gid, gr->gr_name);
- } else {
- printf("%d",gid);
- }
-}
-
-int id_main(int argc, char **argv)
-{
- gid_t list[64];
- int n, max;
- char *secctx;
-
- max = getgroups(64, list);
- if (max < 0) max = 0;
-
- printf("uid=");
- print_uid(getuid());
- printf(" gid=");
- print_gid(getgid());
- if (max) {
- printf(" groups=");
- print_gid(list[0]);
- for(n = 1; n < max; n++) {
- printf(",");
- print_gid(list[n]);
- }
- }
- if (getcon(&secctx) == 0) {
- printf(" context=%s", secctx);
- free(secctx);
- }
- printf("\n");
- return 0;
-}
diff --git a/toolbox/ifconfig.c b/toolbox/ifconfig.c
deleted file mode 100644
index 80c0e5c..0000000
--- a/toolbox/ifconfig.c
+++ /dev/null
@@ -1,163 +0,0 @@
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include <errno.h>
-#include <string.h>
-#include <ctype.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <linux/if.h>
-#include <linux/sockios.h>
-#include <arpa/inet.h>
-
-static void die(const char *s)
-{
- fprintf(stderr,"error: %s (%s)\n", s, strerror(errno));
- exit(-1);
-}
-
-static void setflags(int s, struct ifreq *ifr, int set, int clr)
-{
- if(ioctl(s, SIOCGIFFLAGS, ifr) < 0) die("SIOCGIFFLAGS");
- ifr->ifr_flags = (ifr->ifr_flags & (~clr)) | set;
- if(ioctl(s, SIOCSIFFLAGS, ifr) < 0) die("SIOCSIFFLAGS");
-}
-
-static inline void init_sockaddr_in(struct sockaddr_in *sin, const char *addr)
-{
- sin->sin_family = AF_INET;
- sin->sin_port = 0;
- sin->sin_addr.s_addr = inet_addr(addr);
-}
-
-static void setmtu(int s, struct ifreq *ifr, const char *mtu)
-{
- int m = atoi(mtu);
- ifr->ifr_mtu = m;
- if(ioctl(s, SIOCSIFMTU, ifr) < 0) die("SIOCSIFMTU");
-}
-static void setdstaddr(int s, struct ifreq *ifr, const char *addr)
-{
- init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_dstaddr, addr);
- if(ioctl(s, SIOCSIFDSTADDR, ifr) < 0) die("SIOCSIFDSTADDR");
-}
-
-static void setnetmask(int s, struct ifreq *ifr, const char *addr)
-{
- init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_netmask, addr);
- if(ioctl(s, SIOCSIFNETMASK, ifr) < 0) die("SIOCSIFNETMASK");
-}
-
-static void setaddr(int s, struct ifreq *ifr, const char *addr)
-{
- init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_addr, addr);
- if(ioctl(s, SIOCSIFADDR, ifr) < 0) die("SIOCSIFADDR");
-}
-
-int ifconfig_main(int argc, char *argv[])
-{
- struct ifreq ifr;
- int s;
- unsigned int addr, mask, flags;
- char astring[20];
- char mstring[20];
- char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
-
- argc--;
- argv++;
-
- if(argc == 0) return 0;
-
- memset(&ifr, 0, sizeof(struct ifreq));
- strncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
- ifr.ifr_name[IFNAMSIZ-1] = 0;
- argc--, argv++;
-
- if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- die("cannot open control socket\n");
- }
-
- if (argc == 0) {
- if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
- perror(ifr.ifr_name);
- return -1;
- } else
- addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
-
- if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) {
- perror(ifr.ifr_name);
- return -1;
- } else
- mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
-
- if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
- perror(ifr.ifr_name);
- return -1;
- } else
- flags = ifr.ifr_flags;
-
- sprintf(astring, "%d.%d.%d.%d",
- addr & 0xff,
- ((addr >> 8) & 0xff),
- ((addr >> 16) & 0xff),
- ((addr >> 24) & 0xff));
- sprintf(mstring, "%d.%d.%d.%d",
- mask & 0xff,
- ((mask >> 8) & 0xff),
- ((mask >> 16) & 0xff),
- ((mask >> 24) & 0xff));
- printf("%s: ip %s mask %s flags [", ifr.ifr_name,
- astring,
- mstring
- );
-
- updown = (flags & IFF_UP) ? "up" : "down";
- brdcst = (flags & IFF_BROADCAST) ? " broadcast" : "";
- loopbk = (flags & IFF_LOOPBACK) ? " loopback" : "";
- ppp = (flags & IFF_POINTOPOINT) ? " point-to-point" : "";
- running = (flags & IFF_RUNNING) ? " running" : "";
- multi = (flags & IFF_MULTICAST) ? " multicast" : "";
- printf("%s%s%s%s%s%s]\n", updown, brdcst, loopbk, ppp, running, multi);
- return 0;
- }
-
- while(argc > 0) {
- if (!strcmp(argv[0], "up")) {
- setflags(s, &ifr, IFF_UP, 0);
- } else if (!strcmp(argv[0], "mtu")) {
- argc--, argv++;
- if (!argc) {
- errno = EINVAL;
- die("expecting a value for parameter \"mtu\"");
- }
- setmtu(s, &ifr, argv[0]);
- } else if (!strcmp(argv[0], "-pointopoint")) {
- setflags(s, &ifr, IFF_POINTOPOINT, 1);
- } else if (!strcmp(argv[0], "pointopoint")) {
- argc--, argv++;
- if (!argc) {
- errno = EINVAL;
- die("expecting an IP address for parameter \"pointtopoint\"");
- }
- setdstaddr(s, &ifr, argv[0]);
- setflags(s, &ifr, IFF_POINTOPOINT, 0);
- } else if (!strcmp(argv[0], "down")) {
- setflags(s, &ifr, 0, IFF_UP);
- } else if (!strcmp(argv[0], "netmask")) {
- argc--, argv++;
- if (!argc) {
- errno = EINVAL;
- die("expecting an IP address for parameter \"netmask\"");
- }
- setnetmask(s, &ifr, argv[0]);
- } else if (isdigit(argv[0][0])) {
- setaddr(s, &ifr, argv[0]);
- setflags(s, &ifr, IFF_UP, 0);
- }
- argc--, argv++;
- }
- return 0;
-}
diff --git a/toolbox/insmod.c b/toolbox/insmod.c
deleted file mode 100644
index fb1448b..0000000
--- a/toolbox/insmod.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <malloc.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-extern int init_module(void *, unsigned long, const char *);
-
-static void *read_file(const char *filename, ssize_t *_size)
-{
- int ret, fd;
- struct stat sb;
- ssize_t size;
- void *buffer = NULL;
-
- /* open the file */
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- return NULL;
-
- /* find out how big it is */
- if (fstat(fd, &sb) < 0)
- goto bail;
- size = sb.st_size;
-
- /* allocate memory for it to be read into */
- buffer = malloc(size);
- if (!buffer)
- goto bail;
-
- /* slurp it into our buffer */
- ret = read(fd, buffer, size);
- if (ret != size)
- goto bail;
-
- /* let the caller know how big it is */
- *_size = size;
-
-bail:
- close(fd);
- return buffer;
-}
-
-int insmod_main(int argc, char **argv)
-{
- void *file;
- ssize_t size = 0;
- char opts[1024];
- int ret;
-
- /* make sure we've got an argument */
- if (argc < 2) {
- fprintf(stderr, "usage: insmod <module.o>\n");
- return -1;
- }
-
- /* read the file into memory */
- file = read_file(argv[1], &size);
- if (!file) {
- fprintf(stderr, "insmod: can't open '%s'\n", argv[1]);
- return -1;
- }
-
- opts[0] = '\0';
- if (argc > 2) {
- int i, len;
- char *end = opts + sizeof(opts) - 1;
- char *ptr = opts;
-
- for (i = 2; (i < argc) && (ptr < end); i++) {
- len = MIN(strlen(argv[i]), end - ptr);
- memcpy(ptr, argv[i], len);
- ptr += len;
- *ptr++ = ' ';
- }
- *(ptr - 1) = '\0';
- }
-
- /* pass it to the kernel */
- ret = init_module(file, size, opts);
- if (ret != 0) {
- fprintf(stderr,
- "insmod: init_module '%s' failed (%s)\n",
- argv[1], strerror(errno));
- }
-
- /* free the file buffer */
- free(file);
-
- return ret;
-}
-
diff --git a/toolbox/ioctl.c b/toolbox/ioctl.c
index fd24885..d1cc14a 100644
--- a/toolbox/ioctl.c
+++ b/toolbox/ioctl.c
@@ -9,6 +9,7 @@
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
+#include <unistd.h>
int ioctl_main(int argc, char *argv[])
{
@@ -21,9 +22,9 @@
int arg_size = 4;
int direct_arg = 0;
uint32_t ioctl_nr;
- void *ioctl_args;
+ void *ioctl_args = NULL;
uint8_t *ioctl_argp;
- uint8_t *ioctl_argp_save;
+ uint8_t *ioctl_argp_save = NULL;
int rem;
do {
@@ -45,7 +46,7 @@
break;
case 'h':
fprintf(stderr, "%s [-l <length>] [-a <argsize>] [-rdh] <device> <ioctlnr>\n"
- " -l <lenght> Length of io buffer\n"
+ " -l <length> Length of io buffer\n"
" -a <argsize> Size of each argument (1-8)\n"
" -r Open device in read only mode\n"
" -d Direct argument (no iobuffer)\n"
@@ -116,6 +117,7 @@
else
res = ioctl(fd, ioctl_nr, 0);
if (res < 0) {
+ free(ioctl_args);
fprintf(stderr, "ioctl 0x%x failed, %d\n", ioctl_nr, res);
return 1;
}
@@ -128,5 +130,6 @@
}
printf("\n");
}
+ free(ioctl_args);
return 0;
}
diff --git a/toolbox/ionice.c b/toolbox/ionice.c
index 4a182f2..7abc261 100644
--- a/toolbox/ionice.c
+++ b/toolbox/ionice.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <errno.h>
diff --git a/toolbox/kill.c b/toolbox/kill.c
deleted file mode 100644
index fa2f649..0000000
--- a/toolbox/kill.c
+++ /dev/null
@@ -1,148 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-
-#include <sys/types.h>
-#include <signal.h>
-
-static struct {
- unsigned int number;
- char *name;
-} signals[] = {
-#define _SIG(name) {SIG##name, #name}
- /* Single Unix Specification signals */
- _SIG(ABRT),
- _SIG(ALRM),
- _SIG(FPE),
- _SIG(HUP),
- _SIG(ILL),
- _SIG(INT),
- _SIG(KILL),
- _SIG(PIPE),
- _SIG(QUIT),
- _SIG(SEGV),
- _SIG(TERM),
- _SIG(USR1),
- _SIG(USR2),
- _SIG(CHLD),
- _SIG(CONT),
- _SIG(STOP),
- _SIG(TSTP),
- _SIG(TTIN),
- _SIG(TTOU),
- _SIG(BUS),
- _SIG(POLL),
- _SIG(PROF),
- _SIG(SYS),
- _SIG(TRAP),
- _SIG(URG),
- _SIG(VTALRM),
- _SIG(XCPU),
- _SIG(XFSZ),
- /* non-SUS signals */
- _SIG(IO),
- _SIG(PWR),
-#ifdef SIGSTKFLT
- _SIG(STKFLT),
-#endif
- _SIG(WINCH),
-#undef _SIG
-};
-
-/* To indicate a matching signal was not found */
-static const unsigned int SENTINEL = (unsigned int) -1;
-
-void list_signals()
-{
- unsigned int sorted_signals[_NSIG];
- unsigned int i;
- unsigned int num;
-
- memset(sorted_signals, SENTINEL, sizeof(sorted_signals));
-
- // Sort the signals
- for (i = 0; i < sizeof(signals)/sizeof(signals[0]); i++) {
- sorted_signals[signals[i].number] = i;
- }
-
- num = 0;
- for (i = 1; i < _NSIG; i++) {
- unsigned int index = sorted_signals[i];
- if (index == SENTINEL) {
- continue;
- }
-
- fprintf(stderr, "%2d) SIG%-9s ", i, signals[index].name);
-
- if ((num++ % 4) == 3) {
- fprintf(stderr, "\n");
- }
- }
-
- if ((num % 4) == 3) {
- fprintf(stderr, "\n");
- }
-}
-
-unsigned int name_to_signal(const char* name)
-{
- unsigned int i;
-
- for (i = 1; i < sizeof(signals) / sizeof(signals[0]); i++) {
- if (!strcasecmp(name, signals[i].name)) {
- return signals[i].number;
- }
- }
-
- return SENTINEL;
-}
-
-int kill_main(int argc, char **argv)
-{
- unsigned int sig = SIGTERM;
- int result = 0;
-
- argc--;
- argv++;
-
- if (argc >= 1 && argv[0][0] == '-') {
- char *endptr;
- size_t arg_len = strlen(argv[0]);
- if (arg_len < 2) {
- fprintf(stderr, "invalid argument: -\n");
- return -1;
- }
-
- char* arg = argv[0] + 1;
- if (arg_len == 2 && *arg == 'l') {
- list_signals();
- return 0;
- }
-
- sig = strtol(arg, &endptr, 10);
- if (*endptr != '\0') {
- sig = name_to_signal(arg);
- if (sig == SENTINEL) {
- fprintf(stderr, "invalid signal name: %s\n", arg);
- return -1;
- }
- }
-
- argc--;
- argv++;
- }
-
- while(argc > 0){
- int pid = atoi(argv[0]);
- int err = kill(pid, sig);
- if (err < 0) {
- result = err;
- fprintf(stderr, "could not kill pid %d: %s\n", pid, strerror(errno));
- }
-
- argc--;
- argv++;
- }
-
- return result;
-}
diff --git a/toolbox/ln.c b/toolbox/ln.c
deleted file mode 100644
index dcd5e3a..0000000
--- a/toolbox/ln.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-
-static int usage()
-{
- fprintf(stderr,"ln [-s] <target> <name>\n");
- return -1;
-}
-
-int ln_main(int argc, char *argv[])
-{
- int symbolic = 0;
- int ret;
- if(argc < 2) return usage();
-
- if(!strcmp(argv[1],"-s")) {
- symbolic = 1;
- argc--;
- argv++;
- }
-
- if(argc < 3) return usage();
-
- if(symbolic) {
- ret = symlink(argv[1], argv[2]);
- } else {
- ret = link(argv[1], argv[2]);
- }
- if(ret < 0)
- fprintf(stderr, "link failed %s\n", strerror(errno));
- return ret;
-}
diff --git a/toolbox/load_policy.c b/toolbox/load_policy.c
index eb5aba6..90d48c4 100644
--- a/toolbox/load_policy.c
+++ b/toolbox/load_policy.c
@@ -10,7 +10,7 @@
int load_policy_main(int argc, char **argv)
{
- int fd, rc, vers;
+ int fd, rc;
struct stat sb;
void *map;
const char *path;
diff --git a/toolbox/ls.c b/toolbox/ls.c
index 3cc5bb2..963fcb5 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -33,7 +33,7 @@
// fwd
static int listpath(const char *name, int flags);
-static char mode2kind(unsigned mode)
+static char mode2kind(mode_t mode)
{
switch(mode & S_IFMT){
case S_IFSOCK: return 's';
@@ -47,7 +47,7 @@
}
}
-static void mode2str(unsigned mode, char *out)
+void strmode(mode_t mode, char *out)
{
*out++ = mode2kind(mode);
@@ -137,7 +137,7 @@
/* blocks are 512 bytes, we want output to be KB */
if ((flags & LIST_SIZE) != 0) {
- printf("%lld ", s->st_blocks / 2);
+ printf("%lld ", (long long)s->st_blocks / 2);
}
if ((flags & LIST_CLASSIFY) != 0) {
@@ -180,7 +180,7 @@
name++;
}
- mode2str(s->st_mode, mode);
+ strmode(s->st_mode, mode);
if (flags & LIST_LONG_NUMERIC) {
snprintf(user, sizeof(user), "%u", s->st_uid);
snprintf(group, sizeof(group), "%u", s->st_gid);
@@ -205,7 +205,7 @@
break;
case S_IFREG:
printf("%s %-8s %-8s %8lld %s %s\n",
- mode, user, group, s->st_size, date, name);
+ mode, user, group, (long long)s->st_size, date, name);
break;
case S_IFLNK: {
char linkto[256];
@@ -260,7 +260,7 @@
return -1;
}
- mode2str(s->st_mode, mode);
+ strmode(s->st_mode, mode);
user2str(s->st_uid, user, sizeof(user));
group2str(s->st_gid, group, sizeof(group));
@@ -321,7 +321,7 @@
}
if(flags & LIST_INODE) {
- printf("%8llu ", s.st_ino);
+ printf("%8llu ", (unsigned long long)s.st_ino);
}
if ((flags & LIST_MACLABEL) != 0) {
@@ -443,7 +443,6 @@
int ls_main(int argc, char **argv)
{
int flags = 0;
- int listed = 0;
if(argc > 1) {
int i;
diff --git a/toolbox/lsmod.c b/toolbox/lsmod.c
deleted file mode 100644
index 8b55ee6..0000000
--- a/toolbox/lsmod.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <stdio.h>
-
-extern int cat_main(int argc, char **argv);
-
-int lsmod_main(int argc, char **argv)
-{
- char *cat_argv[] = { "cat", "/proc/modules", NULL };
- return cat_main(2, cat_argv);
-}
-
diff --git a/toolbox/lsof.c b/toolbox/lsof.c
index 113c120..982f5aa 100644
--- a/toolbox/lsof.c
+++ b/toolbox/lsof.c
@@ -35,6 +35,7 @@
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <pwd.h>
@@ -99,10 +100,7 @@
static void print_maps(struct pid_info_t* info)
{
FILE *maps;
- char buffer[PATH_MAX + 100];
-
size_t offset;
- int major, minor;
char device[10];
long int inode;
char file[PATH_MAX];
@@ -113,7 +111,7 @@
if (!maps)
goto out;
- while (fscanf(maps, "%*x-%*x %*s %zx %5s %ld %s\n", &offset, device, &inode,
+ while (fscanf(maps, "%*x-%*x %*s %zx %s %ld %s\n", &offset, device, &inode,
file) == 4) {
// We don't care about non-file maps
if (inode == 0 || !strcmp(device, "00:00"))
diff --git a/toolbox/lsusb.c b/toolbox/lsusb.c
deleted file mode 100644
index 236e74b..0000000
--- a/toolbox/lsusb.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (C) 2010 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 <endian.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-
-#include <usbhost/usbhost.h>
-
-static int verbose = 0;
-static char str_buff[4096];
-
-static const char *get_str(struct usb_device *dev, int id)
-{
- char *str = usb_device_get_string(dev, id);
-
- if (id && str) {
- strlcpy(str_buff, str, sizeof(str_buff));
- free(str);
- } else {
- snprintf(str_buff, sizeof(str_buff), "%02x", id);
- }
-
- return str_buff;
-}
-
-
-static void lsusb_parse_device_descriptor(struct usb_device *dev,
- struct usb_device_descriptor *desc)
-{
- printf(" Device Descriptor\n");
- printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB));
- printf("\tbDeviceClass: %02x\n", desc->bDeviceClass);
- printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass);
- printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol);
- printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0);
- printf("\tidVendor: %04x\n", letoh16(desc->idVendor));
- printf("\tidProduct: %04x\n", letoh16(desc->idProduct));
- printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice));
- printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer));
- printf("\tiProduct: %s\n", get_str(dev, desc->iProduct));
- printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber));
- printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations);
- printf("\n");
-}
-
-static void lsusb_parse_config_descriptor(struct usb_device *dev,
- struct usb_config_descriptor *desc)
-{
- printf(" Config Descriptor\n");
- printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength));
- printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces);
- printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue);
- printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration));
- printf("\tbmAttributes: %02x\n", desc->bmAttributes);
- printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2);
- printf("\n");
-}
-
-static void lsusb_parse_interface_descriptor(struct usb_device *dev,
- struct usb_interface_descriptor *desc)
-{
- printf(" Interface Descriptor\n");
- printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber);
- printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting);
- printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints);
- printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass);
- printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass);
- printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol);
- printf("\tiInterface: %s\n", get_str(dev, desc->iInterface));
- printf("\n");
-}
-
-static void lsusb_parse_endpoint_descriptor(struct usb_device *dev,
- struct usb_endpoint_descriptor *desc)
-{
- printf(" Endpoint Descriptor\n");
- printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress);
- printf("\tbmAttributes: %02x\n", desc->bmAttributes);
- printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize));
- printf("\tbInterval: %02x\n", desc->bInterval);
- printf("\tbRefresh: %02x\n", desc->bRefresh);
- printf("\tbSynchAddress: %02x\n", desc->bSynchAddress);
- printf("\n");
-}
-
-static void lsusb_dump_descriptor(struct usb_device *dev,
- struct usb_descriptor_header *desc)
-{
- int i;
- printf(" Descriptor type %02x\n", desc->bDescriptorType);
-
- for (i = 0; i < desc->bLength; i++ ) {
- if ((i % 16) == 0)
- printf("\t%02x:", i);
- printf(" %02x", ((uint8_t *)desc)[i]);
- if ((i % 16) == 15)
- printf("\n");
- }
-
- if ((i % 16) != 0)
- printf("\n");
- printf("\n");
-}
-
-static void lsusb_parse_descriptor(struct usb_device *dev,
- struct usb_descriptor_header *desc)
-{
- switch (desc->bDescriptorType) {
- case USB_DT_DEVICE:
- lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc);
- break;
-
- case USB_DT_CONFIG:
- lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc);
- break;
-
- case USB_DT_INTERFACE:
- lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc);
- break;
-
- case USB_DT_ENDPOINT:
- lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc);
- break;
-
- default:
- lsusb_dump_descriptor(dev, desc);
-
- break;
- }
-}
-
-static int lsusb_device_added(const char *dev_name, void *client_data)
-{
- struct usb_device *dev = usb_device_open(dev_name);
-
- if (!dev) {
- fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
- return 0;
- }
-
- if (verbose) {
- struct usb_descriptor_iter iter;
- struct usb_descriptor_header *desc;
-
- printf("%s:\n", dev_name);
-
- usb_descriptor_iter_init(dev, &iter);
-
- while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
- lsusb_parse_descriptor(dev, desc);
-
- } else {
- uint16_t vid, pid;
- char *mfg_name, *product_name, *serial;
-
- vid = usb_device_get_vendor_id(dev);
- pid = usb_device_get_product_id(dev);
- mfg_name = usb_device_get_manufacturer_name(dev);
- product_name = usb_device_get_product_name(dev);
- serial = usb_device_get_serial(dev);
-
- printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
- mfg_name, product_name, serial);
-
- free(mfg_name);
- free(product_name);
- free(serial);
- }
-
- usb_device_close(dev);
-
- return 0;
-}
-
-static int lsusb_device_removed(const char *dev_name, void *client_data)
-{
- return 0;
-}
-
-
-static int lsusb_discovery_done(void *client_data)
-{
- return 1;
-}
-
-
-
-int lsusb_main(int argc, char **argv)
-{
- struct usb_host_context *ctx;
-
- if (argc == 2 && !strcmp(argv[1], "-v"))
- verbose = 1;
-
- ctx = usb_host_init();
- if (!ctx) {
- perror("usb_host_init:");
- return 1;
- }
-
- usb_host_run(ctx,
- lsusb_device_added,
- lsusb_device_removed,
- lsusb_discovery_done,
- NULL);
-
- usb_host_cleanup(ctx);
-
- return 0;
-}
-
diff --git a/toolbox/md5.c b/toolbox/md5.c
deleted file mode 100644
index 2fb8b05..0000000
--- a/toolbox/md5.c
+++ /dev/null
@@ -1,77 +0,0 @@
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <md5.h>
-
-/* When this was written, bionic's md5.h did not define this. */
-#ifndef MD5_DIGEST_LENGTH
-#define MD5_DIGEST_LENGTH 16
-#endif
-
-static int usage()
-{
- fprintf(stderr,"md5 file ...\n");
- return -1;
-}
-
-static int do_md5(const char *path)
-{
- unsigned int i;
- int fd;
- MD5_CTX md5_ctx;
- unsigned char md5[MD5_DIGEST_LENGTH];
-
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr,"could not open %s, %s\n", path, strerror(errno));
- return -1;
- }
-
- /* Note that bionic's MD5_* functions return void. */
- MD5_Init(&md5_ctx);
-
- while (1) {
- char buf[4096];
- ssize_t rlen;
- rlen = read(fd, buf, sizeof(buf));
- if (rlen == 0)
- break;
- else if (rlen < 0) {
- (void)close(fd);
- fprintf(stderr,"could not read %s, %s\n", path, strerror(errno));
- return -1;
- }
- MD5_Update(&md5_ctx, buf, rlen);
- }
- if (close(fd)) {
- fprintf(stderr,"could not close %s, %s\n", path, strerror(errno));
- return -1;
- }
-
- MD5_Final(md5, &md5_ctx);
-
- for (i = 0; i < (int)sizeof(md5); i++)
- printf("%02x", md5[i]);
- printf(" %s\n", path);
-
- return 0;
-}
-
-int md5_main(int argc, char *argv[])
-{
- int i, ret = 0;
-
- if (argc < 2)
- return usage();
-
- /* loop over the file args */
- for (i = 1; i < argc; i++) {
- if (do_md5(argv[i]))
- ret = 1;
- }
-
- return ret;
-}
diff --git a/toolbox/mkdir.c b/toolbox/mkdir.c
deleted file mode 100644
index 656970a..0000000
--- a/toolbox/mkdir.c
+++ /dev/null
@@ -1,78 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/limits.h>
-#include <sys/stat.h>
-
-static int usage()
-{
- fprintf(stderr,"mkdir [OPTION] <target>\n");
- fprintf(stderr," --help display usage and exit\n");
- fprintf(stderr," -p, --parents create parent directories as needed\n");
- return -1;
-}
-
-int mkdir_main(int argc, char *argv[])
-{
- int symbolic = 0;
- int ret;
- if(argc < 2 || strcmp(argv[1], "--help") == 0) {
- return usage();
- }
-
- int recursive = (strcmp(argv[1], "-p") == 0 ||
- strcmp(argv[1], "--parents") == 0) ? 1 : 0;
-
- if(recursive && argc < 3) {
- // -p specified without a path
- return usage();
- }
-
- if(recursive) {
- argc--;
- argv++;
- }
-
- char currpath[PATH_MAX], *pathpiece;
- struct stat st;
-
- while(argc > 1) {
- argc--;
- argv++;
- if(recursive) {
- // reset path
- strcpy(currpath, "");
- // create the pieces of the path along the way
- pathpiece = strtok(argv[0], "/");
- if(argv[0][0] == '/') {
- // prepend / if needed
- strcat(currpath, "/");
- }
- while(pathpiece != NULL) {
- if(strlen(currpath) + strlen(pathpiece) + 2/*NUL and slash*/ > PATH_MAX) {
- fprintf(stderr, "Invalid path specified: too long\n");
- return 1;
- }
- strcat(currpath, pathpiece);
- strcat(currpath, "/");
- if(stat(currpath, &st) != 0) {
- ret = mkdir(currpath, 0777);
- if(ret < 0) {
- fprintf(stderr, "mkdir failed for %s, %s\n", currpath, strerror(errno));
- return ret;
- }
- }
- pathpiece = strtok(NULL, "/");
- }
- } else {
- ret = mkdir(argv[0], 0777);
- if(ret < 0) {
- fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno));
- return ret;
- }
- }
- }
-
- return 0;
-}
diff --git a/toolbox/mknod.c b/toolbox/mknod.c
deleted file mode 100644
index 0fedece..0000000
--- a/toolbox/mknod.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2014, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-static int print_usage() {
- fprintf(stderr, "mknod <path> [b|c|u|p] <major> <minor>\n");
- return EXIT_FAILURE;
-}
-
-int mknod_main(int argc, char **argv) {
- char *path = NULL;
- int major = 0;
- int minor = 0;
- int args = 0;
- mode_t mode = 0660;
-
- /* Check correct argument count is 3 or 5 */
- if (argc != 3 && argc != 5) {
- fprintf(stderr, "Incorrect argument count\n");
- return print_usage();
- }
-
- path = argv[1];
-
- const char node_type = *argv[2];
- switch (node_type) {
- case 'b':
- mode |= S_IFBLK;
- args = 5;
- break;
- case 'c':
- case 'u':
- mode |= S_IFCHR;
- args = 5;
- break;
- case 'p':
- mode |= S_IFIFO;
- args = 3;
- break;
- default:
- fprintf(stderr, "Invalid node type '%c'\n", node_type);
- return print_usage();
- }
-
- if (argc != args) {
- if (args == 5) {
- fprintf(stderr, "Node type '%c' requires <major> and <minor>\n", node_type);
- } else {
- fprintf(stderr, "Node type '%c' does not require <major> and <minor>\n", node_type);
- }
- return print_usage();
- }
-
- if (args == 5) {
- major = atoi(argv[3]);
- minor = atoi(argv[4]);
- }
-
- if (mknod(path, mode, makedev(major, minor))) {
- perror("Unable to create node");
- return EXIT_FAILURE;
- }
- return 0;
-}
diff --git a/toolbox/mkswap.c b/toolbox/mkswap.c
deleted file mode 100644
index 1710ef6..0000000
--- a/toolbox/mkswap.c
+++ /dev/null
@@ -1,94 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <asm/page.h>
-#include <sys/swap.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-/* XXX This needs to be obtained from kernel headers. See b/9336527 */
-struct linux_swap_header {
- char bootbits[1024]; /* Space for disklabel etc. */
- uint32_t version;
- uint32_t last_page;
- uint32_t nr_badpages;
- unsigned char sws_uuid[16];
- unsigned char sws_volume[16];
- uint32_t padding[117];
- uint32_t badpages[1];
-};
-
-#define MAGIC_SWAP_HEADER "SWAPSPACE2"
-#define MAGIC_SWAP_HEADER_LEN 10
-#define MIN_PAGES 10
-
-int mkswap_main(int argc, char **argv)
-{
- int err = 0;
- int fd;
- ssize_t len;
- off_t swap_size;
- int pagesize;
- struct linux_swap_header sw_hdr;
-
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
- return -EINVAL;
- }
-
- fd = open(argv[1], O_WRONLY);
- if (fd < 0) {
- err = errno;
- fprintf(stderr, "Cannot open %s\n", argv[1]);
- return err;
- }
-
- pagesize = getpagesize();
- /* Determine the length of the swap file */
- swap_size = lseek(fd, 0, SEEK_END);
- if (swap_size < MIN_PAGES * pagesize) {
- fprintf(stderr, "Swap file needs to be at least %dkB\n",
- (MIN_PAGES * pagesize) >> 10);
- err = -ENOSPC;
- goto err;
- }
- if (lseek(fd, 0, SEEK_SET)) {
- err = errno;
- fprintf(stderr, "Can't seek to the beginning of the file\n");
- goto err;
- }
-
- memset(&sw_hdr, 0, sizeof(sw_hdr));
- sw_hdr.version = 1;
- sw_hdr.last_page = (swap_size / pagesize) - 1;
-
- len = write(fd, &sw_hdr, sizeof(sw_hdr));
- if (len != sizeof(sw_hdr)) {
- err = errno;
- fprintf(stderr, "Failed to write swap header into %s\n", argv[1]);
- goto err;
- }
-
- /* Write the magic header */
- if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
- err = errno;
- fprintf(stderr, "Failed to seek into %s\n", argv[1]);
- goto err;
- }
-
- len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
- if (len != MAGIC_SWAP_HEADER_LEN) {
- err = errno;
- fprintf(stderr, "Failed to write magic swap header into %s\n", argv[1]);
- goto err;
- }
-
- if (fsync(fd) < 0) {
- err = errno;
- fprintf(stderr, "Failed to sync %s\n", argv[1]);
- goto err;
- }
-err:
- close(fd);
- return err;
-}
diff --git a/toolbox/mv.c b/toolbox/mv.c
deleted file mode 100644
index a5bc225..0000000
--- a/toolbox/mv.c
+++ /dev/null
@@ -1,59 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <limits.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-
-int mv_main(int argc, char *argv[])
-{
- const char* dest;
- struct stat st;
- int i;
-
- if (argc < 3) {
- fprintf(stderr,"USAGE: %s <source...> <destination>\n", argv[0]);
- return -1;
- }
-
- /* check if destination exists */
- dest = argv[argc - 1];
- if (stat(dest, &st)) {
- /* an error, unless the destination was missing */
- if (errno != ENOENT) {
- fprintf(stderr, "failed on %s - %s\n", dest, strerror(errno));
- return -1;
- }
- st.st_mode = 0;
- }
-
- for (i = 1; i < argc - 1; i++) {
- const char *source = argv[i];
- char fullDest[PATH_MAX + 1 + PATH_MAX + 1];
- /* assume we build "dest/source", and let rename() fail on pathsize */
- if (strlen(dest) + 1 + strlen(source) + 1 > sizeof(fullDest)) {
- fprintf(stderr, "path too long\n");
- return -1;
- }
- strcpy(fullDest, dest);
-
- /* if destination is a directory, concat the source file name */
- if (S_ISDIR(st.st_mode)) {
- const char *fileName = strrchr(source, '/');
- if (fullDest[strlen(fullDest)-1] != '/') {
- strcat(fullDest, "/");
- }
- strcat(fullDest, fileName ? fileName + 1 : source);
- }
-
- /* attempt to move it */
- if (rename(source, fullDest)) {
- fprintf(stderr, "failed on '%s' - %s\n", source, strerror(errno));
- return -1;
- }
- }
-
- return 0;
-}
-
diff --git a/toolbox/nandread.c b/toolbox/nandread.c
index 971c232..bd19942 100644
--- a/toolbox/nandread.c
+++ b/toolbox/nandread.c
@@ -1,9 +1,10 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <mtd/mtd-user.h>
@@ -189,18 +190,18 @@
for (pos = start, opos = 0; pos < end; pos += mtdinfo.writesize) {
bad_block = 0;
if (verbose > 3)
- printf("reading at %llx\n", pos);
+ printf("reading at %" PRIx64 "\n", pos);
lseek64(fd, pos, SEEK_SET);
ret = read(fd, buffer, mtdinfo.writesize + rawmode);
if (ret < (int)mtdinfo.writesize) {
- fprintf(stderr, "short read at %llx, %d\n", pos, ret);
+ fprintf(stderr, "short read at %" PRIx64 ", %d\n", pos, ret);
bad_block = 2;
}
if (!rawmode) {
oobbuf.start = pos;
ret = ioctl(fd, MEMREADOOB, &oobbuf);
if (ret) {
- fprintf(stderr, "failed to read oob data at %llx, %d\n", pos, ret);
+ fprintf(stderr, "failed to read oob data at %" PRIx64 ", %d\n", pos, ret);
bad_block = 2;
}
}
@@ -213,17 +214,17 @@
bpos = pos / mtdinfo.erasesize * mtdinfo.erasesize;
ret = ioctl(fd, MEMGETBADBLOCK, &bpos);
if (ret && errno != EOPNOTSUPP) {
- printf("badblock at %llx\n", pos);
+ printf("badblock at %" PRIx64 "\n", pos);
bad_block = 1;
}
if (ecc.corrected != last_ecc.corrected)
- printf("ecc corrected, %u, at %llx\n", ecc.corrected - last_ecc.corrected, pos);
+ printf("ecc corrected, %u, at %" PRIx64 "\n", ecc.corrected - last_ecc.corrected, pos);
if (ecc.failed != last_ecc.failed)
- printf("ecc failed, %u, at %llx\n", ecc.failed - last_ecc.failed, pos);
+ printf("ecc failed, %u, at %" PRIx64 "\n", ecc.failed - last_ecc.failed, pos);
if (ecc.badblocks != last_ecc.badblocks)
- printf("ecc badblocks, %u, at %llx\n", ecc.badblocks - last_ecc.badblocks, pos);
+ printf("ecc badblocks, %u, at %" PRIx64 "\n", ecc.badblocks - last_ecc.badblocks, pos);
if (ecc.bbtblocks != last_ecc.bbtblocks)
- printf("ecc bbtblocks, %u, at %llx\n", ecc.bbtblocks - last_ecc.bbtblocks, pos);
+ printf("ecc bbtblocks, %u, at %" PRIx64 "\n", ecc.bbtblocks - last_ecc.bbtblocks, pos);
if (!rawmode) {
oob_fixed = (uint8_t *)oob_data;
@@ -241,18 +242,18 @@
if (outfd >= 0) {
ret = write(outfd, buffer, mtdinfo.writesize + spare_size);
if (ret < (int)(mtdinfo.writesize + spare_size)) {
- fprintf(stderr, "short write at %llx, %d\n", pos, ret);
+ fprintf(stderr, "short write at %" PRIx64 ", %d\n", pos, ret);
close(outfd);
outfd = -1;
}
if (ecc.corrected != last_ecc.corrected)
- fprintf(statusfile, "%08llx: ecc corrected\n", opos);
+ fprintf(statusfile, "%08" PRIx64 ": ecc corrected\n", opos);
if (ecc.failed != last_ecc.failed)
- fprintf(statusfile, "%08llx: ecc failed\n", opos);
+ fprintf(statusfile, "%08" PRIx64 ": ecc failed\n", opos);
if (bad_block == 1)
- fprintf(statusfile, "%08llx: badblock\n", opos);
+ fprintf(statusfile, "%08" PRIx64 ": badblock\n", opos);
if (bad_block == 2)
- fprintf(statusfile, "%08llx: read error\n", opos);
+ fprintf(statusfile, "%08" PRIx64 ": read error\n", opos);
opos += mtdinfo.writesize + spare_size;
}
@@ -261,7 +262,7 @@
if (test_empty(buffer, mtdinfo.writesize + mtdinfo.oobsize + spare_size))
empty_pages++;
else if (verbose > 2 || (verbose > 1 && !(pos & (mtdinfo.erasesize - 1))))
- printf("page at %llx (%d oobbytes): %08x %08x %08x %08x "
+ printf("page at %" PRIx64 " (%d oobbytes): %08x %08x %08x %08x "
"%08x %08x %08x %08x\n", pos, oobbuf.start,
oob_data[0], oob_data[1], oob_data[2], oob_data[3],
oob_data[4], oob_data[5], oob_data[6], oob_data[7]);
diff --git a/toolbox/netstat.c b/toolbox/netstat.c
deleted file mode 100644
index 05dc640..0000000
--- a/toolbox/netstat.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2008, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-
-typedef union iaddr iaddr;
-typedef union iaddr6 iaddr6;
-
-union iaddr {
- unsigned u;
- unsigned char b[4];
-};
-
-union iaddr6 {
- struct {
- unsigned a;
- unsigned b;
- unsigned c;
- unsigned d;
- } u;
- unsigned char b[16];
-};
-
-static const char *state2str(unsigned state)
-{
- switch(state){
- case 0x1: return "ESTABLISHED";
- case 0x2: return "SYN_SENT";
- case 0x3: return "SYN_RECV";
- case 0x4: return "FIN_WAIT1";
- case 0x5: return "FIN_WAIT2";
- case 0x6: return "TIME_WAIT";
- case 0x7: return "CLOSE";
- case 0x8: return "CLOSE_WAIT";
- case 0x9: return "LAST_ACK";
- case 0xA: return "LISTEN";
- case 0xB: return "CLOSING";
- default: return "UNKNOWN";
- }
-}
-
-/* addr + : + port + \0 */
-#define ADDR_LEN INET6_ADDRSTRLEN + 1 + 5 + 1
-
-static void addr2str(int af, const void *addr, unsigned port, char *buf)
-{
- if (inet_ntop(af, addr, buf, ADDR_LEN) == NULL) {
- *buf = '\0';
- return;
- }
- size_t len = strlen(buf);
- if (port) {
- snprintf(buf+len, ADDR_LEN-len, ":%d", port);
- } else {
- strncat(buf+len, ":*", ADDR_LEN-len-1);
- }
-}
-
-static void ipv4(const char *filename, const char *label) {
- FILE *fp = fopen(filename, "r");
- if (fp == NULL) {
- return;
- }
- char buf[BUFSIZ];
- fgets(buf, BUFSIZ, fp);
- while (fgets(buf, BUFSIZ, fp)){
- char lip[ADDR_LEN];
- char rip[ADDR_LEN];
- iaddr laddr, raddr;
- unsigned lport, rport, state, txq, rxq, num;
- int n = sscanf(buf, " %d: %x:%x %x:%x %x %x:%x",
- &num, &laddr.u, &lport, &raddr.u, &rport,
- &state, &txq, &rxq);
- if (n == 8) {
- addr2str(AF_INET, &laddr, lport, lip);
- addr2str(AF_INET, &raddr, rport, rip);
-
- printf("%4s %6d %6d %-22s %-22s %s\n",
- label, rxq, txq, lip, rip,
- state2str(state));
- }
- }
- fclose(fp);
-}
-
-static void ipv6(const char *filename, const char *label) {
- FILE *fp = fopen(filename, "r");
- if (fp == NULL) {
- return;
- }
- char buf[BUFSIZ];
- fgets(buf, BUFSIZ, fp);
- while (fgets(buf, BUFSIZ, fp)){
- char lip[ADDR_LEN];
- char rip[ADDR_LEN];
- iaddr6 laddr6, raddr6;
- unsigned lport, rport, state, txq, rxq, num;
- int n = sscanf(buf, " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x",
- &num, &laddr6.u.a, &laddr6.u.b, &laddr6.u.c, &laddr6.u.d, &lport,
- &raddr6.u.a, &raddr6.u.b, &raddr6.u.c, &raddr6.u.d, &rport,
- &state, &txq, &rxq);
- if (n == 14) {
- addr2str(AF_INET6, &laddr6, lport, lip);
- addr2str(AF_INET6, &raddr6, rport, rip);
-
- printf("%4s %6d %6d %-22s %-22s %s\n",
- label, rxq, txq, lip, rip,
- state2str(state));
- }
- }
- fclose(fp);
-}
-
-int netstat_main(int argc, char *argv[])
-{
- printf("Proto Recv-Q Send-Q Local Address Foreign Address State\n");
- ipv4("/proc/net/tcp", "tcp");
- ipv4("/proc/net/udp", "udp");
- ipv6("/proc/net/tcp6", "tcp6");
- ipv6("/proc/net/udp6", "udp6");
- return 0;
-}
diff --git a/toolbox/newfs_msdos.c b/toolbox/newfs_msdos.c
index 27dca42..01517fd 100644
--- a/toolbox/newfs_msdos.c
+++ b/toolbox/newfs_msdos.c
@@ -27,20 +27,20 @@
#ifndef lint
static const char rcsid[] =
- "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.33 2009/04/11 14:56:29 ed Exp $";
+ "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.33 2009/04/11 14:56:29 ed Exp $";
#endif /* not lint */
#include <sys/param.h>
#ifndef ANDROID
- #include <sys/fdcio.h>
- #include <sys/disk.h>
- #include <sys/disklabel.h>
- #include <sys/mount.h>
+#include <sys/fdcio.h>
+#include <sys/disk.h>
+#include <sys/disklabel.h>
+#include <sys/mount.h>
#else
- #include <stdarg.h>
- #include <linux/fs.h>
- #include <linux/hdreg.h>
+#include <stdarg.h>
+#include <linux/fs.h>
+#include <linux/hdreg.h>
#endif
#include <sys/stat.h>
@@ -58,44 +58,44 @@
#include <time.h>
#include <unistd.h>
-#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
-#define BPN 4 /* bits per nibble */
-#define NPB 2 /* nibbles per byte */
+#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
+#define BPN 4 /* bits per nibble */
+#define NPB 2 /* nibbles per byte */
-#define DOSMAGIC 0xaa55 /* DOS magic number */
-#define MINBPS 512 /* minimum bytes per sector */
-#define MAXSPC 128 /* maximum sectors per cluster */
-#define MAXNFT 16 /* maximum number of FATs */
-#define DEFBLK 4096 /* default block size */
-#define DEFBLK16 2048 /* default block size FAT16 */
-#define DEFRDE 512 /* default root directory entries */
-#define RESFTE 2 /* reserved FAT entries */
-#define MINCLS12 1 /* minimum FAT12 clusters */
-#define MINCLS16 0x1000 /* minimum FAT16 clusters */
-#define MINCLS32 2 /* minimum FAT32 clusters */
-#define MAXCLS12 0xfed /* maximum FAT12 clusters */
-#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
-#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
+#define DOSMAGIC 0xaa55 /* DOS magic number */
+#define MINBPS 512 /* minimum bytes per sector */
+#define MAXSPC 128 /* maximum sectors per cluster */
+#define MAXNFT 16 /* maximum number of FATs */
+#define DEFBLK 4096 /* default block size */
+#define DEFBLK16 2048 /* default block size FAT16 */
+#define DEFRDE 512 /* default root directory entries */
+#define RESFTE 2 /* reserved FAT entries */
+#define MINCLS12 1 /* minimum FAT12 clusters */
+#define MINCLS16 0x1000 /* minimum FAT16 clusters */
+#define MINCLS32 2 /* minimum FAT32 clusters */
+#define MAXCLS12 0xfed /* maximum FAT12 clusters */
+#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
+#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
-#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
- (fat) == 16 ? MINCLS16 : \
- MINCLS32)
+#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
+ (fat) == 16 ? MINCLS16 : \
+ MINCLS32)
-#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
- (fat) == 16 ? MAXCLS16 : \
- MAXCLS32)
+#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
+ (fat) == 16 ? MAXCLS16 : \
+ MAXCLS32)
-#define mk1(p, x) \
+#define mk1(p, x) \
(p) = (u_int8_t)(x)
-#define mk2(p, x) \
- (p)[0] = (u_int8_t)(x), \
+#define mk2(p, x) \
+ (p)[0] = (u_int8_t)(x), \
(p)[1] = (u_int8_t)((x) >> 010)
-#define mk4(p, x) \
- (p)[0] = (u_int8_t)(x), \
- (p)[1] = (u_int8_t)((x) >> 010), \
- (p)[2] = (u_int8_t)((x) >> 020), \
+#define mk4(p, x) \
+ (p)[0] = (u_int8_t)(x), \
+ (p)[1] = (u_int8_t)((x) >> 010), \
+ (p)[2] = (u_int8_t)((x) >> 020), \
(p)[3] = (u_int8_t)((x) >> 030)
#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
@@ -104,71 +104,71 @@
#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
struct bs {
- u_int8_t jmp[3]; /* bootstrap entry point */
- u_int8_t oem[8]; /* OEM name and version */
+ u_int8_t jmp[3]; /* bootstrap entry point */
+ u_int8_t oem[8]; /* OEM name and version */
};
struct bsbpb {
- u_int8_t bps[2]; /* bytes per sector */
- u_int8_t spc; /* sectors per cluster */
- u_int8_t res[2]; /* reserved sectors */
- u_int8_t nft; /* number of FATs */
- u_int8_t rde[2]; /* root directory entries */
- u_int8_t sec[2]; /* total sectors */
- u_int8_t mid; /* media descriptor */
- u_int8_t spf[2]; /* sectors per FAT */
- u_int8_t spt[2]; /* sectors per track */
- u_int8_t hds[2]; /* drive heads */
- u_int8_t hid[4]; /* hidden sectors */
- u_int8_t bsec[4]; /* big total sectors */
+ u_int8_t bps[2]; /* bytes per sector */
+ u_int8_t spc; /* sectors per cluster */
+ u_int8_t res[2]; /* reserved sectors */
+ u_int8_t nft; /* number of FATs */
+ u_int8_t rde[2]; /* root directory entries */
+ u_int8_t sec[2]; /* total sectors */
+ u_int8_t mid; /* media descriptor */
+ u_int8_t spf[2]; /* sectors per FAT */
+ u_int8_t spt[2]; /* sectors per track */
+ u_int8_t hds[2]; /* drive heads */
+ u_int8_t hid[4]; /* hidden sectors */
+ u_int8_t bsec[4]; /* big total sectors */
};
struct bsxbpb {
- u_int8_t bspf[4]; /* big sectors per FAT */
- u_int8_t xflg[2]; /* FAT control flags */
- u_int8_t vers[2]; /* file system version */
- u_int8_t rdcl[4]; /* root directory start cluster */
- u_int8_t infs[2]; /* file system info sector */
- u_int8_t bkbs[2]; /* backup boot sector */
- u_int8_t rsvd[12]; /* reserved */
+ u_int8_t bspf[4]; /* big sectors per FAT */
+ u_int8_t xflg[2]; /* FAT control flags */
+ u_int8_t vers[2]; /* file system version */
+ u_int8_t rdcl[4]; /* root directory start cluster */
+ u_int8_t infs[2]; /* file system info sector */
+ u_int8_t bkbs[2]; /* backup boot sector */
+ u_int8_t rsvd[12]; /* reserved */
};
struct bsx {
- u_int8_t drv; /* drive number */
- u_int8_t rsvd; /* reserved */
- u_int8_t sig; /* extended boot signature */
- u_int8_t volid[4]; /* volume ID number */
- u_int8_t label[11]; /* volume label */
- u_int8_t type[8]; /* file system type */
+ u_int8_t drv; /* drive number */
+ u_int8_t rsvd; /* reserved */
+ u_int8_t sig; /* extended boot signature */
+ u_int8_t volid[4]; /* volume ID number */
+ u_int8_t label[11]; /* volume label */
+ u_int8_t type[8]; /* file system type */
};
struct de {
- u_int8_t namext[11]; /* name and extension */
- u_int8_t attr; /* attributes */
- u_int8_t rsvd[10]; /* reserved */
- u_int8_t time[2]; /* creation time */
- u_int8_t date[2]; /* creation date */
- u_int8_t clus[2]; /* starting cluster */
- u_int8_t size[4]; /* size */
+ u_int8_t namext[11]; /* name and extension */
+ u_int8_t attr; /* attributes */
+ u_int8_t rsvd[10]; /* reserved */
+ u_int8_t time[2]; /* creation time */
+ u_int8_t date[2]; /* creation date */
+ u_int8_t clus[2]; /* starting cluster */
+ u_int8_t size[4]; /* size */
};
struct bpb {
- u_int bps; /* bytes per sector */
- u_int spc; /* sectors per cluster */
- u_int res; /* reserved sectors */
- u_int nft; /* number of FATs */
- u_int rde; /* root directory entries */
- u_int sec; /* total sectors */
- u_int mid; /* media descriptor */
- u_int spf; /* sectors per FAT */
- u_int spt; /* sectors per track */
- u_int hds; /* drive heads */
- u_int hid; /* hidden sectors */
- u_int bsec; /* big total sectors */
- u_int bspf; /* big sectors per FAT */
- u_int rdcl; /* root directory start cluster */
- u_int infs; /* file system info sector */
- u_int bkbs; /* backup boot sector */
+ u_int bps; /* bytes per sector */
+ u_int spc; /* sectors per cluster */
+ u_int res; /* reserved sectors */
+ u_int nft; /* number of FATs */
+ u_int rde; /* root directory entries */
+ u_int sec; /* total sectors */
+ u_int mid; /* media descriptor */
+ u_int spf; /* sectors per FAT */
+ u_int spt; /* sectors per track */
+ u_int hds; /* drive heads */
+ u_int hid; /* hidden sectors */
+ u_int bsec; /* big total sectors */
+ u_int bspf; /* big sectors per FAT */
+ u_int rdcl; /* root directory start cluster */
+ u_int infs; /* file system info sector */
+ u_int bkbs; /* backup boot sector */
};
#define BPBGAP 0, 0, 0, 0, 0, 0
@@ -181,35 +181,35 @@
{"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
{"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
{"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
- {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
+ {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
{"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
{"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
- {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
+ {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
{"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
{"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
};
static const u_int8_t bootcode[] = {
- 0xfa, /* cli */
- 0x31, 0xc0, /* xor ax,ax */
- 0x8e, 0xd0, /* mov ss,ax */
- 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
- 0xfb, /* sti */
- 0x8e, 0xd8, /* mov ds,ax */
- 0xe8, 0x00, 0x00, /* call $ + 3 */
- 0x5e, /* pop si */
- 0x83, 0xc6, 0x19, /* add si,+19h */
- 0xbb, 0x07, 0x00, /* mov bx,0007h */
- 0xfc, /* cld */
- 0xac, /* lodsb */
- 0x84, 0xc0, /* test al,al */
- 0x74, 0x06, /* jz $ + 8 */
- 0xb4, 0x0e, /* mov ah,0eh */
- 0xcd, 0x10, /* int 10h */
- 0xeb, 0xf5, /* jmp $ - 9 */
- 0x30, 0xe4, /* xor ah,ah */
- 0xcd, 0x16, /* int 16h */
- 0xcd, 0x19, /* int 19h */
+ 0xfa, /* cli */
+ 0x31, 0xc0, /* xor ax,ax */
+ 0x8e, 0xd0, /* mov ss,ax */
+ 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
+ 0xfb, /* sti */
+ 0x8e, 0xd8, /* mov ds,ax */
+ 0xe8, 0x00, 0x00, /* call $ + 3 */
+ 0x5e, /* pop si */
+ 0x83, 0xc6, 0x19, /* add si,+19h */
+ 0xbb, 0x07, 0x00, /* mov bx,0007h */
+ 0xfc, /* cld */
+ 0xac, /* lodsb */
+ 0x84, 0xc0, /* test al,al */
+ 0x74, 0x06, /* jz $ + 8 */
+ 0xb4, 0x0e, /* mov ah,0eh */
+ 0xcd, 0x10, /* int 10h */
+ 0xeb, 0xf5, /* jmp $ - 9 */
+ 0x30, 0xe4, /* xor ah,ah */
+ 0xcd, 0x16, /* int 16h */
+ 0xcd, 0x19, /* int 19h */
0x0d, 0x0a,
'N', 'o', 'n', '-', 's', 'y', 's', 't',
'e', 'm', ' ', 'd', 'i', 's', 'k',
@@ -223,8 +223,7 @@
static void check_mounted(const char *, mode_t);
static void getstdfmt(const char *, struct bpb *);
-static void getdiskinfo(int, const char *, const char *, int,
- struct bpb *);
+static void getdiskinfo(int, const char *, const char *, int, struct bpb *);
static void print_bpb(struct bpb *);
static u_int ckgeom(const char *, u_int, const char *);
static u_int argtou(const char *, u_int, u_int, const char *);
@@ -237,14 +236,14 @@
/*
* Construct a FAT12, FAT16, or FAT32 file system.
*/
-int
-newfs_msdos_main(int argc, char *argv[])
+int newfs_msdos_main(int argc, char *argv[])
{
- static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
+ static const char opts[] = "@:NAB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
const char *opt_B = NULL, *opt_L = NULL, *opt_O = NULL, *opt_f = NULL;
u_int opt_F = 0, opt_I = 0, opt_S = 0, opt_a = 0, opt_b = 0, opt_c = 0;
u_int opt_e = 0, opt_h = 0, opt_i = 0, opt_k = 0, opt_m = 0, opt_n = 0;
u_int opt_o = 0, opt_r = 0, opt_s = 0, opt_u = 0;
+ u_int opt_A = 0;
int opt_N = 0;
int Iflag = 0, mflag = 0, oflag = 0;
char buf[MAXPATHLEN];
@@ -262,462 +261,486 @@
ssize_t n;
time_t now;
u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
+ u_int extra_res, alignment=0, set_res, set_spf, set_spc, tempx, attempts=0;
int ch, fd, fd1;
off_t opt_create = 0, opt_ofs = 0;
while ((ch = getopt(argc, argv, opts)) != -1)
- switch (ch) {
- case '@':
- opt_ofs = argtooff(optarg, "offset");
- break;
- case 'N':
- opt_N = 1;
- break;
- case 'B':
- opt_B = optarg;
- break;
- case 'C':
- opt_create = argtooff(optarg, "create size");
- break;
- case 'F':
- if (strcmp(optarg, "12") &&
- strcmp(optarg, "16") &&
- strcmp(optarg, "32"))
- errx(1, "%s: bad FAT type", optarg);
- opt_F = atoi(optarg);
- break;
- case 'I':
- opt_I = argto4(optarg, 0, "volume ID");
- Iflag = 1;
- break;
- case 'L':
- if (!oklabel(optarg))
- errx(1, "%s: bad volume label", optarg);
- opt_L = optarg;
- break;
- case 'O':
- if (strlen(optarg) > 8)
- errx(1, "%s: bad OEM string", optarg);
- opt_O = optarg;
- break;
- case 'S':
- opt_S = argto2(optarg, 1, "bytes/sector");
- break;
- case 'a':
- opt_a = argto4(optarg, 1, "sectors/FAT");
- break;
- case 'b':
- opt_b = argtox(optarg, 1, "block size");
- opt_c = 0;
- break;
- case 'c':
- opt_c = argto1(optarg, 1, "sectors/cluster");
- opt_b = 0;
- break;
- case 'e':
- opt_e = argto2(optarg, 1, "directory entries");
- break;
- case 'f':
- opt_f = optarg;
- break;
- case 'h':
- opt_h = argto2(optarg, 1, "drive heads");
- break;
- case 'i':
- opt_i = argto2(optarg, 1, "info sector");
- break;
- case 'k':
- opt_k = argto2(optarg, 1, "backup sector");
- break;
- case 'm':
- opt_m = argto1(optarg, 0, "media descriptor");
- mflag = 1;
- break;
- case 'n':
- opt_n = argto1(optarg, 1, "number of FATs");
- break;
- case 'o':
- opt_o = argto4(optarg, 0, "hidden sectors");
- oflag = 1;
- break;
- case 'r':
- opt_r = argto2(optarg, 1, "reserved sectors");
- break;
- case 's':
- opt_s = argto4(optarg, 1, "file system size");
- break;
- case 'u':
- opt_u = argto2(optarg, 1, "sectors/track");
- break;
- default:
- usage();
- }
+ switch (ch) {
+ case '@':
+ opt_ofs = argtooff(optarg, "offset");
+ break;
+ case 'N':
+ opt_N = 1;
+ break;
+ case 'A':
+ opt_A = 1;
+ break;
+ case 'B':
+ opt_B = optarg;
+ break;
+ case 'C':
+ opt_create = argtooff(optarg, "create size");
+ break;
+ case 'F':
+ if (strcmp(optarg, "12") && strcmp(optarg, "16") && strcmp(optarg, "32"))
+ errx(1, "%s: bad FAT type", optarg);
+ opt_F = atoi(optarg);
+ break;
+ case 'I':
+ opt_I = argto4(optarg, 0, "volume ID");
+ Iflag = 1;
+ break;
+ case 'L':
+ if (!oklabel(optarg))
+ errx(1, "%s: bad volume label", optarg);
+ opt_L = optarg;
+ break;
+ case 'O':
+ if (strlen(optarg) > 8)
+ errx(1, "%s: bad OEM string", optarg);
+ opt_O = optarg;
+ break;
+ case 'S':
+ opt_S = argto2(optarg, 1, "bytes/sector");
+ break;
+ case 'a':
+ opt_a = argto4(optarg, 1, "sectors/FAT");
+ break;
+ case 'b':
+ opt_b = argtox(optarg, 1, "block size");
+ opt_c = 0;
+ break;
+ case 'c':
+ opt_c = argto1(optarg, 1, "sectors/cluster");
+ opt_b = 0;
+ break;
+ case 'e':
+ opt_e = argto2(optarg, 1, "directory entries");
+ break;
+ case 'f':
+ opt_f = optarg;
+ break;
+ case 'h':
+ opt_h = argto2(optarg, 1, "drive heads");
+ break;
+ case 'i':
+ opt_i = argto2(optarg, 1, "info sector");
+ break;
+ case 'k':
+ opt_k = argto2(optarg, 1, "backup sector");
+ break;
+ case 'm':
+ opt_m = argto1(optarg, 0, "media descriptor");
+ mflag = 1;
+ break;
+ case 'n':
+ opt_n = argto1(optarg, 1, "number of FATs");
+ break;
+ case 'o':
+ opt_o = argto4(optarg, 0, "hidden sectors");
+ oflag = 1;
+ break;
+ case 'r':
+ opt_r = argto2(optarg, 1, "reserved sectors");
+ break;
+ case 's':
+ opt_s = argto4(optarg, 1, "file system size");
+ break;
+ case 'u':
+ opt_u = argto2(optarg, 1, "sectors/track");
+ break;
+ default:
+ usage();
+ }
argc -= optind;
argv += optind;
if (argc < 1 || argc > 2)
- usage();
+ usage();
fname = *argv++;
if (!opt_create && !strchr(fname, '/')) {
- snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
- if (!(fname = strdup(buf)))
- err(1, NULL);
+ snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
+ if (!(fname = strdup(buf)))
+ err(1, "%s", buf);
}
dtype = *argv;
+ if (opt_A) {
+ if (opt_r)
+ errx(1, "align (-A) is incompatible with -r");
+ if (opt_N)
+ errx(1, "align (-A) is incompatible with -N");
+ }
if (opt_create) {
- if (opt_N)
- errx(1, "create (-C) is incompatible with -N");
- fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
- if (fd == -1)
- errx(1, "failed to create %s", fname);
- if (ftruncate(fd, opt_create))
- errx(1, "failed to initialize %jd bytes", (intmax_t)opt_create);
+ if (opt_N)
+ errx(1, "create (-C) is incompatible with -N");
+ fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
+ if (fd == -1)
+ errx(1, "failed to create %s", fname);
+ if (ftruncate(fd, opt_create))
+ errx(1, "failed to initialize %jd bytes", (intmax_t)opt_create);
} else if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1)
- err(1, "%s", fname);
+ err(1, "%s", fname);
if (fstat(fd, &sb))
- err(1, "%s", fname);
+ err(1, "%s", fname);
if (opt_create) {
- if (!S_ISREG(sb.st_mode))
- warnx("warning, %s is not a regular file", fname);
+ if (!S_ISREG(sb.st_mode))
+ warnx("warning, %s is not a regular file", fname);
} else {
- if (!S_ISCHR(sb.st_mode))
- warnx("warning, %s is not a character device", fname);
+ if (!S_ISCHR(sb.st_mode))
+ warnx("warning, %s is not a character device", fname);
}
if (!opt_N)
- check_mounted(fname, sb.st_mode);
+ check_mounted(fname, sb.st_mode);
if (opt_ofs && opt_ofs != lseek(fd, opt_ofs, SEEK_SET))
- errx(1, "cannot seek to %jd", (intmax_t)opt_ofs);
+ errx(1, "cannot seek to %jd", (intmax_t)opt_ofs);
memset(&bpb, 0, sizeof(bpb));
if (opt_f) {
- getstdfmt(opt_f, &bpb);
- bpb.bsec = bpb.sec;
- bpb.sec = 0;
- bpb.bspf = bpb.spf;
- bpb.spf = 0;
+ getstdfmt(opt_f, &bpb);
+ bpb.bsec = bpb.sec;
+ bpb.sec = 0;
+ bpb.bspf = bpb.spf;
+ bpb.spf = 0;
}
if (opt_h)
- bpb.hds = opt_h;
+ bpb.hds = opt_h;
if (opt_u)
- bpb.spt = opt_u;
+ bpb.spt = opt_u;
if (opt_S)
- bpb.bps = opt_S;
+ bpb.bps = opt_S;
if (opt_s)
- bpb.bsec = opt_s;
+ bpb.bsec = opt_s;
if (oflag)
- bpb.hid = opt_o;
+ bpb.hid = opt_o;
if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag))) {
- off_t delta;
- getdiskinfo(fd, fname, dtype, oflag, &bpb);
+ off_t delta;
+ getdiskinfo(fd, fname, dtype, oflag, &bpb);
if (opt_s) {
bpb.bsec = opt_s;
}
- bpb.bsec -= (opt_ofs / bpb.bps);
- delta = bpb.bsec % bpb.spt;
- if (delta != 0) {
- warnx("trim %d sectors from %d to adjust to a multiple of %d",
- (int)delta, bpb.bsec, bpb.spt);
- bpb.bsec -= delta;
- }
- if (bpb.spc == 0) { /* set defaults */
- if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
- bpb.spc = 1;
- else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
- bpb.spc = 8;
- else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
- bpb.spc = 16;
- else if (bpb.bsec <= (1<<22)) /* 2G -> 16k, some versions of windows
- require a minimum of 65527 clusters */
- bpb.spc = 32;
- else
- bpb.spc = 64; /* otherwise 32k */
- }
+ bpb.bsec -= (opt_ofs / bpb.bps);
+ delta = bpb.bsec % bpb.spt;
+ if (delta != 0) {
+ warnx("trim %d sectors from %d to adjust to a multiple of %d",
+ (int)delta, bpb.bsec, bpb.spt);
+ bpb.bsec -= delta;
+ }
+ if (bpb.spc == 0) { /* set defaults */
+ if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
+ bpb.spc = 1;
+ else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
+ bpb.spc = 8;
+ else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
+ bpb.spc = 16;
+ else if (bpb.bsec <= (1<<22)) /* 2G -> 16k, some versions of windows
+ require a minimum of 65527 clusters */
+ bpb.spc = 32;
+ else
+ bpb.spc = 64; /* otherwise 32k */
+ }
}
if (!powerof2(bpb.bps))
- errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
+ errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
if (bpb.bps < MINBPS)
- errx(1, "bytes/sector (%u) is too small; minimum is %u",
- bpb.bps, MINBPS);
+ errx(1, "bytes/sector (%u) is too small; minimum is %u",
+ bpb.bps, MINBPS);
if (!(fat = opt_F)) {
- if (opt_f)
- fat = 12;
- else if (!opt_e && (opt_i || opt_k))
- fat = 32;
+ if (opt_f)
+ fat = 12;
+ else if (!opt_e && (opt_i || opt_k))
+ fat = 32;
}
if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
- errx(1, "-%c is not a legal FAT%s option",
- fat == 32 ? 'e' : opt_i ? 'i' : 'k',
- fat == 32 ? "32" : "12/16");
+ errx(1, "-%c is not a legal FAT%s option",
+ fat == 32 ? 'e' : opt_i ? 'i' : 'k',
+ fat == 32 ? "32" : "12/16");
if (opt_f && fat == 32)
- bpb.rde = 0;
+ bpb.rde = 0;
if (opt_b) {
- if (!powerof2(opt_b))
- errx(1, "block size (%u) is not a power of 2", opt_b);
- if (opt_b < bpb.bps)
- errx(1, "block size (%u) is too small; minimum is %u",
- opt_b, bpb.bps);
- if (opt_b > bpb.bps * MAXSPC)
- errx(1, "block size (%u) is too large; maximum is %u",
- opt_b, bpb.bps * MAXSPC);
- bpb.spc = opt_b / bpb.bps;
+ if (!powerof2(opt_b))
+ errx(1, "block size (%u) is not a power of 2", opt_b);
+ if (opt_b < bpb.bps)
+ errx(1, "block size (%u) is too small; minimum is %u",
+ opt_b, bpb.bps);
+ if (opt_b > bpb.bps * MAXSPC)
+ errx(1, "block size (%u) is too large; maximum is %u", opt_b, bpb.bps * MAXSPC);
+ bpb.spc = opt_b / bpb.bps;
}
if (opt_c) {
- if (!powerof2(opt_c))
- errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
- bpb.spc = opt_c;
+ if (!powerof2(opt_c))
+ errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
+ bpb.spc = opt_c;
}
if (opt_r)
- bpb.res = opt_r;
+ bpb.res = opt_r;
if (opt_n) {
- if (opt_n > MAXNFT)
- errx(1, "number of FATs (%u) is too large; maximum is %u",
- opt_n, MAXNFT);
- bpb.nft = opt_n;
+ if (opt_n > MAXNFT)
+ errx(1, "number of FATs (%u) is too large; maximum is %u", opt_n, MAXNFT);
+ bpb.nft = opt_n;
}
if (opt_e)
- bpb.rde = opt_e;
+ bpb.rde = opt_e;
if (mflag) {
- if (opt_m < 0xf0)
- errx(1, "illegal media descriptor (%#x)", opt_m);
- bpb.mid = opt_m;
+ if (opt_m < 0xf0)
+ errx(1, "illegal media descriptor (%#x)", opt_m);
+ bpb.mid = opt_m;
}
if (opt_a)
- bpb.bspf = opt_a;
+ bpb.bspf = opt_a;
if (opt_i)
- bpb.infs = opt_i;
+ bpb.infs = opt_i;
if (opt_k)
- bpb.bkbs = opt_k;
+ bpb.bkbs = opt_k;
bss = 1;
bname = NULL;
fd1 = -1;
if (opt_B) {
- bname = opt_B;
- if (!strchr(bname, '/')) {
- snprintf(buf, sizeof(buf), "/boot/%s", bname);
- if (!(bname = strdup(buf)))
- err(1, NULL);
- }
- if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
- err(1, "%s", bname);
- if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
- sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
- errx(1, "%s: inappropriate file type or format", bname);
- bss = sb.st_size / bpb.bps;
+ bname = opt_B;
+ if (!strchr(bname, '/')) {
+ snprintf(buf, sizeof(buf), "/boot/%s", bname);
+ if (!(bname = strdup(buf)))
+ err(1, "%s", buf);
+ }
+ if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
+ err(1, "%s", bname);
+ if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
+ sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
+ errx(1, "%s: inappropriate file type or format", bname);
+ bss = sb.st_size / bpb.bps;
}
if (!bpb.nft)
- bpb.nft = 2;
+ bpb.nft = 2;
if (!fat) {
- if (bpb.bsec < (bpb.res ? bpb.res : bss) +
- howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
- ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
- bpb.nft +
- howmany(bpb.rde ? bpb.rde : DEFRDE,
- bpb.bps / sizeof(struct de)) +
- (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
- (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
- fat = 12;
- else if (bpb.rde || bpb.bsec <
- (bpb.res ? bpb.res : bss) +
- howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
- howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
- (MAXCLS16 + 1) *
- (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
- fat = 16;
- else
- fat = 32;
+ if (bpb.bsec < (bpb.res ? bpb.res : bss) +
+ howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
+ ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
+ bpb.nft +
+ howmany(bpb.rde ? bpb.rde : DEFRDE,
+ bpb.bps / sizeof(struct de)) +
+ (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
+ (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
+ fat = 12;
+ else if (bpb.rde || bpb.bsec <
+ (bpb.res ? bpb.res : bss) +
+ howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
+ howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
+ (MAXCLS16 + 1) *
+ (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
+ fat = 16;
+ else
+ fat = 32;
}
x = bss;
if (fat == 32) {
- if (!bpb.infs) {
- if (x == MAXU16 || x == bpb.bkbs)
- errx(1, "no room for info sector");
- bpb.infs = x;
- }
- if (bpb.infs != MAXU16 && x <= bpb.infs)
- x = bpb.infs + 1;
- if (!bpb.bkbs) {
- if (x == MAXU16)
- errx(1, "no room for backup sector");
- bpb.bkbs = x;
- } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
- errx(1, "backup sector would overwrite info sector");
- if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
- x = bpb.bkbs + 1;
+ if (!bpb.infs) {
+ if (x == MAXU16 || x == bpb.bkbs)
+ errx(1, "no room for info sector");
+ bpb.infs = x;
+ }
+ if (bpb.infs != MAXU16 && x <= bpb.infs)
+ x = bpb.infs + 1;
+ if (!bpb.bkbs) {
+ if (x == MAXU16)
+ errx(1, "no room for backup sector");
+ bpb.bkbs = x;
+ } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
+ errx(1, "backup sector would overwrite info sector");
+ if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
+ x = bpb.bkbs + 1;
}
- if (!bpb.res)
- bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
- else if (bpb.res < x)
- errx(1, "too few reserved sectors");
- if (fat != 32 && !bpb.rde)
- bpb.rde = DEFRDE;
- rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
- if (!bpb.spc)
- for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
- bpb.spc < MAXSPC &&
- bpb.res +
- howmany((RESFTE + maxcls(fat)) * (fat / BPN),
- bpb.bps * NPB) * bpb.nft +
- rds +
- (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
- bpb.spc <<= 1);
- if (fat != 32 && bpb.bspf > MAXU16)
- errx(1, "too many sectors/FAT for FAT12/16");
- x1 = bpb.res + rds;
- x = bpb.bspf ? bpb.bspf : 1;
- if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
- errx(1, "meta data exceeds file system size");
- x1 += x * bpb.nft;
- x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
- (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
- x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
- bpb.bps * NPB);
- if (!bpb.bspf) {
- bpb.bspf = x2;
- x1 += (bpb.bspf - 1) * bpb.nft;
- }
+
+ extra_res = 0;
+ set_res = !bpb.res;
+ set_spf = !bpb.bspf;
+ set_spc = !bpb.spc;
+ tempx = x;
+ /*
+ * Attempt to align if opt_A is set. This is done by increasing the number
+ * of reserved blocks. This can cause other factors to change, which can in
+ * turn change the alignment. This should take at most 2 iterations, as
+ * increasing the reserved amount may cause the FAT size to decrease by 1,
+ * requiring another nft reserved blocks. If spc changes, it will
+ * be half of its previous size, and thus will not throw off alignment.
+ */
+ do {
+ x = tempx;
+ if (set_res)
+ bpb.res = (fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x) + extra_res;
+ else if (bpb.res < x)
+ errx(1, "too few reserved sectors");
+ if (fat != 32 && !bpb.rde)
+ bpb.rde = DEFRDE;
+ rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
+ if (set_spc)
+ for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
+ bpb.spc < MAXSPC &&
+ bpb.res +
+ howmany((RESFTE + maxcls(fat)) * (fat / BPN),
+ bpb.bps * NPB) * bpb.nft +
+ rds +
+ (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
+ bpb.spc <<= 1);
+ if (fat != 32 && bpb.bspf > MAXU16)
+ errx(1, "too many sectors/FAT for FAT12/16");
+ x1 = bpb.res + rds;
+ x = bpb.bspf ? bpb.bspf : 1;
+ if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
+ errx(1, "meta data exceeds file system size");
+ x1 += x * bpb.nft;
+ x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
+ (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
+ x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), bpb.bps * NPB);
+ if (set_spf) {
+ if (!bpb.bspf) {
+ bpb.bspf = x2;
+ }
+ x1 += (bpb.bspf - 1) * bpb.nft;
+ }
+ if(set_res) {
+ /* attempt to align root directory */
+ alignment = (bpb.res + bpb.bspf * bpb.nft) % bpb.spc;
+ extra_res += bpb.spc - alignment;
+ }
+ attempts++;
+ } while(opt_A && alignment != 0 && attempts < 2);
+ if (alignment != 0)
+ warnx("warning: Alignment failed.");
+
cls = (bpb.bsec - x1) / bpb.spc;
x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
if (cls > x)
- cls = x;
+ cls = x;
if (bpb.bspf < x2)
- warnx("warning: sectors/FAT limits file system to %u clusters",
- cls);
+ warnx("warning: sectors/FAT limits file system to %u clusters", cls);
if (cls < mincls(fat))
- errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
- mincls(fat));
+ errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, mincls(fat));
if (cls > maxcls(fat)) {
- cls = maxcls(fat);
- bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
- warnx("warning: FAT type limits file system to %u sectors",
- bpb.bsec);
+ cls = maxcls(fat);
+ bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
+ warnx("warning: FAT type limits file system to %u sectors", bpb.bsec);
}
- printf("%s: %u sector%s in %u FAT%u cluster%s "
- "(%u bytes/cluster)\n", fname, cls * bpb.spc,
- cls * bpb.spc == 1 ? "" : "s", cls, fat,
- cls == 1 ? "" : "s", bpb.bps * bpb.spc);
+ printf("%s: %u sector%s in %u FAT%u cluster%s (%u bytes/cluster)\n",
+ fname, cls * bpb.spc, cls * bpb.spc == 1 ? "" : "s", cls, fat,
+ cls == 1 ? "" : "s", bpb.bps * bpb.spc);
if (!bpb.mid)
- bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
+ bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
if (fat == 32)
- bpb.rdcl = RESFTE;
+ bpb.rdcl = RESFTE;
if (bpb.hid + bpb.bsec <= MAXU16) {
- bpb.sec = bpb.bsec;
- bpb.bsec = 0;
+ bpb.sec = bpb.bsec;
+ bpb.bsec = 0;
}
if (fat != 32) {
- bpb.spf = bpb.bspf;
- bpb.bspf = 0;
+ bpb.spf = bpb.bspf;
+ bpb.bspf = 0;
}
print_bpb(&bpb);
if (!opt_N) {
- gettimeofday(&tv, NULL);
- now = tv.tv_sec;
- tm = localtime(&now);
- if (!(img = malloc(bpb.bps)))
- err(1, NULL);
- dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
- for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
- x = lsn;
- if (opt_B &&
- fat == 32 && bpb.bkbs != MAXU16 &&
- bss <= bpb.bkbs && x >= bpb.bkbs) {
- x -= bpb.bkbs;
- if (!x && lseek(fd1, opt_ofs, SEEK_SET))
- err(1, "%s", bname);
- }
- if (opt_B && x < bss) {
- if ((n = read(fd1, img, bpb.bps)) == -1)
- err(1, "%s", bname);
- if ((unsigned)n != bpb.bps)
- errx(1, "%s: can't read sector %u", bname, x);
- } else
- memset(img, 0, bpb.bps);
- if (!lsn ||
- (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
- x1 = sizeof(struct bs);
- bsbpb = (struct bsbpb *)(img + x1);
- mk2(bsbpb->bps, bpb.bps);
- mk1(bsbpb->spc, bpb.spc);
- mk2(bsbpb->res, bpb.res);
- mk1(bsbpb->nft, bpb.nft);
- mk2(bsbpb->rde, bpb.rde);
- mk2(bsbpb->sec, bpb.sec);
- mk1(bsbpb->mid, bpb.mid);
- mk2(bsbpb->spf, bpb.spf);
- mk2(bsbpb->spt, bpb.spt);
- mk2(bsbpb->hds, bpb.hds);
- mk4(bsbpb->hid, bpb.hid);
- mk4(bsbpb->bsec, bpb.bsec);
- x1 += sizeof(struct bsbpb);
- if (fat == 32) {
- bsxbpb = (struct bsxbpb *)(img + x1);
- mk4(bsxbpb->bspf, bpb.bspf);
- mk2(bsxbpb->xflg, 0);
- mk2(bsxbpb->vers, 0);
- mk4(bsxbpb->rdcl, bpb.rdcl);
- mk2(bsxbpb->infs, bpb.infs);
- mk2(bsxbpb->bkbs, bpb.bkbs);
- x1 += sizeof(struct bsxbpb);
- }
- bsx = (struct bsx *)(img + x1);
- mk1(bsx->sig, 0x29);
- if (Iflag)
- x = opt_I;
- else
- x = (((u_int)(1 + tm->tm_mon) << 8 |
- (u_int)tm->tm_mday) +
- ((u_int)tm->tm_sec << 8 |
- (u_int)(tv.tv_usec / 10))) << 16 |
- ((u_int)(1900 + tm->tm_year) +
- ((u_int)tm->tm_hour << 8 |
- (u_int)tm->tm_min));
- mk4(bsx->volid, x);
- mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
- sprintf(buf, "FAT%u", fat);
- setstr(bsx->type, buf, sizeof(bsx->type));
- if (!opt_B) {
- x1 += sizeof(struct bsx);
- bs = (struct bs *)img;
- mk1(bs->jmp[0], 0xeb);
- mk1(bs->jmp[1], x1 - 2);
- mk1(bs->jmp[2], 0x90);
- setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
- sizeof(bs->oem));
- memcpy(img + x1, bootcode, sizeof(bootcode));
- mk2(img + MINBPS - 2, DOSMAGIC);
- }
- } else if (fat == 32 && bpb.infs != MAXU16 &&
- (lsn == bpb.infs ||
- (bpb.bkbs != MAXU16 &&
- lsn == bpb.bkbs + bpb.infs))) {
- mk4(img, 0x41615252);
- mk4(img + MINBPS - 28, 0x61417272);
- mk4(img + MINBPS - 24, 0xffffffff);
- mk4(img + MINBPS - 20, bpb.rdcl);
- mk2(img + MINBPS - 2, DOSMAGIC);
- } else if (lsn >= bpb.res && lsn < dir &&
- !((lsn - bpb.res) %
- (bpb.spf ? bpb.spf : bpb.bspf))) {
- mk1(img[0], bpb.mid);
- for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
- mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
- } else if (lsn == dir && opt_L) {
- de = (struct de *)img;
- mklabel(de->namext, opt_L);
- mk1(de->attr, 050);
- x = (u_int)tm->tm_hour << 11 |
- (u_int)tm->tm_min << 5 |
- (u_int)tm->tm_sec >> 1;
- mk2(de->time, x);
- x = (u_int)(tm->tm_year - 80) << 9 |
- (u_int)(tm->tm_mon + 1) << 5 |
- (u_int)tm->tm_mday;
- mk2(de->date, x);
- }
- if ((n = write(fd, img, bpb.bps)) == -1)
- err(1, "%s", fname);
- if ((unsigned)n != bpb.bps) {
- errx(1, "%s: can't write sector %u", fname, lsn);
+ gettimeofday(&tv, NULL);
+ now = tv.tv_sec;
+ tm = localtime(&now);
+ if (!(img = malloc(bpb.bps)))
+ err(1, "%u", bpb.bps);
+ dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
+ for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
+ x = lsn;
+ if (opt_B && fat == 32 && bpb.bkbs != MAXU16 && bss <= bpb.bkbs && x >= bpb.bkbs) {
+ x -= bpb.bkbs;
+ if (!x && lseek(fd1, opt_ofs, SEEK_SET))
+ err(1, "%s", bname);
+ }
+ if (opt_B && x < bss) {
+ if ((n = read(fd1, img, bpb.bps)) == -1)
+ err(1, "%s", bname);
+ if ((unsigned)n != bpb.bps)
+ errx(1, "%s: can't read sector %u", bname, x);
+ } else
+ memset(img, 0, bpb.bps);
+ if (!lsn || (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
+ x1 = sizeof(struct bs);
+ bsbpb = (struct bsbpb *)(img + x1);
+ mk2(bsbpb->bps, bpb.bps);
+ mk1(bsbpb->spc, bpb.spc);
+ mk2(bsbpb->res, bpb.res);
+ mk1(bsbpb->nft, bpb.nft);
+ mk2(bsbpb->rde, bpb.rde);
+ mk2(bsbpb->sec, bpb.sec);
+ mk1(bsbpb->mid, bpb.mid);
+ mk2(bsbpb->spf, bpb.spf);
+ mk2(bsbpb->spt, bpb.spt);
+ mk2(bsbpb->hds, bpb.hds);
+ mk4(bsbpb->hid, bpb.hid);
+ mk4(bsbpb->bsec, bpb.bsec);
+ x1 += sizeof(struct bsbpb);
+ if (fat == 32) {
+ bsxbpb = (struct bsxbpb *)(img + x1);
+ mk4(bsxbpb->bspf, bpb.bspf);
+ mk2(bsxbpb->xflg, 0);
+ mk2(bsxbpb->vers, 0);
+ mk4(bsxbpb->rdcl, bpb.rdcl);
+ mk2(bsxbpb->infs, bpb.infs);
+ mk2(bsxbpb->bkbs, bpb.bkbs);
+ x1 += sizeof(struct bsxbpb);
+ }
+ bsx = (struct bsx *)(img + x1);
+ mk1(bsx->sig, 0x29);
+ if (Iflag)
+ x = opt_I;
+ else
+ x = (((u_int)(1 + tm->tm_mon) << 8 |
+ (u_int)tm->tm_mday) +
+ ((u_int)tm->tm_sec << 8 |
+ (u_int)(tv.tv_usec / 10))) << 16 |
+ ((u_int)(1900 + tm->tm_year) +
+ ((u_int)tm->tm_hour << 8 |
+ (u_int)tm->tm_min));
+ mk4(bsx->volid, x);
+ mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
+ sprintf(buf, "FAT%u", fat);
+ setstr(bsx->type, buf, sizeof(bsx->type));
+ if (!opt_B) {
+ x1 += sizeof(struct bsx);
+ bs = (struct bs *)img;
+ mk1(bs->jmp[0], 0xeb);
+ mk1(bs->jmp[1], x1 - 2);
+ mk1(bs->jmp[2], 0x90);
+ setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
+ sizeof(bs->oem));
+ memcpy(img + x1, bootcode, sizeof(bootcode));
+ mk2(img + MINBPS - 2, DOSMAGIC);
+ }
+ } else if (fat == 32 && bpb.infs != MAXU16 &&
+ (lsn == bpb.infs || (bpb.bkbs != MAXU16 &&
+ lsn == bpb.bkbs + bpb.infs))) {
+ mk4(img, 0x41615252);
+ mk4(img + MINBPS - 28, 0x61417272);
+ mk4(img + MINBPS - 24, 0xffffffff);
+ mk4(img + MINBPS - 20, bpb.rdcl);
+ mk2(img + MINBPS - 2, DOSMAGIC);
+ } else if (lsn >= bpb.res && lsn < dir &&
+ !((lsn - bpb.res) % (bpb.spf ? bpb.spf : bpb.bspf))) {
+ mk1(img[0], bpb.mid);
+ for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
+ mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
+ } else if (lsn == dir && opt_L) {
+ de = (struct de *)img;
+ mklabel(de->namext, opt_L);
+ mk1(de->attr, 050);
+ x = (u_int)tm->tm_hour << 11 |
+ (u_int)tm->tm_min << 5 |
+ (u_int)tm->tm_sec >> 1;
+ mk2(de->time, x);
+ x = (u_int)(tm->tm_year - 80) << 9 |
+ (u_int)(tm->tm_mon + 1) << 5 |
+ (u_int)tm->tm_mday;
+ mk2(de->date, x);
+ }
+ if ((n = write(fd, img, bpb.bps)) == -1)
+ err(1, "%s", fname);
+ if ((unsigned)n != bpb.bps) {
+ errx(1, "%s: can't write sector %u", fname, lsn);
exit(1);
}
- }
+ }
}
return 0;
}
@@ -725,31 +748,29 @@
/*
* Exit with error if file system is mounted.
*/
-static void
-check_mounted(const char *fname, mode_t mode)
+static void check_mounted(const char *fname, mode_t mode)
{
+#ifdef ANDROID
+ warnx("Skipping mount checks");
+#else
struct statfs *mp;
const char *s1, *s2;
size_t len;
int n, r;
-#ifdef ANDROID
- warnx("Skipping mount checks");
-#else
if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
- err(1, "getmntinfo");
+ err(1, "getmntinfo");
len = strlen(_PATH_DEV);
s1 = fname;
if (!strncmp(s1, _PATH_DEV, len))
- s1 += len;
+ s1 += len;
r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
for (; n--; mp++) {
- s2 = mp->f_mntfromname;
- if (!strncmp(s2, _PATH_DEV, len))
- s2 += len;
- if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
- !strcmp(s1, s2))
- errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
+ s2 = mp->f_mntfromname;
+ if (!strncmp(s2, _PATH_DEV, len))
+ s2 += len;
+ if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || !strcmp(s1, s2))
+ errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
}
#endif
}
@@ -757,15 +778,14 @@
/*
* Get a standard format.
*/
-static void
-getstdfmt(const char *fmt, struct bpb *bpb)
+static void getstdfmt(const char *fmt, struct bpb *bpb)
{
u_int x, i;
x = sizeof(stdfmt) / sizeof(stdfmt[0]);
for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
if (i == x)
- errx(1, "%s: unknown standard format", fmt);
+ errx(1, "%s: unknown standard format", fmt);
*bpb = stdfmt[i].bpb;
}
@@ -774,9 +794,8 @@
*/
#ifdef ANDROID
-static void
-getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
- struct bpb *bpb)
+static void getdiskinfo(int fd, const char *fname, const char *dtype,
+ __unused int oflag,struct bpb *bpb)
{
struct hd_geometry geom;
@@ -817,9 +836,8 @@
#else
-static void
-getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
- struct bpb *bpb)
+static void getdiskinfo(int fd, const char *fname, const char *dtype,
+ __unused int oflag, struct bpb *bpb)
{
struct disklabel *lp, dlp;
struct fd_type type;
@@ -829,97 +847,96 @@
/* If the user specified a disk type, try to use that */
if (dtype != NULL) {
- lp = getdiskbyname(dtype);
+ lp = getdiskbyname(dtype);
}
/* Maybe it's a floppy drive */
if (lp == NULL) {
- if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
- struct stat st;
+ if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
+ struct stat st;
- if (fstat(fd, &st))
- err(1, "Cannot get disk size");
- /* create a fake geometry for a file image */
- ms = st.st_size;
- dlp.d_secsize = 512;
- dlp.d_nsectors = 63;
- dlp.d_ntracks = 255;
- dlp.d_secperunit = ms / dlp.d_secsize;
- lp = &dlp;
- } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
- dlp.d_secsize = 128 << type.secsize;
- dlp.d_nsectors = type.sectrac;
- dlp.d_ntracks = type.heads;
- dlp.d_secperunit = ms / dlp.d_secsize;
- lp = &dlp;
- }
+ if (fstat(fd, &st))
+ err(1, "Cannot get disk size");
+ /* create a fake geometry for a file image */
+ ms = st.st_size;
+ dlp.d_secsize = 512;
+ dlp.d_nsectors = 63;
+ dlp.d_ntracks = 255;
+ dlp.d_secperunit = ms / dlp.d_secsize;
+ lp = &dlp;
+ } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
+ dlp.d_secsize = 128 << type.secsize;
+ dlp.d_nsectors = type.sectrac;
+ dlp.d_ntracks = type.heads;
+ dlp.d_secperunit = ms / dlp.d_secsize;
+ lp = &dlp;
+ }
}
/* Maybe it's a fixed drive */
if (lp == NULL) {
- if (ioctl(fd, DIOCGDINFO, &dlp) == -1) {
- if (bpb->bps == 0 && ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1)
- errx(1, "Cannot get sector size, %s", strerror(errno));
+ if (ioctl(fd, DIOCGDINFO, &dlp) == -1) {
+ if (bpb->bps == 0 && ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1)
+ errx(1, "Cannot get sector size, %s", strerror(errno));
- /* XXX Should we use bpb->bps if it's set? */
- dlp.d_secperunit = ms / dlp.d_secsize;
+ /* XXX Should we use bpb->bps if it's set? */
+ dlp.d_secperunit = ms / dlp.d_secsize;
- if (bpb->spt == 0 && ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1) {
- warnx("Cannot get number of sectors per track, %s", strerror(errno));
- dlp.d_nsectors = 63;
- }
- if (bpb->hds == 0 && ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
- warnx("Cannot get number of heads, %s", strerror(errno));
- if (dlp.d_secperunit <= 63*1*1024)
- dlp.d_ntracks = 1;
- else if (dlp.d_secperunit <= 63*16*1024)
- dlp.d_ntracks = 16;
- else
- dlp.d_ntracks = 255;
- }
- }
+ if (bpb->spt == 0 && ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1) {
+ warnx("Cannot get number of sectors per track, %s", strerror(errno));
+ dlp.d_nsectors = 63;
+ }
+ if (bpb->hds == 0 && ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
+ warnx("Cannot get number of heads, %s", strerror(errno));
+ if (dlp.d_secperunit <= 63*1*1024)
+ dlp.d_ntracks = 1;
+ else if (dlp.d_secperunit <= 63*16*1024)
+ dlp.d_ntracks = 16;
+ else
+ dlp.d_ntracks = 255;
+ }
+ }
- hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
- lp = &dlp;
+ hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
+ lp = &dlp;
}
if (bpb->bps == 0)
- bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
+ bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
if (bpb->spt == 0)
- bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
+ bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
if (bpb->hds == 0)
- bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
+ bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
if (bpb->bsec == 0)
- bpb->bsec = lp->d_secperunit;
+ bpb->bsec = lp->d_secperunit;
if (bpb->hid == 0)
- bpb->hid = hs;
+ bpb->hid = hs;
}
#endif
/*
* Print out BPB values.
*/
-static void
-print_bpb(struct bpb *bpb)
+static void print_bpb(struct bpb *bpb)
{
printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
- bpb->nft);
+ bpb->nft);
if (bpb->rde)
- printf(" rde=%u", bpb->rde);
+ printf(" rde=%u", bpb->rde);
if (bpb->sec)
- printf(" sec=%u", bpb->sec);
+ printf(" sec=%u", bpb->sec);
printf(" mid=%#x", bpb->mid);
if (bpb->spf)
- printf(" spf=%u", bpb->spf);
+ printf(" spf=%u", bpb->spf);
printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
if (bpb->bsec)
- printf(" bsec=%u", bpb->bsec);
+ printf(" bsec=%u", bpb->bsec);
if (!bpb->spf) {
- printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
- printf(" infs=");
- printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
- printf(" bkbs=");
- printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
+ printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
+ printf(" infs=");
+ printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
+ printf(" bkbs=");
+ printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
}
printf("\n");
}
@@ -927,21 +944,19 @@
/*
* Check a disk geometry value.
*/
-static u_int
-ckgeom(const char *fname, u_int val, const char *msg)
+static u_int ckgeom(const char *fname, u_int val, const char *msg)
{
if (!val)
- errx(1, "%s: no default %s", fname, msg);
+ errx(1, "%s: no default %s", fname, msg);
if (val > MAXU16)
- errx(1, "%s: illegal %s %d", fname, msg, val);
+ errx(1, "%s: illegal %s %d", fname, msg, val);
return val;
}
/*
* Convert and check a numeric option argument.
*/
-static u_int
-argtou(const char *arg, u_int lo, u_int hi, const char *msg)
+static u_int argtou(const char *arg, u_int lo, u_int hi, const char *msg)
{
char *s;
u_long x;
@@ -949,15 +964,14 @@
errno = 0;
x = strtoul(arg, &s, 0);
if (errno || !*arg || *s || x < lo || x > hi)
- errx(1, "%s: bad %s", arg, msg);
+ errx(1, "%s: bad %s", arg, msg);
return x;
}
/*
* Same for off_t, with optional skmgpP suffix
*/
-static off_t
-argtooff(const char *arg, const char *msg)
+static off_t argtooff(const char *arg, const char *msg)
{
char *s;
off_t x;
@@ -965,40 +979,40 @@
x = strtoll(arg, &s, 0);
/* allow at most one extra char */
if (errno || x < 0 || (s[0] && s[1]) )
- errx(1, "%s: bad %s", arg, msg);
- if (*s) { /* the extra char is the multiplier */
- switch (*s) {
- default:
- errx(1, "%s: bad %s", arg, msg);
- /* notreached */
-
- case 's': /* sector */
- case 'S':
- x <<= 9; /* times 512 */
- break;
+ errx(1, "%s: bad %s", arg, msg);
+ if (*s) { /* the extra char is the multiplier */
+ switch (*s) {
+ default:
+ errx(1, "%s: bad %s", arg, msg);
+ /* notreached */
- case 'k': /* kilobyte */
- case 'K':
- x <<= 10; /* times 1024 */
- break;
+ case 's': /* sector */
+ case 'S':
+ x <<= 9; /* times 512 */
+ break;
- case 'm': /* megabyte */
- case 'M':
- x <<= 20; /* times 1024*1024 */
- break;
+ case 'k': /* kilobyte */
+ case 'K':
+ x <<= 10; /* times 1024 */
+ break;
- case 'g': /* gigabyte */
- case 'G':
- x <<= 30; /* times 1024*1024*1024 */
- break;
+ case 'm': /* megabyte */
+ case 'M':
+ x <<= 20; /* times 1024*1024 */
+ break;
- case 'p': /* partition start */
- case 'P': /* partition start */
- case 'l': /* partition length */
- case 'L': /* partition length */
- errx(1, "%s: not supported yet %s", arg, msg);
- /* notreached */
- }
+ case 'g': /* gigabyte */
+ case 'G':
+ x <<= 30; /* times 1024*1024*1024 */
+ break;
+
+ case 'p': /* partition start */
+ case 'P': /* partition start */
+ case 'l': /* partition length */
+ case 'L': /* partition length */
+ errx(1, "%s: not supported yet %s", arg, msg);
+ /* notreached */
+ }
}
return x;
}
@@ -1006,15 +1020,14 @@
/*
* Check a volume label.
*/
-static int
-oklabel(const char *src)
+static int oklabel(const char *src)
{
int c, i;
for (i = 0; i <= 11; i++) {
- c = (u_char)*src++;
- if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
- break;
+ c = (u_char)*src++;
+ if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
+ break;
}
return i && !c;
}
@@ -1022,58 +1035,56 @@
/*
* Make a volume label.
*/
-static void
-mklabel(u_int8_t *dest, const char *src)
+static void mklabel(u_int8_t *dest, const char *src)
{
int c, i;
for (i = 0; i < 11; i++) {
- c = *src ? toupper(*src++) : ' ';
- *dest++ = !i && c == '\xe5' ? 5 : c;
+ c = *src ? toupper(*src++) : ' ';
+ *dest++ = !i && c == '\xe5' ? 5 : c;
}
}
/*
* Copy string, padding with spaces.
*/
-static void
-setstr(u_int8_t *dest, const char *src, size_t len)
+static void setstr(u_int8_t *dest, const char *src, size_t len)
{
while (len--)
- *dest++ = *src ? *src++ : ' ';
+ *dest++ = *src ? *src++ : ' ';
}
/*
* Print usage message.
*/
-static void
-usage(void)
+static void usage(void)
{
- fprintf(stderr,
- "usage: newfs_msdos [ -options ] special [disktype]\n"
- "where the options are:\n"
- "\t-@ create file system at specified offset\n"
- "\t-B get bootstrap from file\n"
- "\t-C create image file with specified size\n"
- "\t-F FAT type (12, 16, or 32)\n"
- "\t-I volume ID\n"
- "\t-L volume label\n"
- "\t-N don't create file system: just print out parameters\n"
- "\t-O OEM string\n"
- "\t-S bytes/sector\n"
- "\t-a sectors/FAT\n"
- "\t-b block size\n"
- "\t-c sectors/cluster\n"
- "\t-e root directory entries\n"
- "\t-f standard format\n"
- "\t-h drive heads\n"
- "\t-i file system info sector\n"
- "\t-k backup boot sector\n"
- "\t-m media descriptor\n"
- "\t-n number of FATs\n"
- "\t-o hidden sectors\n"
- "\t-r reserved sectors\n"
- "\t-s file system size (sectors)\n"
- "\t-u sectors/track\n");
- exit(1);
+ fprintf(stderr,
+ "usage: newfs_msdos [ -options ] special [disktype]\n"
+ "where the options are:\n"
+ "\t-@ create file system at specified offset\n"
+ "\t-A Attempt to cluster align root directory\n"
+ "\t-B get bootstrap from file\n"
+ "\t-C create image file with specified size\n"
+ "\t-F FAT type (12, 16, or 32)\n"
+ "\t-I volume ID\n"
+ "\t-L volume label\n"
+ "\t-N don't create file system: just print out parameters\n"
+ "\t-O OEM string\n"
+ "\t-S bytes/sector\n"
+ "\t-a sectors/FAT\n"
+ "\t-b block size\n"
+ "\t-c sectors/cluster\n"
+ "\t-e root directory entries\n"
+ "\t-f standard format\n"
+ "\t-h drive heads\n"
+ "\t-i file system info sector\n"
+ "\t-k backup boot sector\n"
+ "\t-m media descriptor\n"
+ "\t-n number of FATs\n"
+ "\t-o hidden sectors\n"
+ "\t-r reserved sectors\n"
+ "\t-s file system size (sectors)\n"
+ "\t-u sectors/track\n");
+ exit(1);
}
diff --git a/toolbox/nohup.c b/toolbox/nohup.c
deleted file mode 100644
index 363999d..0000000
--- a/toolbox/nohup.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-int nohup_main(int argc, char *argv[])
-{
- if (argc < 2) {
- fprintf(stderr, "Usage: %s [-n] program args...\n", argv[0]);
- return EXIT_FAILURE;
- }
- signal(SIGHUP, SIG_IGN);
- argv++;
- if (strcmp(argv[0], "-n") == 0) {
- argv++;
- signal(SIGINT, SIG_IGN);
- signal(SIGSTOP, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGTTOU, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGTERM, SIG_IGN);
- }
- execvp(argv[0], argv);
- perror(argv[0]);
- return EXIT_FAILURE;
-}
diff --git a/toolbox/notify.c b/toolbox/notify.c
deleted file mode 100644
index c983ed5..0000000
--- a/toolbox/notify.c
+++ /dev/null
@@ -1,145 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/inotify.h>
-#include <errno.h>
-
-int notify_main(int argc, char *argv[])
-{
- int c;
- int nfd, ffd;
- int res;
- char event_buf[512];
- struct inotify_event *event;
- int event_mask = IN_ALL_EVENTS;
- int event_count = 1;
- int print_files = 0;
- int verbose = 2;
- int width = 80;
- char **file_names;
- int file_count;
- int id_offset = 0;
- int i;
- char *buf;
-
- do {
- c = getopt(argc, argv, "m:c:pv:w:");
- if (c == EOF)
- break;
- switch (c) {
- case 'm':
- event_mask = strtol(optarg, NULL, 0);
- break;
- case 'c':
- event_count = atoi(optarg);
- break;
- case 'p':
- print_files = 1;
- break;
- case 'v':
- verbose = atoi(optarg);
- break;
- case 'w':
- width = atoi(optarg);
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
-
- if (argc <= optind) {
- fprintf(stderr, "Usage: %s [-m eventmask] [-c count] [-p] [-v verbosity] path [path ...]\n", argv[0]);
- return 1;
- }
-
- nfd = inotify_init();
- if(nfd < 0) {
- fprintf(stderr, "inotify_init failed, %s\n", strerror(errno));
- return 1;
- }
- file_names = argv + optind;
- file_count = argc - optind;
- for(i = 0; i < file_count; i++) {
- res = inotify_add_watch(nfd, file_names[i], event_mask);
- if(res < 0) {
- fprintf(stderr, "inotify_add_watch failed for %s, %s\n", file_names[i], strerror(errno));
- return 1;
- }
- if(i == 0)
- id_offset = -res;
- if(res + id_offset != i) {
- fprintf(stderr, "%s got unexpected id %d instead of %d\n", file_names[i], res, i);
- return 1;
- }
- }
-
- buf = malloc(width + 2);
-
- while(1) {
- int event_pos = 0;
- res = read(nfd, event_buf, sizeof(event_buf));
- if(res < (int)sizeof(*event)) {
- if(errno == EINTR)
- continue;
- fprintf(stderr, "could not get event, %s\n", strerror(errno));
- return 1;
- }
- //printf("got %d bytes of event information\n", res);
- while(res >= (int)sizeof(*event)) {
- int event_size;
- event = (struct inotify_event *)(event_buf + event_pos);
- if(verbose >= 2)
- printf("%s: %08x %08x \"%s\"\n", file_names[event->wd + id_offset], event->mask, event->cookie, event->len ? event->name : "");
- else if(verbose >= 2)
- printf("%s: %08x \"%s\"\n", file_names[event->wd + id_offset], event->mask, event->len ? event->name : "");
- else if(verbose >= 1)
- printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
- if(print_files && (event->mask & IN_MODIFY)) {
- char filename[512];
- ssize_t read_len;
- char *display_name;
- int buflen;
- strcpy(filename, file_names[event->wd + id_offset]);
- if(event->len) {
- strcat(filename, "/");
- strcat(filename, event->name);
- }
- ffd = open(filename, O_RDONLY);
- display_name = (verbose >= 2 || event->len == 0) ? filename : event->name;
- buflen = width - strlen(display_name);
- read_len = read(ffd, buf, buflen);
- if(read_len > 0) {
- if(read_len < buflen && buf[read_len-1] != '\n') {
- buf[read_len] = '\n';
- read_len++;
- }
- if(read_len == buflen) {
- buf[--read_len] = '\0';
- buf[--read_len] = '\n';
- buf[--read_len] = '.';
- buf[--read_len] = '.';
- buf[--read_len] = '.';
- }
- else {
- buf[read_len] = '\0';
- }
- printf("%s: %s", display_name, buf);
- }
- close(ffd);
- }
- if(event_count && --event_count == 0)
- return 0;
- event_size = sizeof(*event) + event->len;
- res -= event_size;
- event_pos += event_size;
- }
- }
-
- return 0;
-}
diff --git a/toolbox/printenv.c b/toolbox/printenv.c
deleted file mode 100644
index d5ea531..0000000
--- a/toolbox/printenv.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-extern char** environ;
-
-int printenv_main (int argc, char **argv)
-{
- char** e;
- char* v;
- int i;
-
- if (argc == 1) {
- e = environ;
- while (*e) {
- printf("%s\n", *e);
- e++;
- }
- } else {
- for (i=1; i<argc; i++) {
- v = getenv(argv[i]);
- if (v) {
- printf("%s\n", v);
- }
- }
- }
-
- return 0;
-}
-
diff --git a/toolbox/sleep.c b/toolbox/prlimit.c
similarity index 65%
rename from toolbox/sleep.c
rename to toolbox/prlimit.c
index c09ae03..8cf202a 100644
--- a/toolbox/sleep.c
+++ b/toolbox/prlimit.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, The Android Open Source Project
+ * Copyright (c) 2014, The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -9,7 +9,7 @@
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
+ * the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
@@ -22,7 +22,7 @@
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
@@ -32,33 +32,45 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
+#include <sys/time.h>
+#include <sys/resource.h>
static void
usage(const char *s)
{
- fprintf(stderr, "USAGE: %s SECONDS\n", s);
- exit(-1);
+ fprintf(stderr, "usage: %s pid resource cur max\n", s);
+ exit(EXIT_FAILURE);
}
-int sleep_main(int argc, char *argv[])
+int prlimit_main(int argc, char *argv[])
{
- unsigned long seconds;
- char *endptr;
+ pid_t pid;
+ struct rlimit64 rl;
+ int resource;
+ int rc;
- if (argc != 2) {
- usage(argv[0]);
+ if (argc != 5)
+ usage(*argv);
+
+ if (sscanf(argv[1], "%d", &pid) != 1)
+ usage(*argv);
+
+ if (sscanf(argv[2], "%d", &resource) != 1)
+ usage(*argv);
+
+ if (sscanf(argv[3], "%llu", &rl.rlim_cur) != 1)
+ usage(*argv);
+
+ if (sscanf(argv[4], "%llu", &rl.rlim_max) != 1)
+ usage(*argv);
+
+ printf("setting resource %d of pid %d to [%llu,%llu]\n", resource, pid,
+ rl.rlim_cur, rl.rlim_max);
+ rc = prlimit64(pid, resource, &rl, NULL);
+ if (rc < 0) {
+ perror("prlimit");
+ exit(EXIT_FAILURE);
}
- seconds = strtoul(argv[1], &endptr, 10);
-
- if (endptr == argv[1]) {
- usage(argv[0]);
- }
-
-
- sleep((unsigned int)seconds);
-
return 0;
}
-
-
diff --git a/toolbox/ps.c b/toolbox/ps.c
index de141fc..cf3f05a 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -1,15 +1,14 @@
+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
-#include <ctype.h>
-#include <fcntl.h>
-
#include <string.h>
-
#include <sys/stat.h>
#include <sys/types.h>
-#include <dirent.h>
-
-#include <pwd.h>
+#include <unistd.h>
#include <cutils/sched_policy.h>
@@ -29,8 +28,18 @@
#define SHOW_CPU 8
#define SHOW_MACLABEL 16
#define SHOW_NUMERIC_UID 32
+#define SHOW_ABI 64
+
+#if __LP64__
+#define PC_WIDTH 10 /* Realistically, the top bits will be 0, so don't waste space. */
+#else
+#define PC_WIDTH (2*sizeof(uintptr_t))
+#endif
static int display_flags = 0;
+static int ppid_filter = 0;
+
+static void print_exe_abi(int pid);
static int ps_line(int pid, int tid, char *namefilter)
{
@@ -41,8 +50,9 @@
struct stat stats;
int fd, r;
char *ptr, *name, *state;
- int ppid, tty;
- unsigned wchan, rss, vss, eip;
+ int ppid;
+ unsigned rss, vss;
+ uintptr_t eip;
unsigned utime, stime;
int prio, nice, rtprio, sched, psr;
struct passwd *pw;
@@ -89,8 +99,7 @@
ppid = atoi(nexttok(&ptr));
nexttok(&ptr); // pgrp
nexttok(&ptr); // sid
- tty = atoi(nexttok(&ptr));
-
+ nexttok(&ptr); // tty
nexttok(&ptr); // tpgid
nexttok(&ptr); // flags
nexttok(&ptr); // minflt
@@ -123,7 +132,7 @@
nexttok(&ptr); // blocked
nexttok(&ptr); // sigignore
nexttok(&ptr); // sigcatch
- wchan = strtoul(nexttok(&ptr), 0, 10); // wchan
+ nexttok(&ptr); // wchan
nexttok(&ptr); // nswap
nexttok(&ptr); // cnswap
nexttok(&ptr); // exit signal
@@ -131,7 +140,7 @@
rtprio = atoi(nexttok(&ptr)); // rt_priority
sched = atoi(nexttok(&ptr)); // scheduling policy
- tty = atoi(nexttok(&ptr));
+ nexttok(&ptr); // tty
if(tid != 0) {
ppid = pid;
@@ -145,7 +154,11 @@
strcpy(user,pw->pw_name);
}
- if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
+ if(ppid_filter != 0 && ppid != ppid_filter) {
+ return 0;
+ }
+
+ if(!namefilter || !strncmp(cmdline[0] ? cmdline : name, namefilter, strlen(namefilter))) {
if (display_flags & SHOW_MACLABEL) {
fd = open(macline, O_RDONLY);
strcpy(macline, "-");
@@ -171,7 +184,20 @@
else
printf(" %.2s ", get_sched_policy_name(p));
}
- printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);
+ char path[PATH_MAX];
+ snprintf(path, sizeof(path), "/proc/%d/wchan", pid);
+ char wchan[10];
+ int fd = open(path, O_RDONLY);
+ ssize_t wchan_len = read(fd, wchan, sizeof(wchan));
+ if (wchan_len == -1) {
+ wchan[wchan_len = 0] = '\0';
+ }
+ close(fd);
+ printf(" %10.*s %0*" PRIxPTR " %s ", (int) wchan_len, wchan, (int) PC_WIDTH, eip, state);
+ if (display_flags & SHOW_ABI) {
+ print_exe_abi(pid);
+ }
+ printf("%s", cmdline[0] ? cmdline : name);
if(display_flags&SHOW_TIME)
printf(" (u:%d, s:%d)", utime, stime);
@@ -180,6 +206,39 @@
return 0;
}
+static void print_exe_abi(int pid)
+{
+ int fd, r;
+ char exeline[1024];
+
+ sprintf(exeline, "/proc/%d/exe", pid);
+ fd = open(exeline, O_RDONLY);
+ if(fd == 0) {
+ printf(" ");
+ return;
+ }
+ r = read(fd, exeline, 5 /* 4 byte ELFMAG + 1 byte EI_CLASS */);
+ close(fd);
+ if(r < 0) {
+ printf(" ");
+ return;
+ }
+ if (memcmp("\177ELF", exeline, 4) != 0) {
+ printf("?? ");
+ return;
+ }
+ switch (exeline[4]) {
+ case 1:
+ printf("32 ");
+ return;
+ case 2:
+ printf("64 ");
+ return;
+ default:
+ printf("?? ");
+ return;
+ }
+}
void ps_threads(int pid, char *namefilter)
{
@@ -227,7 +286,13 @@
display_flags |= SHOW_PRIO;
} else if(!strcmp(argv[1],"-c")) {
display_flags |= SHOW_CPU;
- } else if(isdigit(argv[1][0])){
+ } else if(!strcmp(argv[1],"--abi")) {
+ display_flags |= SHOW_ABI;
+ } else if(!strcmp(argv[1],"--ppid")) {
+ ppid_filter = atoi(argv[2]);
+ argc--;
+ argv++;
+ } else if(isdigit(argv[1][0])){
pidfilter = atoi(argv[1]);
} else {
namefilter = argv[1];
@@ -237,12 +302,14 @@
}
if (display_flags & SHOW_MACLABEL) {
- printf("LABEL USER PID PPID NAME\n");
+ printf("LABEL USER PID PPID NAME\n");
} else {
- printf("USER PID PPID VSIZE RSS %s%s %s WCHAN PC NAME\n",
+ printf("USER PID PPID VSIZE RSS %s%s %sWCHAN %*s %sNAME\n",
(display_flags&SHOW_CPU)?"CPU ":"",
(display_flags&SHOW_PRIO)?"PRIO NICE RTPRI SCHED ":"",
- (display_flags&SHOW_POLICY)?"PCY " : "");
+ (display_flags&SHOW_POLICY)?"PCY " : "",
+ (int) PC_WIDTH, "PC",
+ (display_flags&SHOW_ABI)?"ABI " : "");
}
while((de = readdir(d)) != 0){
if(isdigit(de->d_name[0])){
diff --git a/toolbox/r.c b/toolbox/r.c
index 3b80db7..b96cdb2 100644
--- a/toolbox/r.c
+++ b/toolbox/r.c
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
+#include <unistd.h>
#if __LP64__
#define strtoptr strtoull
@@ -18,7 +19,7 @@
return -1;
}
-int r_main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
if(argc < 2) return usage();
diff --git a/toolbox/readlink.c b/toolbox/readlink.c
deleted file mode 100644
index d114e20..0000000
--- a/toolbox/readlink.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2013, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-static int skip_newline, quiet_errors, canonicalize;
-
-static void usage(char* name) {
- fprintf(stderr, "Usage: %s [OPTION]... FILE\n", name);
-}
-
-int readlink_main(int argc, char* argv[]) {
- int c;
- while ((c = getopt(argc, argv, "nfqs")) != -1) {
- switch (c) {
- case 'n':
- skip_newline = 1;
- break;
- case 'f':
- canonicalize = 1;
- break;
- case 'q':
- case 's':
- quiet_errors = 1;
- break;
- case '?':
- default:
- usage(argv[0]);
- return EXIT_FAILURE;
- }
- }
- int index = optind;
- if (argc - index != 1) {
- usage(argv[0]);
- return EXIT_FAILURE;
- }
-
- char name[PATH_MAX+1];
- if (canonicalize) {
- if(!realpath(argv[optind], name)) {
- if (!quiet_errors) {
- perror("readlink");
- }
- return EXIT_FAILURE;
- }
- } else {
- ssize_t len = readlink(argv[1], name, PATH_MAX);
-
- if (len < 0) {
- if (!quiet_errors) {
- perror("readlink");
- }
- return EXIT_FAILURE;
- }
- name[len] = '\0';
- }
-
- fputs(name, stdout);
- if (!skip_newline) {
- fputs("\n", stdout);
- }
-
- return EXIT_SUCCESS;
-}
diff --git a/toolbox/readtty.c b/toolbox/readtty.c
deleted file mode 100644
index 2b27548..0000000
--- a/toolbox/readtty.c
+++ /dev/null
@@ -1,183 +0,0 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-
-struct {
- char key;
- char *chars;
-} map[] = {
- { '1', "_ -1?!,.:;\"'<=>()_" },
- { '2', "Cabc2ABC" },
- { '3', "Fdef3DEF" },
- { '4', "Ighi4GHI" },
- { '5', "Ljkl5JKL" },
- { '6', "Omno6MNO" },
- { '7', "Spqrs7PQRS" },
- { '8', "Vtuv8TUV" },
- { '9', "Zwxyz9WXYZ" },
- { '0', "*+&0@/#*" },
-};
-
-char next_char(char key, char current)
-{
- int i;
- char *next;
- for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
- if(key == map[i].key) {
- next = strchr(map[i].chars, current);
- if(next && next[1])
- return next[1];
- return map[i].chars[1];
- }
- }
- return key;
-}
-
-char prev_char(char key, char current)
-{
- int i;
- char *next;
- for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
- if(key == map[i].key) {
- next = strchr(map[i].chars+1, current);
- if(next && next[-1])
- return next[-1];
- return map[i].chars[1];
- }
- }
- return key;
-}
-
-int readtty_main(int argc, char *argv[])
-{
- int c;
- //int flags;
- char buf[1];
- int res;
- struct termios ttyarg;
- struct termios savedttyarg;
- int nonblock = 0;
- int timeout = 0;
- int flush = 0;
- int phone = 0;
- char *accept = NULL;
- char *rejectstring = NULL;
- char last_char_in = 0;
- char current_char = 0;
- char *exit_string = NULL;
- int exit_match = 0;
-
- do {
- c = getopt(argc, argv, "nt:fa:r:pe:");
- if (c == EOF)
- break;
- switch (c) {
- case 't':
- timeout = atoi(optarg);
- break;
- case 'n':
- nonblock = 1;
- break;
- case 'f':
- flush = 1;
- break;
- case 'a':
- accept = optarg;
- break;
- case 'r':
- rejectstring = optarg;
- break;
- case 'p':
- phone = 1;
- break;
- case 'e':
- exit_string = optarg;
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
-
- if(flush)
- tcflush(STDIN_FILENO, TCIFLUSH);
- ioctl(STDIN_FILENO, TCGETS , &savedttyarg) ; /* set changed tty arguments */
- ttyarg = savedttyarg;
- ttyarg.c_cc[VMIN] = (timeout > 0 || nonblock) ? 0 : 1; /* minimum of 0 chars */
- ttyarg.c_cc[VTIME] = timeout; /* wait max 15/10 sec */
- ttyarg.c_iflag = BRKINT | ICRNL;
- ttyarg.c_lflag &= ~(ECHO | ICANON);
- ioctl(STDIN_FILENO, TCSETS , &ttyarg);
-
- while (1) {
- res = read(STDIN_FILENO, buf, 1);
- if(res <= 0) {
- if(phone) {
- if(current_char) {
- write(STDERR_FILENO, ¤t_char, 1);
- write(STDOUT_FILENO, ¤t_char, 1);
- if(exit_string && current_char == exit_string[exit_match]) {
- exit_match++;
- if(exit_string[exit_match] == '\0')
- break;
- }
- else
- exit_match = 0;
- current_char = 0;
- }
- continue;
- }
- break;
- }
- if(accept && strchr(accept, buf[0]) == NULL) {
- if(rejectstring) {
- write(STDOUT_FILENO, rejectstring, strlen(rejectstring));
- break;
- }
- if(flush)
- tcflush(STDIN_FILENO, TCIFLUSH);
- continue;
- }
- if(phone) {
- //if(!isprint(buf[0])) {
- // fprintf(stderr, "got unprintable character 0x%x\n", buf[0]);
- //}
- if(buf[0] == '\0') {
- if(current_char) {
- current_char = prev_char(last_char_in, current_char);
- write(STDERR_FILENO, ¤t_char, 1);
- write(STDERR_FILENO, "\b", 1);
- }
- continue;
- }
- if(current_char && buf[0] != last_char_in) {
- write(STDERR_FILENO, ¤t_char, 1);
- write(STDOUT_FILENO, ¤t_char, 1);
- if(exit_string && current_char == exit_string[exit_match]) {
- exit_match++;
- if(exit_string[exit_match] == '\0')
- break;
- }
- else
- exit_match = 0;
- current_char = 0;
- }
- last_char_in = buf[0];
- current_char = next_char(last_char_in, current_char);
- write(STDERR_FILENO, ¤t_char, 1);
- write(STDERR_FILENO, "\b", 1);
- continue;
- }
- write(STDOUT_FILENO, buf, 1);
- break;
- }
- ioctl(STDIN_FILENO, TCSETS , &savedttyarg) ; /* set changed tty arguments */
-
- return 0;
-}
diff --git a/toolbox/renice.c b/toolbox/renice.c
index 9dfeb51..99a06f4 100644
--- a/toolbox/renice.c
+++ b/toolbox/renice.c
@@ -32,6 +32,7 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sched.h>
diff --git a/toolbox/restorecon.c b/toolbox/restorecon.c
index 11532da..cb5799e 100644
--- a/toolbox/restorecon.c
+++ b/toolbox/restorecon.c
@@ -1,6 +1,7 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <errno.h>
#include <selinux/selinux.h>
#include <selinux/android.h>
@@ -9,7 +10,7 @@
static void usage(void)
{
- fprintf(stderr, "usage: %s [-FnrRv] pathname...\n", progname);
+ fprintf(stderr, "usage: %s [-DFnrRv] pathname...\n", progname);
exit(1);
}
@@ -21,10 +22,13 @@
progname = argv[0];
do {
- ch = getopt(argc, argv, "FnrRv");
+ ch = getopt(argc, argv, "DFnrRv");
if (ch == EOF)
break;
switch (ch) {
+ case 'D':
+ flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
+ break;
case 'F':
flags |= SELINUX_ANDROID_RESTORECON_FORCE;
break;
diff --git a/toolbox/rm.c b/toolbox/rm.c
deleted file mode 100644
index 957b586..0000000
--- a/toolbox/rm.c
+++ /dev/null
@@ -1,126 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <dirent.h>
-#include <limits.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#define OPT_RECURSIVE 1
-#define OPT_FORCE 2
-
-static int usage()
-{
- fprintf(stderr,"Usage: rm [-rR] [-f] <target>\n");
- return -1;
-}
-
-/* return -1 on failure, with errno set to the first error */
-static int unlink_recursive(const char* name, int flags)
-{
- struct stat st;
- DIR *dir;
- struct dirent *de;
- int fail = 0;
-
- /* is it a file or directory? */
- if (lstat(name, &st) < 0)
- return ((flags & OPT_FORCE) && errno == ENOENT) ? 0 : -1;
-
- /* a file, so unlink it */
- if (!S_ISDIR(st.st_mode))
- return unlink(name);
-
- /* a directory, so open handle */
- dir = opendir(name);
- if (dir == NULL)
- return -1;
-
- /* recurse over components */
- errno = 0;
- while ((de = readdir(dir)) != NULL) {
- char dn[PATH_MAX];
- if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
- continue;
- sprintf(dn, "%s/%s", name, de->d_name);
- if (unlink_recursive(dn, flags) < 0) {
- if (!(flags & OPT_FORCE)) {
- fail = 1;
- break;
- }
- }
- errno = 0;
- }
- /* in case readdir or unlink_recursive failed */
- if (fail || errno < 0) {
- int save = errno;
- closedir(dir);
- errno = save;
- return -1;
- }
-
- /* close directory handle */
- if (closedir(dir) < 0)
- return -1;
-
- /* delete target directory */
- return rmdir(name);
-}
-
-int rm_main(int argc, char *argv[])
-{
- int ret;
- int i, c;
- int flags = 0;
- int something_failed = 0;
-
- if (argc < 2)
- return usage();
-
- /* check flags */
- do {
- c = getopt(argc, argv, "frR");
- if (c == EOF)
- break;
- switch (c) {
- case 'f':
- flags |= OPT_FORCE;
- break;
- case 'r':
- case 'R':
- flags |= OPT_RECURSIVE;
- break;
- }
- } while (1);
-
- if (optind < 1 || optind >= argc) {
- usage();
- return -1;
- }
-
- /* loop over the file/directory args */
- for (i = optind; i < argc; i++) {
-
- if (flags & OPT_RECURSIVE) {
- ret = unlink_recursive(argv[i], flags);
- } else {
- ret = unlink(argv[i]);
- if (ret < 0 && errno == ENOENT && (flags & OPT_FORCE)) {
- continue;
- }
- }
-
- if (ret < 0) {
- fprintf(stderr, "rm failed for %s, %s\n", argv[i], strerror(errno));
- if (!(flags & OPT_FORCE)) {
- return -1;
- } else {
- something_failed = 1;
- }
- }
- }
-
- return something_failed;
-}
-
diff --git a/toolbox/rmdir.c b/toolbox/rmdir.c
deleted file mode 100644
index 06f3df2..0000000
--- a/toolbox/rmdir.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-
-static int usage()
-{
- fprintf(stderr,"rmdir <directory>\n");
- return -1;
-}
-
-int rmdir_main(int argc, char *argv[])
-{
- int symbolic = 0;
- int ret;
- if(argc < 2) return usage();
-
- while(argc > 1) {
- argc--;
- argv++;
- ret = rmdir(argv[0]);
- if(ret < 0) {
- fprintf(stderr, "rmdir failed for %s, %s\n", argv[0], strerror(errno));
- return ret;
- }
- }
-
- return 0;
-}
diff --git a/toolbox/rmmod.c b/toolbox/rmmod.c
deleted file mode 100644
index c7e0d6a..0000000
--- a/toolbox/rmmod.c
+++ /dev/null
@@ -1,53 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <malloc.h>
-#include <errno.h>
-#include <asm/unistd.h>
-
-extern int delete_module(const char *, unsigned int);
-
-int rmmod_main(int argc, char **argv)
-{
- int ret, i;
- char *modname, *dot;
-
- /* make sure we've got an argument */
- if (argc < 2) {
- fprintf(stderr, "usage: rmmod <module>\n");
- return -1;
- }
-
- /* if given /foo/bar/blah.ko, make a weak attempt
- * to convert to "blah", just for convenience
- */
- modname = strrchr(argv[1], '/');
- if (!modname)
- modname = argv[1];
- else modname++;
-
- dot = strchr(argv[1], '.');
- if (dot)
- *dot = '\0';
-
- /* Replace "-" with "_". This would keep rmmod
- * compatible with module-init-tools version of
- * rmmod
- */
- for (i = 0; modname[i] != '\0'; i++) {
- if (modname[i] == '-')
- modname[i] = '_';
- }
-
- /* pass it to the kernel */
- ret = delete_module(modname, O_NONBLOCK | O_EXCL);
- if (ret != 0) {
- fprintf(stderr, "rmmod: delete_module '%s' failed (errno %d)\n",
- modname, errno);
- return -1;
- }
-
- return 0;
-}
-
diff --git a/toolbox/rotatefb.c b/toolbox/rotatefb.c
deleted file mode 100644
index 2ff4127..0000000
--- a/toolbox/rotatefb.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <errno.h>
-#include <linux/fb.h>
-
-
-int rotatefb_main(int argc, char *argv[])
-{
- int c;
- char *fbdev = "/dev/graphics/fb0";
- int rotation = 0;
- int fd;
- int res;
- struct fb_var_screeninfo fbinfo;
-
- do {
- c = getopt(argc, argv, "d:");
- if (c == EOF)
- break;
- switch (c) {
- case 'd':
- fbdev = optarg;
- break;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- exit(1);
- }
- } while (1);
-
- if(optind + 1 != argc) {
- fprintf(stderr, "%s: specify rotation\n", argv[0]);
- exit(1);
- }
- rotation = atoi(argv[optind]);
-
- fd = open(fbdev, O_RDWR);
- if(fd < 0) {
- fprintf(stderr, "cannot open %s\n", fbdev);
- return 1;
- }
-
- res = ioctl(fd, FBIOGET_VSCREENINFO, &fbinfo);
- if(res < 0) {
- fprintf(stderr, "failed to get fbinfo: %s\n", strerror(errno));
- return 1;
- }
- if((fbinfo.rotate ^ rotation) & 1) {
- unsigned int xres = fbinfo.yres;
- fbinfo.yres = fbinfo.xres;
- fbinfo.xres = xres;
- fbinfo.xres_virtual = fbinfo.xres;
- fbinfo.yres_virtual = fbinfo.yres * 2;
- if(fbinfo.yoffset == xres)
- fbinfo.yoffset = fbinfo.yres;
- }
- fbinfo.rotate = rotation;
- res = ioctl(fd, FBIOPUT_VSCREENINFO, &fbinfo);
- if(res < 0) {
- fprintf(stderr, "failed to set fbinfo: %s\n", strerror(errno));
- return 1;
- }
-
- return 0;
-}
diff --git a/toolbox/schedtop.c b/toolbox/schedtop.c
deleted file mode 100644
index a76f968..0000000
--- a/toolbox/schedtop.c
+++ /dev/null
@@ -1,336 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include <stdint.h>
-#include <string.h>
-
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <signal.h>
-
-#include <pwd.h>
-
-struct thread_info {
- int pid;
- int tid;
- char name[64];
- uint64_t exec_time;
- uint64_t delay_time;
- uint32_t run_count;
-};
-
-struct thread_table {
- size_t allocated;
- size_t active;
- struct thread_info *data;
-};
-
-enum {
- FLAG_BATCH = 1U << 0,
- FLAG_HIDE_IDLE = 1U << 1,
- FLAG_SHOW_THREADS = 1U << 2,
- FLAG_USE_ALTERNATE_SCREEN = 1U << 3,
-};
-
-static int time_dp = 9;
-static int time_div = 1;
-#define NS_TO_S_D(ns) \
- (uint32_t)((ns) / 1000000000), time_dp, ((uint32_t)((ns) % 1000000000) / time_div)
-
-struct thread_table processes;
-struct thread_table last_processes;
-struct thread_table threads;
-struct thread_table last_threads;
-
-static void grow_table(struct thread_table *table)
-{
- size_t size = table->allocated;
- struct thread_info *new_table;
- if (size < 128)
- size = 128;
- else
- size *= 2;
-
- new_table = realloc(table->data, size * sizeof(*table->data));
- if (new_table == NULL) {
- fprintf(stderr, "out of memory\n");
- exit(1);
- }
- table->data = new_table;
- table->allocated = size;
-}
-
-static struct thread_info *get_item(struct thread_table *table)
-{
- if (table->active >= table->allocated)
- grow_table(table);
- return table->data + table->active;
-}
-
-static void commit_item(struct thread_table *table)
-{
- table->active++;
-}
-
-static int read_line(char *line, size_t line_size)
-{
- int fd;
- int len;
- fd = open(line, O_RDONLY);
- if(fd == 0)
- return -1;
- len = read(fd, line, line_size - 1);
- close(fd);
- if (len <= 0)
- return -1;
- line[len] = '\0';
- return 0;
-}
-
-static void add_thread(int pid, int tid, struct thread_info *proc_info)
-{
- char line[1024];
- char *name, *name_end;
- size_t name_len;
- struct thread_info *info;
- if(tid == 0)
- info = get_item(&processes);
- else
- info = get_item(&threads);
- info->pid = pid;
- info->tid = tid;
-
- if(tid)
- sprintf(line, "/proc/%d/task/%d/schedstat", pid, tid);
- else
- sprintf(line, "/proc/%d/schedstat", pid);
- if (read_line(line, sizeof(line)))
- return;
- if(sscanf(line, "%llu %llu %u", &info->exec_time, &info->delay_time, &info->run_count) != 3)
- return;
- if (proc_info) {
- proc_info->exec_time += info->exec_time;
- proc_info->delay_time += info->delay_time;
- proc_info->run_count += info->run_count;
- }
-
- name = NULL;
- if (!tid) {
- sprintf(line, "/proc/%d/cmdline", pid);
- if (read_line(line, sizeof(line)) == 0 && line[0]) {
- name = line;
- name_len = strlen(name);
- }
- }
- if (!name) {
- if (tid)
- sprintf(line, "/proc/%d/task/%d/stat", pid, tid);
- else
- sprintf(line, "/proc/%d/stat", pid);
- if (read_line(line, sizeof(line)))
- return;
- name = strchr(line, '(');
- if (name == NULL)
- return;
- name_end = strchr(name, ')');
- if (name_end == NULL)
- return;
- name++;
- name_len = name_end - name;
- }
- if (name_len >= sizeof(info->name))
- name_len = sizeof(info->name) - 1;
- memcpy(info->name, name, name_len);
- info->name[name_len] = '\0';
- if(tid == 0)
- commit_item(&processes);
- else
- commit_item(&threads);
-}
-
-static void add_threads(int pid, struct thread_info *proc_info)
-{
- char path[1024];
- DIR *d;
- struct dirent *de;
- sprintf(path, "/proc/%d/task", pid);
- d = opendir(path);
- if(d == 0) return;
- while((de = readdir(d)) != 0){
- if(isdigit(de->d_name[0])){
- int tid = atoi(de->d_name);
- add_thread(pid, tid, proc_info);
- }
- }
- closedir(d);
-}
-
-static void print_threads(int pid, uint32_t flags)
-{
- size_t i, j;
- for (i = 0; i < last_threads.active; i++) {
- int epid = last_threads.data[i].pid;
- int tid = last_threads.data[i].tid;
- if (epid != pid)
- continue;
- for (j = 0; j < threads.active; j++)
- if (tid == threads.data[j].tid)
- break;
- if (j == threads.active)
- printf(" %5u died\n", tid);
- else if (!(flags & FLAG_HIDE_IDLE) || threads.data[j].run_count - last_threads.data[i].run_count)
- printf(" %5u %2u.%0*u %2u.%0*u %5u %5u.%0*u %5u.%0*u %7u %s\n", tid,
- NS_TO_S_D(threads.data[j].exec_time - last_threads.data[i].exec_time),
- NS_TO_S_D(threads.data[j].delay_time - last_threads.data[i].delay_time),
- threads.data[j].run_count - last_threads.data[i].run_count,
- NS_TO_S_D(threads.data[j].exec_time), NS_TO_S_D(threads.data[j].delay_time),
- threads.data[j].run_count, threads.data[j].name);
- }
-}
-
-static void update_table(DIR *d, uint32_t flags)
-{
- size_t i, j;
- struct dirent *de;
-
- rewinddir(d);
- while((de = readdir(d)) != 0){
- if(isdigit(de->d_name[0])){
- int pid = atoi(de->d_name);
- struct thread_info *proc_info;
- add_thread(pid, 0, NULL);
- proc_info = &processes.data[processes.active - 1];
- proc_info->exec_time = 0;
- proc_info->delay_time = 0;
- proc_info->run_count = 0;
- add_threads(pid, proc_info);
- }
- }
- if (!(flags & FLAG_BATCH))
- printf("\e[H\e[0J");
- printf("Processes: %zu, Threads %zu\n", processes.active, threads.active);
- switch (time_dp) {
- case 3:
- printf(" TID --- SINCE LAST ---- ---------- TOTAL ----------\n");
- printf(" PID EXEC_T DELAY SCHED EXEC_TIME DELAY_TIM SCHED NAME\n");
- break;
- case 6:
- printf(" TID ------ SINCE LAST ------- ------------ TOTAL -----------\n");
- printf(" PID EXEC_TIME DELAY_TIM SCHED EXEC_TIME DELAY_TIME SCHED NAME\n");
- break;
- default:
- printf(" TID -------- SINCE LAST -------- ------------- TOTAL -------------\n");
- printf(" PID EXEC_TIME DELAY_TIME SCHED EXEC_TIME DELAY_TIME SCHED NAME\n");
- break;
- }
- for (i = 0; i < last_processes.active; i++) {
- int pid = last_processes.data[i].pid;
- int tid = last_processes.data[i].tid;
- for (j = 0; j < processes.active; j++)
- if (pid == processes.data[j].pid)
- break;
- if (j == processes.active)
- printf("%5u died\n", pid);
- else if (!(flags & FLAG_HIDE_IDLE) || processes.data[j].run_count - last_processes.data[i].run_count) {
- printf("%5u %2u.%0*u %2u.%0*u %5u %5u.%0*u %5u.%0*u %7u %s\n", pid,
- NS_TO_S_D(processes.data[j].exec_time - last_processes.data[i].exec_time),
- NS_TO_S_D(processes.data[j].delay_time - last_processes.data[i].delay_time),
- processes.data[j].run_count - last_processes.data[i].run_count,
- NS_TO_S_D(processes.data[j].exec_time), NS_TO_S_D(processes.data[j].delay_time),
- processes.data[j].run_count, processes.data[j].name);
- if (flags & FLAG_SHOW_THREADS)
- print_threads(pid, flags);
- }
- }
-
- {
- struct thread_table tmp;
- tmp = last_processes;
- last_processes = processes;
- processes = tmp;
- processes.active = 0;
- tmp = last_threads;
- last_threads = threads;
- threads = tmp;
- threads.active = 0;
- }
-}
-
-void
-sig_abort(int signum)
-{
- printf("\e[?47l");
- exit(0);
-}
-
-
-int schedtop_main(int argc, char **argv)
-{
- int c;
- DIR *d;
- struct dirent *de;
- char *namefilter = 0;
- int pidfilter = 0;
- uint32_t flags = 0;
- int delay = 3000000;
- float delay_f;
-
- while(1) {
- c = getopt(argc, argv, "d:ibtamun");
- if (c == EOF)
- break;
- switch (c) {
- case 'd':
- delay_f = atof(optarg);
- delay = delay_f * 1000000;
- break;
- case 'b':
- flags |= FLAG_BATCH;
- break;
- case 'i':
- flags |= FLAG_HIDE_IDLE;
- break;
- case 't':
- flags |= FLAG_SHOW_THREADS;
- break;
- case 'a':
- flags |= FLAG_USE_ALTERNATE_SCREEN;
- break;
- case 'm':
- time_dp = 3;
- time_div = 1000000;
- break;
- case 'u':
- time_dp = 6;
- time_div = 1000;
- break;
- case 'n':
- time_dp = 9;
- time_div = 1;
- break;
- }
- }
-
- d = opendir("/proc");
- if(d == 0) return -1;
-
- if (!(flags & FLAG_BATCH)) {
- if(flags & FLAG_USE_ALTERNATE_SCREEN) {
- signal(SIGINT, sig_abort);
- signal(SIGPIPE, sig_abort);
- signal(SIGTERM, sig_abort);
- printf("\e7\e[?47h");
- }
- printf("\e[2J");
- }
- while (1) {
- update_table(d, flags);
- usleep(delay);
- }
- closedir(d);
- return 0;
-}
diff --git a/toolbox/sendevent.c b/toolbox/sendevent.c
index 1608e6c..4d0ca17 100644
--- a/toolbox/sendevent.c
+++ b/toolbox/sendevent.c
@@ -1,55 +1,17 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdint.h>
-#include <fcntl.h>
#include <sys/ioctl.h>
-//#include <linux/input.h> // this does not compile
-#include <errno.h>
-
-
-// from <linux/input.h>
-
-struct input_event {
- struct timeval time;
- __u16 type;
- __u16 code;
- __s32 value;
-};
-
-#define EVIOCGVERSION _IOR('E', 0x01, int) /* get driver version */
-#define EVIOCGID _IOR('E', 0x02, struct input_id) /* get device ID */
-#define EVIOCGKEYCODE _IOR('E', 0x04, int[2]) /* get keycode */
-#define EVIOCSKEYCODE _IOW('E', 0x04, int[2]) /* set keycode */
-
-#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */
-#define EVIOCGPHYS(len) _IOC(_IOC_READ, 'E', 0x07, len) /* get physical location */
-#define EVIOCGUNIQ(len) _IOC(_IOC_READ, 'E', 0x08, len) /* get unique identifier */
-
-#define EVIOCGKEY(len) _IOC(_IOC_READ, 'E', 0x18, len) /* get global keystate */
-#define EVIOCGLED(len) _IOC(_IOC_READ, 'E', 0x19, len) /* get all LEDs */
-#define EVIOCGSND(len) _IOC(_IOC_READ, 'E', 0x1a, len) /* get all sounds status */
-#define EVIOCGSW(len) _IOC(_IOC_READ, 'E', 0x1b, len) /* get all switch states */
-
-#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, 'E', 0x20 + ev, len) /* get event bits */
-#define EVIOCGABS(abs) _IOR('E', 0x40 + abs, struct input_absinfo) /* get abs value/limits */
-#define EVIOCSABS(abs) _IOW('E', 0xc0 + abs, struct input_absinfo) /* set abs value/limits */
-
-#define EVIOCSFF _IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect)) /* send a force effect to a force feedback device */
-#define EVIOCRMFF _IOW('E', 0x81, int) /* Erase a force effect */
-#define EVIOCGEFFECTS _IOR('E', 0x84, int) /* Report number of effects playable at the same time */
-
-#define EVIOCGRAB _IOW('E', 0x90, int) /* Grab/Release device */
-
-// end <linux/input.h>
-
-
+#include <unistd.h>
int sendevent_main(int argc, char *argv[])
{
- int i;
int fd;
- int ret;
+ ssize_t ret;
int version;
struct input_event event;
@@ -72,7 +34,7 @@
event.code = atoi(argv[3]);
event.value = atoi(argv[4]);
ret = write(fd, &event, sizeof(event));
- if(ret < sizeof(event)) {
+ if(ret < (ssize_t) sizeof(event)) {
fprintf(stderr, "write event failed, %s\n", strerror(errno));
return -1;
}
diff --git a/toolbox/setenforce.c b/toolbox/setenforce.c
deleted file mode 100644
index 444073d..0000000
--- a/toolbox/setenforce.c
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
-#include <strings.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-static void usage(const char *progname)
-{
- fprintf(stderr, "usage: %s [ Enforcing | Permissive | 1 | 0 ]\n",
- progname);
- exit(1);
-}
-
-int setenforce_main(int argc, char **argv)
-{
- int rc = 0;
- if (argc != 2) {
- usage(argv[0]);
- }
-
- if (is_selinux_enabled() <= 0) {
- fprintf(stderr, "%s: SELinux is disabled\n", argv[0]);
- return 1;
- }
- if (strlen(argv[1]) == 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
- rc = security_setenforce(atoi(argv[1]));
- } else {
- if (strcasecmp(argv[1], "enforcing") == 0) {
- rc = security_setenforce(1);
- } else if (strcasecmp(argv[1], "permissive") == 0) {
- rc = security_setenforce(0);
- } else
- usage(argv[0]);
- }
- if (rc < 0) {
- fprintf(stderr, "%s: Could not set enforcing status: %s\n",
- argv[0], strerror(errno));
- return 2;
- }
- return 0;
-}
diff --git a/toolbox/setkey.c b/toolbox/setkey.c
deleted file mode 100644
index 1ff2774..0000000
--- a/toolbox/setkey.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <linux/kd.h>
-#include <linux/vt.h>
-#include <errno.h>
-
-static void setkey_usage(char *argv[])
-{
- fprintf(stderr, "%s [-t <table>] [-k <index>] [-v value] [-r] [-h]\n"
- " -t <table> Select table\n"
- " -k <index> Select key\n"
- " -v <value> Set entry\n"
- " -r Read current entry\n"
- " -h Print help\n", argv[0]);
-}
-
-#define TTYDEV "/dev/tty0"
-
-int setkey_main(int argc, char *argv[])
-{
- int fd;
- struct kbentry kbe;
- int did_something = 0;
-
- kbe.kb_table = 0;
- kbe.kb_index = -1;
- kbe.kb_value = 0;
-
- fd = open(TTYDEV, O_RDWR | O_SYNC);
- if (fd < 0) {
- fprintf(stderr, "open %s: %s\n", TTYDEV, strerror(errno));
- return 1;
- }
-
- do {
- int c, ret;
-
- c = getopt(argc, argv, "t:k:v:hr");
- if (c == EOF)
- break;
-
- switch (c) {
- case 't':
- kbe.kb_table = strtol(optarg, NULL, 0);
- break;
- case 'k':
- kbe.kb_index = strtol(optarg, NULL, 0);
- break;
- case 'v':
- kbe.kb_value = strtol(optarg, NULL, 0);
- ret = ioctl(fd, KDSKBENT, &kbe);
- if (ret < 0) {
- fprintf(stderr, "KDSKBENT %d %d %d failed: %s\n",
- kbe.kb_table, kbe.kb_index, kbe.kb_value,
- strerror(errno));
- return 1;
- }
- did_something = 1;
- break;
- case 'r':
- ret = ioctl(fd, KDGKBENT, &kbe);
- if (ret < 0) {
- fprintf(stderr, "KDGKBENT %d %d failed: %s\n",
- kbe.kb_table, kbe.kb_index, strerror(errno));
- return 1;
- }
- printf("0x%x 0x%x 0x%x\n",
- kbe.kb_table, kbe.kb_index, kbe.kb_value);
- did_something = 1;
- break;
- case 'h':
- setkey_usage(argv);
- return 1;
- case '?':
- fprintf(stderr, "%s: invalid option -%c\n",
- argv[0], optopt);
- return 1;
- }
- } while (1);
-
- if(optind != argc || !did_something) {
- setkey_usage(argv);
- return 1;
- }
-
- return 0;
-}
diff --git a/toolbox/setsebool.c b/toolbox/setsebool.c
deleted file mode 100644
index f79a612..0000000
--- a/toolbox/setsebool.c
+++ /dev/null
@@ -1,46 +0,0 @@
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-#include <errno.h>
-
-static int do_setsebool(int nargs, char **args) {
- const char *name = args[1];
- const char *value = args[2];
- SELboolean b;
-
- if (is_selinux_enabled() <= 0)
- return 0;
-
- b.name = name;
- if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
- b.value = 1;
- else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
- b.value = 0;
- else {
- fprintf(stderr, "setsebool: invalid value %s\n", value);
- return -1;
- }
-
- if (security_set_boolean_list(1, &b, 0) < 0)
- {
- fprintf(stderr, "setsebool: could not set %s to %s: %s", name, value, strerror(errno));
- return -1;
- }
-
- return 0;
-}
-
-int setsebool_main(int argc, char **argv)
-{
- if (argc != 3) {
- fprintf(stderr, "Usage: %s name value\n", argv[0]);
- exit(1);
- }
-
- return do_setsebool(argc, argv);
-}
diff --git a/toolbox/smd.c b/toolbox/smd.c
deleted file mode 100644
index 91e495c..0000000
--- a/toolbox/smd.c
+++ /dev/null
@@ -1,41 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <errno.h>
-
-int smd_main(int argc, char **argv)
-{
- int fd, len, r, port = 0;
- char devname[32];
- argc--;
- argv++;
-
- if((argc > 0) && (argv[0][0] == '-')) {
- port = atoi(argv[0] + 1);
- argc--;
- argv++;
- }
-
- sprintf(devname,"/dev/smd%d",port);
- fd = open(devname, O_WRONLY);
- if(fd < 0) {
- fprintf(stderr,"failed to open smd0 - %s\n",
- strerror(errno));
- return -1;
- }
- while(argc > 0) {
- len = strlen(argv[0]);
- r = write(fd, argv[0], len);
- if(r != len) {
- fprintf(stderr,"failed to write smd0 (%d) %s\n",
- r, strerror(errno));
- return -1;
- }
- argc--;
- argv++;
- write(fd, argc ? " " : "\r", 1);
- }
- close(fd);
- return 0;
-}
diff --git a/toolbox/start.c b/toolbox/start.c
index 665a941..6c8a3f2 100644
--- a/toolbox/start.c
+++ b/toolbox/start.c
@@ -7,14 +7,14 @@
int start_main(int argc, char *argv[])
{
- char buf[1024];
-
if(argc > 1) {
property_set("ctl.start", argv[1]);
} else {
/* defaults to starting the common services stopped by stop.c */
+ property_set("ctl.start", "netd");
property_set("ctl.start", "surfaceflinger");
property_set("ctl.start", "zygote");
+ property_set("ctl.start", "zygote_secondary");
}
return 0;
diff --git a/toolbox/stop.c b/toolbox/stop.c
index 460f377..5e3ce3c 100644
--- a/toolbox/stop.c
+++ b/toolbox/stop.c
@@ -5,14 +5,14 @@
int stop_main(int argc, char *argv[])
{
- char buf[1024];
-
if(argc > 1) {
property_set("ctl.stop", argv[1]);
} else{
/* defaults to stopping the common services */
+ property_set("ctl.stop", "zygote_secondary");
property_set("ctl.stop", "zygote");
property_set("ctl.stop", "surfaceflinger");
+ property_set("ctl.stop", "netd");
}
return 0;
diff --git a/toolbox/swapoff.c b/toolbox/swapoff.c
deleted file mode 100644
index 8f14158..0000000
--- a/toolbox/swapoff.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <asm/page.h>
-#include <sys/swap.h>
-
-int swapoff_main(int argc, char **argv)
-{
- int err = 0;
-
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
- return -EINVAL;
- }
-
- err = swapoff(argv[1]);
- if (err) {
- fprintf(stderr, "swapoff failed for %s\n", argv[1]);
- }
-
- return err;
-}
diff --git a/toolbox/swapon.c b/toolbox/swapon.c
deleted file mode 100644
index a810b3d..0000000
--- a/toolbox/swapon.c
+++ /dev/null
@@ -1,67 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <getopt.h>
-#include <asm/page.h>
-#include <sys/swap.h>
-
-void usage(char *name)
-{
- fprintf(stderr, "Usage: %s [-p prio] <filename>\n"
- " prio must be between 0 and %d\n", name, SWAP_FLAG_PRIO_MASK);
-}
-
-int parse_prio(char *prio_str)
-{
- unsigned long p = strtoul(prio_str, NULL, 10);
-
- return (p > SWAP_FLAG_PRIO_MASK)? -1 : (int)p;
-}
-
-int swapon_main(int argc, char **argv)
-{
- int err = 0;
- int flags = 0;
- int prio;
-
- opterr = 0;
- do {
- int c = getopt(argc, argv, "hp:");
- if (c == -1)
- break;
-
- switch (c) {
- case 'p':
- if (optarg != NULL)
- prio = parse_prio(optarg);
- else
- prio = -1;
-
- if (prio < 0) {
- usage(argv[0]);
- return -EINVAL;
- }
- flags |= SWAP_FLAG_PREFER;
- flags |= (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
- break;
- case 'h':
- usage(argv[0]);
- return 0;
- case '?':
- fprintf(stderr, "unknown option: %c\n", optopt);
- return -EINVAL;
- }
- } while (1);
-
- if (optind != argc - 1) {
- usage(argv[0]);
- return -EINVAL;
- }
-
- err = swapon(argv[argc - 1], flags);
- if (err) {
- fprintf(stderr, "swapon failed for %s\n", argv[argc - 1]);
- }
-
- return err;
-}
diff --git a/toolbox/sync.c b/toolbox/sync.c
deleted file mode 100644
index 8284276..0000000
--- a/toolbox/sync.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <unistd.h>
-
-int sync_main(int argc, char **argv)
-{
- sync();
- return 0;
-}
diff --git a/toolbox/syren.c b/toolbox/syren.c
deleted file mode 100644
index 06e329e..0000000
--- a/toolbox/syren.c
+++ /dev/null
@@ -1,154 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <malloc.h>
-
-/* ioctl crap */
-#define SYREN_RD 101
-#define SYREN_WR 102
-#define SYREN_OLD_RD 108
-#define SYREN_OLD_WR 109
-
-struct syren_io_args {
- unsigned long page;
- unsigned long addr;
- unsigned long value;
-};
-
-typedef struct {
- u_char page;
- u_char addr;
- const char *name;
-} syren_reg;
-
-static syren_reg registers[] = {
- { 0, 0x04, "TOGBR1" },
- { 0, 0x05, "TOGBR2" },
- { 0, 0x06, "VBDCTRL" },
- { 1, 0x07, "VBUCTRL" },
- { 1, 0x08, "VBCTRL" },
- { 1, 0x09, "PWDNRG" },
- { 1, 0x0a, "VBPOP" },
- { 1, 0x0b, "VBCTRL2" },
- { 1, 0x0f, "VAUDCTRL" },
- { 1, 0x10, "VAUSCTRL" },
- { 1, 0x11, "VAUOCTRL" },
- { 1, 0x12, "VAUDPLL" },
- { 1, 0x17, "VRPCSIMR" },
- { 0, 0, 0 }
-};
-
-static syren_reg *find_reg(const char *name)
-{
- int i;
-
- for (i = 0; registers[i].name != 0; i++) {
- if (!strcasecmp(registers[i].name, name))
- return ®isters[i];
- }
-
- return NULL;
-}
-
-static int usage(void)
-{
- fprintf(stderr, "usage: syren [r/w] [REGNAME | page:addr] (value)\n");
- return 1;
-}
-
-int
-syren_main(int argc, char **argv)
-{
- int cmd = -1;
- syren_reg *r;
- struct syren_io_args sio;
- char name[32];
- int fd;
-
- if (argc < 3) {
- return usage();
- }
-
- switch(argv[1][0]) {
- case 'r':
- cmd = SYREN_RD;
- break;
- case 'w':
- cmd = SYREN_WR;
- break;
- case 'R':
- cmd = SYREN_OLD_RD;
- break;
- case 'W':
- cmd = SYREN_OLD_WR;
- break;
- default:
- return usage();
- }
-
- if (cmd == SYREN_WR || cmd == SYREN_OLD_WR) {
- if (argc < 4)
- return usage();
- sio.value = strtoul(argv[3], 0, 0);
- }
-
- fd = open("/dev/eac", O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "can't open /dev/eac\n");
- return 1;
- }
-
- if (strcasecmp(argv[2], "all") == 0) {
- int i;
- if (cmd != SYREN_RD && cmd != SYREN_OLD_RD) {
- fprintf(stderr, "can only read all registers\n");
- return 1;
- }
-
- for (i = 0; registers[i].name; i++) {
- sio.page = registers[i].page;
- sio.addr = registers[i].addr;
- if (ioctl(fd, cmd, &sio) < 0) {
- fprintf(stderr, "%s: error\n", registers[i].name);
- } else {
- fprintf(stderr, "%s: %04x\n", registers[i].name, sio.value);
- }
- }
-
- close(fd);
- return 0;
- }
-
- r = find_reg(argv[2]);
- if (r == NULL) {
- strcpy(name, argv[2]);
- char *addr_str = strchr(argv[2], ':');
- if (addr_str == NULL)
- return usage();
- *addr_str++ = 0;
- sio.page = strtoul(argv[2], 0, 0);
- sio.addr = strtoul(addr_str, 0, 0);
- } else {
- strcpy(name, r->name);
- sio.page = r->page;
- sio.addr = r->addr;
- }
-
- if (ioctl(fd, cmd, &sio) < 0) {
- fprintf(stderr, "ioctl(%d) failed\n", cmd);
- return 1;
- }
-
- if (cmd == SYREN_RD || cmd == SYREN_OLD_RD) {
- printf("%s: %04x\n", name, sio.value);
- } else {
- printf("wrote %04x to %s\n", sio.value, name);
- }
-
- close(fd);
-
- return 0;
-}
-
diff --git a/toolbox/top.c b/toolbox/top.c
index 7642522..b1a275c 100644
--- a/toolbox/top.c
+++ b/toolbox/top.c
@@ -9,7 +9,7 @@
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
+ * the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
@@ -22,7 +22,7 @@
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
@@ -32,6 +32,7 @@
#include <ctype.h>
#include <dirent.h>
#include <grp.h>
+#include <inttypes.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
@@ -59,13 +60,13 @@
char name[PROC_NAME_LEN];
char tname[THREAD_NAME_LEN];
char state;
- long unsigned utime;
- long unsigned stime;
- long unsigned delta_utime;
- long unsigned delta_stime;
- long unsigned delta_time;
- long vss;
- long rss;
+ uint64_t utime;
+ uint64_t stime;
+ uint64_t delta_utime;
+ uint64_t delta_stime;
+ uint64_t delta_time;
+ uint64_t vss;
+ uint64_t rss;
int prs;
int num_threads;
char policy[POLICY_NAME_LEN];
@@ -108,16 +109,20 @@
static int numcmp(long long a, long long b);
static void usage(char *cmd);
-int top_main(int argc, char *argv[]) {
- int i;
+static void exit_top(int signal) {
+ exit(EXIT_FAILURE);
+}
+int top_main(int argc, char *argv[]) {
num_used_procs = num_free_procs = 0;
+ signal(SIGPIPE, exit_top);
+
max_procs = 0;
delay = 3;
iterations = -1;
proc_cmp = &proc_cpu_cmp;
- for (i = 1; i < argc; i++) {
+ for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-m")) {
if (i + 1 >= argc) {
fprintf(stderr, "Option -m expects an argument.\n");
@@ -249,9 +254,9 @@
continue;
pid = atoi(pid_dir->d_name);
-
+
struct proc_info cur_proc;
-
+
if (!threads) {
proc = alloc_proc();
@@ -275,7 +280,7 @@
sprintf(filename, "/proc/%d/status", pid);
read_status(filename, &cur_proc);
-
+
proc = NULL;
}
@@ -310,7 +315,7 @@
}
closedir(task_dir);
-
+
if (!threads)
add_proc(proc_num++, proc);
}
@@ -324,7 +329,6 @@
static int read_stat(char *filename, struct proc_info *proc) {
FILE *file;
char buf[MAX_LINE], *open_paren, *close_paren;
- int res, idx;
file = fopen(filename, "r");
if (!file) return 1;
@@ -339,12 +343,21 @@
*open_paren = *close_paren = '\0';
strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN);
proc->tname[THREAD_NAME_LEN-1] = 0;
-
+
/* Scan rest of string. */
- sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
- "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld "
- "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d",
- &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss, &proc->prs);
+ sscanf(close_paren + 1,
+ " %c " "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
+ "%" SCNu64
+ "%" SCNu64 "%*d %*d %*d %*d %*d %*d %*d "
+ "%" SCNu64
+ "%" SCNu64 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
+ "%d",
+ &proc->state,
+ &proc->utime,
+ &proc->stime,
+ &proc->vss,
+ &proc->rss,
+ &proc->prs);
return 0;
}
@@ -410,9 +423,7 @@
struct proc_info *old_proc, *proc;
long unsigned total_delta_time;
struct passwd *user;
- struct group *group;
char *user_str, user_buf[20];
- char *group_str, group_buf[20];
for (i = 0; i < num_new_procs; i++) {
if (new_procs[i]) {
@@ -452,7 +463,7 @@
new_cpu.sirqtime - old_cpu.sirqtime,
total_delta_time);
printf("\n");
- if (!threads)
+ if (!threads)
printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name");
else
printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc");
@@ -463,25 +474,21 @@
if (!proc || (max_procs && (i >= max_procs)))
break;
user = getpwuid(proc->uid);
- group = getgrgid(proc->gid);
if (user && user->pw_name) {
user_str = user->pw_name;
} else {
snprintf(user_buf, 20, "%d", proc->uid);
user_str = user_buf;
}
- if (group && group->gr_name) {
- group_str = group->gr_name;
+ if (!threads) {
+ printf("%5d %2d %3" PRIu64 "%% %c %5d %6" PRIu64 "K %6" PRIu64 "K %3s %-8.8s %s\n",
+ proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
+ proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
} else {
- snprintf(group_buf, 20, "%d", proc->gid);
- group_str = group_buf;
+ printf("%5d %5d %2d %3" PRIu64 "%% %c %6" PRIu64 "K %6" PRIu64 "K %3s %-8.8s %-15s %s\n",
+ proc->pid, proc->tid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state,
+ proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name);
}
- if (!threads)
- printf("%5d %2d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
- proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
- else
- printf("%5d %5d %2d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state,
- proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name);
}
}
diff --git a/toolbox/touch.c b/toolbox/touch.c
deleted file mode 100644
index 52ddf2a..0000000
--- a/toolbox/touch.c
+++ /dev/null
@@ -1,114 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <time.h>
-
-static void usage(void)
-{
- fprintf(stderr, "touch: usage: touch [-alm] [-t YYYYMMDD[.hhmmss]] <file>\n");
- exit(1);
-}
-
-static time_t parse_time(char *s)
-{
- struct tm tm;
- int day = atoi(s);
- int hour = 0;
-
- while (*s && *s != '.') {
- s++;
- }
-
- if (*s) {
- s++;
- hour = atoi(s);
- }
-
- tm.tm_year = day / 10000 - 1900;
- tm.tm_mon = (day % 10000) / 100 - 1;
- tm.tm_mday = day % 100;
- tm.tm_hour = hour / 10000;
- tm.tm_min = (hour % 10000) / 100;
- tm.tm_sec = hour % 100;
- tm.tm_isdst = -1;
-
- return mktime(&tm);
-}
-
-int touch_main(int argc, char *argv[])
-{
- int i, fd, aflag = 0, mflag = 0, debug = 0, flags = 0;
- struct timespec specified_time, times[2];
- char *file = 0;
-
- specified_time.tv_nsec = UTIME_NOW;
-
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- /* an option */
- const char *arg = argv[i]+1;
- while (arg[0]) {
- switch (arg[0]) {
- case 'a': aflag = 1; break;
- case 'm': mflag = 1; break;
- case 't':
- if ((i+1) >= argc)
- usage();
- specified_time.tv_sec = parse_time(argv[++i]);
- if (specified_time.tv_sec == -1) {
- fprintf(stderr, "touch: invalid timestamp specified\n");
- exit(1);
- }
- specified_time.tv_nsec = 0;
- break;
- case 'l': flags |= AT_SYMLINK_NOFOLLOW; break;
- case 'd': debug = 1; break;
- default:
- usage();
- }
- arg++;
- }
- } else {
- /* not an option, and only accept one filename */
- if (i+1 != argc)
- usage();
- file = argv[i];
- }
- }
-
- if (! file) {
- fprintf(stderr, "touch: no file specified\n");
- exit(1);
- }
-
- if (access(file, F_OK))
- if ((fd=creat(file, 0666)) != -1)
- close(fd);
-
- if ((mflag == 0) && (aflag == 0))
- aflag = mflag = 1;
-
- if (aflag)
- times[0] = specified_time;
- else
- times[0].tv_nsec = UTIME_OMIT;
-
- if (mflag)
- times[1] = specified_time;
- else
- times[1].tv_nsec = UTIME_OMIT;
-
- if (debug) {
- fprintf(stderr, "file = %s\n", file);
- fprintf(stderr, "times[0].tv_sec = %ld, times[0].tv_nsec = %ld\n", times[0].tv_sec, times[0].tv_nsec);
- fprintf(stderr, "times[1].tv_sec = %ld, times[1].tv_nsec = %ld\n", times[1].tv_sec, times[1].tv_nsec);
- fprintf(stderr, "flags = 0x%8.8x\n", flags);
- }
-
- return utimensat(AT_FDCWD, file, times, flags);
-}
-
diff --git a/toolbox/umount.c b/toolbox/umount.c
index 890e870..3e17396 100644
--- a/toolbox/umount.c
+++ b/toolbox/umount.c
@@ -33,7 +33,6 @@
char mount_path[256];
char rest[256];
int result = 0;
- int path_length = strlen(path);
f = fopen("/proc/mounts", "r");
if (!f) {
diff --git a/toolbox/upstream-netbsd/bin/dd/args.c b/toolbox/upstream-netbsd/bin/dd/args.c
new file mode 100644
index 0000000..207e300
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/args.c
@@ -0,0 +1,391 @@
+/* $NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "dd.h"
+#include "extern.h"
+
+static int c_arg(const void *, const void *);
+
+#ifdef NO_MSGFMT
+static void f_msgfmt(char *) __dead;
+#else
+static void f_msgfmt(char *);
+#endif /* NO_MSGFMT */
+
+#ifdef NO_CONV
+static void f_conv(char *) __dead;
+#else
+static void f_conv(char *);
+static int c_conv(const void *, const void *);
+#endif /* NO_CONV */
+
+static void f_bs(char *);
+static void f_cbs(char *);
+static void f_count(char *);
+static void f_files(char *);
+static void f_ibs(char *);
+static void f_if(char *);
+static void f_obs(char *);
+static void f_of(char *);
+static void f_seek(char *);
+static void f_skip(char *);
+static void f_progress(char *);
+
+static const struct arg {
+ const char *name;
+ void (*f)(char *);
+ u_int set, noset;
+} args[] = {
+ /* the array needs to be sorted by the first column so
+ bsearch() can be used to find commands quickly */
+ { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
+ { "cbs", f_cbs, C_CBS, C_CBS },
+ { "conv", f_conv, 0, 0 },
+ { "count", f_count, C_COUNT, C_COUNT },
+ { "files", f_files, C_FILES, C_FILES },
+ { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
+ { "if", f_if, C_IF, C_IF },
+ { "iseek", f_skip, C_SKIP, C_SKIP },
+ { "msgfmt", f_msgfmt, 0, 0 },
+ { "obs", f_obs, C_OBS, C_BS|C_OBS },
+ { "of", f_of, C_OF, C_OF },
+ { "oseek", f_seek, C_SEEK, C_SEEK },
+ { "progress", f_progress, 0, 0 },
+ { "seek", f_seek, C_SEEK, C_SEEK },
+ { "skip", f_skip, C_SKIP, C_SKIP },
+};
+
+/*
+ * args -- parse JCL syntax of dd.
+ */
+void
+jcl(char **argv)
+{
+ struct arg *ap, tmp;
+ char *oper, *arg;
+
+ in.dbsz = out.dbsz = 512;
+
+ while ((oper = *++argv) != NULL) {
+ if ((oper = strdup(oper)) == NULL) {
+ errx(EXIT_FAILURE,
+ "unable to allocate space for the argument %s",
+ *argv);
+ /* NOTREACHED */
+ }
+ if ((arg = strchr(oper, '=')) == NULL) {
+ errx(EXIT_FAILURE, "unknown operand %s", oper);
+ /* NOTREACHED */
+ }
+ *arg++ = '\0';
+ if (!*arg) {
+ errx(EXIT_FAILURE, "no value specified for %s", oper);
+ /* NOTREACHED */
+ }
+ tmp.name = oper;
+ if (!(ap = bsearch(&tmp, args,
+ __arraycount(args), sizeof(*args), c_arg))) {
+ errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
+ /* NOTREACHED */
+ }
+ if (ddflags & ap->noset) {
+ errx(EXIT_FAILURE,
+ "%s: illegal argument combination or already set",
+ tmp.name);
+ /* NOTREACHED */
+ }
+ ddflags |= ap->set;
+ ap->f(arg);
+ }
+
+ /* Final sanity checks. */
+
+ if (ddflags & C_BS) {
+ /*
+ * Bs is turned off by any conversion -- we assume the user
+ * just wanted to set both the input and output block sizes
+ * and didn't want the bs semantics, so we don't warn.
+ */
+ if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
+ C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
+ ddflags &= ~C_BS;
+ ddflags |= C_IBS|C_OBS;
+ }
+
+ /* Bs supersedes ibs and obs. */
+ if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
+ warnx("bs supersedes ibs and obs");
+ }
+
+ /*
+ * Ascii/ebcdic and cbs implies block/unblock.
+ * Block/unblock requires cbs and vice-versa.
+ */
+ if (ddflags & (C_BLOCK|C_UNBLOCK)) {
+ if (!(ddflags & C_CBS)) {
+ errx(EXIT_FAILURE, "record operations require cbs");
+ /* NOTREACHED */
+ }
+ cfunc = ddflags & C_BLOCK ? block : unblock;
+ } else if (ddflags & C_CBS) {
+ if (ddflags & (C_ASCII|C_EBCDIC)) {
+ if (ddflags & C_ASCII) {
+ ddflags |= C_UNBLOCK;
+ cfunc = unblock;
+ } else {
+ ddflags |= C_BLOCK;
+ cfunc = block;
+ }
+ } else {
+ errx(EXIT_FAILURE,
+ "cbs meaningless if not doing record operations");
+ /* NOTREACHED */
+ }
+ } else
+ cfunc = def;
+
+ /* Read, write and seek calls take off_t as arguments.
+ *
+ * The following check is not done because an off_t is a quad
+ * for current NetBSD implementations.
+ *
+ * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
+ * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
+ */
+}
+
+static int
+c_arg(const void *a, const void *b)
+{
+
+ return (strcmp(((const struct arg *)a)->name,
+ ((const struct arg *)b)->name));
+}
+
+static void
+f_bs(char *arg)
+{
+
+ in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
+}
+
+static void
+f_cbs(char *arg)
+{
+
+ cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
+}
+
+static void
+f_count(char *arg)
+{
+
+ cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
+ if (!cpy_cnt)
+ terminate(0);
+}
+
+static void
+f_files(char *arg)
+{
+
+ files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
+ if (!files_cnt)
+ terminate(0);
+}
+
+static void
+f_ibs(char *arg)
+{
+
+ if (!(ddflags & C_BS))
+ in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
+}
+
+static void
+f_if(char *arg)
+{
+
+ in.name = arg;
+}
+
+#ifdef NO_MSGFMT
+/* Build a small version (i.e. for a ramdisk root) */
+static void
+f_msgfmt(char *arg)
+{
+
+ errx(EXIT_FAILURE, "msgfmt option disabled");
+ /* NOTREACHED */
+}
+#else /* NO_MSGFMT */
+static void
+f_msgfmt(char *arg)
+{
+
+ /*
+ * If the format string is not valid, dd_write_msg() will print
+ * an error and exit.
+ */
+ dd_write_msg(arg, 0);
+
+ msgfmt = arg;
+}
+#endif /* NO_MSGFMT */
+
+static void
+f_obs(char *arg)
+{
+
+ if (!(ddflags & C_BS))
+ out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
+}
+
+static void
+f_of(char *arg)
+{
+
+ out.name = arg;
+}
+
+static void
+f_seek(char *arg)
+{
+
+ out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
+}
+
+static void
+f_skip(char *arg)
+{
+
+ in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
+}
+
+static void
+f_progress(char *arg)
+{
+
+ progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
+}
+
+#ifdef NO_CONV
+/* Build a small version (i.e. for a ramdisk root) */
+static void
+f_conv(char *arg)
+{
+
+ errx(EXIT_FAILURE, "conv option disabled");
+ /* NOTREACHED */
+}
+#else /* NO_CONV */
+
+static const struct conv {
+ const char *name;
+ u_int set, noset;
+ const u_char *ctab;
+} clist[] = {
+ { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
+ { "block", C_BLOCK, C_UNBLOCK, NULL },
+ { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
+ { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
+ { "lcase", C_LCASE, C_UCASE, NULL },
+ { "noerror", C_NOERROR, 0, NULL },
+ { "notrunc", C_NOTRUNC, 0, NULL },
+ { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
+ { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
+ { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
+ { "osync", C_OSYNC, C_BS, NULL },
+ { "sparse", C_SPARSE, 0, NULL },
+ { "swab", C_SWAB, 0, NULL },
+ { "sync", C_SYNC, 0, NULL },
+ { "ucase", C_UCASE, C_LCASE, NULL },
+ { "unblock", C_UNBLOCK, C_BLOCK, NULL },
+ /* If you add items to this table, be sure to add the
+ * conversions to the C_BS check in the jcl routine above.
+ */
+};
+
+static void
+f_conv(char *arg)
+{
+ struct conv *cp, tmp;
+
+ while (arg != NULL) {
+ tmp.name = strsep(&arg, ",");
+ if (!(cp = bsearch(&tmp, clist,
+ __arraycount(clist), sizeof(*clist), c_conv))) {
+ errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
+ /* NOTREACHED */
+ }
+ if (ddflags & cp->noset) {
+ errx(EXIT_FAILURE,
+ "%s: illegal conversion combination", tmp.name);
+ /* NOTREACHED */
+ }
+ ddflags |= cp->set;
+ if (cp->ctab)
+ ctab = cp->ctab;
+ }
+}
+
+static int
+c_conv(const void *a, const void *b)
+{
+
+ return (strcmp(((const struct conv *)a)->name,
+ ((const struct conv *)b)->name));
+}
+
+#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/conv.c b/toolbox/upstream-netbsd/bin/dd/conv.c
new file mode 100644
index 0000000..d4a8a09
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/conv.c
@@ -0,0 +1,283 @@
+/* $NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "dd.h"
+#include "extern.h"
+
+/*
+ * def --
+ * Copy input to output. Input is buffered until reaches obs, and then
+ * output until less than obs remains. Only a single buffer is used.
+ * Worst case buffer calculation is (ibs + obs - 1).
+ */
+void
+def(void)
+{
+ uint64_t cnt;
+ u_char *inp;
+ const u_char *t;
+
+ if ((t = ctab) != NULL)
+ for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
+ *inp = t[*inp];
+
+ /* Make the output buffer look right. */
+ out.dbp = in.dbp;
+ out.dbcnt = in.dbcnt;
+
+ if (in.dbcnt >= out.dbsz) {
+ /* If the output buffer is full, write it. */
+ dd_out(0);
+
+ /*
+ * Ddout copies the leftover output to the beginning of
+ * the buffer and resets the output buffer. Reset the
+ * input buffer to match it.
+ */
+ in.dbp = out.dbp;
+ in.dbcnt = out.dbcnt;
+ }
+}
+
+void
+def_close(void)
+{
+
+ /* Just update the count, everything is already in the buffer. */
+ if (in.dbcnt)
+ out.dbcnt = in.dbcnt;
+}
+
+#ifdef NO_CONV
+/* Build a smaller version (i.e. for a miniroot) */
+/* These can not be called, but just in case... */
+static const char no_block[] = "unblock and -DNO_CONV?";
+void block(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
+void block_close(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
+void unblock(void) { errx(EXIT_FAILURE, "%s", no_block); }
+void unblock_close(void) { errx(EXIT_FAILURE, "%s", no_block); }
+#else /* NO_CONV */
+
+/*
+ * Copy variable length newline terminated records with a max size cbsz
+ * bytes to output. Records less than cbs are padded with spaces.
+ *
+ * max in buffer: MAX(ibs, cbsz)
+ * max out buffer: obs + cbsz
+ */
+void
+block(void)
+{
+ static int intrunc;
+ int ch = 0; /* pacify gcc */
+ uint64_t cnt, maxlen;
+ u_char *inp, *outp;
+ const u_char *t;
+
+ /*
+ * Record truncation can cross block boundaries. If currently in a
+ * truncation state, keep tossing characters until reach a newline.
+ * Start at the beginning of the buffer, as the input buffer is always
+ * left empty.
+ */
+ if (intrunc) {
+ for (inp = in.db, cnt = in.dbrcnt;
+ cnt && *inp++ != '\n'; --cnt);
+ if (!cnt) {
+ in.dbcnt = 0;
+ in.dbp = in.db;
+ return;
+ }
+ intrunc = 0;
+ /* Adjust the input buffer numbers. */
+ in.dbcnt = cnt - 1;
+ in.dbp = inp + cnt - 1;
+ }
+
+ /*
+ * Copy records (max cbsz size chunks) into the output buffer. The
+ * translation is done as we copy into the output buffer.
+ */
+ for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
+ maxlen = MIN(cbsz, in.dbcnt);
+ if ((t = ctab) != NULL)
+ for (cnt = 0;
+ cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
+ *outp++ = t[ch];
+ else
+ for (cnt = 0;
+ cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
+ *outp++ = ch;
+ /*
+ * Check for short record without a newline. Reassemble the
+ * input block.
+ */
+ if (ch != '\n' && in.dbcnt < cbsz) {
+ (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
+ break;
+ }
+
+ /* Adjust the input buffer numbers. */
+ in.dbcnt -= cnt;
+ if (ch == '\n')
+ --in.dbcnt;
+
+ /* Pad short records with spaces. */
+ if (cnt < cbsz)
+ (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
+ else {
+ /*
+ * If the next character wouldn't have ended the
+ * block, it's a truncation.
+ */
+ if (!in.dbcnt || *inp != '\n')
+ ++st.trunc;
+
+ /* Toss characters to a newline. */
+ for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
+ if (!in.dbcnt)
+ intrunc = 1;
+ else
+ --in.dbcnt;
+ }
+
+ /* Adjust output buffer numbers. */
+ out.dbp += cbsz;
+ if ((out.dbcnt += cbsz) >= out.dbsz)
+ dd_out(0);
+ outp = out.dbp;
+ }
+ in.dbp = in.db + in.dbcnt;
+}
+
+void
+block_close(void)
+{
+
+ /*
+ * Copy any remaining data into the output buffer and pad to a record.
+ * Don't worry about truncation or translation, the input buffer is
+ * always empty when truncating, and no characters have been added for
+ * translation. The bottom line is that anything left in the input
+ * buffer is a truncated record. Anything left in the output buffer
+ * just wasn't big enough.
+ */
+ if (in.dbcnt) {
+ ++st.trunc;
+ (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
+ (void)memset(out.dbp + in.dbcnt,
+ ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
+ out.dbcnt += cbsz;
+ }
+}
+
+/*
+ * Convert fixed length (cbsz) records to variable length. Deletes any
+ * trailing blanks and appends a newline.
+ *
+ * max in buffer: MAX(ibs, cbsz) + cbsz
+ * max out buffer: obs + cbsz
+ */
+void
+unblock(void)
+{
+ uint64_t cnt;
+ u_char *inp;
+ const u_char *t;
+
+ /* Translation and case conversion. */
+ if ((t = ctab) != NULL)
+ for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
+ *inp = t[*inp];
+ /*
+ * Copy records (max cbsz size chunks) into the output buffer. The
+ * translation has to already be done or we might not recognize the
+ * spaces.
+ */
+ for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
+ for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
+ if (t >= inp) {
+ cnt = t - inp + 1;
+ (void)memmove(out.dbp, inp, cnt);
+ out.dbp += cnt;
+ out.dbcnt += cnt;
+ }
+ ++out.dbcnt;
+ *out.dbp++ = '\n';
+ if (out.dbcnt >= out.dbsz)
+ dd_out(0);
+ }
+ if (in.dbcnt)
+ (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
+ in.dbp = in.db + in.dbcnt;
+}
+
+void
+unblock_close(void)
+{
+ uint64_t cnt;
+ u_char *t;
+
+ if (in.dbcnt) {
+ warnx("%s: short input record", in.name);
+ for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
+ if (t >= in.db) {
+ cnt = t - in.db + 1;
+ (void)memmove(out.dbp, in.db, cnt);
+ out.dbp += cnt;
+ out.dbcnt += cnt;
+ }
+ ++out.dbcnt;
+ *out.dbp++ = '\n';
+ }
+}
+
+#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/dd.c b/toolbox/upstream-netbsd/bin/dd/dd.c
new file mode 100644
index 0000000..03d080c
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/dd.c
@@ -0,0 +1,598 @@
+/* $NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
+ The Regents of the University of California. All rights reserved.");
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/mtio.h>
+#include <sys/time.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "dd.h"
+#include "extern.h"
+
+static void dd_close(void);
+static void dd_in(void);
+static void getfdtype(IO *);
+static void redup_clean_fd(IO *);
+static void setup(void);
+
+int main(int, char *[]);
+
+IO in, out; /* input/output state */
+STAT st; /* statistics */
+void (*cfunc)(void); /* conversion function */
+uint64_t cpy_cnt; /* # of blocks to copy */
+static off_t pending = 0; /* pending seek if sparse */
+u_int ddflags; /* conversion options */
+uint64_t cbsz; /* conversion block size */
+u_int files_cnt = 1; /* # of files to copy */
+uint64_t progress = 0; /* display sign of life */
+const u_char *ctab; /* conversion table */
+sigset_t infoset; /* a set blocking SIGINFO */
+const char *msgfmt = "posix"; /* default summary() message format */
+
+/*
+ * Ops for stdin/stdout and crunch'd dd. These are always host ops.
+ */
+static const struct ddfops ddfops_stdfd = {
+ .op_open = open,
+ .op_close = close,
+ .op_fcntl = fcntl,
+ .op_ioctl = ioctl,
+ .op_fstat = fstat,
+ .op_fsync = fsync,
+ .op_ftruncate = ftruncate,
+ .op_lseek = lseek,
+ .op_read = read,
+ .op_write = write,
+};
+extern const struct ddfops ddfops_prog;
+
+int
+main(int argc, char *argv[])
+{
+ int ch;
+
+ setprogname(argv[0]);
+ (void)setlocale(LC_ALL, "");
+
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ errx(EXIT_FAILURE, "usage: dd [operand ...]");
+ /* NOTREACHED */
+ }
+ }
+ argc -= (optind - 1);
+ argv += (optind - 1);
+
+ jcl(argv);
+#ifndef CRUNCHOPS
+ if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
+ err(1, "prog init");
+#endif
+ setup();
+
+ (void)signal(SIGINFO, summaryx);
+ (void)signal(SIGINT, terminate);
+ (void)sigemptyset(&infoset);
+ (void)sigaddset(&infoset, SIGINFO);
+
+ (void)atexit(summary);
+
+ while (files_cnt--)
+ dd_in();
+
+ dd_close();
+ exit(0);
+ /* NOTREACHED */
+}
+
+static void
+setup(void)
+{
+#ifdef CRUNCHOPS
+ const struct ddfops *prog_ops = &ddfops_stdfd;
+#else
+ const struct ddfops *prog_ops = &ddfops_prog;
+#endif
+
+ if (in.name == NULL) {
+ in.name = "stdin";
+ in.fd = STDIN_FILENO;
+ in.ops = &ddfops_stdfd;
+ } else {
+ in.ops = prog_ops;
+ in.fd = ddop_open(in, in.name, O_RDONLY, 0);
+ if (in.fd < 0)
+ err(EXIT_FAILURE, "%s", in.name);
+ /* NOTREACHED */
+
+ /* Ensure in.fd is outside the stdio descriptor range */
+ redup_clean_fd(&in);
+ }
+
+ getfdtype(&in);
+
+ if (files_cnt > 1 && !(in.flags & ISTAPE)) {
+ errx(EXIT_FAILURE, "files is not supported for non-tape devices");
+ /* NOTREACHED */
+ }
+
+ if (out.name == NULL) {
+ /* No way to check for read access here. */
+ out.fd = STDOUT_FILENO;
+ out.name = "stdout";
+ out.ops = &ddfops_stdfd;
+ } else {
+ out.ops = prog_ops;
+#define OFLAGS \
+ (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
+ out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
+ /*
+ * May not have read access, so try again with write only.
+ * Without read we may have a problem if output also does
+ * not support seeks.
+ */
+ if (out.fd < 0) {
+ out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
+ DEFFILEMODE);
+ out.flags |= NOREAD;
+ }
+ if (out.fd < 0) {
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+ }
+
+ /* Ensure out.fd is outside the stdio descriptor range */
+ redup_clean_fd(&out);
+ }
+
+ getfdtype(&out);
+
+ /*
+ * Allocate space for the input and output buffers. If not doing
+ * record oriented I/O, only need a single buffer.
+ */
+ if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
+ size_t dbsz = out.dbsz;
+ if (!(ddflags & C_BS))
+ dbsz += in.dbsz - 1;
+ if ((in.db = malloc(dbsz)) == NULL) {
+ err(EXIT_FAILURE, NULL);
+ /* NOTREACHED */
+ }
+ out.db = in.db;
+ } else if ((in.db =
+ malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
+ (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
+ err(EXIT_FAILURE, NULL);
+ /* NOTREACHED */
+ }
+ in.dbp = in.db;
+ out.dbp = out.db;
+
+ /* Position the input/output streams. */
+ if (in.offset)
+ pos_in();
+ if (out.offset)
+ pos_out();
+
+ /*
+ * Truncate the output file; ignore errors because it fails on some
+ * kinds of output files, tapes, for example.
+ */
+ if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
+ (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
+
+ /*
+ * If converting case at the same time as another conversion, build a
+ * table that does both at once. If just converting case, use the
+ * built-in tables.
+ */
+ if (ddflags & (C_LCASE|C_UCASE)) {
+#ifdef NO_CONV
+ /* Should not get here, but just in case... */
+ errx(EXIT_FAILURE, "case conv and -DNO_CONV");
+ /* NOTREACHED */
+#else /* NO_CONV */
+ u_int cnt;
+
+ if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
+ if (ddflags & C_LCASE) {
+ for (cnt = 0; cnt < 256; ++cnt)
+ casetab[cnt] = tolower(ctab[cnt]);
+ } else {
+ for (cnt = 0; cnt < 256; ++cnt)
+ casetab[cnt] = toupper(ctab[cnt]);
+ }
+ } else {
+ if (ddflags & C_LCASE) {
+ for (cnt = 0; cnt < 256; ++cnt)
+ casetab[cnt] = tolower(cnt);
+ } else {
+ for (cnt = 0; cnt < 256; ++cnt)
+ casetab[cnt] = toupper(cnt);
+ }
+ }
+
+ ctab = casetab;
+#endif /* NO_CONV */
+ }
+
+ (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
+}
+
+static void
+getfdtype(IO *io)
+{
+ struct mtget mt;
+ struct stat sb;
+
+ if (io->ops->op_fstat(io->fd, &sb)) {
+ err(EXIT_FAILURE, "%s", io->name);
+ /* NOTREACHED */
+ }
+ if (S_ISCHR(sb.st_mode))
+ io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
+ ? ISCHR : ISTAPE;
+ else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
+ && errno == ESPIPE)
+ io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
+}
+
+/*
+ * Move the parameter file descriptor to a descriptor that is outside the
+ * stdio descriptor range, if necessary. This is required to avoid
+ * accidentally outputting completion or error messages into the
+ * output file that were intended for the tty.
+ */
+static void
+redup_clean_fd(IO *io)
+{
+ int fd = io->fd;
+ int newfd;
+
+ if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
+ fd != STDERR_FILENO)
+ /* File descriptor is ok, return immediately. */
+ return;
+
+ /*
+ * 3 is the first descriptor greater than STD*_FILENO. Any
+ * free descriptor valued 3 or above is acceptable...
+ */
+ newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
+ if (newfd < 0) {
+ err(EXIT_FAILURE, "dupfd IO");
+ /* NOTREACHED */
+ }
+
+ io->ops->op_close(fd);
+ io->fd = newfd;
+}
+
+static void
+dd_in(void)
+{
+ int flags;
+ int64_t n;
+
+ for (flags = ddflags;;) {
+ if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
+ return;
+
+ /*
+ * Clear the buffer first if doing "sync" on input.
+ * If doing block operations use spaces. This will
+ * affect not only the C_NOERROR case, but also the
+ * last partial input block which should be padded
+ * with zero and not garbage.
+ */
+ if (flags & C_SYNC) {
+ if (flags & (C_BLOCK|C_UNBLOCK))
+ (void)memset(in.dbp, ' ', in.dbsz);
+ else
+ (void)memset(in.dbp, 0, in.dbsz);
+ }
+
+ n = ddop_read(in, in.fd, in.dbp, in.dbsz);
+ if (n == 0) {
+ in.dbrcnt = 0;
+ return;
+ }
+
+ /* Read error. */
+ if (n < 0) {
+
+ /*
+ * If noerror not specified, die. POSIX requires that
+ * the warning message be followed by an I/O display.
+ */
+ if (!(flags & C_NOERROR)) {
+ err(EXIT_FAILURE, "%s", in.name);
+ /* NOTREACHED */
+ }
+ warn("%s", in.name);
+ summary();
+
+ /*
+ * If it's not a tape drive or a pipe, seek past the
+ * error. If your OS doesn't do the right thing for
+ * raw disks this section should be modified to re-read
+ * in sector size chunks.
+ */
+ if (!(in.flags & (ISPIPE|ISTAPE)) &&
+ ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
+ warn("%s", in.name);
+
+ /* If sync not specified, omit block and continue. */
+ if (!(ddflags & C_SYNC))
+ continue;
+
+ /* Read errors count as full blocks. */
+ in.dbcnt += in.dbrcnt = in.dbsz;
+ ++st.in_full;
+
+ /* Handle full input blocks. */
+ } else if ((uint64_t)n == in.dbsz) {
+ in.dbcnt += in.dbrcnt = n;
+ ++st.in_full;
+
+ /* Handle partial input blocks. */
+ } else {
+ /* If sync, use the entire block. */
+ if (ddflags & C_SYNC)
+ in.dbcnt += in.dbrcnt = in.dbsz;
+ else
+ in.dbcnt += in.dbrcnt = n;
+ ++st.in_part;
+ }
+
+ /*
+ * POSIX states that if bs is set and no other conversions
+ * than noerror, notrunc or sync are specified, the block
+ * is output without buffering as it is read.
+ */
+ if (ddflags & C_BS) {
+ out.dbcnt = in.dbcnt;
+ dd_out(1);
+ in.dbcnt = 0;
+ continue;
+ }
+
+ if (ddflags & C_SWAB) {
+ if ((n = in.dbrcnt) & 1) {
+ ++st.swab;
+ --n;
+ }
+ swab(in.dbp, in.dbp, n);
+ }
+
+ in.dbp += in.dbrcnt;
+ (*cfunc)();
+ }
+}
+
+/*
+ * Cleanup any remaining I/O and flush output. If necessary, output file
+ * is truncated.
+ */
+static void
+dd_close(void)
+{
+
+ if (cfunc == def)
+ def_close();
+ else if (cfunc == block)
+ block_close();
+ else if (cfunc == unblock)
+ unblock_close();
+ if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
+ (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
+ out.dbcnt = out.dbsz;
+ }
+ /* If there are pending sparse blocks, make sure
+ * to write out the final block un-sparse
+ */
+ if ((out.dbcnt == 0) && pending) {
+ memset(out.db, 0, out.dbsz);
+ out.dbcnt = out.dbsz;
+ out.dbp = out.db + out.dbcnt;
+ pending -= out.dbsz;
+ }
+ if (out.dbcnt)
+ dd_out(1);
+
+ /*
+ * Reporting nfs write error may be deferred until next
+ * write(2) or close(2) system call. So, we need to do an
+ * extra check. If an output is stdout, the file structure
+ * may be shared with other processes and close(2) just
+ * decreases the reference count.
+ */
+ if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
+ && errno != EINVAL) {
+ err(EXIT_FAILURE, "fsync stdout");
+ /* NOTREACHED */
+ }
+ if (ddop_close(out, out.fd) == -1) {
+ err(EXIT_FAILURE, "close");
+ /* NOTREACHED */
+ }
+}
+
+void
+dd_out(int force)
+{
+ static int warned;
+ int64_t cnt, n, nw;
+ u_char *outp;
+
+ /*
+ * Write one or more blocks out. The common case is writing a full
+ * output block in a single write; increment the full block stats.
+ * Otherwise, we're into partial block writes. If a partial write,
+ * and it's a character device, just warn. If a tape device, quit.
+ *
+ * The partial writes represent two cases. 1: Where the input block
+ * was less than expected so the output block was less than expected.
+ * 2: Where the input block was the right size but we were forced to
+ * write the block in multiple chunks. The original versions of dd(1)
+ * never wrote a block in more than a single write, so the latter case
+ * never happened.
+ *
+ * One special case is if we're forced to do the write -- in that case
+ * we play games with the buffer size, and it's usually a partial write.
+ */
+ outp = out.db;
+ for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
+ for (cnt = n;; cnt -= nw) {
+
+ if (!force && ddflags & C_SPARSE) {
+ int sparse, i;
+ sparse = 1; /* Is buffer sparse? */
+ for (i = 0; i < cnt; i++)
+ if (outp[i] != 0) {
+ sparse = 0;
+ break;
+ }
+ if (sparse) {
+ pending += cnt;
+ outp += cnt;
+ nw = 0;
+ break;
+ }
+ }
+ if (pending != 0) {
+ if (ddop_lseek(out,
+ out.fd, pending, SEEK_CUR) == -1)
+ err(EXIT_FAILURE, "%s: seek error creating sparse file",
+ out.name);
+ }
+ nw = bwrite(&out, outp, cnt);
+ if (nw <= 0) {
+ if (nw == 0)
+ errx(EXIT_FAILURE,
+ "%s: end of device", out.name);
+ /* NOTREACHED */
+ if (errno != EINTR)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+ nw = 0;
+ }
+ if (pending) {
+ st.bytes += pending;
+ st.sparse += pending/out.dbsz;
+ st.out_full += pending/out.dbsz;
+ pending = 0;
+ }
+ outp += nw;
+ st.bytes += nw;
+ if (nw == n) {
+ if ((uint64_t)n != out.dbsz)
+ ++st.out_part;
+ else
+ ++st.out_full;
+ break;
+ }
+ ++st.out_part;
+ if (nw == cnt)
+ break;
+ if (out.flags & ISCHR && !warned) {
+ warned = 1;
+ warnx("%s: short write on character device", out.name);
+ }
+ if (out.flags & ISTAPE)
+ errx(EXIT_FAILURE,
+ "%s: short write on tape device", out.name);
+ /* NOTREACHED */
+
+ }
+ if ((out.dbcnt -= n) < out.dbsz)
+ break;
+ }
+
+ /* Reassemble the output block. */
+ if (out.dbcnt)
+ (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
+ out.dbp = out.db + out.dbcnt;
+
+ if (progress && (st.out_full + st.out_part) % progress == 0)
+ (void)write(STDERR_FILENO, ".", 1);
+}
+
+/*
+ * A protected against SIGINFO write
+ */
+ssize_t
+bwrite(IO *io, const void *buf, size_t len)
+{
+ sigset_t oset;
+ ssize_t rv;
+ int oerrno;
+
+ (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
+ rv = io->ops->op_write(io->fd, buf, len);
+ oerrno = errno;
+ (void)sigprocmask(SIG_SETMASK, &oset, NULL);
+ errno = oerrno;
+ return (rv);
+}
diff --git a/toolbox/dd.h b/toolbox/upstream-netbsd/bin/dd/dd.h
similarity index 74%
rename from toolbox/dd.h
rename to toolbox/upstream-netbsd/bin/dd/dd.h
index 89f2833..b01c7b3 100644
--- a/toolbox/dd.h
+++ b/toolbox/upstream-netbsd/bin/dd/dd.h
@@ -1,4 +1,4 @@
-/* $NetBSD: dd.h,v 1.12 2004/01/17 20:48:57 dbj Exp $ */
+/* $NetBSD: dd.h,v 1.15 2011/02/04 19:42:12 pooka Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
@@ -35,7 +35,40 @@
* @(#)dd.h 8.3 (Berkeley) 4/2/94
*/
-#include <stdint.h>
+#include <sys/stat.h>
+
+struct ddfops {
+ int (*op_init)(void);
+
+ int (*op_open)(const char *, int, ...);
+ int (*op_close)(int);
+
+ int (*op_fcntl)(int, int, ...);
+#ifdef __ANDROID__
+ int (*op_ioctl)(int, int, ...);
+#else
+ int (*op_ioctl)(int, unsigned long, ...);
+#endif
+
+ int (*op_fstat)(int, struct stat *);
+ int (*op_fsync)(int);
+ int (*op_ftruncate)(int, off_t);
+
+ off_t (*op_lseek)(int, off_t, int);
+
+ ssize_t (*op_read)(int, void *, size_t);
+ ssize_t (*op_write)(int, const void *, size_t);
+};
+
+#define ddop_open(dir, a1, a2, ...) dir.ops->op_open(a1, a2, __VA_ARGS__)
+#define ddop_close(dir, a1) dir.ops->op_close(a1)
+#define ddop_fcntl(dir, a1, a2, ...) dir.ops->op_fcntl(a1, a2, __VA_ARGS__)
+#define ddop_ioctl(dir, a1, a2, ...) dir.ops->op_ioctl(a1, a2, __VA_ARGS__)
+#define ddop_fsync(dir, a1) dir.ops->op_fsync(a1)
+#define ddop_ftruncate(dir, a1, a2) dir.ops->op_ftruncate(a1, a2)
+#define ddop_lseek(dir, a1, a2, a3) dir.ops->op_lseek(a1, a2, a3)
+#define ddop_read(dir, a1, a2, a3) dir.ops->op_read(a1, a2, a3)
+#define ddop_write(dir, a1, a2, a3) dir.ops->op_write(a1, a2, a3)
/* Input/output stream state. */
typedef struct {
@@ -54,6 +87,7 @@
const char *name; /* name */
int fd; /* file descriptor */
uint64_t offset; /* # of blocks to skip */
+ struct ddfops const *ops; /* ops to use with fd */
} IO;
typedef struct {
@@ -91,4 +125,3 @@
#define C_UNBLOCK 0x80000
#define C_OSYNC 0x100000
#define C_SPARSE 0x200000
-#define C_FDATASYNC 0x400000
diff --git a/toolbox/upstream-netbsd/bin/dd/dd_hostops.c b/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
new file mode 100644
index 0000000..d6e7a89
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
@@ -0,0 +1,53 @@
+/* $NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $ */
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $");
+#endif /* !lint */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "dd.h"
+
+const struct ddfops ddfops_prog = {
+ .op_open = open,
+ .op_close = close,
+ .op_fcntl = fcntl,
+ .op_ioctl = ioctl,
+ .op_fstat = fstat,
+ .op_fsync = fsync,
+ .op_ftruncate = ftruncate,
+ .op_lseek = lseek,
+ .op_read = read,
+ .op_write = write,
+};
diff --git a/toolbox/upstream-netbsd/bin/dd/extern.h b/toolbox/upstream-netbsd/bin/dd/extern.h
new file mode 100644
index 0000000..9c59021
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/extern.h
@@ -0,0 +1,82 @@
+/* $NetBSD: extern.h,v 1.22 2011/11/07 22:24:23 jym Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)extern.h 8.3 (Berkeley) 4/2/94
+ */
+
+#include <sys/cdefs.h>
+
+#ifdef NO_CONV
+__dead void block(void);
+__dead void block_close(void);
+__dead void unblock(void);
+__dead void unblock_close(void);
+#else
+void block(void);
+void block_close(void);
+void unblock(void);
+void unblock_close(void);
+#endif
+
+#ifndef NO_MSGFMT
+int dd_write_msg(const char *, int);
+#endif
+
+void dd_out(int);
+void def(void);
+void def_close(void);
+void jcl(char **);
+void pos_in(void);
+void pos_out(void);
+void summary(void);
+void summaryx(int);
+__dead void terminate(int);
+void unblock(void);
+void unblock_close(void);
+ssize_t bwrite(IO *, const void *, size_t);
+
+extern IO in, out;
+extern STAT st;
+extern void (*cfunc)(void);
+extern uint64_t cpy_cnt;
+extern uint64_t cbsz;
+extern u_int ddflags;
+extern u_int files_cnt;
+extern uint64_t progress;
+extern const u_char *ctab;
+extern const u_char a2e_32V[], a2e_POSIX[];
+extern const u_char e2a_32V[], e2a_POSIX[];
+extern const u_char a2ibm_32V[], a2ibm_POSIX[];
+extern u_char casetab[];
+extern const char *msgfmt;
diff --git a/toolbox/upstream-netbsd/bin/dd/misc.c b/toolbox/upstream-netbsd/bin/dd/misc.c
new file mode 100644
index 0000000..0fac98b
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/misc.c
@@ -0,0 +1,342 @@
+/* $NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <util.h>
+#include <inttypes.h>
+
+#include "dd.h"
+#include "extern.h"
+
+#define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
+
+static void posix_summary(void);
+#ifndef NO_MSGFMT
+static void custom_summary(void);
+static void human_summary(void);
+static void quiet_summary(void);
+
+static void buffer_write(const char *, size_t, int);
+#endif /* NO_MSGFMT */
+
+void
+summary(void)
+{
+
+ if (progress)
+ (void)write(STDERR_FILENO, "\n", 1);
+
+#ifdef NO_MSGFMT
+ return posix_summary();
+#else /* NO_MSGFMT */
+ if (strncmp(msgfmt, "human", sizeof("human")) == 0)
+ return human_summary();
+
+ if (strncmp(msgfmt, "posix", sizeof("posix")) == 0)
+ return posix_summary();
+
+ if (strncmp(msgfmt, "quiet", sizeof("quiet")) == 0)
+ return quiet_summary();
+
+ return custom_summary();
+#endif /* NO_MSGFMT */
+}
+
+static void
+posix_summary(void)
+{
+ char buf[100];
+ int64_t mS;
+ struct timeval tv;
+
+ if (progress)
+ (void)write(STDERR_FILENO, "\n", 1);
+
+ (void)gettimeofday(&tv, NULL);
+ mS = tv2mS(tv) - tv2mS(st.start);
+ if (mS == 0)
+ mS = 1;
+
+ /* Use snprintf(3) so that we don't reenter stdio(3). */
+ (void)snprintf(buf, sizeof(buf),
+ "%llu+%llu records in\n%llu+%llu records out\n",
+ (unsigned long long)st.in_full, (unsigned long long)st.in_part,
+ (unsigned long long)st.out_full, (unsigned long long)st.out_part);
+ (void)write(STDERR_FILENO, buf, strlen(buf));
+ if (st.swab) {
+ (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
+ (unsigned long long)st.swab,
+ (st.swab == 1) ? "block" : "blocks");
+ (void)write(STDERR_FILENO, buf, strlen(buf));
+ }
+ if (st.trunc) {
+ (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
+ (unsigned long long)st.trunc,
+ (st.trunc == 1) ? "block" : "blocks");
+ (void)write(STDERR_FILENO, buf, strlen(buf));
+ }
+ if (st.sparse) {
+ (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
+ (unsigned long long)st.sparse,
+ (st.sparse == 1) ? "block" : "blocks");
+ (void)write(STDERR_FILENO, buf, strlen(buf));
+ }
+ (void)snprintf(buf, sizeof(buf),
+ "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
+ (unsigned long long) st.bytes,
+ (long) (mS / 1000),
+ (int) (mS % 1000),
+ (unsigned long long) (st.bytes * 1000LL / mS));
+ (void)write(STDERR_FILENO, buf, strlen(buf));
+}
+
+/* ARGSUSED */
+void
+summaryx(int notused)
+{
+
+ summary();
+}
+
+/* ARGSUSED */
+void
+terminate(int signo)
+{
+
+ summary();
+ (void)raise_default_signal(signo);
+ _exit(127);
+}
+
+#ifndef NO_MSGFMT
+/*
+ * Buffer write(2) calls
+ */
+static void
+buffer_write(const char *str, size_t size, int flush)
+{
+ static char wbuf[128];
+ static size_t cnt = 0; /* Internal counter to allow wbuf to wrap */
+
+ unsigned int i;
+
+ for (i = 0; i < size; i++) {
+ if (str != NULL) {
+ wbuf[cnt++] = str[i];
+ }
+ if (cnt >= sizeof(wbuf)) {
+ (void)write(STDERR_FILENO, wbuf, cnt);
+ cnt = 0;
+ }
+ }
+
+ if (flush != 0) {
+ (void)write(STDERR_FILENO, wbuf, cnt);
+ cnt = 0;
+ }
+}
+
+/*
+ * Write summary to stderr according to format 'fmt'. If 'enable' is 0, it
+ * will not attempt to write anything. Can be used to validate the
+ * correctness of the 'fmt' string.
+ */
+int
+dd_write_msg(const char *fmt, int enable)
+{
+ char hbuf[7], nbuf[32];
+ const char *ptr;
+ int64_t mS;
+ struct timeval tv;
+
+ (void)gettimeofday(&tv, NULL);
+ mS = tv2mS(tv) - tv2mS(st.start);
+ if (mS == 0)
+ mS = 1;
+
+#define ADDC(c) do { if (enable != 0) buffer_write(&c, 1, 0); } \
+ while (/*CONSTCOND*/0)
+#define ADDS(p) do { if (enable != 0) buffer_write(p, strlen(p), 0); } \
+ while (/*CONSTCOND*/0)
+
+ for (ptr = fmt; *ptr; ptr++) {
+ if (*ptr != '%') {
+ ADDC(*ptr);
+ continue;
+ }
+
+ switch (*++ptr) {
+ case 'b':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.bytes);
+ ADDS(nbuf);
+ break;
+ case 'B':
+ if (humanize_number(hbuf, sizeof(hbuf),
+ st.bytes, "B",
+ HN_AUTOSCALE, HN_DECIMAL) == -1)
+ warnx("humanize_number (bytes transferred)");
+ ADDS(hbuf);
+ break;
+ case 'e':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long) (st.bytes * 1000LL / mS));
+ ADDS(nbuf);
+ break;
+ case 'E':
+ if (humanize_number(hbuf, sizeof(hbuf),
+ st.bytes * 1000LL / mS, "B",
+ HN_AUTOSCALE, HN_DECIMAL) == -1)
+ warnx("humanize_number (bytes per second)");
+ ADDS(hbuf); ADDS("/sec");
+ break;
+ case 'i':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.in_part);
+ ADDS(nbuf);
+ break;
+ case 'I':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.in_full);
+ ADDS(nbuf);
+ break;
+ case 'o':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.out_part);
+ ADDS(nbuf);
+ break;
+ case 'O':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.out_full);
+ ADDS(nbuf);
+ break;
+ case 's':
+ (void)snprintf(nbuf, sizeof(nbuf), "%li.%03d",
+ (long) (mS / 1000), (int) (mS % 1000));
+ ADDS(nbuf);
+ break;
+ case 'p':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.sparse);
+ ADDS(nbuf);
+ break;
+ case 't':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.trunc);
+ ADDS(nbuf);
+ break;
+ case 'w':
+ (void)snprintf(nbuf, sizeof(nbuf), "%llu",
+ (unsigned long long)st.swab);
+ ADDS(nbuf);
+ break;
+ case 'P':
+ ADDS("block");
+ if (st.sparse != 1) ADDS("s");
+ break;
+ case 'T':
+ ADDS("block");
+ if (st.trunc != 1) ADDS("s");
+ break;
+ case 'W':
+ ADDS("block");
+ if (st.swab != 1) ADDS("s");
+ break;
+ case '%':
+ ADDC(*ptr);
+ break;
+ default:
+ if (*ptr == '\0')
+ goto done;
+ errx(EXIT_FAILURE, "unknown specifier '%c' in "
+ "msgfmt string", *ptr);
+ /* NOTREACHED */
+ }
+ }
+
+done:
+ /* flush buffer */
+ buffer_write(NULL, 0, 1);
+ return 0;
+}
+
+static void
+custom_summary(void)
+{
+
+ dd_write_msg(msgfmt, 1);
+}
+
+static void
+human_summary(void)
+{
+ (void)dd_write_msg("%I+%i records in\n%O+%o records out\n", 1);
+ if (st.swab) {
+ (void)dd_write_msg("%w odd length swab %W\n", 1);
+ }
+ if (st.trunc) {
+ (void)dd_write_msg("%t truncated %T\n", 1);
+ }
+ if (st.sparse) {
+ (void)dd_write_msg("%p sparse output %P\n", 1);
+ }
+ (void)dd_write_msg("%b bytes (%B) transferred in %s secs "
+ "(%e bytes/sec - %E)\n", 1);
+}
+
+static void
+quiet_summary(void)
+{
+
+ /* stay quiet */
+}
+#endif /* NO_MSGFMT */
diff --git a/toolbox/upstream-netbsd/bin/dd/position.c b/toolbox/upstream-netbsd/bin/dd/position.c
new file mode 100644
index 0000000..36dd580
--- /dev/null
+++ b/toolbox/upstream-netbsd/bin/dd/position.c
@@ -0,0 +1,185 @@
+/* $NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/mtio.h>
+#include <sys/time.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "dd.h"
+#include "extern.h"
+
+/*
+ * Position input/output data streams before starting the copy. Device type
+ * dependent. Seekable devices use lseek, and the rest position by reading.
+ * Seeking past the end of file can cause null blocks to be written to the
+ * output.
+ */
+void
+pos_in(void)
+{
+ int bcnt, cnt, nr, warned;
+
+ /* If not a pipe or tape device, try to seek on it. */
+ if (!(in.flags & (ISPIPE|ISTAPE))) {
+ if (ddop_lseek(in, in.fd,
+ (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
+ err(EXIT_FAILURE, "%s", in.name);
+ /* NOTREACHED */
+ }
+ return;
+ /* NOTREACHED */
+ }
+
+ /*
+ * Read the data. If a pipe, read until satisfy the number of bytes
+ * being skipped. No differentiation for reading complete and partial
+ * blocks for other devices.
+ */
+ for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
+ if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) {
+ if (in.flags & ISPIPE) {
+ if (!(bcnt -= nr)) {
+ bcnt = in.dbsz;
+ --cnt;
+ }
+ } else
+ --cnt;
+ continue;
+ }
+
+ if (nr == 0) {
+ if (files_cnt > 1) {
+ --files_cnt;
+ continue;
+ }
+ errx(EXIT_FAILURE, "skip reached end of input");
+ /* NOTREACHED */
+ }
+
+ /*
+ * Input error -- either EOF with no more files, or I/O error.
+ * If noerror not set die. POSIX requires that the warning
+ * message be followed by an I/O display.
+ */
+ if (ddflags & C_NOERROR) {
+ if (!warned) {
+
+ warn("%s", in.name);
+ warned = 1;
+ summary();
+ }
+ continue;
+ }
+ err(EXIT_FAILURE, "%s", in.name);
+ /* NOTREACHED */
+ }
+}
+
+void
+pos_out(void)
+{
+ struct mtop t_op;
+ int n;
+ uint64_t cnt;
+
+ /*
+ * If not a tape, try seeking on the file. Seeking on a pipe is
+ * going to fail, but don't protect the user -- they shouldn't
+ * have specified the seek operand.
+ */
+ if (!(out.flags & ISTAPE)) {
+ if (ddop_lseek(out, out.fd,
+ (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+ return;
+ }
+
+ /* If no read access, try using mtio. */
+ if (out.flags & NOREAD) {
+ t_op.mt_op = MTFSR;
+ t_op.mt_count = out.offset;
+
+ if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+ return;
+ }
+
+ /* Read it. */
+ for (cnt = 0; cnt < out.offset; ++cnt) {
+ if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0)
+ continue;
+
+ if (n < 0)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+
+ /*
+ * If reach EOF, fill with NUL characters; first, back up over
+ * the EOF mark. Note, cnt has not yet been incremented, so
+ * the EOF read does not count as a seek'd block.
+ */
+ t_op.mt_op = MTBSR;
+ t_op.mt_count = 1;
+ if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+
+ while (cnt++ < out.offset)
+ if ((uint64_t)(n = bwrite(&out,
+ out.db, out.dbsz)) != out.dbsz)
+ err(EXIT_FAILURE, "%s", out.name);
+ /* NOTREACHED */
+ break;
+ }
+}
diff --git a/libcorkscrew/MODULE_LICENSE_APACHE2 b/toolbox/upstream-netbsd/include/namespace.h
similarity index 100%
rename from libcorkscrew/MODULE_LICENSE_APACHE2
rename to toolbox/upstream-netbsd/include/namespace.h
diff --git a/libcorkscrew/MODULE_LICENSE_APACHE2 b/toolbox/upstream-netbsd/include/sys/extattr.h
similarity index 100%
copy from libcorkscrew/MODULE_LICENSE_APACHE2
copy to toolbox/upstream-netbsd/include/sys/extattr.h
diff --git a/toolbox/upstream-netbsd/include/sys/mtio.h b/toolbox/upstream-netbsd/include/sys/mtio.h
new file mode 100644
index 0000000..8fb5655
--- /dev/null
+++ b/toolbox/upstream-netbsd/include/sys/mtio.h
@@ -0,0 +1 @@
+#include <linux/mtio.h>
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c b/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
new file mode 100644
index 0000000..a9ce2c1
--- /dev/null
+++ b/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
@@ -0,0 +1,118 @@
+/* $NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $ */
+
+/*-
+ * Copyright (c) 1991, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $");
+#endif
+#endif /* not lint */
+
+#include "namespace.h"
+
+#include <assert.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __weak_alias
+__weak_alias(getbsize,_getbsize)
+#endif
+
+char *
+getbsize(int *headerlenp, long *blocksizep)
+{
+ static char header[20];
+ long n, max, mul, blocksize;
+ char *ep, *p;
+ const char *form;
+
+#define KB (1024L)
+#define MB (1024L * 1024L)
+#define GB (1024L * 1024L * 1024L)
+#define MAXB GB /* No tera, peta, nor exa. */
+ form = "";
+ if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
+ if ((n = strtol(p, &ep, 10)) < 0)
+ goto underflow;
+ if (n == 0)
+ n = 1;
+ if (*ep && ep[1])
+ goto fmterr;
+ switch (*ep) {
+ case 'G': case 'g':
+ form = "G";
+ max = MAXB / GB;
+ mul = GB;
+ break;
+ case 'K': case 'k':
+ form = "K";
+ max = MAXB / KB;
+ mul = KB;
+ break;
+ case 'M': case 'm':
+ form = "M";
+ max = MAXB / MB;
+ mul = MB;
+ break;
+ case '\0':
+ max = MAXB;
+ mul = 1;
+ break;
+ default:
+fmterr: warnx("%s: unknown blocksize", p);
+ n = 512;
+ mul = 1;
+ max = 0;
+ break;
+ }
+ if (n > max) {
+ warnx("maximum blocksize is %ldG", MAXB / GB);
+ n = max;
+ }
+ if ((blocksize = n * mul) < 512) {
+underflow: warnx("%s: minimum blocksize is 512", p);
+ form = "";
+ blocksize = n = 512;
+ }
+ } else
+ blocksize = n = 512;
+
+ if (headerlenp)
+ *headerlenp =
+ snprintf(header, sizeof(header), "%ld%s-blocks", n, form);
+ if (blocksizep)
+ *blocksizep = blocksize;
+ return (header);
+}
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c b/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
new file mode 100644
index 0000000..533560f
--- /dev/null
+++ b/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
@@ -0,0 +1,161 @@
+/* $NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $ */
+
+/*
+ * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#include "namespace.h"
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <locale.h>
+
+int
+humanize_number(char *buf, size_t len, int64_t bytes,
+ const char *suffix, int scale, int flags)
+{
+ const char *prefixes, *sep;
+ int b, r, s1, s2, sign;
+ int64_t divisor, max, post = 1;
+ size_t i, baselen, maxscale;
+
+ _DIAGASSERT(buf != NULL);
+ _DIAGASSERT(suffix != NULL);
+ _DIAGASSERT(scale >= 0);
+
+ if (flags & HN_DIVISOR_1000) {
+ /* SI for decimal multiplies */
+ divisor = 1000;
+ if (flags & HN_B)
+ prefixes = "B\0k\0M\0G\0T\0P\0E";
+ else
+ prefixes = "\0\0k\0M\0G\0T\0P\0E";
+ } else {
+ /*
+ * binary multiplies
+ * XXX IEC 60027-2 recommends Ki, Mi, Gi...
+ */
+ divisor = 1024;
+ if (flags & HN_B)
+ prefixes = "B\0K\0M\0G\0T\0P\0E";
+ else
+ prefixes = "\0\0K\0M\0G\0T\0P\0E";
+ }
+
+#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
+ maxscale = 7;
+
+ if ((size_t)scale >= maxscale &&
+ (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
+ return (-1);
+
+ if (buf == NULL || suffix == NULL)
+ return (-1);
+
+ if (len > 0)
+ buf[0] = '\0';
+ if (bytes < 0) {
+ sign = -1;
+ baselen = 3; /* sign, digit, prefix */
+ if (-bytes < INT64_MAX / 100)
+ bytes *= -100;
+ else {
+ bytes = -bytes;
+ post = 100;
+ baselen += 2;
+ }
+ } else {
+ sign = 1;
+ baselen = 2; /* digit, prefix */
+ if (bytes < INT64_MAX / 100)
+ bytes *= 100;
+ else {
+ post = 100;
+ baselen += 2;
+ }
+ }
+ if (flags & HN_NOSPACE)
+ sep = "";
+ else {
+ sep = " ";
+ baselen++;
+ }
+ baselen += strlen(suffix);
+
+ /* Check if enough room for `x y' + suffix + `\0' */
+ if (len < baselen + 1)
+ return (-1);
+
+ if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
+ /* See if there is additional columns can be used. */
+ for (max = 100, i = len - baselen; i-- > 0;)
+ max *= 10;
+
+ /*
+ * Divide the number until it fits the given column.
+ * If there will be an overflow by the rounding below,
+ * divide once more.
+ */
+ for (i = 0; bytes >= max - 50 && i < maxscale; i++)
+ bytes /= divisor;
+
+ if (scale & HN_GETSCALE) {
+ _DIAGASSERT(__type_fit(int, i));
+ return (int)i;
+ }
+ } else
+ for (i = 0; i < (size_t)scale && i < maxscale; i++)
+ bytes /= divisor;
+ bytes *= post;
+
+ /* If a value <= 9.9 after rounding and ... */
+ if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
+ /* baselen + \0 + .N */
+ if (len < baselen + 1 + 2)
+ return (-1);
+ b = ((int)bytes + 5) / 10;
+ s1 = b / 10;
+ s2 = b % 10;
+ r = snprintf(buf, len, "%d%s%d%s%s%s",
+ sign * s1, localeconv()->decimal_point, s2,
+ sep, SCALE2PREFIX(i), suffix);
+ } else
+ r = snprintf(buf, len, "%" PRId64 "%s%s%s",
+ sign * ((bytes + 50) / 100),
+ sep, SCALE2PREFIX(i), suffix);
+
+ return (r);
+}
diff --git a/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c b/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
new file mode 100644
index 0000000..80fc52f
--- /dev/null
+++ b/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
@@ -0,0 +1,249 @@
+/* $NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $ */
+/*-
+ * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#ifdef _LIBC
+#include "namespace.h"
+#endif
+
+#if !HAVE_STRSUFTOLL
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef _LIBC
+# ifdef __weak_alias
+__weak_alias(strsuftoll, _strsuftoll)
+__weak_alias(strsuftollx, _strsuftollx)
+# endif
+#endif /* LIBC */
+
+/*
+ * Convert an expression of the following forms to a (u)int64_t.
+ * 1) A positive decimal number.
+ * 2) A positive decimal number followed by a b (mult by 512).
+ * 3) A positive decimal number followed by a k (mult by 1024).
+ * 4) A positive decimal number followed by a m (mult by 1048576).
+ * 5) A positive decimal number followed by a g (mult by 1073741824).
+ * 6) A positive decimal number followed by a t (mult by 1099511627776).
+ * 7) A positive decimal number followed by a w (mult by sizeof int)
+ * 8) Two or more positive decimal numbers (with/without k,b or w).
+ * separated by x (also * for backwards compatibility), specifying
+ * the product of the indicated values.
+ * Returns the result upon successful conversion, or exits with an
+ * appropriate error.
+ *
+ */
+/* LONGLONG */
+long long
+strsuftoll(const char *desc, const char *val,
+ long long min, long long max)
+{
+ long long result;
+ char errbuf[100];
+
+ result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
+ if (*errbuf != '\0')
+ errx(EXIT_FAILURE, "%s", errbuf);
+ return result;
+}
+
+/*
+ * As strsuftoll(), but returns the error message into the provided buffer
+ * rather than exiting with it.
+ */
+/* LONGLONG */
+static long long
+__strsuftollx(const char *desc, const char *val,
+ long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
+{
+ long long num, t;
+ char *expr;
+
+ _DIAGASSERT(desc != NULL);
+ _DIAGASSERT(val != NULL);
+ _DIAGASSERT(ebuf != NULL);
+
+ if (depth > 16) {
+ snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
+ return 0;
+ }
+
+ while (isspace((unsigned char)*val)) /* Skip leading space */
+ val++;
+
+ errno = 0;
+ num = strtoll(val, &expr, 10);
+ if (errno == ERANGE)
+ goto erange; /* Overflow */
+
+ if (expr == val) /* No digits */
+ goto badnum;
+
+ switch (*expr) {
+ case 'b':
+ t = num;
+ num *= 512; /* 1 block */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ case 'k':
+ t = num;
+ num *= 1024; /* 1 kibibyte */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ case 'm':
+ t = num;
+ num *= 1048576; /* 1 mebibyte */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ case 'g':
+ t = num;
+ num *= 1073741824; /* 1 gibibyte */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ case 't':
+ t = num;
+ num *= 1099511627776LL; /* 1 tebibyte */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ case 'w':
+ t = num;
+ num *= sizeof(int); /* 1 word */
+ if (t > num)
+ goto erange;
+ ++expr;
+ break;
+ }
+
+ switch (*expr) {
+ case '\0':
+ break;
+ case '*': /* Backward compatible */
+ case 'x':
+ t = num;
+ num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
+ depth + 1);
+ if (*ebuf != '\0')
+ return 0;
+ if (t > num) {
+ erange:
+ errno = ERANGE;
+ snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
+ return 0;
+ }
+ break;
+ default:
+ badnum:
+ snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
+ return 0;
+ }
+ if (num < min) {
+ /* LONGLONG */
+ snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
+ desc, (long long)num, (long long)min);
+ return 0;
+ }
+ if (num > max) {
+ /* LONGLONG */
+ snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
+ desc, (long long)num, (long long)max);
+ return 0;
+ }
+ *ebuf = '\0';
+ return num;
+}
+
+long long
+strsuftollx(const char *desc, const char *val,
+ long long min, long long max, char *ebuf, size_t ebuflen)
+{
+ return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
+}
+#endif /* !HAVE_STRSUFTOLL */
diff --git a/toolbox/cp/extern.h b/toolbox/upstream-netbsd/lib/libc/string/swab.c
similarity index 61%
rename from toolbox/cp/extern.h
rename to toolbox/upstream-netbsd/lib/libc/string/swab.c
index ffbadf7..392b186 100644
--- a/toolbox/cp/extern.h
+++ b/toolbox/upstream-netbsd/lib/libc/string/swab.c
@@ -1,9 +1,12 @@
-/* $NetBSD: extern.h,v 1.17 2012/01/04 15:58:37 christos Exp $ */
+/* $NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $ */
-/*-
- * Copyright (c) 1991, 1993, 1994
+/*
+ * Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
+ * This code is derived from software contributed to Berkeley by
+ * Jeffrey Mogul.
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -27,35 +30,51 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * @(#)extern.h 8.2 (Berkeley) 4/1/94
*/
-#ifndef _EXTERN_H_
-#define _EXTERN_H_
-
-typedef struct {
- char *p_end; /* pointer to NULL at end of path */
- char *target_end; /* pointer to end of target base */
- char p_path[MAXPATHLEN + 1]; /* pointer to the start of a path */
-} PATH_T;
-
-extern PATH_T to;
-extern uid_t myuid;
-extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, pflag, Nflag;
-extern mode_t myumask;
-extern sig_atomic_t pinfo;
-
#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)swab.c 8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
-__BEGIN_DECLS
-int copy_fifo(struct stat *, int);
-int copy_file(FTSENT *, int);
-int copy_link(FTSENT *, int);
-int copy_special(struct stat *, int);
-int set_utimes(const char *, struct stat *);
-int setfile(struct stat *, int);
-void cp_usage(void) __attribute__((__noreturn__));
-__END_DECLS
+#include <assert.h>
+#include <unistd.h>
-#endif /* !_EXTERN_H_ */
+void
+swab(const void * __restrict from, void * __restrict to, ssize_t len)
+{
+ char temp;
+ const char *fp;
+ char *tp;
+
+ if (len <= 1)
+ return;
+
+ _DIAGASSERT(from != NULL);
+ _DIAGASSERT(to != NULL);
+
+ len /= 2;
+ fp = (const char *)from;
+ tp = (char *)to;
+#define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
+
+ if (__predict_false(len == 1)) {
+ STEP;
+ return;
+ }
+
+ /* round to multiple of 8 */
+ while ((--len % 8) != 0)
+ STEP;
+ len /= 8;
+ if (len == 0)
+ return;
+ while (len-- != 0) {
+ STEP; STEP; STEP; STEP;
+ STEP; STEP; STEP; STEP;
+ }
+}
diff --git a/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c b/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
new file mode 100644
index 0000000..50cffd4
--- /dev/null
+++ b/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
@@ -0,0 +1,117 @@
+/* $NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $ */
+
+/*-
+ * Copyright (c) 2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $");
+#endif
+
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <util.h>
+
+#if ! HAVE_RAISE_DEFAULT_SIGNAL
+/*
+ * raise_default_signal sig
+ * Raise the default signal handler for sig, by
+ * - block all signals
+ * - set the signal handler to SIG_DFL
+ * - raise the signal
+ * - unblock the signal to deliver it
+ *
+ * The original signal mask and signal handler is restored on exit
+ * (whether successful or not).
+ *
+ * Returns 0 on success, or -1 on failure with errno set to
+ * on of the values for sigemptyset(), sigaddset(), sigprocmask(),
+ * sigaction(), or raise().
+ */
+int
+raise_default_signal(int sig)
+{
+ struct sigaction origact, act;
+ sigset_t origmask, fullmask, mask;
+ int retval, oerrno;
+
+ retval = -1;
+
+ /* Setup data structures */
+ /* XXX memset(3) isn't async-safe according to signal(7) */
+ (void)memset(&act, 0, sizeof(act));
+ act.sa_handler = SIG_DFL;
+ act.sa_flags = 0;
+ if ((sigemptyset(&act.sa_mask) == -1) ||
+ (sigfillset(&fullmask) == -1) ||
+ (sigemptyset(&mask) == -1) ||
+ (sigaddset(&mask, sig) == -1))
+ goto restore_none;
+
+ /* Block all signals */
+ if (sigprocmask(SIG_BLOCK, &fullmask, &origmask) == -1)
+ goto restore_none;
+ /* (use 'goto restore_mask' to restore state) */
+
+ /* Enable the SIG_DFL handler */
+ if (sigaction(sig, &act, &origact) == -1)
+ goto restore_mask;
+ /* (use 'goto restore_act' to restore state) */
+
+ /* Raise the signal, and unblock the signal to deliver it */
+ if ((raise(sig) == -1) ||
+ (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1))
+ goto restore_act;
+
+ /* Flag successful raise() */
+ retval = 0;
+
+ /* Restore the original handler */
+ restore_act:
+ oerrno = errno;
+ (void)sigaction(sig, &origact, NULL);
+ errno = oerrno;
+
+ /* Restore the original mask */
+ restore_mask:
+ oerrno = errno;
+ (void)sigprocmask(SIG_SETMASK, &origmask, NULL);
+ errno = oerrno;
+
+ restore_none:
+ return retval;
+}
+
+#endif /* ! HAVE_RAISE_DEFAULT_SIGNAL */
diff --git a/toolbox/du.c b/toolbox/upstream-netbsd/usr.bin/du/du.c
similarity index 81%
rename from toolbox/du.c
rename to toolbox/upstream-netbsd/usr.bin/du/du.c
index fc7c943..086ac4a 100644
--- a/toolbox/du.c
+++ b/toolbox/upstream-netbsd/usr.bin/du/du.c
@@ -1,4 +1,4 @@
-/* $NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $ */
+/* $NetBSD: du.c,v 1.36 2012/03/11 11:23:20 shattered Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@@ -42,10 +42,11 @@
#if 0
static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
#else
-__RCSID("$NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $");
+__RCSID("$NetBSD: du.c,v 1.36 2012/03/11 11:23:20 shattered Exp $");
#endif
#endif /* not lint */
+#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -53,6 +54,7 @@
#include <err.h>
#include <errno.h>
#include <fts.h>
+#include <inttypes.h>
#include <util.h>
#include <stdio.h>
#include <stdlib.h>
@@ -60,16 +62,18 @@
#include <unistd.h>
#include <limits.h>
-int linkchk(dev_t, ino_t);
-void prstat(const char *, int64_t);
-static void usage(void);
+/* Count inodes or file size */
+#define COUNT (iflag ? 1 : p->fts_statp->st_blocks)
-long blocksize;
+static int linkchk(dev_t, ino_t);
+static void prstat(const char *, int64_t);
+__dead static void usage(void);
-#define howmany(x, y) (((x)+((y)-1))/(y))
+static int hflag, iflag;
+static long blocksize;
int
-du_main(int argc, char *argv[])
+main(int argc, char *argv[])
{
FTS *fts;
FTSENT *p;
@@ -79,11 +83,11 @@
int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag;
const char *noargv[2];
- Hflag = Lflag = aflag = cflag = dflag = gkmflag = sflag = 0;
+ Hflag = Lflag = aflag = cflag = dflag = gkmflag = nflag = sflag = 0;
totalblocks = 0;
ftsoptions = FTS_PHYSICAL;
depth = INT_MAX;
- while ((ch = getopt(argc, argv, "HLPacd:ghkmnrsx")) != -1)
+ while ((ch = getopt(argc, argv, "HLPacd:ghikmnrsx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@@ -115,6 +119,12 @@
blocksize = 1024 * 1024 * 1024;
gkmflag = 1;
break;
+ case 'h':
+ hflag = 1;
+ break;
+ case 'i':
+ iflag = 1;
+ break;
case 'k':
blocksize = 1024;
gkmflag = 1;
@@ -123,6 +133,9 @@
blocksize = 1024 * 1024;
gkmflag = 1;
break;
+ case 'n':
+ nflag = 1;
+ break;
case 'r':
break;
case 's':
@@ -175,21 +188,36 @@
}
if (!gkmflag)
- blocksize = 512;
+ (void)getbsize(NULL, &blocksize);
blocksize /= 512;
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
err(1, "fts_open `%s'", *argv);
for (rval = 0; (p = fts_read(fts)) != NULL;) {
+#ifndef __ANDROID__
+ if (nflag) {
+ switch (p->fts_info) {
+ case FTS_NS:
+ case FTS_SLNONE:
+ /* nothing */
+ break;
+ default:
+ if (p->fts_statp->st_flags & UF_NODUMP) {
+ fts_set(fts, p, FTS_SKIP);
+ continue;
+ }
+ }
+ }
+#endif
switch (p->fts_info) {
case FTS_D: /* Ignore. */
break;
case FTS_DP:
p->fts_parent->fts_number +=
- p->fts_number += p->fts_statp->st_blocks;
+ p->fts_number += COUNT;
if (cflag)
- totalblocks += p->fts_statp->st_blocks;
+ totalblocks += COUNT;
/*
* If listing each directory, or not listing files
* or directories and this is post-order of the
@@ -216,10 +244,10 @@
* the root of a traversal, display the total.
*/
if (listfiles || !p->fts_level)
- prstat(p->fts_path, p->fts_statp->st_blocks);
- p->fts_parent->fts_number += p->fts_statp->st_blocks;
+ prstat(p->fts_path, COUNT);
+ p->fts_parent->fts_number += COUNT;
if (cflag)
- totalblocks += p->fts_statp->st_blocks;
+ totalblocks += COUNT;
}
}
if (errno)
@@ -229,15 +257,29 @@
exit(rval);
}
-void
+static void
prstat(const char *fname, int64_t blocks)
{
- (void)printf("%lld\t%s\n",
- (long long)howmany(blocks, (int64_t)blocksize),
- fname);
+ if (iflag) {
+ (void)printf("%" PRId64 "\t%s\n", blocks, fname);
+ return;
+ }
+
+ if (hflag) {
+ char buf[5];
+ int64_t sz = blocks * 512;
+
+ humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
+ HN_B | HN_NOSPACE | HN_DECIMAL);
+
+ (void)printf("%s\t%s\n", buf, fname);
+ } else
+ (void)printf("%" PRId64 "\t%s\n",
+ howmany(blocks, (int64_t)blocksize),
+ fname);
}
-int
+static int
linkchk(dev_t dev, ino_t ino)
{
static struct entry {
@@ -317,6 +359,6 @@
{
(void)fprintf(stderr,
- "usage: du [-H | -L | -P] [-a | -d depth | -s] [-cgkmrx] [file ...]\n");
+ "usage: du [-H | -L | -P] [-a | -d depth | -s] [-cghikmnrx] [file ...]\n");
exit(1);
}
diff --git a/toolbox/grep/fastgrep.c b/toolbox/upstream-netbsd/usr.bin/grep/fastgrep.c
similarity index 100%
rename from toolbox/grep/fastgrep.c
rename to toolbox/upstream-netbsd/usr.bin/grep/fastgrep.c
diff --git a/toolbox/grep/file.c b/toolbox/upstream-netbsd/usr.bin/grep/file.c
similarity index 97%
rename from toolbox/grep/file.c
rename to toolbox/upstream-netbsd/usr.bin/grep/file.c
index 86b7658..cf4a0fa 100644
--- a/toolbox/grep/file.c
+++ b/toolbox/upstream-netbsd/usr.bin/grep/file.c
@@ -41,7 +41,7 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifndef ANDROID
+#ifndef __ANDROID__
#include <bzlib.h>
#endif
#include <err.h>
@@ -53,7 +53,7 @@
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
-#ifndef ANDROID
+#ifndef __ANDROID__
#include <zlib.h>
#endif
@@ -62,7 +62,7 @@
#define MAXBUFSIZ (32 * 1024)
#define LNBUFBUMP 80
-#ifndef ANDROID
+#ifndef __ANDROID__
static gzFile gzbufdesc;
static BZFILE* bzbufdesc;
#endif
@@ -78,12 +78,14 @@
grep_refill(struct file *f)
{
ssize_t nr;
+#ifndef __ANDROID__
int bzerr;
+#endif
bufpos = buffer;
bufrem = 0;
-#ifndef ANDROID
+#ifndef __ANDROID__
if (filebehave == FILE_GZIP)
nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
@@ -202,7 +204,7 @@
grep_file_init(struct file *f)
{
-#ifndef ANDROID
+#ifndef __ANDROID__
if (filebehave == FILE_GZIP &&
(gzbufdesc = gzdopen(f->fd, "r")) == NULL)
goto error;
diff --git a/toolbox/grep/grep.c b/toolbox/upstream-netbsd/usr.bin/grep/grep.c
similarity index 98%
rename from toolbox/grep/grep.c
rename to toolbox/upstream-netbsd/usr.bin/grep/grep.c
index b5bb2ef..1ea6ed3 100644
--- a/toolbox/grep/grep.c
+++ b/toolbox/upstream-netbsd/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $NetBSD: grep.c,v 1.11 2012/05/06 22:27:00 joerg Exp $ */
+/* $NetBSD: grep.c,v 1.12 2014/07/11 16:30:45 christos Exp $ */
/* $FreeBSD: head/usr.bin/grep/grep.c 211519 2010-08-19 22:55:17Z delphij $ */
/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
@@ -34,7 +34,7 @@
#endif
#include <sys/cdefs.h>
-__RCSID("$NetBSD: grep.c,v 1.11 2012/05/06 22:27:00 joerg Exp $");
+__RCSID("$NetBSD: grep.c,v 1.12 2014/07/11 16:30:45 christos Exp $");
#include <sys/stat.h>
#include <sys/types.h>
@@ -159,7 +159,6 @@
{
fprintf(stderr, getstr(4), __progname);
fprintf(stderr, "%s", getstr(5));
- fprintf(stderr, "%s", getstr(5));
fprintf(stderr, "%s", getstr(6));
fprintf(stderr, "%s", getstr(7));
exit(2);
@@ -294,10 +293,8 @@
err(2, "%s", fn);
line = NULL;
len = 0;
-#ifndef ANDROID
while ((rlen = getline(&line, &len, f)) != -1)
add_pattern(line, *line == '\n' ? 0 : (size_t)rlen);
-#endif
free(line);
if (ferror(f))
err(2, "%s", fn);
@@ -314,7 +311,7 @@
}
int
-grep_main(int argc, char *argv[])
+main(int argc, char *argv[])
{
char **aargv, **eargv, *eopts;
char *ep;
diff --git a/toolbox/grep/grep.h b/toolbox/upstream-netbsd/usr.bin/grep/grep.h
similarity index 97%
rename from toolbox/grep/grep.h
rename to toolbox/upstream-netbsd/usr.bin/grep/grep.h
index 6454f93..fa2a3e3 100644
--- a/toolbox/grep/grep.h
+++ b/toolbox/upstream-netbsd/usr.bin/grep/grep.h
@@ -29,18 +29,14 @@
* SUCH DAMAGE.
*/
-#ifdef ANDROID
-#define WITHOUT_NLS
-#endif
-
-#ifndef ANDROID
+#ifndef __ANDROID__
#include <bzlib.h>
#endif
#include <limits.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
-#ifndef ANDROID
+#ifndef __ANDROID__
#include <zlib.h>
#endif
diff --git a/toolbox/grep/queue.c b/toolbox/upstream-netbsd/usr.bin/grep/queue.c
similarity index 100%
rename from toolbox/grep/queue.c
rename to toolbox/upstream-netbsd/usr.bin/grep/queue.c
diff --git a/toolbox/grep/util.c b/toolbox/upstream-netbsd/usr.bin/grep/util.c
similarity index 96%
rename from toolbox/grep/util.c
rename to toolbox/upstream-netbsd/usr.bin/grep/util.c
index 497db06..ecd948d 100644
--- a/toolbox/grep/util.c
+++ b/toolbox/upstream-netbsd/usr.bin/grep/util.c
@@ -1,4 +1,4 @@
-/* $NetBSD: util.c,v 1.16 2012/05/06 22:32:05 joerg Exp $ */
+/* $NetBSD: util.c,v 1.17 2013/01/21 03:24:43 msaitoh Exp $ */
/* $FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $ */
/* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
@@ -34,7 +34,7 @@
#endif
#include <sys/cdefs.h>
-__RCSID("$NetBSD: util.c,v 1.16 2012/05/06 22:32:05 joerg Exp $");
+__RCSID("$NetBSD: util.c,v 1.17 2013/01/21 03:24:43 msaitoh Exp $");
#include <sys/stat.h>
#include <sys/types.h>
@@ -74,9 +74,10 @@
for (i = 0; i < fpatterns; ++i) {
if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
- if (fpattern[i].mode == EXCL_PAT)
+ if (fpattern[i].mode == EXCL_PAT) {
+ free(fname_copy);
return (false);
- else
+ } else
ret = true;
}
}
@@ -128,7 +129,7 @@
break;
default:
fts_flags = FTS_LOGICAL;
-
+
}
fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
@@ -323,7 +324,7 @@
continue;
/* Check for whole word match */
if (fg_pattern[i].word && pmatch.rm_so != 0) {
- wint_t wbegin, wend;
+ wchar_t wbegin, wend;
wbegin = wend = L' ';
if (pmatch.rm_so != 0 &&
@@ -474,13 +475,13 @@
if (!oflag)
fwrite(line->dat + a, matches[i].rm_so - a, 1,
stdout);
- if (color)
+ if (color)
fprintf(stdout, "\33[%sm\33[K", color);
- fwrite(line->dat + matches[i].rm_so,
+ fwrite(line->dat + matches[i].rm_so,
matches[i].rm_eo - matches[i].rm_so, 1,
stdout);
- if (color)
+ if (color)
fprintf(stdout, "\33[m\33[K");
a = matches[i].rm_eo;
if (oflag)
diff --git a/toolbox/uptime.c b/toolbox/uptime.c
index 3fb4606..2dd8084 100644
--- a/toolbox/uptime.c
+++ b/toolbox/uptime.c
@@ -36,7 +36,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
-
+#include <unistd.h>
static void format_time(int time, char* buffer) {
int seconds, minutes, hours, days;
diff --git a/toolbox/vmstat.c b/toolbox/vmstat.c
deleted file mode 100644
index 4086ed0..0000000
--- a/toolbox/vmstat.c
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- * Copyright (c) 2008, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google, Inc. nor the names of its contributors
- * may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/param.h>
-#include <unistd.h>
-
-struct state {
- long procs_r;
- long procs_b;
-
- long mem_free;
- long mem_mapped;
- long mem_anon;
- long mem_slab;
-
- long sys_in;
- long sys_cs;
- long sys_flt;
-
- long cpu_us;
- long cpu_ni;
- long cpu_sy;
- long cpu_id;
- long cpu_wa;
- long cpu_ir;
- long cpu_si;
-};
-
-#define MAX_LINE 256
-
-char line[MAX_LINE];
-
-static void read_state(struct state *s);
-static int read_meminfo(struct state *s);
-static int read_stat(struct state *s);
-static int read_vmstat(struct state *s);
-static void print_header(void);
-static void print_line(struct state *old, struct state *new);
-static void usage(char *cmd);
-
-int vmstat_main(int argc, char *argv[]) {
- struct state s[2];
- int iterations, delay, header_interval;
- int toggle, count;
- int i;
-
- iterations = -1;
- delay = 1;
- header_interval = 20;
-
- for (i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "-n")) {
- if (i >= argc - 1) {
- fprintf(stderr, "Option -n requires an argument.\n");
- exit(EXIT_FAILURE);
- }
- iterations = atoi(argv[++i]);
- continue;
- }
- if (!strcmp(argv[i], "-d")) {
- if (i >= argc - 1) {
- fprintf(stderr, "Option -d requires an argument.\n");
- exit(EXIT_FAILURE);
- }
- delay = atoi(argv[++i]);
- continue;
- }
- if (!strcmp(argv[i], "-r")) {
- if (i >= argc - 1) {
- fprintf(stderr, "Option -r requires an argument.\n");
- exit(EXIT_FAILURE);
- }
- header_interval = atoi(argv[++i]);
- continue;
- }
- if (!strcmp(argv[i], "-h")) {
- usage(argv[0]);
- exit(EXIT_SUCCESS);
- }
- fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
- usage(argv[0]);
- exit(EXIT_FAILURE);
- }
-
- toggle = 0;
- count = 0;
-
- if (!header_interval)
- print_header();
- read_state(&s[1 - toggle]);
- while ((iterations < 0) || (iterations-- > 0)) {
- sleep(delay);
- read_state(&s[toggle]);
- if (header_interval) {
- if (count == 0)
- print_header();
- count = (count + 1) % header_interval;
- }
- print_line(&s[1 - toggle], &s[toggle]);
- toggle = 1 - toggle;
- }
-
- return 0;
-}
-
-static void read_state(struct state *s) {
- int error;
-
- error = read_meminfo(s);
- if (error) {
- fprintf(stderr, "vmstat: could not read /proc/meminfo: %s\n", strerror(error));
- exit(EXIT_FAILURE);
- }
-
- error = read_stat(s);
- if (error) {
- fprintf(stderr, "vmstat: could not read /proc/stat: %s\n", strerror(error));
- exit(EXIT_FAILURE);
- }
-
- error = read_vmstat(s);
- if (error) {
- fprintf(stderr, "vmstat: could not read /proc/vmstat: %s\n", strerror(error));
- exit(EXIT_FAILURE);
- }
-}
-
-static int read_meminfo(struct state *s) {
- FILE *f;
-
- f = fopen("/proc/meminfo", "r");
- if (!f) return errno;
-
- while (fgets(line, MAX_LINE, f)) {
- sscanf(line, "MemFree: %ld kB", &s->mem_free);
- sscanf(line, "AnonPages: %ld kB", &s->mem_anon);
- sscanf(line, "Mapped: %ld kB", &s->mem_mapped);
- sscanf(line, "Slab: %ld kB", &s->mem_slab);
- }
-
- fclose(f);
-
- return 0;
-}
-
-static int read_stat(struct state *s) {
- FILE *f;
-
- f = fopen("/proc/stat", "r");
- if (!f) return errno;
-
- while (fgets(line, MAX_LINE, f)) {
- if (!strncmp(line, "cpu ", 4)) {
- sscanf(line, "cpu %ld %ld %ld %ld %ld %ld %ld",
- &s->cpu_us, &s->cpu_ni, &s->cpu_sy, &s->cpu_id, &s->cpu_wa,
- &s->cpu_ir, &s->cpu_si);
- }
- sscanf(line, "intr %ld", &s->sys_in);
- sscanf(line, "ctxt %ld", &s->sys_cs);
- sscanf(line, "procs_running %ld", &s->procs_r);
- sscanf(line, "procs_blocked %ld", &s->procs_b);
- }
-
- fclose(f);
-
- return 0;
-}
-
-static int read_vmstat(struct state *s) {
- FILE *f;
-
- f = fopen("/proc/vmstat", "r");
- if (!f) return errno;
-
- while (fgets(line, MAX_LINE, f)) {
- sscanf(line, "pgmajfault %ld", &s->sys_flt);
- }
-
- fclose(f);
-
- return 0;
-}
-
-static void print_header(void) {
- printf("%-5s %-27s %-14s %-17s\n", "procs", "memory", "system", "cpu");
- printf("%2s %2s %6s %6s %6s %6s %4s %4s %4s %2s %2s %2s %2s %2s %2s\n", "r", "b", "free", "mapped", "anon", "slab", "in", "cs", "flt", "us", "ni", "sy", "id", "wa", "ir");
-}
-
-/* Jiffies to percent conversion */
-#define JP(jif) ((jif) * 100 / (HZ))
-#define NORM(var) ((var) = (((var) > 99) ? (99) : (var)))
-
-static void print_line(struct state *old, struct state *new) {
- int us, ni, sy, id, wa, ir;
- us = JP(new->cpu_us - old->cpu_us); NORM(us);
- ni = JP(new->cpu_ni - old->cpu_ni); NORM(ni);
- sy = JP(new->cpu_sy - old->cpu_sy); NORM(sy);
- id = JP(new->cpu_id - old->cpu_id); NORM(id);
- wa = JP(new->cpu_wa - old->cpu_wa); NORM(wa);
- ir = JP(new->cpu_ir - old->cpu_ir); NORM(ir);
- printf("%2ld %2ld %6ld %6ld %6ld %6ld %4ld %4ld %4ld %2d %2d %2d %2d %2d %2d\n",
- new->procs_r ? (new->procs_r - 1) : 0, new->procs_b,
- new->mem_free, new->mem_mapped, new->mem_anon, new->mem_slab,
- new->sys_in - old->sys_in, new->sys_cs - old->sys_cs, new->sys_flt - old->sys_flt,
- us, ni, sy, id, wa, ir);
-}
-
-static void usage(char *cmd) {
- fprintf(stderr, "Usage: %s [ -h ] [ -n iterations ] [ -d delay ] [ -r header_repeat ]\n"
- " -n iterations How many rows of data to print.\n"
- " -d delay How long to sleep between rows.\n"
- " -r header_repeat How many rows to print before repeating\n"
- " the header. Zero means never repeat.\n"
- " -h Displays this help screen.\n",
- cmd);
-}
diff --git a/toolbox/watchprops.c b/toolbox/watchprops.c
index bf82882..cd62922 100644
--- a/toolbox/watchprops.c
+++ b/toolbox/watchprops.c
@@ -1,13 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <time.h>
#include <errno.h>
#include <cutils/properties.h>
#include <cutils/hashmap.h>
-#include <sys/atomics.h>
-
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
@@ -23,9 +22,9 @@
static void announce(char *name, char *value)
{
- char *x;
+ unsigned char *x;
- for(x = value; *x; x++) {
+ for(x = (unsigned char *)value; *x; x++) {
if((*x < 32) || (*x > 127)) *x = '.';
}
@@ -77,9 +76,7 @@
int watchprops_main(int argc, char *argv[])
{
- unsigned serial = 0;
- unsigned count = 0;
- unsigned n;
+ unsigned serial;
Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
if (!watchlist)
@@ -87,7 +84,7 @@
__system_property_foreach(populate_watchlist, watchlist);
- for(;;) {
+ for(serial = 0;;) {
serial = __system_property_wait_any(serial);
__system_property_foreach(update_watchlist, watchlist);
}
diff --git a/toolbox/wipe.c b/toolbox/wipe.c
deleted file mode 100644
index 650a0d6..0000000
--- a/toolbox/wipe.c
+++ /dev/null
@@ -1,176 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <dirent.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <cutils/android_reboot.h>
-#include <sys/stat.h>
-
-#ifndef PATH_MAX
-#define PATH_MAX 4096
-#endif
-
-
-/* Directories created by init defined in system/rootdir/init.rc */
-static char *INIT_DIRS[] = {
- "/system/etc/ppp",
- "/data/misc",
- "/data/local",
- "/data/local/tmp",
- "/data/data",
- "/data/app_private",
- "/data/app",
- NULL
-};
-
-static void wipe (const char *path);
-
-static int usage()
-{
- fprintf(stderr, "wipe <system|data|all>\n\n"
- "system means '/system'\n"
- "data means '/data'\n");
-
- return -1;
-}
-
-int wipe_main (int argc, char *argv[])
-{
- char *whatToWipe;
-
- if (argc != 2) return usage();
-
- whatToWipe = argv[1];
-
- if (0 == strcmp (whatToWipe, "system")) {
- fprintf(stdout, "Wiping /system\n");
- wipe ("/system");
- fprintf(stdout, "Done wiping /android\n");
- } else if (0 == strcmp (whatToWipe, "data")) {
- fprintf(stdout, "Wiping /data\n");
- wipe ("/data");
- fprintf(stdout, "Done wiping /data\n");
- } else if (0 == strcmp (whatToWipe, "all")) {
- fprintf(stdout, "Wiping /system and /data\n");
- wipe ("/system");
- wipe ("/data");
- fprintf(stdout, "Done wiping /system and /data\n");
- } else if (0 == strcmp(whatToWipe, "nuke")) {
- int ret;
- fprintf(stdout, "Nuking the device...\n");
- wipe ("/system");
- wipe ("/data");
- fprintf(stdout, "Device nuked! Rebooting...\n");
- ret = android_reboot(ANDROID_RB_RESTART, 0, 0);
- if (ret < 0) {
- fprintf(stderr, "Reboot failed, %s\n", strerror(errno));
- return 1;
- }
- } else {
- return usage();
- }
-
- return 0;
-}
-
-static char nameBuffer[PATH_MAX];
-static struct stat statBuffer;
-
-static void wipe (const char *path)
-{
- DIR *dir;
- struct dirent *de;
- int ret;
-
- dir = opendir(path);
-
- if (dir == NULL) {
- fprintf (stderr, "Error opendir'ing %s '%s'\n",
- path, strerror(errno));
- return;
- }
-
- char *filenameOffset;
-
- strcpy(nameBuffer, path);
- strcat(nameBuffer, "/");
-
- filenameOffset = nameBuffer + strlen(nameBuffer);
-
- for (;;) {
- de = readdir(dir);
-
- if (de == NULL) {
- break;
- }
-
- if (0 == strcmp(de->d_name, ".")
- || 0 == strcmp(de->d_name, "..")
- || 0 == strcmp(de->d_name, "lost+found")
- ) {
- continue;
- }
-
- strcpy(filenameOffset, de->d_name);
-
- ret = lstat (nameBuffer, &statBuffer);
-
- if (ret != 0) {
- fprintf(stderr, "stat() error on '%s' '%s'\n",
- nameBuffer, strerror(errno));
- }
-
- if(S_ISDIR(statBuffer.st_mode)) {
- int i;
- char *newpath;
-
-#if 0
- closedir(dir);
-#endif
-
- newpath = strdup(nameBuffer);
- wipe(newpath);
-
- /* Leave directories created by init, they have special permissions. */
- for (i = 0; INIT_DIRS[i]; i++) {
- if (strcmp(INIT_DIRS[i], newpath) == 0) {
- break;
- }
- }
- if (INIT_DIRS[i] == NULL) {
- ret = rmdir(newpath);
- if (ret != 0) {
- fprintf(stderr, "rmdir() error on '%s' '%s'\n",
- newpath, strerror(errno));
- }
- }
-
- free(newpath);
-
-#if 0
- dir = opendir(path);
- if (dir == NULL) {
- fprintf (stderr, "Error opendir'ing %s '%s'\n",
- path, strerror(errno));
- return;
- }
-#endif
-
- strcpy(nameBuffer, path);
- strcat(nameBuffer, "/");
-
- } else {
- ret = unlink(nameBuffer);
-
- if (ret != 0) {
- fprintf(stderr, "unlink() error on '%s' '%s'\n",
- nameBuffer, strerror(errno));
- }
- }
- }
-
- closedir(dir);
-
-}