Merge "graphics: add android raw12 format"
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 7f9536e..b3661e4 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -55,3 +55,4 @@
$(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)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/bin/grep $(PRODUCT_OUT)/system/bin/toolbox)
diff --git a/adb/Android.mk b/adb/Android.mk
index 8d38077..4f19d47 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -208,10 +208,11 @@
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_C_INCLUDES += system/extras/ext4_utils
LOCAL_STATIC_LIBRARIES := \
libadbd \
+ libbase \
libfs_mgr \
liblog \
libcutils \
diff --git a/adb/adb.cpp b/adb/adb.cpp
index d37ca36..ad85184f 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -79,18 +79,19 @@
#if !ADB_HOST
void start_device_log(void) {
- adb_mkdir("/data/adb", 0775);
-
struct tm now;
time_t t;
tzset();
time(&t);
localtime_r(&t, &now);
- char path[PATH_MAX];
- strftime(path, sizeof(path), "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", &now);
+ char timestamp[PATH_MAX];
+ strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
- int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+ char path[PATH_MAX];
+ snprintf(path, sizeof(path), "/data/adb/adb-%s-%d", timestamp, getpid());
+
+ int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
if (fd == -1) {
return;
}
@@ -100,10 +101,6 @@
dup2(fd, STDERR_FILENO);
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
diff --git a/adb/adb_main.cpp b/adb/adb_main.cpp
index c1e4b13..fb17e89 100644
--- a/adb/adb_main.cpp
+++ b/adb/adb_main.cpp
@@ -110,6 +110,13 @@
#if defined(ALLOW_ADBD_ROOT)
char value[PROPERTY_VALUE_MAX];
+ // The emulator is never secure, so don't drop privileges there.
+ // TODO: this seems like a bug --- shouldn't the emulator behave like a device?
+ property_get("ro.kernel.qemu", value, "");
+ if (strcmp(value, "1") == 0) {
+ return false;
+ }
+
// 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.
@@ -349,9 +356,23 @@
return 0;
}
+#if !ADB_HOST
+void close_stdin() {
+ int fd = unix_open("/dev/null", O_RDONLY);
+ if (fd == -1) {
+ perror("failed to open /dev/null, stdin will remain open");
+ return;
+ }
+ dup2(fd, 0);
+ adb_close(fd);
+}
+#endif
+
int main(int argc, char **argv) {
#if ADB_HOST
adb_sysdeps_init();
+#else
+ close_stdin();
#endif
adb_trace_init();
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index c9b1eab..f9ca5ed 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -209,7 +209,11 @@
" adb get-devpath - prints: <device-path>\n"
" adb status-window - continuously print device status for a specified device\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|recovery]\n"
+ " - reboots the device, optionally into the bootloader or recovery program.\n"
+ " adb reboot sideload - reboots the device into the sideload mode in recovery program (adb root required).\n"
+ " adb reboot sideload-auto-reboot\n"
+ " - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.\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"
@@ -245,13 +249,10 @@
#if defined(_WIN32)
-// Windows does not have <termio.h>.
-static void stdin_raw_init(int fd) {
-
-}
-
-static void stdin_raw_restore(int fd) {
-
+// Implemented in sysdeps_win32.c.
+extern "C" {
+void stdin_raw_init(int fd);
+void stdin_raw_restore(int fd);
}
#else
@@ -1129,6 +1130,17 @@
}
}
+static int adb_connect_command(const char* command) {
+ int fd = adb_connect(command);
+ if (fd != -1) {
+ read_and_dump(fd);
+ adb_close(fd);
+ return 0;
+ }
+ fprintf(stderr, "Error: %s\n", adb_error());
+ return 1;
+}
+
int adb_commandline(int argc, const char **argv)
{
char buf[4096];
@@ -1478,20 +1490,14 @@
!strcmp(argv[0], "disable-verity") ||
!strcmp(argv[0], "enable-verity")) {
char command[100];
- if (!strcmp(argv[0], "reboot-bootloader"))
+ if (!strcmp(argv[0], "reboot-bootloader")) {
snprintf(command, sizeof(command), "reboot:bootloader");
- else if (argc > 1)
+ } else if (argc > 1) {
snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
- else
+ } 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;
+ return adb_connect_command(command);
}
else if (!strcmp(argv[0], "bugreport")) {
if (argc != 1) return usage();
@@ -1716,15 +1722,7 @@
return adb_auth_keygen(argv[1]);
}
else if (!strcmp(argv[0], "jdwp")) {
- int fd = adb_connect("jdwp");
- if (fd >= 0) {
- read_and_dump(fd);
- adb_close(fd);
- return 0;
- } else {
- fprintf(stderr, "error: %s\n", adb_error());
- return -1;
- }
+ return adb_connect_command("jdwp");
}
/* "adb /?" is a common idiom under Windows */
else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
diff --git a/adb/services.cpp b/adb/services.cpp
index abf8ea5..12eb406 100644
--- a/adb/services.cpp
+++ b/adb/services.cpp
@@ -32,6 +32,7 @@
#endif
#if !ADB_HOST
+#include "base/file.h"
#include "cutils/android_reboot.h"
#include "cutils/properties.h"
#endif
@@ -132,31 +133,72 @@
adb_close(fd);
}
-void reboot_service(int fd, void *arg)
-{
+static bool reboot_service_impl(int fd, const char* arg) {
+ const char* reboot_arg = arg;
+ bool auto_reboot = false;
+
+ if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
+ auto_reboot = true;
+ reboot_arg = "sideload";
+ }
+
char buf[100];
- char property_val[PROPERTY_VALUE_MAX];
- int ret;
+ // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
+ // in the command file.
+ if (strcmp(reboot_arg, "sideload") == 0) {
+ if (getuid() != 0) {
+ snprintf(buf, sizeof(buf), "'adb root' is required for 'adb reboot sideload'.\n");
+ WriteStringFully(fd, buf);
+ return false;
+ }
+
+ const char* const recovery_dir = "/cache/recovery";
+ const char* const command_file = "/cache/recovery/command";
+ // Ensure /cache/recovery exists.
+ if (adb_mkdir(recovery_dir, 0770) == -1 && errno != EEXIST) {
+ D("Failed to create directory '%s': %s\n", recovery_dir, strerror(errno));
+ return false;
+ }
+
+ bool write_status = android::base::WriteStringToFile(
+ auto_reboot ? "--sideload_auto_reboot" : "--sideload", command_file);
+ if (!write_status) {
+ return false;
+ }
+
+ reboot_arg = "recovery";
+ }
sync();
- ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
- if (ret >= (int) sizeof(property_val)) {
+ char property_val[PROPERTY_VALUE_MAX];
+ int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
+ if (ret >= static_cast<int>(sizeof(property_val))) {
snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
- WriteFdExactly(fd, buf, strlen(buf));
- goto cleanup;
+ WriteStringFully(fd, buf);
+ return false;
}
ret = property_set(ANDROID_RB_PROPERTY, property_val);
if (ret < 0) {
snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
- WriteFdExactly(fd, buf, strlen(buf));
- goto cleanup;
+ WriteStringFully(fd, buf);
+ return false;
}
- // Don't return early. Give the reboot command time to take effect
- // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
- while(1) { pause(); }
-cleanup:
+
+ return true;
+}
+
+void reboot_service(int fd, void* arg)
+{
+ if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
+ // Don't return early. Give the reboot command time to take effect
+ // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
+ while (1) {
+ pause();
+ }
+ }
+
free(arg);
adb_close(fd);
}
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index c317e3a..2ad28fa 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -150,10 +150,8 @@
#undef close
#define close ____xxx_close
-static __inline__ int unix_read(int fd, void* buf, size_t len)
-{
- return read(fd, buf, len);
-}
+extern int unix_read(int fd, void* buf, size_t len);
+
#undef read
#define read ___xxx_read
diff --git a/adb/sysdeps_win32.c b/adb/sysdeps_win32.c
index c28e031..c2742f1 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -22,6 +22,7 @@
#include <windows.h>
#include <errno.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -2250,3 +2251,905 @@
}
/* NOTREACHED */
}
+
+/**************************************************************************/
+/**************************************************************************/
+/***** *****/
+/***** Console Window Terminal Emulation *****/
+/***** *****/
+/**************************************************************************/
+/**************************************************************************/
+
+// This reads input from a Win32 console window and translates it into Unix
+// terminal-style sequences. This emulates mostly Gnome Terminal (in Normal
+// mode, not Application mode), which itself emulates xterm. Gnome Terminal
+// is emulated instead of xterm because it is probably more popular than xterm:
+// Ubuntu's default Ctrl-Alt-T shortcut opens Gnome Terminal, Gnome Terminal
+// supports modern fonts, etc. It seems best to emulate the terminal that most
+// Android developers use because they'll fix apps (the shell, etc.) to keep
+// working with that terminal's emulation.
+//
+// The point of this emulation is not to be perfect or to solve all issues with
+// console windows on Windows, but to be better than the original code which
+// just called read() (which called ReadFile(), which called ReadConsoleA())
+// which did not support Ctrl-C, tab completion, shell input line editing
+// keys, server echo, and more.
+//
+// This implementation reconfigures the console with SetConsoleMode(), then
+// calls ReadConsoleInput() to get raw input which it remaps to Unix
+// terminal-style sequences which is returned via unix_read() which is used
+// by the 'adb shell' command.
+//
+// Code organization:
+//
+// * stdin_raw_init() and stdin_raw_restore() reconfigure the console.
+// * unix_read() detects console windows (as opposed to pipes, files, etc.).
+// * _console_read() is the main code of the emulation.
+
+
+// Read an input record from the console; one that should be processed.
+static bool _get_interesting_input_record_uncached(const HANDLE console,
+ INPUT_RECORD* const input_record) {
+ for (;;) {
+ DWORD read_count = 0;
+ memset(input_record, 0, sizeof(*input_record));
+ if (!ReadConsoleInputA(console, input_record, 1, &read_count)) {
+ D("_get_interesting_input_record_uncached: ReadConsoleInputA() "
+ "failure, error %ld\n", GetLastError());
+ errno = EIO;
+ return false;
+ }
+
+ if (read_count == 0) { // should be impossible
+ fatal("ReadConsoleInputA returned 0");
+ }
+
+ if (read_count != 1) { // should be impossible
+ fatal("ReadConsoleInputA did not return one input record");
+ }
+
+ if ((input_record->EventType == KEY_EVENT) &&
+ (input_record->Event.KeyEvent.bKeyDown)) {
+ if (input_record->Event.KeyEvent.wRepeatCount == 0) {
+ fatal("ReadConsoleInputA returned a key event with zero repeat"
+ " count");
+ }
+
+ // Got an interesting INPUT_RECORD, so return
+ return true;
+ }
+ }
+}
+
+// Cached input record (in case _console_read() is passed a buffer that doesn't
+// have enough space to fit wRepeatCount number of key sequences). A non-zero
+// wRepeatCount indicates that a record is cached.
+static INPUT_RECORD _win32_input_record;
+
+// Get the next KEY_EVENT_RECORD that should be processed.
+static KEY_EVENT_RECORD* _get_key_event_record(const HANDLE console) {
+ // If nothing cached, read directly from the console until we get an
+ // interesting record.
+ if (_win32_input_record.Event.KeyEvent.wRepeatCount == 0) {
+ if (!_get_interesting_input_record_uncached(console,
+ &_win32_input_record)) {
+ // There was an error, so make sure wRepeatCount is zero because
+ // that signifies no cached input record.
+ _win32_input_record.Event.KeyEvent.wRepeatCount = 0;
+ return NULL;
+ }
+ }
+
+ return &_win32_input_record.Event.KeyEvent;
+}
+
+static __inline__ bool _is_shift_pressed(const DWORD control_key_state) {
+ return (control_key_state & SHIFT_PRESSED) != 0;
+}
+
+static __inline__ bool _is_ctrl_pressed(const DWORD control_key_state) {
+ return (control_key_state & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0;
+}
+
+static __inline__ bool _is_alt_pressed(const DWORD control_key_state) {
+ return (control_key_state & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0;
+}
+
+static __inline__ bool _is_numlock_on(const DWORD control_key_state) {
+ return (control_key_state & NUMLOCK_ON) != 0;
+}
+
+static __inline__ bool _is_capslock_on(const DWORD control_key_state) {
+ return (control_key_state & CAPSLOCK_ON) != 0;
+}
+
+static __inline__ bool _is_enhanced_key(const DWORD control_key_state) {
+ return (control_key_state & ENHANCED_KEY) != 0;
+}
+
+// Constants from MSDN for ToAscii().
+static const BYTE TOASCII_KEY_OFF = 0x00;
+static const BYTE TOASCII_KEY_DOWN = 0x80;
+static const BYTE TOASCII_KEY_TOGGLED_ON = 0x01; // for CapsLock
+
+// Given a key event, ignore a modifier key and return the character that was
+// entered without the modifier. Writes to *ch and returns the number of bytes
+// written.
+static size_t _get_char_ignoring_modifier(char* const ch,
+ const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state,
+ const WORD modifier) {
+ // If there is no character from Windows, try ignoring the specified
+ // modifier and look for a character. Note that if AltGr is being used,
+ // there will be a character from Windows.
+ if (key_event->uChar.AsciiChar == '\0') {
+ // Note that we read the control key state from the passed in argument
+ // instead of from key_event since the argument has been normalized.
+ if (((modifier == VK_SHIFT) &&
+ _is_shift_pressed(control_key_state)) ||
+ ((modifier == VK_CONTROL) &&
+ _is_ctrl_pressed(control_key_state)) ||
+ ((modifier == VK_MENU) && _is_alt_pressed(control_key_state))) {
+
+ BYTE key_state[256] = {0};
+ key_state[VK_SHIFT] = _is_shift_pressed(control_key_state) ?
+ TOASCII_KEY_DOWN : TOASCII_KEY_OFF;
+ key_state[VK_CONTROL] = _is_ctrl_pressed(control_key_state) ?
+ TOASCII_KEY_DOWN : TOASCII_KEY_OFF;
+ key_state[VK_MENU] = _is_alt_pressed(control_key_state) ?
+ TOASCII_KEY_DOWN : TOASCII_KEY_OFF;
+ key_state[VK_CAPITAL] = _is_capslock_on(control_key_state) ?
+ TOASCII_KEY_TOGGLED_ON : TOASCII_KEY_OFF;
+
+ // cause this modifier to be ignored
+ key_state[modifier] = TOASCII_KEY_OFF;
+
+ WORD translated = 0;
+ if (ToAscii(key_event->wVirtualKeyCode,
+ key_event->wVirtualScanCode, key_state, &translated, 0) == 1) {
+ // Ignoring the modifier, we found a character.
+ *ch = (CHAR)translated;
+ return 1;
+ }
+ }
+ }
+
+ // Just use whatever Windows told us originally.
+ *ch = key_event->uChar.AsciiChar;
+
+ // If the character from Windows is NULL, return a size of zero.
+ return (*ch == '\0') ? 0 : 1;
+}
+
+// If a Ctrl key is pressed, lookup the character, ignoring the Ctrl key,
+// but taking into account the shift key. This is because for a sequence like
+// Ctrl-Alt-0, we want to find the character '0' and for Ctrl-Alt-Shift-0,
+// we want to find the character ')'.
+//
+// Note that Windows doesn't seem to pass bKeyDown for Ctrl-Shift-NoAlt-0
+// because it is the default key-sequence to switch the input language.
+// This is configurable in the Region and Language control panel.
+static __inline__ size_t _get_non_control_char(char* const ch,
+ const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) {
+ return _get_char_ignoring_modifier(ch, key_event, control_key_state,
+ VK_CONTROL);
+}
+
+// Get without Alt.
+static __inline__ size_t _get_non_alt_char(char* const ch,
+ const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) {
+ return _get_char_ignoring_modifier(ch, key_event, control_key_state,
+ VK_MENU);
+}
+
+// Ignore the control key, find the character from Windows, and apply any
+// Control key mappings (for example, Ctrl-2 is a NULL character). Writes to
+// *pch and returns number of bytes written.
+static size_t _get_control_character(char* const pch,
+ const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) {
+ const size_t len = _get_non_control_char(pch, key_event,
+ control_key_state);
+
+ if ((len == 1) && _is_ctrl_pressed(control_key_state)) {
+ char ch = *pch;
+ switch (ch) {
+ case '2':
+ case '@':
+ case '`':
+ ch = '\0';
+ break;
+ case '3':
+ case '[':
+ case '{':
+ ch = '\x1b';
+ break;
+ case '4':
+ case '\\':
+ case '|':
+ ch = '\x1c';
+ break;
+ case '5':
+ case ']':
+ case '}':
+ ch = '\x1d';
+ break;
+ case '6':
+ case '^':
+ case '~':
+ ch = '\x1e';
+ break;
+ case '7':
+ case '-':
+ case '_':
+ ch = '\x1f';
+ break;
+ case '8':
+ ch = '\x7f';
+ break;
+ case '/':
+ if (!_is_alt_pressed(control_key_state)) {
+ ch = '\x1f';
+ }
+ break;
+ case '?':
+ if (!_is_alt_pressed(control_key_state)) {
+ ch = '\x7f';
+ }
+ break;
+ }
+ *pch = ch;
+ }
+
+ return len;
+}
+
+static DWORD _normalize_altgr_control_key_state(
+ const KEY_EVENT_RECORD* const key_event) {
+ DWORD control_key_state = key_event->dwControlKeyState;
+
+ // If we're in an AltGr situation where the AltGr key is down (depending on
+ // the keyboard layout, that might be the physical right alt key which
+ // produces a control_key_state where Right-Alt and Left-Ctrl are down) or
+ // AltGr-equivalent keys are down (any Ctrl key + any Alt key), and we have
+ // a character (which indicates that there was an AltGr mapping), then act
+ // as if alt and control are not really down for the purposes of modifiers.
+ // This makes it so that if the user with, say, a German keyboard layout
+ // presses AltGr-] (which we see as Right-Alt + Left-Ctrl + key), we just
+ // output the key and we don't see the Alt and Ctrl keys.
+ if (_is_ctrl_pressed(control_key_state) &&
+ _is_alt_pressed(control_key_state)
+ && (key_event->uChar.AsciiChar != '\0')) {
+ // Try to remove as few bits as possible to improve our chances of
+ // detecting combinations like Left-Alt + AltGr, Right-Ctrl + AltGr, or
+ // Left-Alt + Right-Ctrl + AltGr.
+ if ((control_key_state & RIGHT_ALT_PRESSED) != 0) {
+ // Remove Right-Alt.
+ control_key_state &= ~RIGHT_ALT_PRESSED;
+ // If uChar is set, a Ctrl key is pressed, and Right-Alt is
+ // pressed, Left-Ctrl is almost always set, except if the user
+ // presses Right-Ctrl, then AltGr (in that specific order) for
+ // whatever reason. At any rate, make sure the bit is not set.
+ control_key_state &= ~LEFT_CTRL_PRESSED;
+ } else if ((control_key_state & LEFT_ALT_PRESSED) != 0) {
+ // Remove Left-Alt.
+ control_key_state &= ~LEFT_ALT_PRESSED;
+ // Whichever Ctrl key is down, remove it from the state. We only
+ // remove one key, to improve our chances of detecting the
+ // corner-case of Left-Ctrl + Left-Alt + Right-Ctrl.
+ if ((control_key_state & LEFT_CTRL_PRESSED) != 0) {
+ // Remove Left-Ctrl.
+ control_key_state &= ~LEFT_CTRL_PRESSED;
+ } else if ((control_key_state & RIGHT_CTRL_PRESSED) != 0) {
+ // Remove Right-Ctrl.
+ control_key_state &= ~RIGHT_CTRL_PRESSED;
+ }
+ }
+
+ // Note that this logic isn't 100% perfect because Windows doesn't
+ // allow us to detect all combinations because a physical AltGr key
+ // press shows up as two bits, plus some combinations are ambiguous
+ // about what is actually physically pressed.
+ }
+
+ return control_key_state;
+}
+
+// If NumLock is on and Shift is pressed, SHIFT_PRESSED is not set in
+// dwControlKeyState for the following keypad keys: period, 0-9. If we detect
+// this scenario, set the SHIFT_PRESSED bit so we can add modifiers
+// appropriately.
+static DWORD _normalize_keypad_control_key_state(const WORD vk,
+ const DWORD control_key_state) {
+ if (!_is_numlock_on(control_key_state)) {
+ return control_key_state;
+ }
+ if (!_is_enhanced_key(control_key_state)) {
+ switch (vk) {
+ case VK_INSERT: // 0
+ case VK_DELETE: // .
+ case VK_END: // 1
+ case VK_DOWN: // 2
+ case VK_NEXT: // 3
+ case VK_LEFT: // 4
+ case VK_CLEAR: // 5
+ case VK_RIGHT: // 6
+ case VK_HOME: // 7
+ case VK_UP: // 8
+ case VK_PRIOR: // 9
+ return control_key_state | SHIFT_PRESSED;
+ }
+ }
+
+ return control_key_state;
+}
+
+static const char* _get_keypad_sequence(const DWORD control_key_state,
+ const char* const normal, const char* const shifted) {
+ if (_is_shift_pressed(control_key_state)) {
+ // Shift is pressed and NumLock is off
+ return shifted;
+ } else {
+ // Shift is not pressed and NumLock is off, or,
+ // Shift is pressed and NumLock is on, in which case we want the
+ // NumLock and Shift to neutralize each other, thus, we want the normal
+ // sequence.
+ return normal;
+ }
+ // If Shift is not pressed and NumLock is on, a different virtual key code
+ // is returned by Windows, which can be taken care of by a different case
+ // statement in _console_read().
+}
+
+// Write sequence to buf and return the number of bytes written.
+static size_t _get_modifier_sequence(char* const buf, const WORD vk,
+ DWORD control_key_state, const char* const normal) {
+ // Copy the base sequence into buf.
+ const size_t len = strlen(normal);
+ memcpy(buf, normal, len);
+
+ int code = 0;
+
+ control_key_state = _normalize_keypad_control_key_state(vk,
+ control_key_state);
+
+ if (_is_shift_pressed(control_key_state)) {
+ code |= 0x1;
+ }
+ if (_is_alt_pressed(control_key_state)) { // any alt key pressed
+ code |= 0x2;
+ }
+ if (_is_ctrl_pressed(control_key_state)) { // any control key pressed
+ code |= 0x4;
+ }
+ // If some modifier was held down, then we need to insert the modifier code
+ if (code != 0) {
+ if (len == 0) {
+ // Should be impossible because caller should pass a string of
+ // non-zero length.
+ return 0;
+ }
+ size_t index = len - 1;
+ const char lastChar = buf[index];
+ if (lastChar != '~') {
+ buf[index++] = '1';
+ }
+ buf[index++] = ';'; // modifier separator
+ // 2 = shift, 3 = alt, 4 = shift & alt, 5 = control,
+ // 6 = shift & control, 7 = alt & control, 8 = shift & alt & control
+ buf[index++] = '1' + code;
+ buf[index++] = lastChar; // move ~ (or other last char) to the end
+ return index;
+ }
+ return len;
+}
+
+// Write sequence to buf and return the number of bytes written.
+static size_t _get_modifier_keypad_sequence(char* const buf, const WORD vk,
+ const DWORD control_key_state, const char* const normal,
+ const char shifted) {
+ if (_is_shift_pressed(control_key_state)) {
+ // Shift is pressed and NumLock is off
+ if (shifted != '\0') {
+ buf[0] = shifted;
+ return sizeof(buf[0]);
+ } else {
+ return 0;
+ }
+ } else {
+ // Shift is not pressed and NumLock is off, or,
+ // Shift is pressed and NumLock is on, in which case we want the
+ // NumLock and Shift to neutralize each other, thus, we want the normal
+ // sequence.
+ return _get_modifier_sequence(buf, vk, control_key_state, normal);
+ }
+ // If Shift is not pressed and NumLock is on, a different virtual key code
+ // is returned by Windows, which can be taken care of by a different case
+ // statement in _console_read().
+}
+
+// The decimal key on the keypad produces a '.' for U.S. English and a ',' for
+// Standard German. Figure this out at runtime so we know what to output for
+// Shift-VK_DELETE.
+static char _get_decimal_char() {
+ return (char)MapVirtualKeyA(VK_DECIMAL, MAPVK_VK_TO_CHAR);
+}
+
+// Prefix the len bytes in buf with the escape character, and then return the
+// new buffer length.
+size_t _escape_prefix(char* const buf, const size_t len) {
+ // If nothing to prefix, don't do anything. We might be called with
+ // len == 0, if alt was held down with a dead key which produced nothing.
+ if (len == 0) {
+ return 0;
+ }
+
+ memmove(&buf[1], buf, len);
+ buf[0] = '\x1b';
+ return len + 1;
+}
+
+// Writes to buffer buf (of length len), returning number of bytes written or
+// -1 on error. Never returns zero because Win32 consoles are never 'closed'
+// (as far as I can tell).
+static int _console_read(const HANDLE console, void* buf, size_t len) {
+ for (;;) {
+ KEY_EVENT_RECORD* const key_event = _get_key_event_record(console);
+ if (key_event == NULL) {
+ return -1;
+ }
+
+ const WORD vk = key_event->wVirtualKeyCode;
+ const CHAR ch = key_event->uChar.AsciiChar;
+ const DWORD control_key_state = _normalize_altgr_control_key_state(
+ key_event);
+
+ // The following emulation code should write the output sequence to
+ // either seqstr or to seqbuf and seqbuflen.
+ const char* seqstr = NULL; // NULL terminated C-string
+ // Enough space for max sequence string below, plus modifiers and/or
+ // escape prefix.
+ char seqbuf[16];
+ size_t seqbuflen = 0; // Space used in seqbuf.
+
+#define MATCH(vk, normal) \
+ case (vk): \
+ { \
+ seqstr = (normal); \
+ } \
+ break;
+
+ // Modifier keys should affect the output sequence.
+#define MATCH_MODIFIER(vk, normal) \
+ case (vk): \
+ { \
+ seqbuflen = _get_modifier_sequence(seqbuf, (vk), \
+ control_key_state, (normal)); \
+ } \
+ break;
+
+ // The shift key should affect the output sequence.
+#define MATCH_KEYPAD(vk, normal, shifted) \
+ case (vk): \
+ { \
+ seqstr = _get_keypad_sequence(control_key_state, (normal), \
+ (shifted)); \
+ } \
+ break;
+
+ // The shift key and other modifier keys should affect the output
+ // sequence.
+#define MATCH_MODIFIER_KEYPAD(vk, normal, shifted) \
+ case (vk): \
+ { \
+ seqbuflen = _get_modifier_keypad_sequence(seqbuf, (vk), \
+ control_key_state, (normal), (shifted)); \
+ } \
+ break;
+
+#define ESC "\x1b"
+#define CSI ESC "["
+#define SS3 ESC "O"
+
+ // Only support normal mode, not application mode.
+
+ // Enhanced keys:
+ // * 6-pack: insert, delete, home, end, page up, page down
+ // * cursor keys: up, down, right, left
+ // * keypad: divide, enter
+ // * Undocumented: VK_PAUSE (Ctrl-NumLock), VK_SNAPSHOT,
+ // VK_CANCEL (Ctrl-Pause/Break), VK_NUMLOCK
+ if (_is_enhanced_key(control_key_state)) {
+ switch (vk) {
+ case VK_RETURN: // Enter key on keypad
+ if (_is_ctrl_pressed(control_key_state)) {
+ seqstr = "\n";
+ } else {
+ seqstr = "\r";
+ }
+ break;
+
+ MATCH_MODIFIER(VK_PRIOR, CSI "5~"); // Page Up
+ MATCH_MODIFIER(VK_NEXT, CSI "6~"); // Page Down
+
+ // gnome-terminal currently sends SS3 "F" and SS3 "H", but that
+ // will be fixed soon to match xterm which sends CSI "F" and
+ // CSI "H". https://bugzilla.redhat.com/show_bug.cgi?id=1119764
+ MATCH(VK_END, CSI "F");
+ MATCH(VK_HOME, CSI "H");
+
+ MATCH_MODIFIER(VK_LEFT, CSI "D");
+ MATCH_MODIFIER(VK_UP, CSI "A");
+ MATCH_MODIFIER(VK_RIGHT, CSI "C");
+ MATCH_MODIFIER(VK_DOWN, CSI "B");
+
+ MATCH_MODIFIER(VK_INSERT, CSI "2~");
+ MATCH_MODIFIER(VK_DELETE, CSI "3~");
+
+ MATCH(VK_DIVIDE, "/");
+ }
+ } else { // Non-enhanced keys:
+ switch (vk) {
+ case VK_BACK: // backspace
+ if (_is_alt_pressed(control_key_state)) {
+ seqstr = ESC "\x7f";
+ } else {
+ seqstr = "\x7f";
+ }
+ break;
+
+ case VK_TAB:
+ if (_is_shift_pressed(control_key_state)) {
+ seqstr = CSI "Z";
+ } else {
+ seqstr = "\t";
+ }
+ break;
+
+ // Number 5 key in keypad when NumLock is off, or if NumLock is
+ // on and Shift is down.
+ MATCH_KEYPAD(VK_CLEAR, CSI "E", "5");
+
+ case VK_RETURN: // Enter key on main keyboard
+ if (_is_alt_pressed(control_key_state)) {
+ seqstr = ESC "\n";
+ } else if (_is_ctrl_pressed(control_key_state)) {
+ seqstr = "\n";
+ } else {
+ seqstr = "\r";
+ }
+ break;
+
+ // VK_ESCAPE: Don't do any special handling. The OS uses many
+ // of the sequences with Escape and many of the remaining
+ // sequences don't produce bKeyDown messages, only !bKeyDown
+ // for whatever reason.
+
+ case VK_SPACE:
+ if (_is_alt_pressed(control_key_state)) {
+ seqstr = ESC " ";
+ } else if (_is_ctrl_pressed(control_key_state)) {
+ seqbuf[0] = '\0'; // NULL char
+ seqbuflen = 1;
+ } else {
+ seqstr = " ";
+ }
+ break;
+
+ MATCH_MODIFIER_KEYPAD(VK_PRIOR, CSI "5~", '9'); // Page Up
+ MATCH_MODIFIER_KEYPAD(VK_NEXT, CSI "6~", '3'); // Page Down
+
+ MATCH_KEYPAD(VK_END, CSI "4~", "1");
+ MATCH_KEYPAD(VK_HOME, CSI "1~", "7");
+
+ MATCH_MODIFIER_KEYPAD(VK_LEFT, CSI "D", '4');
+ MATCH_MODIFIER_KEYPAD(VK_UP, CSI "A", '8');
+ MATCH_MODIFIER_KEYPAD(VK_RIGHT, CSI "C", '6');
+ MATCH_MODIFIER_KEYPAD(VK_DOWN, CSI "B", '2');
+
+ MATCH_MODIFIER_KEYPAD(VK_INSERT, CSI "2~", '0');
+ MATCH_MODIFIER_KEYPAD(VK_DELETE, CSI "3~",
+ _get_decimal_char());
+
+ case 0x30: // 0
+ case 0x31: // 1
+ case 0x39: // 9
+ case VK_OEM_1: // ;:
+ case VK_OEM_PLUS: // =+
+ case VK_OEM_COMMA: // ,<
+ case VK_OEM_PERIOD: // .>
+ case VK_OEM_7: // '"
+ case VK_OEM_102: // depends on keyboard, could be <> or \|
+ case VK_OEM_2: // /?
+ case VK_OEM_3: // `~
+ case VK_OEM_4: // [{
+ case VK_OEM_5: // \|
+ case VK_OEM_6: // ]}
+ {
+ seqbuflen = _get_control_character(seqbuf, key_event,
+ control_key_state);
+
+ if (_is_alt_pressed(control_key_state)) {
+ seqbuflen = _escape_prefix(seqbuf, seqbuflen);
+ }
+ }
+ break;
+
+ case 0x32: // 2
+ case 0x36: // 6
+ case VK_OEM_MINUS: // -_
+ {
+ seqbuflen = _get_control_character(seqbuf, key_event,
+ control_key_state);
+
+ // If Alt is pressed and it isn't Ctrl-Alt-ShiftUp, then
+ // prefix with escape.
+ if (_is_alt_pressed(control_key_state) &&
+ !(_is_ctrl_pressed(control_key_state) &&
+ !_is_shift_pressed(control_key_state))) {
+ seqbuflen = _escape_prefix(seqbuf, seqbuflen);
+ }
+ }
+ break;
+
+ case 0x33: // 3
+ case 0x34: // 4
+ case 0x35: // 5
+ case 0x37: // 7
+ case 0x38: // 8
+ {
+ seqbuflen = _get_control_character(seqbuf, key_event,
+ control_key_state);
+
+ // If Alt is pressed and it isn't Ctrl-Alt-ShiftUp, then
+ // prefix with escape.
+ if (_is_alt_pressed(control_key_state) &&
+ !(_is_ctrl_pressed(control_key_state) &&
+ !_is_shift_pressed(control_key_state))) {
+ seqbuflen = _escape_prefix(seqbuf, seqbuflen);
+ }
+ }
+ break;
+
+ case 0x41: // a
+ case 0x42: // b
+ case 0x43: // c
+ case 0x44: // d
+ case 0x45: // e
+ case 0x46: // f
+ case 0x47: // g
+ case 0x48: // h
+ case 0x49: // i
+ case 0x4a: // j
+ case 0x4b: // k
+ case 0x4c: // l
+ case 0x4d: // m
+ case 0x4e: // n
+ case 0x4f: // o
+ case 0x50: // p
+ case 0x51: // q
+ case 0x52: // r
+ case 0x53: // s
+ case 0x54: // t
+ case 0x55: // u
+ case 0x56: // v
+ case 0x57: // w
+ case 0x58: // x
+ case 0x59: // y
+ case 0x5a: // z
+ {
+ seqbuflen = _get_non_alt_char(seqbuf, key_event,
+ control_key_state);
+
+ // If Alt is pressed, then prefix with escape.
+ if (_is_alt_pressed(control_key_state)) {
+ seqbuflen = _escape_prefix(seqbuf, seqbuflen);
+ }
+ }
+ break;
+
+ // These virtual key codes are generated by the keys on the
+ // keypad *when NumLock is on* and *Shift is up*.
+ MATCH(VK_NUMPAD0, "0");
+ MATCH(VK_NUMPAD1, "1");
+ MATCH(VK_NUMPAD2, "2");
+ MATCH(VK_NUMPAD3, "3");
+ MATCH(VK_NUMPAD4, "4");
+ MATCH(VK_NUMPAD5, "5");
+ MATCH(VK_NUMPAD6, "6");
+ MATCH(VK_NUMPAD7, "7");
+ MATCH(VK_NUMPAD8, "8");
+ MATCH(VK_NUMPAD9, "9");
+
+ MATCH(VK_MULTIPLY, "*");
+ MATCH(VK_ADD, "+");
+ MATCH(VK_SUBTRACT, "-");
+ // VK_DECIMAL is generated by the . key on the keypad *when
+ // NumLock is on* and *Shift is up* and the sequence is not
+ // Ctrl-Alt-NoShift-. (which causes Ctrl-Alt-Del and the
+ // Windows Security screen to come up).
+ case VK_DECIMAL:
+ // U.S. English uses '.', Germany German uses ','.
+ seqbuflen = _get_non_control_char(seqbuf, key_event,
+ control_key_state);
+ break;
+
+ MATCH_MODIFIER(VK_F1, SS3 "P");
+ MATCH_MODIFIER(VK_F2, SS3 "Q");
+ MATCH_MODIFIER(VK_F3, SS3 "R");
+ MATCH_MODIFIER(VK_F4, SS3 "S");
+ MATCH_MODIFIER(VK_F5, CSI "15~");
+ MATCH_MODIFIER(VK_F6, CSI "17~");
+ MATCH_MODIFIER(VK_F7, CSI "18~");
+ MATCH_MODIFIER(VK_F8, CSI "19~");
+ MATCH_MODIFIER(VK_F9, CSI "20~");
+ MATCH_MODIFIER(VK_F10, CSI "21~");
+ MATCH_MODIFIER(VK_F11, CSI "23~");
+ MATCH_MODIFIER(VK_F12, CSI "24~");
+
+ MATCH_MODIFIER(VK_F13, CSI "25~");
+ MATCH_MODIFIER(VK_F14, CSI "26~");
+ MATCH_MODIFIER(VK_F15, CSI "28~");
+ MATCH_MODIFIER(VK_F16, CSI "29~");
+ MATCH_MODIFIER(VK_F17, CSI "31~");
+ MATCH_MODIFIER(VK_F18, CSI "32~");
+ MATCH_MODIFIER(VK_F19, CSI "33~");
+ MATCH_MODIFIER(VK_F20, CSI "34~");
+
+ // MATCH_MODIFIER(VK_F21, ???);
+ // MATCH_MODIFIER(VK_F22, ???);
+ // MATCH_MODIFIER(VK_F23, ???);
+ // MATCH_MODIFIER(VK_F24, ???);
+ }
+ }
+
+#undef MATCH
+#undef MATCH_MODIFIER
+#undef MATCH_KEYPAD
+#undef MATCH_MODIFIER_KEYPAD
+#undef ESC
+#undef CSI
+#undef SS3
+
+ const char* out;
+ size_t outlen;
+
+ // Check for output in any of:
+ // * seqstr is set (and strlen can be used to determine the length).
+ // * seqbuf and seqbuflen are set
+ // Fallback to ch from Windows.
+ if (seqstr != NULL) {
+ out = seqstr;
+ outlen = strlen(seqstr);
+ } else if (seqbuflen > 0) {
+ out = seqbuf;
+ outlen = seqbuflen;
+ } else if (ch != '\0') {
+ // Use whatever Windows told us it is.
+ seqbuf[0] = ch;
+ seqbuflen = 1;
+ out = seqbuf;
+ outlen = seqbuflen;
+ } else {
+ // No special handling for the virtual key code and Windows isn't
+ // telling us a character code, then we don't know how to translate
+ // the key press.
+ //
+ // Consume the input and 'continue' to cause us to get a new key
+ // event.
+ D("_console_read: unknown virtual key code: %d, enhanced: %s\n",
+ vk, _is_enhanced_key(control_key_state) ? "true" : "false");
+ key_event->wRepeatCount = 0;
+ continue;
+ }
+
+ int bytesRead = 0;
+
+ // put output wRepeatCount times into buf/len
+ while (key_event->wRepeatCount > 0) {
+ if (len >= outlen) {
+ // Write to buf/len
+ memcpy(buf, out, outlen);
+ buf = (void*)((char*)buf + outlen);
+ len -= outlen;
+ bytesRead += outlen;
+
+ // consume the input
+ --key_event->wRepeatCount;
+ } else {
+ // Not enough space, so just leave it in _win32_input_record
+ // for a subsequent retrieval.
+ if (bytesRead == 0) {
+ // We didn't write anything because there wasn't enough
+ // space to even write one sequence. This should never
+ // happen if the caller uses sensible buffer sizes
+ // (i.e. >= maximum sequence length which is probably a
+ // few bytes long).
+ D("_console_read: no buffer space to write one sequence; "
+ "buffer: %ld, sequence: %ld\n", (long)len,
+ (long)outlen);
+ errno = ENOMEM;
+ return -1;
+ } else {
+ // Stop trying to write to buf/len, just return whatever
+ // we wrote so far.
+ break;
+ }
+ }
+ }
+
+ return bytesRead;
+ }
+}
+
+static DWORD _old_console_mode; // previous GetConsoleMode() result
+static HANDLE _console_handle; // when set, console mode should be restored
+
+void stdin_raw_init(const int fd) {
+ if (STDIN_FILENO == fd) {
+ const HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
+ if ((in == INVALID_HANDLE_VALUE) || (in == NULL)) {
+ return;
+ }
+
+ if (GetFileType(in) != FILE_TYPE_CHAR) {
+ // stdin might be a file or pipe.
+ return;
+ }
+
+ if (!GetConsoleMode(in, &_old_console_mode)) {
+ // If GetConsoleMode() fails, stdin is probably is not a console.
+ return;
+ }
+
+ // Disable ENABLE_PROCESSED_INPUT so that Ctrl-C is read instead of
+ // calling the process Ctrl-C routine (configured by
+ // SetConsoleCtrlHandler()).
+ // Disable ENABLE_LINE_INPUT so that input is immediately sent.
+ // Disable ENABLE_ECHO_INPUT to disable local echo. Disabling this
+ // flag also seems necessary to have proper line-ending processing.
+ if (!SetConsoleMode(in, _old_console_mode & ~(ENABLE_PROCESSED_INPUT |
+ ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))) {
+ // This really should not fail.
+ D("stdin_raw_init: SetConsoleMode() failure, error %ld\n",
+ GetLastError());
+ }
+
+ // Once this is set, it means that stdin has been configured for
+ // reading from and that the old console mode should be restored later.
+ _console_handle = in;
+
+ // Note that we don't need to configure C Runtime line-ending
+ // translation because _console_read() does not call the C Runtime to
+ // read from the console.
+ }
+}
+
+void stdin_raw_restore(const int fd) {
+ if (STDIN_FILENO == fd) {
+ if (_console_handle != NULL) {
+ const HANDLE in = _console_handle;
+ _console_handle = NULL; // clear state
+
+ if (!SetConsoleMode(in, _old_console_mode)) {
+ // This really should not fail.
+ D("stdin_raw_restore: SetConsoleMode() failure, error %ld\n",
+ GetLastError());
+ }
+ }
+ }
+}
+
+// Called by 'adb shell' command to read from stdin.
+int unix_read(int fd, void* buf, size_t len) {
+ if ((fd == STDIN_FILENO) && (_console_handle != NULL)) {
+ // If it is a request to read from stdin, and stdin_raw_init() has been
+ // called, and it successfully configured the console, then read from
+ // the console using Win32 console APIs and partially emulate a unix
+ // terminal.
+ return _console_read(_console_handle, buf, len);
+ } else {
+ // Just call into C Runtime which can read from pipes/files and which
+ // can do LF/CR translation.
+#undef read
+ return read(fd, buf, len);
+ }
+}
diff --git a/base/Android.mk b/base/Android.mk
index 17d6ece..ad85c6b 100644
--- a/base/Android.mk
+++ b/base/Android.mk
@@ -25,6 +25,8 @@
file_test.cpp \
stringprintf_test.cpp \
strings_test.cpp \
+ test_main.cpp \
+ test_utils.cpp \
libbase_cppflags := \
-Wall \
@@ -36,10 +38,11 @@
include $(CLEAR_VARS)
LOCAL_MODULE := libbase
LOCAL_CLANG := true
-LOCAL_SRC_FILES := $(libbase_src_files)
+LOCAL_SRC_FILES := $(libbase_src_files) logging.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CPPFLAGS := $(libbase_cppflags)
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_MULTILIB := both
include $(BUILD_STATIC_LIBRARY)
@@ -49,6 +52,7 @@
LOCAL_WHOLE_STATIC_LIBRARIES := libbase
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_SHARED_LIBRARIES := libcutils
LOCAL_MULTILIB := both
include $(BUILD_SHARED_LIBRARY)
@@ -57,9 +61,13 @@
include $(CLEAR_VARS)
LOCAL_MODULE := libbase
LOCAL_SRC_FILES := $(libbase_src_files)
+ifneq ($(HOST_OS),windows)
+ LOCAL_SRC_FILES += logging.cpp
+endif
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CPPFLAGS := $(libbase_cppflags)
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_MULTILIB := both
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -68,6 +76,7 @@
LOCAL_WHOLE_STATIC_LIBRARIES := libbase
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_MULTILIB := both
include $(BUILD_HOST_SHARED_LIBRARY)
@@ -76,7 +85,8 @@
include $(CLEAR_VARS)
LOCAL_MODULE := libbase_test
LOCAL_CLANG := true
-LOCAL_SRC_FILES := $(libbase_test_src_files)
+LOCAL_SRC_FILES := $(libbase_test_src_files) logging_test.cpp
+LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CPPFLAGS := $(libbase_cppflags)
LOCAL_SHARED_LIBRARIES := libbase
LOCAL_MULTILIB := both
@@ -87,6 +97,10 @@
include $(CLEAR_VARS)
LOCAL_MODULE := libbase_test
LOCAL_SRC_FILES := $(libbase_test_src_files)
+ifneq ($(HOST_OS),windows)
+ LOCAL_SRC_FILES += logging_test.cpp
+endif
+LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CPPFLAGS := $(libbase_cppflags)
LOCAL_SHARED_LIBRARIES := libbase
LOCAL_MULTILIB := both
diff --git a/base/CPPLINT.cfg b/base/CPPLINT.cfg
index 5ee068e..a61c08d 100644
--- a/base/CPPLINT.cfg
+++ b/base/CPPLINT.cfg
@@ -1,2 +1,2 @@
set noparent
-filter=-build/header_guard
+filter=-build/header_guard,-build/include,-build/c++11
diff --git a/base/file.cpp b/base/file.cpp
index 118071e..a51c5ff 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -26,6 +26,7 @@
#include "base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
#define LOG_TAG "base.file"
#include "cutils/log.h"
+#include "utils/Compat.h"
namespace android {
namespace base {
diff --git a/base/file_test.cpp b/base/file_test.cpp
index 34b8755..fc48b32 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -24,29 +24,7 @@
#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);
- }
-};
+#include "test_utils.h"
TEST(file, ReadFileToString_ENOENT) {
std::string s("hello");
diff --git a/base/include/base/logging.h b/base/include/base/logging.h
new file mode 100644
index 0000000..5e115fe
--- /dev/null
+++ b/base/include/base/logging.h
@@ -0,0 +1,267 @@
+/*
+ * 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_LOGGING_H
+#define BASE_LOGGING_H
+
+#include <memory>
+#include <ostream>
+
+#include "base/macros.h"
+
+namespace android {
+namespace base {
+
+enum LogSeverity {
+ VERBOSE,
+ DEBUG,
+ INFO,
+ WARNING,
+ ERROR,
+ FATAL,
+};
+
+// Configure logging based on ANDROID_LOG_TAGS environment variable.
+// We need to parse a string that looks like
+//
+// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
+//
+// The tag (or '*' for the global level) comes first, followed by a colon and a
+// letter indicating the minimum priority level we're expected to log. This can
+// be used to reveal or conceal logs with specific tags.
+extern void InitLogging(char* argv[]);
+
+// Returns the command line used to invoke the current tool or nullptr if
+// InitLogging hasn't been performed.
+extern const char* GetCmdLine();
+
+// The command used to start the program, such as "/system/bin/dalvikvm". If
+// InitLogging hasn't been performed then just returns "unknown"
+extern const char* ProgramInvocationName();
+
+// A short version of the command used to start the program, such as "dalvikvm".
+// If InitLogging hasn't been performed then just returns "unknown"
+extern const char* ProgramInvocationShortName();
+
+// Logs a message to logcat on Android otherwise to stderr. If the severity is
+// FATAL it also causes an abort. For example:
+//
+// LOG(FATAL) << "We didn't expect to reach here";
+#define LOG(severity) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::severity, \
+ -1).stream()
+
+// A variant of LOG that also logs the current errno value. To be used when
+// library calls fail.
+#define PLOG(severity) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::severity, \
+ errno).stream()
+
+// Marker that code is yet to be implemented.
+#define UNIMPLEMENTED(level) \
+ LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
+
+// Check whether condition x holds and LOG(FATAL) if not. The value of the
+// expression x is only evaluated once. Extra logging can be appended using <<
+// after. For example:
+//
+// CHECK(false == true) results in a log message of
+// "Check failed: false == true".
+#define CHECK(x) \
+ if (UNLIKELY(!(x))) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, \
+ -1).stream() \
+ << "Check failed: " #x << " "
+
+// Helper for CHECK_xx(x,y) macros.
+#define CHECK_OP(LHS, RHS, OP) \
+ for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
+ UNLIKELY(!(_values.lhs OP _values.rhs)); \
+ /* empty */) \
+ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, -1) \
+ .stream() \
+ << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
+ << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
+
+// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
+// of the expressions x and y is evaluated once. Extra logging can be appended
+// using << after. For example:
+//
+// CHECK_NE(0 == 1, false) results in
+// "Check failed: false != false (0==1=false, false=false) ".
+#define CHECK_EQ(x, y) CHECK_OP(x, y, == )
+#define CHECK_NE(x, y) CHECK_OP(x, y, != )
+#define CHECK_LE(x, y) CHECK_OP(x, y, <= )
+#define CHECK_LT(x, y) CHECK_OP(x, y, < )
+#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
+#define CHECK_GT(x, y) CHECK_OP(x, y, > )
+
+// Helper for CHECK_STRxx(s1,s2) macros.
+#define CHECK_STROP(s1, s2, sense) \
+ if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
+ LOG(FATAL) << "Check failed: " \
+ << "\"" << s1 << "\"" \
+ << (sense ? " == " : " != ") << "\"" << s2 << "\""
+
+// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
+#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
+#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
+
+// Perform the pthread function call(args), LOG(FATAL) on error.
+#define CHECK_PTHREAD_CALL(call, args, what) \
+ do { \
+ int rc = call args; \
+ if (rc != 0) { \
+ errno = rc; \
+ PLOG(FATAL) << #call << " failed for " << what; \
+ } \
+ } while (false)
+
+// CHECK that can be used in a constexpr function. For example:
+//
+// constexpr int half(int n) {
+// return
+// DCHECK_CONSTEXPR(n >= 0, , 0)
+// CHECK_CONSTEXPR((n & 1) == 0),
+// << "Extra debugging output: n = " << n, 0)
+// n / 2;
+// }
+#define CHECK_CONSTEXPR(x, out, dummy) \
+ (UNLIKELY(!(x))) \
+ ? (LOG(FATAL) << "Check failed: " << #x out, dummy) \
+ :
+
+// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally
+// CHECK should be used unless profiling identifies a CHECK as being in
+// performance critical code.
+#if defined(NDEBUG)
+static constexpr bool kEnableDChecks = false;
+#else
+static constexpr bool kEnableDChecks = true;
+#endif
+
+#define DCHECK(x) \
+ if (::android::base::kEnableDChecks) CHECK(x)
+#define DCHECK_EQ(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_EQ(x, y)
+#define DCHECK_NE(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_NE(x, y)
+#define DCHECK_LE(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_LE(x, y)
+#define DCHECK_LT(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_LT(x, y)
+#define DCHECK_GE(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_GE(x, y)
+#define DCHECK_GT(x, y) \
+ if (::android::base::kEnableDChecks) CHECK_GT(x, y)
+#define DCHECK_STREQ(s1, s2) \
+ if (::android::base::kEnableDChecks) CHECK_STREQ(s1, s2)
+#define DCHECK_STRNE(s1, s2) \
+ if (::android::base::kEnableDChecks) CHECK_STRNE(s1, s2)
+#if defined(NDEBUG)
+#define DCHECK_CONSTEXPR(x, out, dummy)
+#else
+#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
+#endif
+
+// Temporary class created to evaluate the LHS and RHS, used with
+// MakeEagerEvaluator to infer the types of LHS and RHS.
+template <typename LHS, typename RHS>
+struct EagerEvaluator {
+ EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) {
+ }
+ LHS lhs;
+ RHS rhs;
+};
+
+// Helper function for CHECK_xx.
+template <typename LHS, typename RHS>
+static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
+ return EagerEvaluator<LHS, RHS>(lhs, rhs);
+}
+
+// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated
+// as strings. To compare strings use CHECK_STREQ and CHECK_STRNE. We rely on
+// signed/unsigned warnings to protect you against combinations not explicitly
+// listed below.
+#define EAGER_PTR_EVALUATOR(T1, T2) \
+ template <> \
+ struct EagerEvaluator<T1, T2> { \
+ EagerEvaluator(T1 l, T2 r) \
+ : lhs(reinterpret_cast<const void*>(l)), \
+ rhs(reinterpret_cast<const void*>(r)) { \
+ } \
+ const void* lhs; \
+ const void* rhs; \
+ }
+EAGER_PTR_EVALUATOR(const char*, const char*);
+EAGER_PTR_EVALUATOR(const char*, char*);
+EAGER_PTR_EVALUATOR(char*, const char*);
+EAGER_PTR_EVALUATOR(char*, char*);
+EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
+EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
+EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
+EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
+EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
+EAGER_PTR_EVALUATOR(const signed char*, signed char*);
+EAGER_PTR_EVALUATOR(signed char*, const signed char*);
+EAGER_PTR_EVALUATOR(signed char*, signed char*);
+
+// Data for the log message, not stored in LogMessage to avoid increasing the
+// stack size.
+class LogMessageData;
+
+// A LogMessage is a temporarily scoped object used by LOG and the unlikely part
+// of a CHECK. The destructor will abort if the severity is FATAL.
+class LogMessage {
+ public:
+ LogMessage(const char* file, unsigned int line, LogSeverity severity,
+ int error);
+
+ ~LogMessage();
+
+ // Returns the stream associated with the message, the LogMessage performs
+ // output when it goes out of scope.
+ std::ostream& stream();
+
+ // The routine that performs the actual logging.
+ static void LogLine(const char* file, unsigned int line, LogSeverity severity,
+ const char* msg);
+
+ // A variant of the above for use with little stack.
+ static void LogLineLowStack(const char* file, unsigned int line,
+ LogSeverity severity, const char* msg);
+
+ private:
+ const std::unique_ptr<LogMessageData> data_;
+
+ DISALLOW_COPY_AND_ASSIGN(LogMessage);
+};
+
+// Allows to temporarily change the minimum severity level for logging.
+class ScopedLogSeverity {
+ public:
+ explicit ScopedLogSeverity(LogSeverity level);
+ ~ScopedLogSeverity();
+
+ private:
+ LogSeverity old_;
+};
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_LOGGING_H
diff --git a/base/include/base/strings.h b/base/include/base/strings.h
index 5ddfbbd..ab56aad 100644
--- a/base/include/base/strings.h
+++ b/base/include/base/strings.h
@@ -23,10 +23,15 @@
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);
+// Splits a string into a vector of strings.
+//
+// The string is split at each occurence of a character in delimiters.
+//
+// Empty splits will be omitted. I.e. Split("a,,b", ",") -> {"a", "b"}
+//
+// The empty string is not a valid delimiter list.
+std::vector<std::string> Split(const std::string& s,
+ const std::string& delimiters);
// Trims whitespace off both ends of the given string.
std::string Trim(const std::string& s);
diff --git a/base/logging.cpp b/base/logging.cpp
new file mode 100644
index 0000000..38ee2af
--- /dev/null
+++ b/base/logging.cpp
@@ -0,0 +1,302 @@
+/*
+ * 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/logging.h"
+
+#include <iostream>
+#include <limits>
+#include <mutex>
+#include <sstream>
+#include <string>
+#include <vector>
+
+#include "base/strings.h"
+#include "cutils/threads.h"
+
+// Headers for LogMessage::LogLine.
+#ifdef __ANDROID__
+#include <android/set_abort_message.h>
+#include "cutils/log.h"
+#else
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+namespace android {
+namespace base {
+
+static std::mutex logging_lock;
+
+static LogSeverity gMinimumLogSeverity = INFO;
+static std::unique_ptr<std::string> gCmdLine;
+static std::unique_ptr<std::string> gProgramInvocationName;
+static std::unique_ptr<std::string> gProgramInvocationShortName;
+
+const char* GetCmdLine() {
+ return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
+}
+
+const char* ProgramInvocationName() {
+ return (gProgramInvocationName.get() != nullptr)
+ ? gProgramInvocationName->c_str()
+ : "unknown";
+}
+
+const char* ProgramInvocationShortName() {
+ return (gProgramInvocationShortName.get() != nullptr)
+ ? gProgramInvocationShortName->c_str()
+ : "unknown";
+}
+
+void InitLogging(char* argv[]) {
+ if (gCmdLine.get() != nullptr) {
+ return;
+ }
+
+ // Stash the command line for later use. We can use /proc/self/cmdline on
+ // Linux to recover this, but we don't have that luxury on the Mac, and there
+ // are a couple of argv[0] variants that are commonly used.
+ if (argv != nullptr) {
+ gCmdLine.reset(new std::string(argv[0]));
+ for (size_t i = 1; argv[i] != nullptr; ++i) {
+ gCmdLine->append(" ");
+ gCmdLine->append(argv[i]);
+ }
+ gProgramInvocationName.reset(new std::string(argv[0]));
+ const char* last_slash = strrchr(argv[0], '/');
+ gProgramInvocationShortName.reset(
+ new std::string((last_slash != nullptr) ? last_slash + 1 : argv[0]));
+ } else {
+ // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux.
+ gCmdLine.reset(new std::string("<unset>"));
+ }
+ const char* tags = getenv("ANDROID_LOG_TAGS");
+ if (tags == nullptr) {
+ return;
+ }
+
+ std::vector<std::string> specs = Split(tags, " ");
+ for (size_t i = 0; i < specs.size(); ++i) {
+ // "tag-pattern:[vdiwefs]"
+ std::string spec(specs[i]);
+ if (spec.size() == 3 && StartsWith(spec, "*:")) {
+ switch (spec[2]) {
+ case 'v':
+ gMinimumLogSeverity = VERBOSE;
+ continue;
+ case 'd':
+ gMinimumLogSeverity = DEBUG;
+ continue;
+ case 'i':
+ gMinimumLogSeverity = INFO;
+ continue;
+ case 'w':
+ gMinimumLogSeverity = WARNING;
+ continue;
+ case 'e':
+ gMinimumLogSeverity = ERROR;
+ continue;
+ case 'f':
+ gMinimumLogSeverity = FATAL;
+ continue;
+ // liblog will even suppress FATAL if you say 's' for silent, but that's
+ // crazy!
+ case 's':
+ gMinimumLogSeverity = FATAL;
+ continue;
+ }
+ }
+ LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
+ << ")";
+ }
+}
+
+// This indirection greatly reduces the stack impact of having lots of
+// checks/logging in a function.
+class LogMessageData {
+ public:
+ LogMessageData(const char* file, unsigned int line, LogSeverity severity,
+ int error)
+ : file_(file), line_number_(line), severity_(severity), error_(error) {
+ const char* last_slash = strrchr(file, '/');
+ file = (last_slash == nullptr) ? file : last_slash + 1;
+ }
+
+ const char* GetFile() const {
+ return file_;
+ }
+
+ unsigned int GetLineNumber() const {
+ return line_number_;
+ }
+
+ LogSeverity GetSeverity() const {
+ return severity_;
+ }
+
+ int GetError() const {
+ return error_;
+ }
+
+ std::ostream& GetBuffer() {
+ return buffer_;
+ }
+
+ std::string ToString() const {
+ return buffer_.str();
+ }
+
+ private:
+ std::ostringstream buffer_;
+ const char* const file_;
+ const unsigned int line_number_;
+ const LogSeverity severity_;
+ const int error_;
+
+ DISALLOW_COPY_AND_ASSIGN(LogMessageData);
+};
+
+LogMessage::LogMessage(const char* file, unsigned int line,
+ LogSeverity severity, int error)
+ : data_(new LogMessageData(file, line, severity, error)) {
+}
+
+LogMessage::~LogMessage() {
+ if (data_->GetSeverity() < gMinimumLogSeverity) {
+ return; // No need to format something we're not going to output.
+ }
+
+ // Finish constructing the message.
+ if (data_->GetError() != -1) {
+ data_->GetBuffer() << ": " << strerror(data_->GetError());
+ }
+ std::string msg(data_->ToString());
+
+ // Do the actual logging with the lock held.
+ {
+ std::lock_guard<std::mutex> lock(logging_lock);
+ if (msg.find('\n') == std::string::npos) {
+ LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(),
+ msg.c_str());
+ } else {
+ msg += '\n';
+ size_t i = 0;
+ while (i < msg.size()) {
+ size_t nl = msg.find('\n', i);
+ msg[nl] = '\0';
+ LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(),
+ &msg[i]);
+ i = nl + 1;
+ }
+ }
+ }
+
+ // Abort if necessary.
+ if (data_->GetSeverity() == FATAL) {
+#ifdef __ANDROID__
+ android_set_abort_message(msg.c_str());
+#endif
+ abort();
+ }
+}
+
+std::ostream& LogMessage::stream() {
+ return data_->GetBuffer();
+}
+
+#ifdef __ANDROID__
+static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
+ ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
+ ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
+static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
+ "Mismatch in size of kLogSeverityToAndroidLogPriority and values "
+ "in LogSeverity");
+#endif
+
+void LogMessage::LogLine(const char* file, unsigned int line,
+ LogSeverity log_severity, const char* message) {
+#ifdef __ANDROID__
+ const char* tag = ProgramInvocationShortName();
+ int priority = kLogSeverityToAndroidLogPriority[log_severity];
+ if (priority == ANDROID_LOG_FATAL) {
+ LOG_PRI(priority, tag, "%s:%u] %s", file, line, message);
+ } else {
+ LOG_PRI(priority, tag, "%s", message);
+ }
+#else
+ static const char* log_characters = "VDIWEF";
+ CHECK_EQ(strlen(log_characters), FATAL + 1U);
+ char severity = log_characters[log_severity];
+ fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", ProgramInvocationShortName(),
+ severity, getpid(), gettid(), file, line, message);
+#endif
+}
+
+void LogMessage::LogLineLowStack(const char* file, unsigned int line,
+ LogSeverity log_severity, const char* message) {
+#ifdef __ANDROID__
+ // Use android_writeLog() to avoid stack-based buffers used by
+ // android_printLog().
+ const char* tag = ProgramInvocationShortName();
+ int priority = kLogSeverityToAndroidLogPriority[log_severity];
+ char* buf = nullptr;
+ size_t buf_size = 0u;
+ if (priority == ANDROID_LOG_FATAL) {
+ // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line,
+ // message) below. If allocation fails, fall back to printing only the
+ // message.
+ buf_size = strlen(file) + 1 /* ':' */ +
+ std::numeric_limits<typeof(line)>::max_digits10 + 2 /* "] " */ +
+ strlen(message) + 1 /* terminating 0 */;
+ buf = reinterpret_cast<char*>(malloc(buf_size));
+ }
+ if (buf != nullptr) {
+ snprintf(buf, buf_size, "%s:%u] %s", file, line, message);
+ android_writeLog(priority, tag, buf);
+ free(buf);
+ } else {
+ android_writeLog(priority, tag, message);
+ }
+#else
+ static const char* log_characters = "VDIWEF";
+ CHECK_EQ(strlen(log_characters), FATAL + 1U);
+
+ const char* program_name = ProgramInvocationShortName();
+ write(STDERR_FILENO, program_name, strlen(program_name));
+ write(STDERR_FILENO, " ", 1);
+ write(STDERR_FILENO, &log_characters[log_severity], 1);
+ write(STDERR_FILENO, " ", 1);
+ // TODO: pid and tid.
+ write(STDERR_FILENO, file, strlen(file));
+ // TODO: line.
+ UNUSED(line);
+ write(STDERR_FILENO, "] ", 2);
+ write(STDERR_FILENO, message, strlen(message));
+ write(STDERR_FILENO, "\n", 1);
+#endif
+}
+
+ScopedLogSeverity::ScopedLogSeverity(LogSeverity level) {
+ old_ = gMinimumLogSeverity;
+ gMinimumLogSeverity = level;
+}
+
+ScopedLogSeverity::~ScopedLogSeverity() {
+ gMinimumLogSeverity = old_;
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
new file mode 100644
index 0000000..0a03e38
--- /dev/null
+++ b/base/logging_test.cpp
@@ -0,0 +1,171 @@
+/*
+ * 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/logging.h"
+
+#include <regex>
+#include <string>
+
+#include "base/file.h"
+#include "base/stringprintf.h"
+#include "test_utils.h"
+
+#include <gtest/gtest.h>
+
+#ifdef __ANDROID__
+#define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name)
+#else
+#define HOST_TEST(suite, name) TEST(suite, name)
+#endif
+
+class CapturedStderr {
+ public:
+ CapturedStderr() : old_stderr_(-1) {
+ init();
+ }
+
+ ~CapturedStderr() {
+ reset();
+ }
+
+ int fd() const {
+ return temp_file_.fd;
+ }
+
+ private:
+ void init() {
+ old_stderr_ = dup(STDERR_FILENO);
+ ASSERT_NE(-1, old_stderr_);
+ ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
+ }
+
+ void reset() {
+ ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
+ ASSERT_EQ(0, close(old_stderr_));
+ }
+
+ TemporaryFile temp_file_;
+ int old_stderr_;
+};
+
+HOST_TEST(logging, CHECK) {
+ ASSERT_DEATH(CHECK(false), "Check failed: false ");
+ CHECK(true);
+
+ ASSERT_DEATH(CHECK_EQ(0, 1), "Check failed: 0 == 1 ");
+ CHECK_EQ(0, 0);
+
+ ASSERT_DEATH(CHECK_STREQ("foo", "bar"), R"(Check failed: "foo" == "bar")");
+ CHECK_STREQ("foo", "foo");
+}
+
+std::string make_log_pattern(android::base::LogSeverity severity,
+ const char* message) {
+ static const char* log_characters = "VDIWEF";
+ char log_char = log_characters[severity];
+ return android::base::StringPrintf(
+ "%c[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+ " __FILE__
+ ":[[:digit:]]+] %s",
+ log_char, message);
+}
+
+HOST_TEST(logging, LOG) {
+ ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar");
+
+ {
+ CapturedStderr cap;
+ LOG(WARNING) << "foobar";
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+
+ std::regex message_regex(
+ make_log_pattern(android::base::WARNING, "foobar"));
+ ASSERT_TRUE(std::regex_search(output, message_regex));
+ }
+
+ {
+ CapturedStderr cap;
+ LOG(INFO) << "foobar";
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+
+ std::regex message_regex(
+ make_log_pattern(android::base::INFO, "foobar"));
+ ASSERT_TRUE(std::regex_search(output, message_regex));
+ }
+
+ {
+ CapturedStderr cap;
+ LOG(DEBUG) << "foobar";
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+ ASSERT_TRUE(output.empty());
+ }
+
+ {
+ android::base::ScopedLogSeverity severity(android::base::DEBUG);
+ CapturedStderr cap;
+ LOG(DEBUG) << "foobar";
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+
+ std::regex message_regex(
+ make_log_pattern(android::base::DEBUG, "foobar"));
+ ASSERT_TRUE(std::regex_search(output, message_regex));
+ }
+}
+
+HOST_TEST(logging, PLOG) {
+ {
+ CapturedStderr cap;
+ errno = ENOENT;
+ PLOG(INFO) << "foobar";
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+
+ std::regex message_regex(make_log_pattern(
+ android::base::INFO, "foobar: No such file or directory"));
+ ASSERT_TRUE(std::regex_search(output, message_regex));
+ }
+}
+
+HOST_TEST(logging, UNIMPLEMENTED) {
+ {
+ CapturedStderr cap;
+ errno = ENOENT;
+ UNIMPLEMENTED(ERROR);
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+
+ std::string expected_message =
+ android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
+ std::regex message_regex(
+ make_log_pattern(android::base::ERROR, expected_message.c_str()));
+ ASSERT_TRUE(std::regex_search(output, message_regex));
+ }
+}
diff --git a/base/strings.cpp b/base/strings.cpp
index 224a46f..6f698d9 100644
--- a/base/strings.cpp
+++ b/base/strings.cpp
@@ -16,27 +16,40 @@
#include "base/strings.h"
+#include <stdlib.h>
+#include <string.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));
- }
+#define CHECK_NE(a, b) \
+ if ((a) == (b)) abort();
+
+std::vector<std::string> Split(const std::string& s,
+ const std::string& delimiters) {
+ CHECK_NE(delimiters.size(), 0U);
+
+ std::vector<std::string> split;
+ if (s.size() == 0) {
+ // Split("", d) returns {} rather than {""}.
+ return split;
}
+
+ size_t base = 0;
+ size_t found;
+ do {
+ found = s.find_first_of(delimiters, base);
+ if (found != base) {
+ split.push_back(s.substr(base, found - base));
+ }
+
+ base = found + 1;
+ } while (found != s.npos);
+
+ return split;
}
std::string Trim(const std::string& s) {
diff --git a/base/strings_test.cpp b/base/strings_test.cpp
index 824598d..1bf07a1 100644
--- a/base/strings_test.cpp
+++ b/base/strings_test.cpp
@@ -22,21 +22,18 @@
#include <vector>
TEST(strings, split_empty) {
- std::vector<std::string> parts;
- android::base::Split("", '\0', &parts);
+ std::vector<std::string> parts = android::base::Split("", ",");
ASSERT_EQ(0U, parts.size());
}
TEST(strings, split_single) {
- std::vector<std::string> parts;
- android::base::Split("foo", ',', &parts);
+ std::vector<std::string> parts = android::base::Split("foo", ",");
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);
+ std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
ASSERT_EQ(3U, parts.size());
ASSERT_EQ("foo", parts[0]);
ASSERT_EQ("bar", parts[1]);
@@ -44,8 +41,30 @@
}
TEST(strings, split_with_empty_part) {
- std::vector<std::string> parts;
- android::base::Split("foo,,bar", ',', &parts);
+ std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
+ ASSERT_EQ(2U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+}
+
+TEST(strings, split_null_char) {
+ std::vector<std::string> parts =
+ android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
+ ASSERT_EQ(2U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+}
+
+TEST(strings, split_any) {
+ std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
+ ASSERT_EQ(3U, parts.size());
+ ASSERT_EQ("foo", parts[0]);
+ ASSERT_EQ("bar", parts[1]);
+ ASSERT_EQ("baz", parts[2]);
+}
+
+TEST(strings, split_any_with_empty_part) {
+ std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
ASSERT_EQ(2U, parts.size());
ASSERT_EQ("foo", parts[0]);
ASSERT_EQ("bar", parts[1]);
diff --git a/base/test_main.cpp b/base/test_main.cpp
new file mode 100644
index 0000000..c49ca4b
--- /dev/null
+++ b/base/test_main.cpp
@@ -0,0 +1,25 @@
+/*
+ * 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 <gtest/gtest.h>
+
+#include "base/logging.h"
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ android::base::InitLogging(argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
new file mode 100644
index 0000000..1f6d3cf
--- /dev/null
+++ b/base/test_utils.cpp
@@ -0,0 +1,38 @@
+/*
+ * 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 "test_utils.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+TemporaryFile::TemporaryFile() {
+ init("/data/local/tmp");
+ if (fd == -1) {
+ init("/tmp");
+ }
+}
+
+TemporaryFile::~TemporaryFile() {
+ close(fd);
+ unlink(filename);
+}
+
+void TemporaryFile::init(const char* tmp_dir) {
+ snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
+ fd = mkstemp(filename);
+}
diff --git a/base/test_utils.h b/base/test_utils.h
new file mode 100644
index 0000000..132d3a7
--- /dev/null
+++ b/base/test_utils.h
@@ -0,0 +1,32 @@
+/*
+ * 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 TEST_UTILS_H
+#define TEST_UTILS_H
+
+class TemporaryFile {
+ public:
+ TemporaryFile();
+ ~TemporaryFile();
+
+ int fd;
+ char filename[1024];
+
+ private:
+ void init(const char* tmp_dir);
+};
+
+#endif // TEST_UTILS_H
diff --git a/fs_mgr/fs_mgr_fstab.c b/fs_mgr/fs_mgr_fstab.c
index 64f7edc..97cf811 100644
--- a/fs_mgr/fs_mgr_fstab.c
+++ b/fs_mgr/fs_mgr_fstab.c
@@ -24,6 +24,7 @@
struct fs_mgr_flag_values {
char *key_loc;
+ char *verity_loc;
long long part_length;
char *label;
int partnum;
@@ -108,6 +109,14 @@
* location of the keys. Get it and return it.
*/
flag_vals->key_loc = strdup(strchr(p, '=') + 1);
+ } else if ((fl[i].flag == MF_VERIFY) && flag_vals) {
+ /* If the verify flag is followed by an = and the
+ * location for the verity state, get it and return it.
+ */
+ char *start = strchr(p, '=');
+ if (start) {
+ flag_vals->verity_loc = strdup(start + 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.
@@ -292,6 +301,7 @@
fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
&flag_vals, NULL, 0);
fstab->recs[cnt].key_loc = flag_vals.key_loc;
+ fstab->recs[cnt].verity_loc = flag_vals.verity_loc;
fstab->recs[cnt].length = flag_vals.part_length;
fstab->recs[cnt].label = flag_vals.label;
fstab->recs[cnt].partnum = flag_vals.partnum;
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index 5c67333..acdc5a3 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -47,16 +47,23 @@
#define VERITY_METADATA_SIZE 32768
#define VERITY_TABLE_RSA_KEY "/verity_key"
+#define METADATA_MAGIC 0x01564c54
+#define METADATA_TAG_MAX_LENGTH 63
+#define METADATA_EOD "eod"
+
+#define VERITY_STATE_TAG "verity_state"
#define VERITY_STATE_HEADER 0x83c0ae9d
#define VERITY_STATE_VERSION 1
#define VERITY_KMSG_RESTART "dm-verity device corrupted"
#define VERITY_KMSG_BUFSIZE 1024
+#define __STRINGIFY(x) #x
+#define STRINGIFY(x) __STRINGIFY(x)
+
struct verity_state {
uint32_t header;
uint32_t version;
- uint32_t size;
int32_t mode;
};
@@ -453,17 +460,116 @@
return 0;
}
+static int metadata_add(FILE *fp, long start, const char *tag,
+ unsigned int length, off64_t *offset)
+{
+ if (fseek(fp, start, SEEK_SET) < 0 ||
+ fprintf(fp, "%s %u\n", tag, length) < 0) {
+ return -1;
+ }
+
+ *offset = ftell(fp);
+
+ if (fseek(fp, length, SEEK_CUR) < 0 ||
+ fprintf(fp, METADATA_EOD " 0\n") < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int metadata_find(const char *fname, const char *stag,
+ unsigned int slength, off64_t *offset)
+{
+ FILE *fp = NULL;
+ char tag[METADATA_TAG_MAX_LENGTH + 1];
+ int rc = -1;
+ int n;
+ long start = 0x4000; /* skip cryptfs metadata area */
+ uint32_t magic;
+ unsigned int length = 0;
+
+ if (!fname) {
+ return -1;
+ }
+
+ fp = fopen(fname, "r+");
+
+ if (!fp) {
+ ERROR("Failed to open %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ /* check magic */
+ if (fseek(fp, start, SEEK_SET) < 0 ||
+ fread(&magic, sizeof(magic), 1, fp) != 1) {
+ ERROR("Failed to read magic from %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ if (magic != METADATA_MAGIC) {
+ magic = METADATA_MAGIC;
+
+ if (fseek(fp, start, SEEK_SET) < 0 ||
+ fwrite(&magic, sizeof(magic), 1, fp) != 1) {
+ ERROR("Failed to write magic to %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+
+ rc = metadata_add(fp, start + sizeof(magic), stag, slength, offset);
+ if (rc < 0) {
+ ERROR("Failed to add metadata to %s: %s\n", fname, strerror(errno));
+ }
+
+ goto out;
+ }
+
+ start += sizeof(magic);
+
+ while (1) {
+ n = fscanf(fp, "%" STRINGIFY(METADATA_TAG_MAX_LENGTH) "s %u\n",
+ tag, &length);
+
+ if (n == 2 && strcmp(tag, METADATA_EOD)) {
+ /* found a tag */
+ start = ftell(fp);
+
+ if (!strcmp(tag, stag) && length == slength) {
+ *offset = start;
+ rc = 0;
+ goto out;
+ }
+
+ start += length;
+
+ if (fseek(fp, length, SEEK_CUR) < 0) {
+ ERROR("Failed to seek %s (%s)\n", fname, strerror(errno));
+ goto out;
+ }
+ } else {
+ rc = metadata_add(fp, start, stag, slength, offset);
+ if (rc < 0) {
+ ERROR("Failed to write metadata to %s: %s\n", fname,
+ strerror(errno));
+ }
+ goto out;
+ }
+ }
+
+out:
+ if (fp) {
+ fflush(fp);
+ fclose(fp);
+ }
+
+ return rc;
+}
+
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
- };
+ struct verity_state s = { VERITY_STATE_HEADER, VERITY_STATE_VERSION, mode };
fd = TEMP_FAILURE_RETRY(open(fname, O_WRONLY | O_SYNC | O_CLOEXEC));
@@ -472,13 +578,9 @@
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));
+ if (TEMP_FAILURE_RETRY(pwrite64(fd, &s, sizeof(s), offset)) != sizeof(s)) {
+ ERROR("Failed to write %zu bytes to %s to offset %" PRIu64 " (%s)\n",
+ sizeof(s), fname, offset, strerror(errno));
goto out;
}
@@ -492,44 +594,16 @@
return rc;
}
-static int get_verity_state_location(char *location, off64_t *offset)
+static int load_verity_state(struct fstab_rec *fstab, int *mode)
{
- 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 */
+ if (metadata_find(fstab->verity_loc, VERITY_STATE_TAG, sizeof(s),
+ &offset) < 0) {
+ /* fall back to stateless behavior */
*mode = VERITY_MODE_EIO;
rc = 0;
goto out;
@@ -539,28 +613,27 @@
/* 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);
+ rc = write_verity_state(fstab->verity_loc, offset, *mode);
goto out;
}
- fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
+ fd = TEMP_FAILURE_RETRY(open(fstab->verity_loc, O_RDONLY | O_CLOEXEC));
if (fd == -1) {
- ERROR("Failed to open %s (%s)\n", fname, strerror(errno));
+ ERROR("Failed to open %s (%s)\n", fstab->verity_loc, 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));
+ if (TEMP_FAILURE_RETRY(pread64(fd, &s, sizeof(s), offset)) != sizeof(s)) {
+ ERROR("Failed to read %zu bytes from %s offset %" PRIu64 " (%s)\n",
+ sizeof(s), fstab->verity_loc, offset, strerror(errno));
goto out;
}
if (s.header != VERITY_STATE_HEADER) {
+ /* space allocated, but no state written. write default state */
+ *mode = VERITY_MODE_DEFAULT;
+ rc = write_verity_state(fstab->verity_loc, offset, *mode);
goto out;
}
@@ -569,11 +642,6 @@
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);
@@ -591,13 +659,61 @@
return rc;
}
+int fs_mgr_load_verity_state(int *mode)
+{
+ char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+ char propbuf[PROPERTY_VALUE_MAX];
+ int rc = -1;
+ int i;
+ struct fstab *fstab = NULL;
+
+ *mode = VERITY_MODE_DEFAULT;
+
+ 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;
+ }
+
+ rc = load_verity_state(&fstab->recs[i], mode);
+ if (rc < 0) {
+ continue;
+ }
+
+ /* if any of the verified partitions are in logging mode, return */
+ if (*mode == VERITY_MODE_LOGGING) {
+ rc = 0;
+ goto out;
+ }
+ }
+
+ /* if there were multiple partitions, all in non-logging mode, return the
+ * state of the last one */
+ rc = 0;
+
+out:
+ if (fstab) {
+ fs_mgr_free_fstab(fstab);
+ }
+
+ return rc;
+}
+
int fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback)
{
_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;
@@ -606,10 +722,6 @@
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) {
@@ -632,6 +744,11 @@
continue;
}
+ if (metadata_find(fstab->recs[i].verity_loc, VERITY_STATE_TAG,
+ sizeof(struct verity_state), &offset) < 0) {
+ continue;
+ }
+
mount_point = basename(fstab->recs[i].mount_point);
verity_ioctl_init(io, mount_point, 0);
@@ -644,7 +761,8 @@
status = &buffer[io->data_start + sizeof(struct dm_target_spec)];
if (*status == 'C') {
- rc = write_verity_state(state_loc, offset, VERITY_MODE_LOGGING);
+ rc = write_verity_state(fstab->recs[i].verity_loc, offset,
+ VERITY_MODE_LOGGING);
if (rc == -1) {
goto out;
@@ -732,8 +850,12 @@
goto out;
}
- if (fs_mgr_load_verity_state(&mode) < 0) {
- mode = VERITY_MODE_RESTART; /* default dm-verity mode */
+ if (load_verity_state(fstab, &mode) < 0) {
+ /* if accessing or updating the state failed, switch to the default
+ * safe mode. This makes sure the device won't end up in an endless
+ * restart loop, and no corrupted data will be exposed to userspace
+ * without a warning. */
+ mode = VERITY_MODE_EIO;
}
INFO("Enabling dm-verity for %s (mode %d)\n", mount_point, mode);
diff --git a/fs_mgr/include/fs_mgr.h b/fs_mgr/include/fs_mgr.h
index 1af16c2..2655100 100644
--- a/fs_mgr/include/fs_mgr.h
+++ b/fs_mgr/include/fs_mgr.h
@@ -36,7 +36,8 @@
VERITY_MODE_EIO = 0,
VERITY_MODE_LOGGING = 1,
VERITY_MODE_RESTART = 2,
- VERITY_MODE_LAST = VERITY_MODE_RESTART
+ VERITY_MODE_LAST = VERITY_MODE_RESTART,
+ VERITY_MODE_DEFAULT = VERITY_MODE_RESTART
};
/*
diff --git a/include/cutils/klog.h b/include/cutils/klog.h
index d5ae6d7..295d62b 100644
--- a/include/cutils/klog.h
+++ b/include/cutils/klog.h
@@ -18,6 +18,7 @@
#define _CUTILS_KLOG_H_
#include <sys/cdefs.h>
+#include <sys/uio.h>
#include <stdarg.h>
__BEGIN_DECLS
@@ -26,9 +27,10 @@
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);
+void klog_writev(int level, const struct iovec* iov, int iov_count);
__END_DECLS
diff --git a/include/cutils/threads.h b/include/cutils/threads.h
index ade9a0c..3133cdb 100644
--- a/include/cutils/threads.h
+++ b/include/cutils/threads.h
@@ -32,14 +32,16 @@
#if !defined(_WIN32)
#include <pthread.h>
+#include <sys/types.h>
typedef struct {
pthread_mutex_t lock;
int has_tls;
pthread_key_t tls;
-
} thread_store_t;
+extern pid_t gettid();
+
#define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
#else // !defined(_WIN32)
@@ -51,7 +53,6 @@
int has_tls;
DWORD tls;
CRITICAL_SECTION lock;
-
} thread_store_t;
#define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
diff --git a/include/system/audio.h b/include/system/audio.h
index 17bf260..04252e7 100644
--- a/include/system/audio.h
+++ b/include/system/audio.h
@@ -872,7 +872,7 @@
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"*/
+/* the maximum length for the human-readable device name */
#define AUDIO_PORT_MAX_NAME_LEN 128
/* maximum audio device address length */
diff --git a/include/sysutils/NetlinkEvent.h b/include/sysutils/NetlinkEvent.h
index a428d37..b80f3ea 100644
--- a/include/sysutils/NetlinkEvent.h
+++ b/include/sysutils/NetlinkEvent.h
@@ -44,19 +44,6 @@
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;
- const static int NlActionChange;
- const static int NlActionLinkDown;
- const static int NlActionLinkUp;
- const static int NlActionAddressUpdated;
- const static int NlActionAddressRemoved;
- const static int NlActionRdnss;
- const static int NlActionRouteUpdated;
- const static int NlActionRouteRemoved;
-
NetlinkEvent();
virtual ~NetlinkEvent();
diff --git a/include/utils/Compat.h b/include/utils/Compat.h
index a238afe..ca4a8e0 100644
--- a/include/utils/Compat.h
+++ b/include/utils/Compat.h
@@ -36,7 +36,7 @@
#endif /* __APPLE__ */
#if defined(_WIN32)
-#define O_CLOEXEC 0
+#define O_CLOEXEC O_NOINHERIT
#define O_NOFOLLOW 0
#define DEFFILEMODE 0666
#endif /* _WIN32 */
diff --git a/include/utils/Looper.h b/include/utils/Looper.h
index a381251..da2d5f2 100644
--- a/include/utils/Looper.h
+++ b/include/utils/Looper.h
@@ -447,8 +447,7 @@
const bool mAllowNonCallbacks; // immutable
- int mWakeReadPipeFd; // immutable
- int mWakeWritePipeFd; // immutable
+ int mWakeEventFd; // immutable
Mutex mLock;
Vector<MessageEnvelope> mMessageEnvelopes; // guarded by mLock
diff --git a/include/zipfile/zipfile.h b/include/zipfile/zipfile.h
deleted file mode 100644
index 0ae4ee4..0000000
--- a/include/zipfile/zipfile.h
+++ /dev/null
@@ -1,58 +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.
- */
-
-#ifndef _ZIPFILE_ZIPFILE_H
-#define _ZIPFILE_ZIPFILE_H
-
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef void* zipfile_t;
-typedef void* zipentry_t;
-
-// Provide a buffer. Returns NULL on failure.
-zipfile_t init_zipfile(const void* data, size_t size);
-
-// Release the zipfile resources.
-void release_zipfile(zipfile_t file);
-
-// Get a named entry object. Returns NULL if it doesn't exist
-// or if we won't be able to decompress it. The zipentry_t is
-// freed by release_zipfile()
-zipentry_t lookup_zipentry(zipfile_t file, const char* entryName);
-
-// Return the size of the entry.
-size_t get_zipentry_size(zipentry_t entry);
-
-// return the filename of this entry, you own the memory returned
-char* get_zipentry_name(zipentry_t entry);
-
-// The buffer must be 1.001 times the buffer size returned
-// by get_zipentry_size. Returns nonzero on failure.
-int decompress_zipentry(zipentry_t entry, void* buf, int bufsize);
-
-// iterate through the entries in the zip file. pass a pointer to
-// a void* initialized to NULL to start. Returns NULL when done
-zipentry_t iterate_zipfile(zipfile_t file, void** cookie);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-#endif // _ZIPFILE_ZIPFILE_H
diff --git a/init/Android.mk b/init/Android.mk
index 5b8094f..94d3dad 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -18,17 +18,21 @@
-Wno-unused-parameter \
-Werror \
+init_clang := true
+
# --
include $(CLEAR_VARS)
LOCAL_CPPFLAGS := $(init_cflags)
LOCAL_SRC_FILES:= \
init_parser.cpp \
+ log.cpp \
parser.cpp \
util.cpp \
LOCAL_STATIC_LIBRARIES := libbase
LOCAL_MODULE := libinit
+LOCAL_CLANG := $(init_clang)
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
@@ -68,6 +72,7 @@
ln -sf ../init $(TARGET_ROOT_OUT)/sbin/ueventd; \
ln -sf ../init $(TARGET_ROOT_OUT)/sbin/watchdogd
+LOCAL_CLANG := $(init_clang)
include $(BUILD_EXECUTABLE)
@@ -84,4 +89,5 @@
libbase \
LOCAL_STATIC_LIBRARIES := libinit
+LOCAL_CLANG := $(init_clang)
include $(BUILD_NATIVE_TEST)
diff --git a/init/bootchart.cpp b/init/bootchart.cpp
index 530eba8..95687cb 100644
--- a/init/bootchart.cpp
+++ b/init/bootchart.cpp
@@ -30,6 +30,7 @@
#include <time.h>
#include <unistd.h>
+#include <memory>
#include <string>
#include <base/file.h>
@@ -114,9 +115,9 @@
static void do_log_procs(FILE* log) {
do_log_uptime(log);
- DIR* dir = opendir("/proc");
+ std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
struct dirent* entry;
- while ((entry = readdir(dir)) != NULL) {
+ while ((entry = readdir(dir.get())) != NULL) {
// Only match numeric values.
char* end;
int pid = strtol(entry->d_name, &end, 10);
@@ -146,7 +147,6 @@
}
}
}
- closedir(dir);
fputc('\n', log);
}
@@ -195,13 +195,8 @@
}
// 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 );
- }
- }
+ close(open(LOG_ACCT, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
+ acct(LOG_ACCT);
log_header();
return count;
@@ -210,11 +205,12 @@
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));
+ 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);
+ NOTICE("Bootcharting started (will run for %d s).\n",
+ (g_remaining_samples * BOOTCHART_POLLING_MS) / 1000);
} else {
- NOTICE("bootcharting ignored\n");
+ NOTICE("Not bootcharting.\n");
}
return 0;
}
diff --git a/init/builtins.cpp b/init/builtins.cpp
index e659cfa..6daea37 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -475,7 +475,7 @@
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);
+ NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
}
if (WIFEXITED(status)) {
diff --git a/init/devices.cpp b/init/devices.cpp
index 3a9b753..96b1696 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -49,9 +49,9 @@
#include "log.h"
#define SYSFS_PREFIX "/sys"
-#define FIRMWARE_DIR1 "/etc/firmware"
-#define FIRMWARE_DIR2 "/vendor/firmware"
-#define FIRMWARE_DIR3 "/firmware/image"
+static const char *firmware_dirs[] = { "/etc/firmware",
+ "/vendor/firmware",
+ "/firmware/image" };
extern struct selabel_handle *sehandle;
@@ -364,13 +364,6 @@
return 0;
}
-static inline suseconds_t get_usecs(void)
-{
- struct timeval tv;
- gettimeofday(&tv, 0);
- return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
-}
-
static void parse_event(const char *msg, struct uevent *uevent)
{
uevent->action = "";
@@ -818,8 +811,9 @@
static void process_firmware_event(struct uevent *uevent)
{
- char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
+ char *root, *loading, *data;
int l, loading_fd, data_fd, fw_fd;
+ size_t i;
int booting = is_booting();
INFO("firmware: loading '%s' for '%s'\n",
@@ -837,62 +831,49 @@
if (l == -1)
goto loading_free_out;
- l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
- if (l == -1)
- goto data_free_out;
-
- l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
- if (l == -1)
- goto data_free_out;
-
- l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
- if (l == -1)
- goto data_free_out;
-
loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
if(loading_fd < 0)
- goto file_free_out;
+ goto data_free_out;
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|O_CLOEXEC);
- if(fw_fd < 0) {
- fw_fd = open(file2, O_RDONLY|O_CLOEXEC);
- if (fw_fd < 0) {
- fw_fd = open(file3, O_RDONLY|O_CLOEXEC);
- if (fw_fd < 0) {
- if (booting) {
- /* If we're not fully booted, we may be missing
- * filesystems needed for firmware, wait and retry.
- */
- usleep(100000);
- booting = is_booting();
- goto try_loading_again;
- }
- INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
- write(loading_fd, "-1", 2);
- goto data_close_out;
- }
+ for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
+ char *file = NULL;
+ l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
+ if (l == -1)
+ goto data_free_out;
+ fw_fd = open(file, O_RDONLY|O_CLOEXEC);
+ free(file);
+ if (fw_fd >= 0) {
+ if(!load_firmware(fw_fd, loading_fd, data_fd))
+ INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
+ else
+ INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
+ break;
}
}
-
- if(!load_firmware(fw_fd, loading_fd, data_fd))
- INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
- else
- INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
+ if (fw_fd < 0) {
+ if (booting) {
+ /* If we're not fully booted, we may be missing
+ * filesystems needed for firmware, wait and retry.
+ */
+ usleep(100000);
+ booting = is_booting();
+ goto try_loading_again;
+ }
+ INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
+ write(loading_fd, "-1", 2);
+ goto data_close_out;
+ }
close(fw_fd);
data_close_out:
close(data_fd);
loading_close_out:
close(loading_fd);
-file_free_out:
- free(file1);
- free(file2);
- free(file3);
data_free_out:
free(data);
loading_free_out:
@@ -1002,12 +983,7 @@
}
}
-void device_init(void)
-{
- suseconds_t t0, t1;
- struct stat info;
- int fd;
-
+void device_init() {
sehandle = NULL;
if (is_selinux_enabled() > 0) {
sehandle = selinux_android_file_context_handle();
@@ -1016,26 +992,22 @@
/* is 256K enough? udev uses 16MB! */
device_fd = uevent_open_socket(256*1024, true);
- if(device_fd < 0)
+ if (device_fd == -1) {
return;
-
- fcntl(device_fd, F_SETFD, FD_CLOEXEC);
+ }
fcntl(device_fd, F_SETFL, O_NONBLOCK);
- 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|O_CLOEXEC, 0000);
- close(fd);
- if (LOG_UEVENTS) {
- INFO("coldboot %ld uS\n", ((long) (t1 - t0)));
- }
- } else if (LOG_UEVENTS) {
- INFO("skipping coldboot, already done\n");
+ if (access(COLDBOOT_DONE, F_OK) == 0) {
+ NOTICE("Skipping coldboot, already done!\n");
+ return;
}
+
+ Timer t;
+ coldboot("/sys/class");
+ coldboot("/sys/block");
+ coldboot("/sys/devices");
+ close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
+ NOTICE("Coldboot took %.2fs.\n", t.duration());
}
int get_device_fd()
diff --git a/init/init.cpp b/init/init.cpp
index 1449bc6..b1d65db 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -83,7 +83,7 @@
bool waiting_for_exec = false;
void service::NotifyStateChange(const char* new_state) {
- if (!properties_inited()) {
+ if (!properties_initialized()) {
// If properties aren't available yet, we can't set them.
return;
}
@@ -246,7 +246,7 @@
}
}
- NOTICE("starting '%s'\n", svc->name);
+ NOTICE("Starting service '%s'...\n", svc->name);
pid_t pid = fork();
if (pid == 0) {
@@ -256,7 +256,7 @@
int fd, sz;
umask(077);
- if (properties_inited()) {
+ if (properties_initialized()) {
get_property_workspace(&fd, &sz);
snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
@@ -397,7 +397,7 @@
}
if (svc->pid) {
- NOTICE("service '%s' is being killed\n", svc->name);
+ NOTICE("Service '%s' is being killed...\n", svc->name);
kill(-svc->pid, SIGKILL);
svc->NotifyStateChange("stopping");
} else {
@@ -559,17 +559,18 @@
}
}
-void execute_one_command(void)
-{
- int ret, i;
+void execute_one_command() {
+ Timer t;
+
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)
+ if (!cur_action) {
return;
+ }
build_triggers_string(name_str, sizeof(name_str), cur_action);
@@ -579,20 +580,26 @@
cur_command = get_next_command(cur_action, cur_command);
}
- if (!cur_command)
+ if (!cur_command) {
return;
+ }
- ret = cur_command->func(cur_command->nargs, cur_command->args);
+ int result = cur_command->func(cur_command->nargs, cur_command->args);
if (klog_get_level() >= KLOG_INFO_LEVEL) {
- for (i = 0; i < cur_command->nargs; i++) {
+ for (int 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);
+ char source[256];
+ if (cur_command->filename) {
+ snprintf(source, sizeof(source), " (%s:%d)", cur_command->filename, cur_command->line);
+ } else {
+ *source = '\0';
+ }
+ INFO("Command '%s' action=%s%s returned %d took %.2fs\n",
+ cmd_str, cur_action ? name_str : "", source, result, t.duration());
}
}
@@ -928,40 +935,25 @@
return 0;
}
-static 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() {
+ Timer t;
-static void selinux_initialize(void)
-{
+ selinux_callback cb;
+ cb.func_log = selinux_klog_callback;
+ selinux_set_callback(SELINUX_CB_LOG, cb);
+ cb.func_audit = audit_callback;
+ selinux_set_callback(SELINUX_CB_AUDIT, cb);
+
if (selinux_is_disabled()) {
return;
}
- INFO("loading selinux policy\n");
+ INFO("Loading SELinux policy...\n");
if (selinux_android_load_policy() < 0) {
ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
@@ -972,14 +964,18 @@
bool is_enforcing = selinux_is_enforcing();
INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
security_setenforce(is_enforcing);
+
+ NOTICE("(Initializing SELinux took %.2fs.)\n", t.duration());
}
int main(int argc, char** argv) {
- if (!strcmp(basename(argv[0]), "ueventd"))
+ if (!strcmp(basename(argv[0]), "ueventd")) {
return ueventd_main(argc, argv);
+ }
- if (!strcmp(basename(argv[0]), "watchdogd"))
+ if (!strcmp(basename(argv[0]), "watchdogd")) {
return watchdogd_main(argc, argv);
+ }
// Clear the umask.
umask(0);
@@ -1008,36 +1004,32 @@
// to the outside world.
open_devnull_stdio();
klog_init();
+ klog_set_level(KLOG_NOTICE_LEVEL);
+
+ NOTICE("init started!\n");
+
property_init();
+ // If arguments are passed both on the command line and in DT,
+ // properties set in DT always have priority over the command-line ones.
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();
- /* now propogate the kernel variables to internal variables
- * used by init as well as the current required properties
- */
+ // 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.
+ INFO("Running restorecon...\n");
restorecon("/dev");
restorecon("/dev/socket");
restorecon("/dev/__properties__");
restorecon_recursive("/sys");
- INFO("property init\n");
property_load_boot_defaults();
init_parse_config_file("/init.rc");
@@ -1049,7 +1041,7 @@
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.
+ // Trigger all the boot actions to get us started.
action_for_each_trigger("init", action_add_queue_tail);
// Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
diff --git a/init/init_parser.cpp b/init/init_parser.cpp
index 7db203f..294dc19 100644
--- a/init/init_parser.cpp
+++ b/init/init_parser.cpp
@@ -14,15 +14,16 @@
* limitations under the License.
*/
+#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <stdarg.h>
#include <string.h>
-#include <stddef.h>
-#include <ctype.h>
+#include <unistd.h>
#include "init.h"
#include "parser.h"
@@ -118,6 +119,7 @@
switch (*s++) {
case 'b':
if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
+ break;
case 'c':
if (!strcmp(s, "opy")) return K_copy;
if (!strcmp(s, "apability")) return K_capability;
@@ -350,7 +352,7 @@
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);
+ INFO("Added '%s' to import list\n", import->filename);
}
static void parse_new_section(struct parse_state *state, int kw,
@@ -438,6 +440,7 @@
int init_parse_config_file(const char* path) {
INFO("Parsing %s...\n", path);
+ Timer t;
std::string data;
if (!read_file(path, &data)) {
return -1;
@@ -445,6 +448,8 @@
parse_config(path, data);
dump_parser_state();
+
+ NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
return 0;
}
diff --git a/init/keychords.cpp b/init/keychords.cpp
index d6464bd..27894a2 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -80,7 +80,7 @@
ret = write(fd, keychords, keychords_length);
if (ret != keychords_length) {
- ERROR("could not configure /dev/keychord %d (%d)\n", ret, errno);
+ ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno));
close(fd);
fd = -1;
}
@@ -109,7 +109,7 @@
if (!strcmp(adb_enabled, "running")) {
svc = service_find_by_keychord(id);
if (svc) {
- INFO("starting service %s from keychord\n", svc->name);
+ INFO("Starting service %s from keychord\n", svc->name);
service_start(svc, NULL);
} else {
ERROR("service for keychord %d not found\n", id);
diff --git a/init/keywords.h b/init/keywords.h
index 532d7c5..09f645b 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -89,8 +89,8 @@
KEYWORD(symlink, COMMAND, 1, do_symlink)
KEYWORD(sysclktz, COMMAND, 1, do_sysclktz)
KEYWORD(user, OPTION, 0, 0)
- KEYWORD(verity_load_state, COMMAND, 1, do_verity_load_state)
- KEYWORD(verity_update_state, COMMAND, 1, do_verity_update_state)
+ KEYWORD(verity_load_state, COMMAND, 0, do_verity_load_state)
+ KEYWORD(verity_update_state, COMMAND, 0, do_verity_update_state)
KEYWORD(wait, COMMAND, 1, do_wait)
KEYWORD(write, COMMAND, 2, do_write)
KEYWORD(copy, COMMAND, 2, do_copy)
diff --git a/init/log.cpp b/init/log.cpp
new file mode 100644
index 0000000..d32f2da
--- /dev/null
+++ b/init/log.cpp
@@ -0,0 +1,62 @@
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include <sys/uio.h>
+
+#include <selinux/selinux.h>
+
+#include "log.h"
+
+static void init_klog_vwrite(int level, const char* fmt, va_list ap) {
+ static const char* tag = basename(getprogname());
+
+ char prefix[64];
+ snprintf(prefix, sizeof(prefix), "<%d>%s: ", level, tag);
+
+ char msg[512];
+ vsnprintf(msg, sizeof(msg), fmt, ap);
+
+ iovec iov[2];
+ iov[0].iov_base = prefix;
+ iov[0].iov_len = strlen(prefix);
+ iov[1].iov_base = msg;
+ iov[1].iov_len = strlen(msg);
+
+ klog_writev(level, iov, 2);
+}
+
+void init_klog_write(int level, const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ init_klog_vwrite(level, fmt, ap);
+ va_end(ap);
+}
+
+int selinux_klog_callback(int type, const char *fmt, ...) {
+ int level = KLOG_ERROR_LEVEL;
+ if (type == SELINUX_WARNING) {
+ level = KLOG_WARNING_LEVEL;
+ } else if (type == SELINUX_INFO) {
+ level = KLOG_INFO_LEVEL;
+ }
+ va_list ap;
+ va_start(ap, fmt);
+ init_klog_vwrite(level, fmt, ap);
+ va_end(ap);
+ return 0;
+}
diff --git a/init/log.h b/init/log.h
index e9cb65a..b804d1f 100644
--- a/init/log.h
+++ b/init/log.h
@@ -19,10 +19,11 @@
#include <cutils/klog.h>
-#define ERROR(x...) KLOG_ERROR("init", x)
-#define NOTICE(x...) KLOG_NOTICE("init", x)
-#define INFO(x...) KLOG_INFO("init", x)
+#define ERROR(x...) init_klog_write(KLOG_ERROR_LEVEL, x)
+#define NOTICE(x...) init_klog_write(KLOG_NOTICE_LEVEL, x)
+#define INFO(x...) init_klog_write(KLOG_INFO_LEVEL, x)
-extern int log_callback(int type, const char *fmt, ...);
+void init_klog_write(int level, const char* fmt, ...) __printflike(2, 3);
+int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
#endif
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 13d671f..2fa81d4 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -26,6 +26,8 @@
#include <errno.h>
#include <sys/poll.h>
+#include <memory>
+
#include <cutils/misc.h>
#include <cutils/sockets.h>
#include <cutils/multiuser.h>
@@ -52,7 +54,7 @@
#define PERSISTENT_PROPERTY_DIR "/data/property"
static int persistent_properties_loaded = 0;
-static int property_area_inited = 0;
+static bool property_area_initialized = false;
static int property_set_fd = -1;
@@ -61,34 +63,25 @@
int fd;
};
-static int init_workspace(workspace *w, size_t size)
-{
- int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
- if (fd < 0)
- return -1;
-
- w->size = size;
- w->fd = fd;
- return 0;
-}
-
static workspace pa_workspace;
-static int init_property_area(void)
-{
- if (property_area_inited)
- return -1;
+void property_init() {
+ if (property_area_initialized) {
+ return;
+ }
- if(__system_property_area_init())
- return -1;
+ property_area_initialized = true;
- if(init_workspace(&pa_workspace, 0))
- return -1;
+ if (__system_property_area_init()) {
+ return;
+ }
- fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC);
-
- property_area_inited = 1;
- return 0;
+ pa_workspace.size = 0;
+ pa_workspace.fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
+ if (pa_workspace.fd == -1) {
+ ERROR("Failed to open %s: %s\n", PROP_FILENAME, strerror(errno));
+ return;
+ }
}
static int check_mac_perms(const char *name, char *sctx)
@@ -159,7 +152,7 @@
snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
fd = mkstemp(tempPath);
if (fd < 0) {
- ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
+ ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
return;
}
write(fd, value, strlen(value));
@@ -248,7 +241,7 @@
int property_set(const char* name, const char* value) {
int rc = property_set_impl(name, value);
if (rc == -1) {
- ERROR("property_set(\"%s\", \"%s\" failed\n", name, value);
+ ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
}
return rc;
}
@@ -287,15 +280,15 @@
close(s);
return;
} else if (nr < 0) {
- ERROR("sys_prop: error waiting for uid=%d to send property message. err=%d %s\n", cr.uid, errno, strerror(errno));
+ ERROR("sys_prop: error waiting for uid=%d to send property message: %s\n", cr.uid, strerror(errno));
close(s);
return;
}
r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
if(r != sizeof(prop_msg)) {
- ERROR("sys_prop: mis-match msg size received: %d expected: %zu errno: %d\n",
- r, sizeof(prop_msg), errno);
+ ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
+ r, sizeof(prop_msg), strerror(errno));
close(s);
return;
}
@@ -417,87 +410,79 @@
* 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)
-{
+static void load_properties_from_file(const char* filename, const char* filter) {
+ Timer t;
std::string data;
- if (read_file(fn, &data)) {
+ if (read_file(filename, &data)) {
load_properties(&data[0], filter);
}
+ NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
}
-static void load_persistent_properties()
-{
- DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
- int dir_fd;
- struct dirent* entry;
- char value[PROP_VALUE_MAX];
- int fd, length;
- struct stat sb;
+static void load_persistent_properties() {
+ persistent_properties_loaded = 1;
- if (dir) {
- dir_fd = dirfd(dir);
- while ((entry = readdir(dir)) != NULL) {
- if (strncmp("persist.", entry->d_name, strlen("persist.")))
- continue;
- if (entry->d_type != DT_REG)
- continue;
- /* open the file and read the property value */
- fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
- if (fd < 0) {
- ERROR("Unable to open persistent property file \"%s\" errno: %d\n",
- entry->d_name, errno);
- continue;
- }
- if (fstat(fd, &sb) < 0) {
- ERROR("fstat on property file \"%s\" failed errno: %d\n", entry->d_name, errno);
- close(fd);
- continue;
- }
-
- // File must not be accessible to others, be owned by root/root, and
- // not be a hard link to any other file.
- if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0)
- || (sb.st_uid != 0)
- || (sb.st_gid != 0)
- || (sb.st_nlink != 1)) {
- 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;
- }
-
- length = read(fd, value, sizeof(value) - 1);
- if (length >= 0) {
- value[length] = 0;
- property_set(entry->d_name, value);
- } else {
- ERROR("Unable to read persistent property file %s errno: %d\n",
- entry->d_name, errno);
- }
- close(fd);
- }
- closedir(dir);
- } else {
- ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno);
+ std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
+ if (!dir) {
+ ERROR("Unable to open persistent property directory \"%s\": %s\n",
+ PERSISTENT_PROPERTY_DIR, strerror(errno));
+ return;
}
- persistent_properties_loaded = 1;
+ struct dirent* entry;
+ while ((entry = readdir(dir.get())) != NULL) {
+ if (strncmp("persist.", entry->d_name, strlen("persist."))) {
+ continue;
+ }
+ if (entry->d_type != DT_REG) {
+ continue;
+ }
+
+ // Open the file and read the property value.
+ int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
+ if (fd == -1) {
+ ERROR("Unable to open persistent property file \"%s\": %s\n",
+ entry->d_name, strerror(errno));
+ continue;
+ }
+
+ struct stat sb;
+ if (fstat(fd, &sb) == -1) {
+ ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
+ close(fd);
+ continue;
+ }
+
+ // File must not be accessible to others, be owned by root/root, and
+ // not be a hard link to any other file.
+ if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
+ (sb.st_nlink != 1)) {
+ 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;
+ }
+
+ char value[PROP_VALUE_MAX];
+ int length = read(fd, value, sizeof(value) - 1);
+ if (length >= 0) {
+ value[length] = 0;
+ property_set(entry->d_name, value);
+ } else {
+ ERROR("Unable to read persistent property file %s: %s\n",
+ entry->d_name, strerror(errno));
+ }
+ close(fd);
+ }
}
-void property_init(void)
-{
- init_property_area();
-}
-
-void property_load_boot_defaults(void)
-{
+void property_load_boot_defaults() {
load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
}
-int properties_inited(void)
-{
- return property_area_inited;
+bool properties_initialized() {
+ return property_area_initialized;
}
static void load_override_properties() {
@@ -510,21 +495,18 @@
}
}
-
/* When booting an encrypted system, /data is not mounted when the
* property service is started, so any properties stored there are
* not loaded. Vold triggers init to load these properties once it
* has mounted /data.
*/
-void load_persist_props(void)
-{
+void load_persist_props(void) {
load_override_properties();
/* Read persistent properties after all default values have been loaded. */
load_persistent_properties();
}
-void load_all_props(void)
-{
+void load_all_props() {
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);
@@ -537,12 +519,10 @@
load_persistent_properties();
}
-void start_property_service(void)
-{
- int fd;
+void start_property_service() {
+ int fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
+ if (fd == -1) return;
- fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0, NULL);
- if(fd < 0) return;
fcntl(fd, F_SETFD, FD_CLOEXEC);
fcntl(fd, F_SETFL, O_NONBLOCK);
@@ -550,7 +530,6 @@
property_set_fd = fd;
}
-int get_property_set_fd()
-{
+int get_property_set_fd() {
return property_set_fd;
}
diff --git a/init/property_service.h b/init/property_service.h
index 6e7fc00..825a7dd 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -29,7 +29,7 @@
void get_property_workspace(int *fd, int *sz);
extern int __property_get(const char *name, char *value);
extern int property_set(const char *name, const char *value);
-extern int properties_inited();
+extern bool properties_initialized();
int get_property_set_fd(void);
#ifndef __clang__
diff --git a/init/readme.txt b/init/readme.txt
index 7443330..630dd03 100644
--- a/init/readme.txt
+++ b/init/readme.txt
@@ -137,32 +137,9 @@
Commands
--------
-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. 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
- global environment (which will be inherited by all processes
- started after this command is executed)
-
-ifup <interface>
- Bring the network interface <interface> online.
-
-import <filename>
- Parse an init config file, extending the current configuration.
-
-hostname <name>
- Set the host name.
+bootchart_init
+ Start bootcharting if configured (see below).
+ This is included in the default init.rc.
chmod <octal-mode> <path>
Change file access permissions.
@@ -175,9 +152,18 @@
not already running.
class_stop <serviceclass>
- Stop all services of the specified class if they are
+ Stop and disable all services of the specified class if they are
currently running.
+class_reset <serviceclass>
+ Stop all services of the specified class if they are
+ currently running, without disabling them. They can be restarted
+ later using class_start.
+
+copy <src> <dst>
+ Copies a file. Similar to write, but useful for binary/large
+ amounts of data.
+
domainname <name>
Set the domain name.
@@ -190,9 +176,41 @@
on property:ro.boot.myfancyhardware=1
enable my_fancy_service_for_my_fancy_hardware
+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> ]*
+ Use exec instead. This command will be removed after existing callers have
+ moved to exec.
+
+export <name> <value>
+ Set the environment variable <name> equal to <value> in the
+ global environment (which will be inherited by all processes
+ started after this command is executed)
+
+hostname <name>
+ Set the host name.
+
+ifup <interface>
+ Bring the network interface <interface> online.
+
+import <filename>
+ Parse an init config file, extending the current configuration.
+
insmod <path>
Install the module at <path>
+load_all_props
+ Loads properties from /system, /vendor, et cetera.
+ This is included in the default init.rc.
+
+load_persist_props
+ Loads persistent properties when /data has been decrypted.
+ This is included in the default init.rc.
+
loglevel <level>
Sets the kernel log level to level. Properties are expanded within <level>.
@@ -202,6 +220,9 @@
owned by the root user and root group. If provided, the mode, owner and group
will be updated if the directory exists already.
+mount_all <fstab>
+ Calls fs_mgr_mount_all on the given fs_mgr-format fstab.
+
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
@@ -210,6 +231,13 @@
<options> include "barrier=1", "noauto_da_alloc", "discard", ... as
a comma separated string, eg: barrier=1,noauto_da_alloc
+powerctl
+ Internal implementation detail used to respond to changes to the
+ "sys.powerctl" system property, used to implement rebooting.
+
+restart <service>
+ Like stop, but doesn't disable the service.
+
restorecon <path> [ <path> ]*
Restore the file named by <path> to the security context specified
in the file_contexts configuration.
@@ -220,6 +248,14 @@
Recursively restore the directory tree named by <path> to the
security contexts specified in the file_contexts configuration.
+rm <path>
+ Calls unlink(2) on the given path. You might want to
+ use "exec -- rm ..." instead (provided the system partition is
+ already mounted).
+
+rmdir <path>
+ Calls rmdir(2) on the given path.
+
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
@@ -238,6 +274,9 @@
stop <service>
Stop a service from running if it is currently running.
+swapon_all <fstab>
+ Calls fs_mgr_swapon_all on the given fstab file.
+
symlink <target> <path>
Create a symbolic link at <path> with the value <target>
@@ -248,10 +287,18 @@
Trigger an event. Used to queue an action from another
action.
+verity_load_state
+ Internal implementation detail used to load dm-verity state.
+
+verity_update_state <mount_point>
+ Internal implementation detail used to update dm-verity state and
+ set the partition.<mount_point>.verified properties used by adb remount
+ because fs_mgr can't set them directly itself.
+
wait <path> [ <timeout> ]
- Poll for the existence of the given file and return when found,
- or the timeout has been reached. If timeout is not specified it
- currently defaults to five seconds.
+ Poll for the existence of the given file and return when found,
+ or the timeout has been reached. If timeout is not specified it
+ currently defaults to five seconds.
write <path> <content>
Open the file at <path> and write a string to it with write(2).
@@ -276,12 +323,11 @@
Bootcharting
------------
-
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.
-On the emulator, use the new -bootchart <timeout> option to boot with
-bootcharting activated for <timeout> seconds.
+On the emulator, use the -bootchart <timeout> option to boot with bootcharting
+activated for <timeout> seconds.
On a device, create /data/bootchart/start with a command like the following:
@@ -302,9 +348,13 @@
bootchart command-line utility:
sudo apt-get install pybootchartgui
- ANDROID_SERIAL=<device serial number>
+ # grab-bootchart.sh uses $ANDROID_SERIAL.
$ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh
+One thing to watch for is that the bootchart will show init as if it started
+running at 0s. You'll have to look at dmesg to work out when the kernel
+actually started init.
+
Debugging init
--------------
diff --git a/init/signal_handler.cpp b/init/signal_handler.cpp
index c428b96..8be4af5 100644
--- a/init/signal_handler.cpp
+++ b/init/signal_handler.cpp
@@ -18,18 +18,23 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/types.h>
#include <sys/wait.h>
-#include <cutils/sockets.h>
+#include <unistd.h>
+
+#include <base/stringprintf.h>
#include <cutils/android_reboot.h>
#include <cutils/list.h>
+#include <cutils/sockets.h>
#include "init.h"
#include "log.h"
#include "util.h"
+#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
+#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */
+
static int signal_fd = -1;
static int signal_recv_fd = -1;
@@ -37,8 +42,17 @@
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 */
+std::string DescribeStatus(int status) {
+ if (WIFEXITED(status)) {
+ return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status));
+ } else if (WIFSIGNALED(status)) {
+ return android::base::StringPrintf("killed by signal %d", WTERMSIG(status));
+ } else if (WIFSTOPPED(status)) {
+ return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status));
+ } else {
+ return "state changed";
+ }
+}
static int wait_for_one_process() {
int status;
@@ -46,28 +60,26 @@
if (pid <= 0) {
return -1;
}
- INFO("waitpid returned pid %d, status = %08x\n", pid, status);
service* svc = service_find_by_pid(pid);
+
+ std::string name;
+ if (svc) {
+ name = android::base::StringPrintf("Service '%s' (pid %d)", svc->name, pid);
+ } else {
+ name = android::base::StringPrintf("Untracked pid %d", pid);
+ }
+
+ NOTICE("%s %s\n", name.c_str(), DescribeStatus(status).c_str());
+
if (!svc) {
- if (WIFEXITED(status)) {
- ERROR("untracked pid %d exited with status %d\n", pid, WEXITSTATUS(status));
- } else if (WIFSIGNALED(status)) {
- ERROR("untracked pid %d killed by signal %d\n", pid, WTERMSIG(status));
- } else if (WIFSTOPPED(status)) {
- ERROR("untracked pid %d stopped by signal %d\n", pid, WSTOPSIG(status));
- } else {
- ERROR("untracked pid %d state changed\n", pid);
- }
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)) {
- NOTICE("process '%s' killing any children in process group\n", svc->name);
+ NOTICE("Service '%s' (pid %d) killing any children in process group\n", svc->name, pid);
kill(-pid, SIGKILL);
}
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 5af6e3d..c63fdaa 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -14,16 +14,17 @@
* limitations under the License.
*/
-#include <poll.h>
-#include <fcntl.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
#include <ctype.h>
+#include <fcntl.h>
+#include <poll.h>
#include <signal.h>
-#include <selinux/selinux.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <base/stringprintf.h>
#include <private/android_filesystem_config.h>
+#include <selinux/selinux.h>
#include "ueventd.h"
#include "log.h"
@@ -34,11 +35,6 @@
int ueventd_main(int argc, char **argv)
{
- struct pollfd ufd;
- int nr;
- char hardware[PROP_VALUE_MAX];
- char tmp[32];
-
/*
* init sets the umask to 077 for forked processes. We need to
* create files with exact permissions, without modification by
@@ -55,38 +51,35 @@
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);
- }
- }
+ klog_set_level(KLOG_NOTICE_LEVEL);
- union selinux_callback cb;
- cb.func_log = log_callback;
+ NOTICE("ueventd started!\n");
+
+ selinux_callback cb;
+ cb.func_log = selinux_klog_callback;
selinux_set_callback(SELINUX_CB_LOG, cb);
- INFO("starting ueventd\n");
-
+ char hardware[PROP_VALUE_MAX];
property_get("ro.hardware", hardware);
ueventd_parse_config_file("/ueventd.rc");
-
- snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
- ueventd_parse_config_file(tmp);
+ ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware).c_str());
device_init();
+ pollfd ufd;
ufd.events = POLLIN;
ufd.fd = get_device_fd();
- while(1) {
+ while (true) {
ufd.revents = 0;
- nr = poll(&ufd, 1, -1);
- if (nr <= 0)
+ int nr = poll(&ufd, 1, -1);
+ if (nr <= 0) {
continue;
- if (ufd.revents & POLLIN)
- handle_device_fd();
+ }
+ if (ufd.revents & POLLIN) {
+ handle_device_fd();
+ }
}
return 0;
diff --git a/init/util.cpp b/init/util.cpp
index 8b238d4..3b49b30 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -258,22 +258,16 @@
return -1;
}
-/*
- * gettime() - returns the time in seconds of the system's monotonic clock or
- * zero on error.
- */
-time_t gettime(void)
-{
- struct timespec ts;
- int ret;
+time_t gettime() {
+ timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ return now.tv_sec;
+}
- ret = clock_gettime(CLOCK_MONOTONIC, &ts);
- if (ret < 0) {
- ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
- return 0;
- }
-
- return ts.tv_sec;
+uint64_t gettime_ns() {
+ timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
}
int mkdir_recursive(const char *pathname, mode_t mode)
diff --git a/init/util.h b/init/util.h
index e0b3c69..8fec7a8 100644
--- a/init/util.h
+++ b/init/util.h
@@ -33,7 +33,22 @@
bool read_file(const char* path, std::string* content);
int write_file(const char* path, const char* content);
-time_t gettime(void);
+time_t gettime();
+uint64_t gettime_ns();
+
+class Timer {
+ public:
+ Timer() : t0(gettime_ns()) {
+ }
+
+ double duration() {
+ return static_cast<double>(gettime_ns() - t0) / 1000000000.0;
+ }
+
+ private:
+ uint64_t t0;
+};
+
unsigned int decode_uid(const char *s);
int mkdir_recursive(const char *pathname, mode_t mode);
diff --git a/init/watchdogd.cpp b/init/watchdogd.cpp
index 0790811..881a4df 100644
--- a/init/watchdogd.cpp
+++ b/init/watchdogd.cpp
@@ -27,52 +27,45 @@
#define DEV_NAME "/dev/watchdog"
-int watchdogd_main(int argc, char **argv)
-{
- int fd;
- int ret;
- int interval = 10;
- int margin = 10;
- int timeout;
-
+int watchdogd_main(int argc, char **argv) {
open_devnull_stdio();
klog_init();
+ klog_set_level(KLOG_NOTICE_LEVEL);
- INFO("Starting watchdogd\n");
+ int interval = 10;
+ if (argc >= 2) interval = atoi(argv[1]);
- if (argc >= 2)
- interval = atoi(argv[1]);
+ int margin = 10;
+ if (argc >= 3) margin = atoi(argv[2]);
- if (argc >= 3)
- margin = atoi(argv[2]);
+ NOTICE("watchdogd started (interval %d, margin %d)!\n", interval, margin);
- timeout = interval + margin;
-
- fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
- if (fd < 0) {
+ int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
+ if (fd == -1) {
ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
return 1;
}
- ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
+ int timeout = interval + margin;
+ int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
if (ret) {
ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno));
ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
if (ret) {
ERROR("watchdogd: Failed to get timeout: %s\n", strerror(errno));
} else {
- if (timeout > margin)
+ if (timeout > margin) {
interval = timeout - margin;
- else
+ } else {
interval = 1;
+ }
ERROR("watchdogd: Adjusted interval to timeout returned by driver: timeout %d, interval %d, margin %d\n",
timeout, interval, margin);
}
}
- while(1) {
+ while (true) {
write(fd, "", 1);
sleep(interval);
}
}
-
diff --git a/libbacktrace/Android.build.mk b/libbacktrace/Android.build.mk
index 7e1cd53..d7eaa68 100644
--- a/libbacktrace/Android.build.mk
+++ b/libbacktrace/Android.build.mk
@@ -67,10 +67,7 @@
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
+ LOCAL_CFLAGS += -Wno-extern-c-compat -fno-omit-frame-pointer
include $(BUILD_HOST_$(build_target))
endif
endif
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 2c5e351..d890319 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -64,7 +64,7 @@
endif
-# Static library for host
+# Shared and static library for host
# ========================================================
LOCAL_MODULE := libcutils
LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) dlmalloc_stubs.c
@@ -76,6 +76,16 @@
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
include $(BUILD_HOST_STATIC_LIBRARY)
+include $(CLEAR_VARS)
+LOCAL_MODULE := libcutils
+LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) dlmalloc_stubs.c
+LOCAL_SHARED_LIBRARIES := liblog
+ifneq ($(HOST_OS),windows)
+LOCAL_CFLAGS += -Werror
+endif
+LOCAL_MULTILIB := both
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_HOST_SHARED_LIBRARY)
# Tests for host
# ========================================================
diff --git a/libcutils/klog.c b/libcutils/klog.c
index fbb7b72..f574f08 100644
--- a/libcutils/klog.c
+++ b/libcutils/klog.c
@@ -14,13 +14,14 @@
* limitations under the License.
*/
-#include <sys/stat.h>
-#include <sys/types.h>
+#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#include <cutils/klog.h>
@@ -36,41 +37,36 @@
klog_level = level;
}
-void klog_init(void)
-{
- static const char *name = "/dev/__kmsg__";
-
+void klog_init(void) {
if (klog_fd >= 0) return; /* Already initialized */
+ static const char* name = "/dev/__kmsg__";
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
- klog_fd = open(name, O_WRONLY);
- if (klog_fd < 0)
- return;
- fcntl(klog_fd, F_SETFD, FD_CLOEXEC);
+ klog_fd = open(name, O_WRONLY | O_CLOEXEC);
unlink(name);
}
}
#define LOG_BUF_MAX 512
-void klog_vwrite(int level, const char *fmt, va_list ap)
-{
- char buf[LOG_BUF_MAX];
-
+void klog_writev(int level, const struct iovec* iov, int iov_count) {
if (level > klog_level) return;
if (klog_fd < 0) klog_init();
if (klog_fd < 0) return;
-
- vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
- buf[LOG_BUF_MAX - 1] = 0;
-
- write(klog_fd, buf, strlen(buf));
+ TEMP_FAILURE_RETRY(writev(klog_fd, iov, iov_count));
}
-void klog_write(int level, const char *fmt, ...)
-{
+void klog_write(int level, const char* fmt, ...) {
+ char buf[LOG_BUF_MAX];
va_list ap;
va_start(ap, fmt);
- klog_vwrite(level, fmt, ap);
+ vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
+
+ buf[LOG_BUF_MAX - 1] = 0;
+
+ struct iovec iov[1];
+ iov[0].iov_base = buf;
+ iov[0].iov_len = strlen(buf);
+ klog_writev(level, iov, 1);
}
diff --git a/libcutils/threads.c b/libcutils/threads.c
index ca600b3..5f5577b 100644
--- a/libcutils/threads.c
+++ b/libcutils/threads.c
@@ -14,9 +14,25 @@
** limitations under the License.
*/
-#include <cutils/threads.h>
+#include "cutils/threads.h"
#if !defined(_WIN32)
+
+// For gettid.
+#if defined(__APPLE__)
+#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/syscall.h>
+#include <sys/time.h>
+#include <unistd.h>
+#elif defined(__linux__) && !defined(__ANDROID__)
+#include <syscall.h>
+#include <unistd.h>
+#elif defined(_WIN32)
+#include <Windows.h>
+#endif
+
void* thread_store_get( thread_store_t* store )
{
if (!store->has_tls)
@@ -42,6 +58,24 @@
pthread_setspecific( store->tls, value );
}
+// No definition needed for Android because we'll just pick up bionic's copy.
+#ifndef __ANDROID__
+pid_t gettid() {
+#if defined(__APPLE__)
+ uint64_t owner;
+ int rc = pthread_threadid_np(NULL, &owner);
+ if (rc != 0) {
+ abort();
+ }
+ return owner;
+#elif defined(__linux__)
+ return syscall(__NR_gettid);
+#elif defined(_WIN32)
+ return (pid_t)GetCurrentThreadId();
+#endif
+}
+#endif // __ANDROID__
+
#else /* !defined(_WIN32) */
void* thread_store_get( thread_store_t* store )
{
diff --git a/liblog/logd_write.c b/liblog/logd_write.c
index 8f8cc3f..dfe34d1 100644
--- a/liblog/logd_write.c
+++ b/liblog/logd_write.c
@@ -90,15 +90,6 @@
return (g_log_status == kLogAvailable);
}
-#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
-
/* log_init_lock assumed */
static int __write_to_log_initialize()
{
@@ -111,40 +102,32 @@
log_fds[i] = fakeLogOpen(buf, O_WRONLY);
}
#else
- if (logd_fd >= 0) {
- i = logd_fd;
- logd_fd = -1;
- close(i);
+ if (pstore_fd < 0) {
+ pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
}
- 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) {
+ if (logd_fd < 0) {
+ i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
+ if (i < 0) {
+ ret = -errno;
+ } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
ret = -errno;
close(i);
- i = -1;
+ } 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 (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
+ sizeof(struct sockaddr_un))) < 0) {
+ ret = -errno;
+ close(i);
+ } else {
+ logd_fd = i;
+ }
}
}
- logd_fd = i;
#endif
return ret;
@@ -293,6 +276,8 @@
#if !defined(_WIN32)
pthread_mutex_lock(&log_init_lock);
#endif
+ close(logd_fd);
+ logd_fd = -1;
ret = __write_to_log_initialize();
#if !defined(_WIN32)
pthread_mutex_unlock(&log_init_lock);
@@ -351,6 +336,11 @@
#if !defined(_WIN32)
pthread_mutex_unlock(&log_init_lock);
#endif
+#if (FAKE_LOG_DEVICE == 0)
+ if (pstore_fd >= 0) {
+ __write_to_log_daemon(log_id, vec, nr);
+ }
+#endif
return ret;
}
diff --git a/libprocessgroup/Android.mk b/libprocessgroup/Android.mk
index 051999a..ee6ba58 100644
--- a/libprocessgroup/Android.mk
+++ b/libprocessgroup/Android.mk
@@ -8,7 +8,6 @@
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)
diff --git a/libsync/tests/Android.mk b/libsync/tests/Android.mk
index ad20e50..9c9562a 100644
--- a/libsync/tests/Android.mk
+++ b/libsync/tests/Android.mk
@@ -17,7 +17,6 @@
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
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index 29a86d8..ef30017 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -47,19 +47,6 @@
#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 = Action::kUnknown;
memset(mParams, 0, sizeof(mParams));
diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp
index d739f11..e69784d 100644
--- a/libutils/Looper.cpp
+++ b/libutils/Looper.cpp
@@ -21,6 +21,7 @@
#include <fcntl.h>
#include <limits.h>
#include <inttypes.h>
+#include <sys/eventfd.h>
namespace android {
@@ -71,28 +72,15 @@
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
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);
-
- mWakeReadPipeFd = wakeFds[0];
- mWakeWritePipeFd = wakeFds[1];
-
- result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
- errno);
-
- result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
- LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
- errno);
+ mWakeEventFd = eventfd(0, EFD_NONBLOCK);
+ LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd. errno=%d", errno);
AutoMutex _l(mLock);
rebuildEpollLocked();
}
Looper::~Looper() {
- close(mWakeReadPipeFd);
- close(mWakeWritePipeFd);
+ close(mWakeEventFd);
if (mEpollFd >= 0) {
close(mEpollFd);
}
@@ -165,9 +153,9 @@
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",
+ eventItem.data.fd = mWakeEventFd;
+ int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
+ LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance. errno=%d",
errno);
for (size_t i = 0; i < mRequests.size(); i++) {
@@ -299,11 +287,11 @@
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
- if (fd == mWakeReadPipeFd) {
+ if (fd == mWakeEventFd) {
if (epollEvents & EPOLLIN) {
awoken();
} else {
- ALOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);
+ ALOGW("Ignoring unexpected epoll events 0x%x on wake event fd.", epollEvents);
}
} else {
ssize_t requestIndex = mRequests.indexOfKey(fd);
@@ -418,12 +406,9 @@
ALOGD("%p ~ wake", this);
#endif
- ssize_t nWrite;
- do {
- nWrite = write(mWakeWritePipeFd, "W", 1);
- } while (nWrite == -1 && errno == EINTR);
-
- if (nWrite != 1) {
+ uint64_t inc = 1;
+ ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t)));
+ if (nWrite != sizeof(uint64_t)) {
if (errno != EAGAIN) {
ALOGW("Could not write wake signal, errno=%d", errno);
}
@@ -435,11 +420,8 @@
ALOGD("%p ~ awoken", this);
#endif
- char buffer[16];
- ssize_t nRead;
- do {
- nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
- } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
+ uint64_t counter;
+ TEMP_FAILURE_RETRY(read(mWakeEventFd, &counter, sizeof(uint64_t)));
}
void Looper::pushResponse(int events, const Request& request) {
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 6475649..58285f1 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -1123,7 +1123,7 @@
int32_t ExtractEntryToFile(ZipArchiveHandle handle,
ZipEntry* entry, int fd) {
- const int32_t declared_length = entry->uncompressed_length;
+ const uint32_t declared_length = entry->uncompressed_length;
const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
if (current_offset == -1) {
diff --git a/libzipfile/Android.mk b/libzipfile/Android.mk
deleted file mode 100644
index f054e15..0000000
--- a/libzipfile/Android.mk
+++ /dev/null
@@ -1,48 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-# build host static library
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
- centraldir.c \
- zipfile.c
-
-LOCAL_STATIC_LIBRARIES := libz
-
-LOCAL_MODULE:= libzipfile
-
-LOCAL_CFLAGS := -Werror
-
-LOCAL_MULTILIB := both
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-# build device static library
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
- centraldir.c \
- zipfile.c
-
-LOCAL_STATIC_LIBRARIES := libz
-
-LOCAL_MODULE:= libzipfile
-
-LOCAL_CFLAGS := -Werror
-
-include $(BUILD_STATIC_LIBRARY)
-
-
-# build test_zipfile
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES:= \
- test_zipfile.c
-
-LOCAL_STATIC_LIBRARIES := libzipfile libz
-
-LOCAL_MODULE := test_zipfile
-
-LOCAL_CFLAGS := -Werror
-
-include $(BUILD_HOST_EXECUTABLE)
diff --git a/libzipfile/MODULE_LICENSE_APACHE2 b/libzipfile/MODULE_LICENSE_APACHE2
deleted file mode 100644
index e69de29..0000000
--- a/libzipfile/MODULE_LICENSE_APACHE2
+++ /dev/null
diff --git a/libzipfile/NOTICE b/libzipfile/NOTICE
deleted file mode 100644
index c5b1efa..0000000
--- a/libzipfile/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/libzipfile/centraldir.c b/libzipfile/centraldir.c
deleted file mode 100644
index 69cf47a..0000000
--- a/libzipfile/centraldir.c
+++ /dev/null
@@ -1,222 +0,0 @@
-#include "private.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include <utils/Compat.h>
-
-enum {
- // finding the directory
- CD_SIGNATURE = 0x06054b50,
- EOCD_LEN = 22, // EndOfCentralDir len, excl. comment
- MAX_COMMENT_LEN = 65535,
- MAX_EOCD_SEARCH = MAX_COMMENT_LEN + EOCD_LEN,
-
- // central directory entries
- ENTRY_SIGNATURE = 0x02014b50,
- ENTRY_LEN = 46, // CentralDirEnt len, excl. var fields
-
- // local file header
- LFH_SIZE = 30,
-};
-
-unsigned int
-read_le_int(const unsigned char* buf)
-{
- return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
-}
-
-unsigned int
-read_le_short(const unsigned char* buf)
-{
- return buf[0] | (buf[1] << 8);
-}
-
-static int
-read_central_dir_values(Zipfile* file, const unsigned char* buf, int len)
-{
- if (len < EOCD_LEN) {
- // looks like ZIP file got truncated
- fprintf(stderr, " Zip EOCD: expected >= %d bytes, found %d\n",
- EOCD_LEN, len);
- return -1;
- }
-
- file->disknum = read_le_short(&buf[0x04]);
- file->diskWithCentralDir = read_le_short(&buf[0x06]);
- file->entryCount = read_le_short(&buf[0x08]);
- file->totalEntryCount = read_le_short(&buf[0x0a]);
- file->centralDirSize = read_le_int(&buf[0x0c]);
- file->centralDirOffest = read_le_int(&buf[0x10]);
- file->commentLen = read_le_short(&buf[0x14]);
-
- if (file->commentLen > 0) {
- if (EOCD_LEN + file->commentLen > len) {
- fprintf(stderr, "EOCD(%d) + comment(%d) exceeds len (%d)\n",
- EOCD_LEN, file->commentLen, len);
- return -1;
- }
- file->comment = buf + EOCD_LEN;
- }
-
- return 0;
-}
-
-static int
-read_central_directory_entry(Zipfile* file, Zipentry* entry,
- const unsigned char** buf, ssize_t* len)
-{
- const unsigned char* p;
-
- unsigned short extraFieldLength;
- unsigned short fileCommentLength;
- unsigned long localHeaderRelOffset;
- unsigned int dataOffset;
-
- p = *buf;
-
- if (*len < ENTRY_LEN) {
- fprintf(stderr, "cde entry not large enough\n");
- return -1;
- }
-
- if (read_le_int(&p[0x00]) != ENTRY_SIGNATURE) {
- fprintf(stderr, "Whoops: didn't find expected signature\n");
- return -1;
- }
-
- entry->compressionMethod = read_le_short(&p[0x0a]);
- 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]);
- localHeaderRelOffset = read_le_int(&p[0x2a]);
-
- p += ENTRY_LEN;
-
- // filename
- if (entry->fileNameLength != 0) {
- entry->fileName = p;
- } else {
- entry->fileName = NULL;
- }
- p += entry->fileNameLength;
-
- // extra field
- p += extraFieldLength;
-
- // comment, if any
- p += fileCommentLength;
-
- *buf = p;
-
- // the size of the extraField in the central dir is how much data there is,
- // but the one in the local file header also contains some padding.
- p = file->buf + localHeaderRelOffset;
- extraFieldLength = read_le_short(&p[0x1c]);
-
- dataOffset = localHeaderRelOffset + LFH_SIZE
- + entry->fileNameLength + extraFieldLength;
- entry->data = file->buf + dataOffset;
-#if 0
- printf("file->buf=%p entry->data=%p dataOffset=%x localHeaderRelOffset=%d "
- "entry->fileNameLength=%d extraFieldLength=%d\n",
- file->buf, entry->data, dataOffset, localHeaderRelOffset,
- entry->fileNameLength, extraFieldLength);
-#endif
- return 0;
-}
-
-/*
- * Find the central directory and read the contents.
- *
- * The fun thing about ZIP archives is that they may or may not be
- * readable from start to end. In some cases, notably for archives
- * that were written to stdout, the only length information is in the
- * central directory at the end of the file.
- *
- * Of course, the central directory can be followed by a variable-length
- * comment field, so we have to scan through it backwards. The comment
- * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
- * itself, plus apparently sometimes people throw random junk on the end
- * just for the fun of it.
- *
- * This is all a little wobbly. If the wrong value ends up in the EOCD
- * area, we're hosed. This appears to be the way that everbody handles
- * it though, so we're in pretty good company if this fails.
- */
-int
-read_central_dir(Zipfile *file)
-{
- int err;
-
- const unsigned char* buf = file->buf;
- ZD_TYPE bufsize = file->bufsize;
- const unsigned char* eocd;
- const unsigned char* p;
- const unsigned char* start;
- ssize_t len;
- int i;
-
- // too small to be a ZIP archive?
- if (bufsize < EOCD_LEN) {
- fprintf(stderr, "Length is " ZD " -- too small\n", bufsize);
- goto bail;
- }
-
- // find the end-of-central-dir magic
- if (bufsize > MAX_EOCD_SEARCH) {
- start = buf + bufsize - MAX_EOCD_SEARCH;
- } else {
- start = buf;
- }
- p = buf + bufsize - 4;
- while (p >= start) {
- if (*p == 0x50 && read_le_int(p) == CD_SIGNATURE) {
- eocd = p;
- break;
- }
- p--;
- }
- if (p < start) {
- fprintf(stderr, "EOCD not found, not Zip\n");
- goto bail;
- }
-
- // extract eocd values
- err = read_central_dir_values(file, eocd, (buf+bufsize)-eocd);
- if (err != 0) {
- goto bail;
- }
-
- if (file->disknum != 0
- || file->diskWithCentralDir != 0
- || file->entryCount != file->totalEntryCount) {
- fprintf(stderr, "Archive spanning not supported\n");
- goto bail;
- }
-
- // Loop through and read the central dir entries.
- p = buf + file->centralDirOffest;
- len = (buf+bufsize)-p;
- for (i=0; i < file->totalEntryCount; i++) {
- Zipentry* entry = malloc(sizeof(Zipentry));
- memset(entry, 0, sizeof(Zipentry));
-
- err = read_central_directory_entry(file, entry, &p, &len);
- if (err != 0) {
- fprintf(stderr, "read_central_directory_entry failed\n");
- free(entry);
- goto bail;
- }
-
- // add it to our list
- entry->next = file->entries;
- file->entries = entry;
- }
-
- return 0;
-bail:
- return -1;
-}
diff --git a/libzipfile/private.h b/libzipfile/private.h
deleted file mode 100644
index 06f788d..0000000
--- a/libzipfile/private.h
+++ /dev/null
@@ -1,45 +0,0 @@
-#ifndef PRIVATE_H
-#define PRIVATE_H
-
-#include <stddef.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-typedef struct Zipentry {
- unsigned long fileNameLength;
- const unsigned char* fileName;
- unsigned short compressionMethod;
- unsigned int uncompressedSize;
- unsigned int compressedSize;
- const unsigned char* data;
-
- struct Zipentry* next;
-} Zipentry;
-
-typedef struct Zipfile
-{
- const unsigned char *buf;
- ssize_t bufsize;
-
- // Central directory
- unsigned short disknum; //mDiskNumber;
- unsigned short diskWithCentralDir; //mDiskWithCentralDir;
- unsigned short entryCount; //mNumEntries;
- unsigned short totalEntryCount; //mTotalNumEntries;
- unsigned int centralDirSize; //mCentralDirSize;
- unsigned int centralDirOffest; // offset from first disk //mCentralDirOffset;
- unsigned short commentLen; //mCommentLen;
- const unsigned char* comment; //mComment;
-
- Zipentry* entries;
-} Zipfile;
-
-int read_central_dir(Zipfile* file);
-
-unsigned int read_le_int(const unsigned char* buf);
-unsigned int read_le_short(const unsigned char* buf);
-
-#endif // PRIVATE_H
-
diff --git a/libzipfile/test_zipfile.c b/libzipfile/test_zipfile.c
deleted file mode 100644
index 1aaa913..0000000
--- a/libzipfile/test_zipfile.c
+++ /dev/null
@@ -1,94 +0,0 @@
-#include <zipfile/zipfile.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-void dump_zipfile(FILE* to, zipfile_t file);
-
-int
-main(int argc, char** argv)
-{
- FILE* f;
- size_t size, unsize;
- void* buf;
- void* scratch;
- zipfile_t zip;
- zipentry_t entry;
- int err;
- enum { HUH, LIST, UNZIP } what = HUH;
-
- if (strcmp(argv[2], "-l") == 0 && argc == 3) {
- what = LIST;
- }
- else if (strcmp(argv[2], "-u") == 0 && argc == 5) {
- what = UNZIP;
- }
- else {
- fprintf(stderr, "usage: test_zipfile ZIPFILE -l\n"
- " lists the files in the zipfile\n"
- " test_zipfile ZIPFILE -u FILENAME SAVETO\n"
- " saves FILENAME from the zip file into SAVETO\n");
- return 1;
- }
-
- f = fopen(argv[1], "r");
- if (f == NULL) {
- fprintf(stderr, "couldn't open %s\n", argv[1]);
- return 1;
- }
-
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- rewind(f);
-
- buf = malloc(size);
- fread(buf, 1, size, f);
-
- zip = init_zipfile(buf, size);
- if (zip == NULL) {
- fprintf(stderr, "inti_zipfile failed\n");
- return 1;
- }
-
- fclose(f);
-
-
- switch (what)
- {
- case HUH:
- break;
- case LIST:
- dump_zipfile(stdout, zip);
- break;
- case UNZIP:
- entry = lookup_zipentry(zip, argv[3]);
- if (entry == NULL) {
- fprintf(stderr, "zip file '%s' does not contain file '%s'\n",
- argv[1], argv[1]);
- return 1;
- }
- f = fopen(argv[4], "w");
- if (f == NULL) {
- fprintf(stderr, "can't open file for writing '%s'\n", argv[4]);
- return 1;
- }
- unsize = get_zipentry_size(entry);
- size = unsize * 1.001;
- scratch = malloc(size);
- printf("scratch=%p\n", scratch);
- err = decompress_zipentry(entry, scratch, size);
- if (err != 0) {
- fprintf(stderr, "error decompressing file\n");
- return 1;
- }
- fwrite(scratch, unsize, 1, f);
- free(scratch);
- fclose(f);
- break;
- }
-
- free(buf);
-
- return 0;
-}
-
diff --git a/libzipfile/zipfile.c b/libzipfile/zipfile.c
deleted file mode 100644
index 1032ecc..0000000
--- a/libzipfile/zipfile.c
+++ /dev/null
@@ -1,159 +0,0 @@
-#include <zipfile/zipfile.h>
-
-#include "private.h"
-#include <stdlib.h>
-#include <string.h>
-#include <zlib.h>
-#define DEF_MEM_LEVEL 8 // normally in zutil.h?
-
-zipfile_t
-init_zipfile(const void* data, size_t size)
-{
- int err;
-
- Zipfile *file = malloc(sizeof(Zipfile));
- if (file == NULL) return NULL;
- memset(file, 0, sizeof(Zipfile));
- file->buf = data;
- file->bufsize = size;
-
- err = read_central_dir(file);
- if (err != 0) goto fail;
-
- return file;
-fail:
- free(file);
- return NULL;
-}
-
-void
-release_zipfile(zipfile_t f)
-{
- Zipfile* file = (Zipfile*)f;
- Zipentry* entry = file->entries;
- while (entry) {
- Zipentry* next = entry->next;
- free(entry);
- entry = next;
- }
- free(file);
-}
-
-zipentry_t
-lookup_zipentry(zipfile_t f, const char* entryName)
-{
- Zipfile* file = (Zipfile*)f;
- Zipentry* entry = file->entries;
- while (entry) {
- if (0 == memcmp(entryName, entry->fileName, entry->fileNameLength)) {
- return entry;
- }
- entry = entry->next;
- }
- return NULL;
-}
-
-size_t
-get_zipentry_size(zipentry_t entry)
-{
- return ((Zipentry*)entry)->uncompressedSize;
-}
-
-char*
-get_zipentry_name(zipentry_t entry)
-{
- Zipentry* e = (Zipentry*)entry;
- int l = e->fileNameLength;
- char* s = malloc(l+1);
- memcpy(s, e->fileName, l);
- s[l] = '\0';
- return s;
-}
-
-enum {
- STORED = 0,
- DEFLATED = 8
-};
-
-static int
-inflate_wrapper(unsigned char* out, int unlen, const unsigned char* in, int clen)
-{
- z_stream zstream;
- int err = 0;
- int zerr;
-
- memset(&zstream, 0, sizeof(zstream));
- zstream.zalloc = Z_NULL;
- zstream.zfree = Z_NULL;
- zstream.opaque = Z_NULL;
- zstream.next_in = (void*)in;
- zstream.avail_in = clen;
- zstream.next_out = (Bytef*) out;
- zstream.avail_out = unlen;
- zstream.data_type = Z_UNKNOWN;
-
- // Use the undocumented "negative window bits" feature to tell zlib
- // that there's no zlib header waiting for it.
- zerr = inflateInit2(&zstream, -MAX_WBITS);
- if (zerr != Z_OK) {
- return -1;
- }
-
- // uncompress the data
- zerr = inflate(&zstream, Z_FINISH);
- if (zerr != Z_STREAM_END) {
- fprintf(stderr, "zerr=%d Z_STREAM_END=%d total_out=%lu\n", zerr, Z_STREAM_END,
- zstream.total_out);
- err = -1;
- }
-
- inflateEnd(&zstream);
- return err;
-}
-
-int
-decompress_zipentry(zipentry_t e, void* buf, int bufsize)
-{
- Zipentry* entry = (Zipentry*)e;
- switch (entry->compressionMethod)
- {
- case STORED:
- memcpy(buf, entry->data, entry->uncompressedSize);
- return 0;
- case DEFLATED:
- return inflate_wrapper(buf, bufsize, entry->data, entry->compressedSize);
- default:
- return -1;
- }
-}
-
-void
-dump_zipfile(FILE* to, zipfile_t file)
-{
- Zipfile* zip = (Zipfile*)file;
- Zipentry* entry = zip->entries;
- int i;
-
- fprintf(to, "entryCount=%d\n", zip->entryCount);
- for (i=0; i<zip->entryCount; i++) {
- fprintf(to, " file \"");
- fwrite(entry->fileName, entry->fileNameLength, 1, to);
- fprintf(to, "\"\n");
- entry = entry->next;
- }
-}
-
-zipentry_t
-iterate_zipfile(zipfile_t file, void** cookie)
-{
- Zipentry* entry = (Zipentry*)*cookie;
- if (entry == NULL) {
- Zipfile* zip = (Zipfile*)file;
- *cookie = zip->entries;
- return *cookie;
- } else {
- entry = entry->next;
- *cookie = entry;
- return entry;
- }
-}
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 2693583..d11b129 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -27,8 +27,6 @@
#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?
@@ -193,7 +191,7 @@
LogTimeEntry::unlock();
}
- stats.add(len, log_id, uid, pid);
+ stats.add(elem);
maybePrune(log_id);
pthread_mutex_unlock(&mLogElementsLock);
}
@@ -216,6 +214,16 @@
}
}
+LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
+ LogBufferElement *e = *it;
+
+ it = mLogElements.erase(it);
+ stats.subtract(e);
+ delete e;
+
+ return it;
+}
+
// prune "pruneRows" of type "id" from the buffer.
//
// mLogElementsLock must be held when this function is called.
@@ -250,12 +258,8 @@
continue;
}
- uid_t uid = e->getUid();
-
- if (uid == caller_uid) {
- it = mLogElements.erase(it);
- stats.subtract(e->getMsgLen(), id, uid, e->getPid());
- delete e;
+ if (e->getUid() == caller_uid) {
+ it = erase(it);
pruneRows--;
if (pruneRows == 0) {
break;
@@ -269,6 +273,7 @@
}
// prune by worst offender by uid
+ bool hasBlacklist = mPrune.naughty();
while (pruneRows > 0) {
// recalculate the worst offender on every batched pass
uid_t worst = (uid_t) -1;
@@ -276,19 +281,23 @@
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();
+ const UidEntry **sorted = stats.sort(2, id);
+
+ if (sorted) {
+ if (sorted[0] && sorted[1]) {
+ worst = sorted[0]->getKey();
+ worst_sizes = sorted[0]->getSizes();
+ second_worst_sizes = sorted[1]->getSizes();
}
+ delete [] sorted;
}
}
+ // skip if we have neither worst nor naughty filters
+ if ((worst == (uid_t) -1) && !hasBlacklist) {
+ break;
+ }
+
bool kick = false;
for(it = mLogElements.begin(); it != mLogElements.end();) {
LogBufferElement *e = *it;
@@ -304,24 +313,28 @@
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 {
+ // !Worst and !BlackListed?
+ if ((uid != worst) && (!hasBlacklist || !mPrune.naughty(e))) {
++it;
+ continue;
}
+
+ unsigned short len = e->getMsgLen();
+ it = erase(it);
+ pruneRows--;
+ if (pruneRows == 0) {
+ break;
+ }
+
+ if (uid != worst) {
+ continue;
+ }
+
+ kick = true;
+ if (worst_sizes < second_worst_sizes) {
+ break;
+ }
+ worst_sizes -= len;
}
if (!kick || !mPrune.worstUidEnabled()) {
@@ -330,58 +343,63 @@
}
bool whitelist = false;
+ bool hasWhitelist = mPrune.nice();
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);
- }
- }
+
+ if (e->getLogId() != id) {
+ it++;
+ continue;
+ }
+
+ if (oldest && (oldest->mStart <= e->getSequence())) {
+ if (whitelist) {
break;
}
- if (mPrune.nice(e)) { // WhiteListed
- whitelist = true;
- it++;
- continue;
+ 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);
}
-
- it = mLogElements.erase(it);
- stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid());
- delete e;
- pruneRows--;
- } else {
- it++;
+ break;
}
+
+ if (hasWhitelist && mPrune.nice(e)) { // WhiteListed
+ whitelist = true;
+ it++;
+ continue;
+ }
+
+ it = erase(it);
+ pruneRows--;
}
+ // Do not save the whitelist if we are reader range limited
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++;
+
+ if (e->getLogId() != id) {
+ ++it;
+ continue;
}
+
+ 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 = erase(it);
+ pruneRows--;
}
}
@@ -487,22 +505,9 @@
}
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);
+ stats.format(strp, uid, logMask);
pthread_mutex_unlock(&mLogElementsLock);
}
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
index 13e6aa8..a29e015 100644
--- a/logd/LogBuffer.h
+++ b/logd/LogBuffer.h
@@ -78,7 +78,7 @@
private:
void maybePrune(log_id_t id);
void prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
-
+ LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it);
};
#endif // _LOGD_LOG_BUFFER_H__
diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h
index 25f1450..0628d3e 100644
--- a/logd/LogBufferElement.h
+++ b/logd/LogBufferElement.h
@@ -17,8 +17,9 @@
#ifndef _LOGD_LOG_BUFFER_ELEMENT_H__
#define _LOGD_LOG_BUFFER_ELEMENT_H__
-#include <sys/types.h>
#include <stdatomic.h>
+#include <sys/types.h>
+
#include <sysutils/SocketClient.h>
#include <log/log.h>
#include <log/log_read.h>
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index 5a70689..accd660 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -14,10 +14,12 @@
* limitations under the License.
*/
+#include <algorithm> // std::max
#include <fcntl.h>
-#include <malloc.h>
-#include <stdarg.h>
-#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#include <log/logger.h>
#include <private/android_filesystem_config.h>
@@ -25,80 +27,21 @@
#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;
+LogStatistics::LogStatistics() {
+ log_id_for_each(id) {
+ mSizes[id] = 0;
+ mElements[id] = 0;
+ mSizesTotal[id] = 0;
+ mElementsTotal[id] = 0;
}
}
-// 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) {
+// caller must own and free character string
+char *LogStatistics::pidToName(pid_t pid) {
char *retval = NULL;
if (pid == 0) { // special case from auditd for kernel
retval = strdup("logd.auditd");
- } else if (pid != gone) {
+ } else {
char buffer[512];
snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid);
int fd = open(buffer, O_RDONLY);
@@ -117,358 +60,105 @@
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) {
+void LogStatistics::add(LogBufferElement *e) {
+ log_id_t log_id = e->getLogId();
+ unsigned short size = e->getMsgLen();
mSizes[log_id] += size;
++mElements[log_id];
- if (!mStatistics) {
- return;
+
+ uid_t uid = e->getUid();
+ android::hash_t hash = android::hash_type(uid);
+ uidTable_t &table = uidTable[log_id];
+ ssize_t index = table.find(-1, hash, uid);
+ if (index == -1) {
+ UidEntry initEntry(uid);
+ initEntry.add(size);
+ table.add(hash, initEntry);
+ } else {
+ UidEntry &entry = table.editEntryAt(index);
+ entry.add(size);
}
- id(log_id).add(size, uid, pid);
+
+ mSizesTotal[log_id] += size;
+ ++mElementsTotal[log_id];
}
-void LogStatistics::subtract(unsigned short size,
- log_id_t log_id, uid_t uid, pid_t pid) {
+void LogStatistics::subtract(LogBufferElement *e) {
+ log_id_t log_id = e->getLogId();
+ unsigned short size = e->getMsgLen();
mSizes[log_id] -= size;
--mElements[log_id];
- if (!mStatistics) {
- return;
+
+ uid_t uid = e->getUid();
+ android::hash_t hash = android::hash_type(uid);
+ uidTable_t &table = uidTable[log_id];
+ ssize_t index = table.find(-1, hash, uid);
+ if (index != -1) {
+ UidEntry &entry = table.editEntryAt(index);
+ if (entry.subtract(size)) {
+ table.removeAt(index);
+ }
}
- 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);
+// caller must own and delete UidEntry array
+const UidEntry **LogStatistics::sort(size_t n, log_id id) {
+ if (!n) {
+ return NULL;
}
- size_t sizes = 0;
- log_id_for_each(i) {
- sizes += id(i).sizes(uid, pid);
+
+ const UidEntry **retval = new const UidEntry* [n];
+ memset(retval, 0, sizeof(*retval) * n);
+
+ uidTable_t &table = uidTable[id];
+ ssize_t index = -1;
+ while ((index = table.next(index)) >= 0) {
+ const UidEntry &entry = table.entryAt(index);
+ size_t s = entry.getSizes();
+ ssize_t i = n - 1;
+ while ((!retval[i] || (s > retval[i]->getSizes())) && (--i >= 0));
+ if (++i < (ssize_t)n) {
+ size_t b = n - i - 1;
+ if (b) {
+ memmove(&retval[i+1], &retval[i], b * sizeof(retval[0]));
+ }
+ retval[i] = &entry;
+ }
}
- return sizes;
+ return retval;
}
-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);
+// caller must own and free character string
+char *LogStatistics::uidToName(uid_t uid) {
+ // Local hard coded favourites
+ if (uid == AID_LOGD) {
+ return strdup("auditd");
}
- size_t elements = 0;
- log_id_for_each(i) {
- elements += id(i).elements(uid, pid);
+
+ // Android hard coded
+ const struct android_id_info *info = android_ids;
+
+ for (size_t i = 0; i < android_id_count; ++i) {
+ if (info->aid == uid) {
+ return strdup(info->name);
+ }
+ ++info;
}
- return elements;
+
+ // No one
+ return NULL;
}
-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;
+static void format_line(android::String8 &output,
+ android::String8 &name, android::String8 &size) {
+ static const size_t total_len = 70;
+
+ output.appendFormat("%s%*s\n", name.string(),
+ (int)std::max(total_len - name.length() - 1, size.length() + 1),
+ size.string());
}
-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;
+void LogStatistics::format(char **buf, uid_t uid, unsigned int logMask) {
static const unsigned short spaces_total = 19;
if (*buf) {
@@ -476,368 +166,142 @@
*buf = NULL;
}
- android::String8 string(" span -> size/num");
- size_t oldLength;
- short spaces = 2;
+ // Report on total logging, current and for all time
- log_id_for_each(i) {
- if (!(logMask & (1 << i))) {
+ android::String8 output("size/num");
+ size_t oldLength;
+ short spaces = 1;
+
+ log_id_for_each(id) {
+ if (!(logMask & (1 << id))) {
continue;
}
- oldLength = string.length();
+ oldLength = output.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();
- }
+ output.appendFormat("%*s%s", spaces, "", android_log_id_to_name(id));
+ spaces += spaces_total + oldLength - output.length();
}
- 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);
+ spaces = 4;
+ output.appendFormat("\nTotal");
- 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();
+ log_id_for_each(id) {
+ if (!(logMask & (1 << id))) {
+ continue;
}
- spaces = 1;
+ oldLength = output.length();
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ output.appendFormat("%*s%zu/%zu", spaces, "",
+ sizesTotal(id), elementsTotal(id));
+ spaces += spaces_total + oldLength - output.length();
}
- 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);
+ spaces = 6;
+ output.appendFormat("\nNow");
- log_id_for_each(i) {
- if (!(logMask & (1 << i))) {
+ log_id_for_each(id) {
+ if (!(logMask & (1 << id))) {
continue;
}
- size_t els = elements(i);
+ size_t els = elements(id);
if (els) {
- oldLength = string.length();
+ oldLength = output.length();
if (spaces < 0) {
spaces = 0;
}
- string.appendFormat("%*s%zu/%zu", spaces, "", sizes(i), els);
- spaces -= string.length() - oldLength;
+ output.appendFormat("%*s%zu/%zu", spaces, "", sizes(id), els);
+ spaces -= output.length() - oldLength;
}
spaces += spaces_total;
}
- // Construct list of worst spammers by Pid
- static const unsigned char num_spammers = 10;
- bool header = false;
+ // Report on Chattiest
- log_id_for_each(i) {
- if (!(logMask & (1 << i))) {
+ // Chattiest by application (UID)
+ log_id_for_each(id) {
+ if (!(logMask & (1 << id))) {
continue;
}
- PidStatisticsCollection pids;
- pids.clear();
+ static const size_t maximum_sorted_entries = 32;
+ const UidEntry **sorted = sort(maximum_sorted_entries, id);
- 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);
- }
- }
+ if (!sorted) {
+ continue;
}
- size_t threshold = sizes(i);
- if (threshold < 65536) {
- threshold = 65536;
- }
- threshold /= 100;
+ bool print = false;
+ for(size_t index = 0; index < maximum_sorted_entries; ++index) {
+ const UidEntry *entry = sorted[index];
- 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) {
+ if (!entry) {
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;
- }
+ size_t sizes = entry->getSizes();
+ if (sizes < (65536/100)) {
+ break;
+ }
+
+ uid_t u = entry->getKey();
+ if ((uid != AID_ROOT) && (u != uid)) {
+ continue;
+ }
+
+ if (!print) {
+ if (uid == AID_ROOT) {
+ output.appendFormat(
+ "\n\nChattiest UIDs in %s:\n",
+ android_log_id_to_name(id));
+ android::String8 name("UID");
+ android::String8 size("Size");
+ format_line(output, name, size);
+ } else {
+ output.appendFormat(
+ "\n\nLogging for your UID in %s:\n",
+ android_log_id_to_name(id));
}
+ print = true;
}
- if (!header) {
- string.appendFormat("\n\nChattiest clients:\n"
- "log id %-*s PID[?] name",
- spaces_total, "size/total");
- header = true;
+ android::String8 name("");
+ name.appendFormat("%u", u);
+ char *n = uidToName(u);
+ if (n) {
+ name.appendFormat("%*s%s", (int)std::max(6 - name.length(), (size_t)1), "", n);
+ free(n);
}
- size_t sizesTotal = p->sizesTotal();
+ android::String8 size("");
+ size.appendFormat("%zu", sizes);
- 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 : "");
+ format_line(output, name, size);
}
- pids.clear();
+ delete [] sorted;
}
- 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());
+ *buf = strdup(output.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();
- }
+ char buffer[512];
+ snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid);
+ FILE *fp = fopen(buffer, "r");
+ if (fp) {
+ while (fgets(buffer, sizeof(buffer), fp)) {
+ int uid;
+ if (sscanf(buffer, "Groups: %d", &uid) == 1) {
+ fclose(fp);
+ return uid;
}
}
+ fclose(fp);
}
return getuid(); // associate this with the logger
}
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
index f892cd0..d5b8762 100644
--- a/logd/LogStatistics.h
+++ b/logd/LogStatistics.h
@@ -20,175 +20,60 @@
#include <sys/types.h>
#include <log/log.h>
-#include <log/log_read.h>
-#include <utils/List.h>
+#include <utils/BasicHashtable.h>
+
+#include "LogBufferElement.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 {
+struct UidEntry {
const uid_t uid;
+ size_t size;
- PidStatisticsCollection Pids;
+ UidEntry(uid_t uid):uid(uid),size(0) { }
- 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);
+ inline const uid_t&getKey() const { return uid; }
+ size_t getSizes() const { return size; }
+ inline void add(size_t s) { size += s; }
+ inline bool subtract(size_t s) { size -= s; return !size; }
};
// Log Statistics
class LogStatistics {
- LidStatistics LogIds[LOG_ID_MAX];
-
size_t mSizes[LOG_ID_MAX];
size_t mElements[LOG_ID_MAX];
+ size_t mSizesTotal[LOG_ID_MAX];
+ size_t mElementsTotal[LOG_ID_MAX];
- bool mStatistics;
-
- static const unsigned short mBuckets[14];
- log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])];
+ // uid to size list
+ typedef android::BasicHashtable<uid_t, UidEntry> uidTable_t;
+ uidTable_t uidTable[LOG_ID_MAX];
public:
- const log_time start;
-
LogStatistics();
- LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; }
+ void enableStatistics() { }
- void enableStatistics() { mStatistics = true; }
+ void add(LogBufferElement *entry);
+ void subtract(LogBufferElement *entry);
- 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();
+ // Caller must delete array
+ const UidEntry **sort(size_t n, log_id i);
// 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);
+ size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; }
+ size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; }
// *strp = malloc, balance with free
- void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest);
+ void format(char **strp, uid_t uid, unsigned int logMask);
// helper
- static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); }
+ char *pidToName(pid_t pid);
uid_t pidToUid(pid_t pid);
+ char *uidToName(uid_t uid);
};
#endif // _LOGD_LOG_STATISTICS_H__
diff --git a/logd/LogWhiteBlackList.h b/logd/LogWhiteBlackList.h
index 769d651..5f60801 100644
--- a/logd/LogWhiteBlackList.h
+++ b/logd/LogWhiteBlackList.h
@@ -61,7 +61,9 @@
int init(char *str);
bool naughty(LogBufferElement *element);
+ bool naughty(void) { return !mNaughty.empty(); }
bool nice(LogBufferElement *element);
+ bool nice(void) { return !mNice.empty(); }
bool worstUidEnabled() const { return mWorstUidEnabled; }
// *strp is malloc'd, use free to release
diff --git a/logd/libaudit.c b/logd/libaudit.c
index d00d579..cf76305 100644
--- a/logd/libaudit.c
+++ b/logd/libaudit.c
@@ -177,7 +177,7 @@
*/
status.pid = pid;
status.mask = AUDIT_STATUS_PID | AUDIT_STATUS_RATE_LIMIT;
- status.rate_limit = 20; // audit entries per second
+ status.rate_limit = 5; // 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));
diff --git a/rootdir/init.environ.rc.in b/rootdir/init.environ.rc.in
index 0064790..b34ea01 100644
--- a/rootdir/init.environ.rc.in
+++ b/rootdir/init.environ.rc.in
@@ -5,7 +5,7 @@
export ANDROID_ASSETS /system/app
export ANDROID_DATA /data
export ANDROID_STORAGE /storage
+ export EXTERNAL_STORAGE /sdcard
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 2ff0d04..9fad0ed 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -26,14 +26,9 @@
start ueventd
- # create mountpoints
- mkdir /mnt 0775 root system
-
on init
sysclktz 0
- loglevel 3
-
# Backward compatibility.
symlink /system/etc /etc
symlink /sys/kernel/debug /d
@@ -64,28 +59,29 @@
mkdir /cache 0770 system cache
mkdir /config 0500 root root
+ # Mount staging areas for devices managed by vold
# See storage config details at http://source.android.com/tech/storage/
- mkdir /mnt/shell 0700 shell shell
- mkdir /mnt/media_rw 0700 media_rw media_rw
- mkdir /storage 0751 root sdcard_r
+ mkdir /mnt 0755 root system
+ mount tmpfs tmpfs /mnt mode=0755,uid=0,gid=1000
+ restorecon_recursive /mnt
- # Directory for putting things only root should see.
mkdir /mnt/secure 0700 root root
+ mkdir /mnt/secure/asec 0700 root root
+ mkdir /mnt/asec 0755 root system
+ mkdir /mnt/obb 0755 root system
+ mkdir /mnt/media_rw 0750 root media_rw
+ mkdir /mnt/user 0755 root root
+ mkdir /mnt/user/0 0755 root root
- # Directory for staging bindmounts
- mkdir /mnt/secure/staging 0700 root root
+ # sdcard_r is GID 1028
+ mkdir /storage 0751 root sdcard_r
+ mount tmpfs tmpfs /storage mode=0751,uid=0,gid=1028
+ restorecon_recursive /storage
- # Directory-target for where the secure container
- # imagefile directory will be bind-mounted
- mkdir /mnt/secure/asec 0700 root root
-
- # Secure container public mount points.
- mkdir /mnt/asec 0700 root system
- mount tmpfs tmpfs /mnt/asec mode=0755,gid=1000
-
- # Filesystem image public mount points.
- mkdir /mnt/obb 0700 root system
- mount tmpfs tmpfs /mnt/obb mode=0755,gid=1000
+ # Symlink to keep legacy apps working in multi-user world
+ mkdir /storage/self 0751 root sdcard_r
+ symlink /storage/self/primary /sdcard
+ symlink /mnt/user/0/primary /storage/self/primary
# memory control cgroup
mkdir /dev/memcg 0700 root system
@@ -237,6 +233,9 @@
# We restorecon /data in case the userdata partition has been reset.
restorecon /data
+ # Emulated internal storage area
+ mkdir /data/media 0770 media_rw media_rw
+
# Start bootcharting as soon as possible after the data partition is
# mounted to collect more data.
mkdir /data/bootchart 0755 shell shell
diff --git a/run-as/run-as.c b/run-as/run-as.c
index cc05e63..368b8f1 100644
--- a/run-as/run-as.c
+++ b/run-as/run-as.c
@@ -15,22 +15,25 @@
** limitations under the License.
*/
-#define PROGNAME "run-as"
-#define LOG_TAG PROGNAME
+#define PROGNAME "run-as"
+#define LOG_TAG PROGNAME
+#include <dirent.h>
+#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
+#include <sys/capability.h>
+#include <sys/cdefs.h>
#include <sys/stat.h>
-#include <dirent.h>
-#include <errno.h>
-#include <unistd.h>
+#include <sys/types.h>
#include <time.h>
-#include <stdarg.h>
+#include <unistd.h>
-#include <selinux/android.h>
#include <private/android_filesystem_config.h>
+#include <selinux/android.h>
+
#include "package.h"
/*
@@ -83,37 +86,37 @@
* - Run the 'gdbserver' binary executable to allow native debugging
*/
-static void
-usage(void)
-{
- const char* str = "Usage: " PROGNAME " <package-name> <command> [<args>]\n\n";
- write(1, str, strlen(str));
- exit(1);
-}
-
-
-static void
+__noreturn static void
panic(const char* format, ...)
{
va_list args;
+ int e = errno;
fprintf(stderr, "%s: ", PROGNAME);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
- exit(1);
+ exit(e ? -e : 1);
}
+static void
+usage(void)
+{
+ panic("Usage:\n " PROGNAME " <package-name> <command> [<args>]\n");
+}
int main(int argc, char **argv)
{
const char* pkgname;
int myuid, uid, gid;
PackageInfo info;
+ struct __user_cap_header_struct capheader;
+ struct __user_cap_data_struct capdata[2];
/* check arguments */
- if (argc < 2)
+ if (argc < 2) {
usage();
+ }
/* check userid of caller - must be 'shell' or 'root' */
myuid = getuid();
@@ -121,29 +124,37 @@
panic("only 'shell' or 'root' users can run this program\n");
}
- /* retrieve package information from system */
+ memset(&capheader, 0, sizeof(capheader));
+ memset(&capdata, 0, sizeof(capdata));
+ capheader.version = _LINUX_CAPABILITY_VERSION_3;
+ capdata[CAP_TO_INDEX(CAP_SETUID)].effective |= CAP_TO_MASK(CAP_SETUID);
+ capdata[CAP_TO_INDEX(CAP_SETGID)].effective |= CAP_TO_MASK(CAP_SETGID);
+ capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
+ capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
+
+ if (capset(&capheader, &capdata[0]) < 0) {
+ panic("Could not set capabilities: %s\n", strerror(errno));
+ }
+
+ /* retrieve package information from system (does setegid) */
pkgname = argv[1];
if (get_package_info(pkgname, &info) < 0) {
panic("Package '%s' is unknown\n", pkgname);
- return 1;
}
/* reject system packages */
if (info.uid < AID_APP) {
panic("Package '%s' is not an application\n", pkgname);
- return 1;
}
/* reject any non-debuggable package */
if (!info.isDebuggable) {
panic("Package '%s' is not debuggable\n", pkgname);
- return 1;
}
/* check that the data directory path is valid */
if (check_data_path(info.dataDir, info.uid) < 0) {
panic("Package '%s' has corrupt installation\n", pkgname);
- return 1;
}
/* Ensure that we change all real/effective/saved IDs at the
@@ -152,38 +163,30 @@
uid = gid = info.uid;
if(setresgid(gid,gid,gid) || setresuid(uid,uid,uid)) {
panic("Permission denied\n");
- return 1;
+ }
+
+ /* Required if caller has uid and gid all non-zero */
+ memset(&capdata, 0, sizeof(capdata));
+ if (capset(&capheader, &capdata[0]) < 0) {
+ panic("Could not clear all capabilities: %s\n", strerror(errno));
}
if (selinux_android_setcontext(uid, 0, info.seinfo, pkgname) < 0) {
- panic("Could not set SELinux security context: %s\n", strerror(errno));
- return 1;
+ panic("Could not set SELinux security context: %s\n", strerror(errno));
}
/* cd into the data directory */
- {
- int ret;
- do {
- ret = chdir(info.dataDir);
- } while (ret < 0 && errno == EINTR);
-
- if (ret < 0) {
- panic("Could not cd to package's data directory: %s\n", strerror(errno));
- return 1;
- }
+ if (TEMP_FAILURE_RETRY(chdir(info.dataDir)) < 0) {
+ panic("Could not cd to package's data directory: %s\n", strerror(errno));
}
/* User specified command for exec. */
- if (argc >= 3 ) {
- if (execvp(argv[2], argv+2) < 0) {
- panic("exec failed for %s Error:%s\n", argv[2], strerror(errno));
- return -errno;
- }
+ if ((argc >= 3) && (execvp(argv[2], argv+2) < 0)) {
+ panic("exec failed for %s: %s\n", argv[2], strerror(errno));
}
/* Default exec shell. */
execlp("/system/bin/sh", "sh", NULL);
- panic("exec failed\n");
- return 1;
+ panic("exec failed: %s\n", strerror(errno));
}
diff --git a/sdcard/sdcard.c b/sdcard/sdcard.c
index 4d50bf0..2cb41e5 100644
--- a/sdcard/sdcard.c
+++ b/sdcard/sdcard.c
@@ -146,8 +146,6 @@
PERM_ANDROID_OBB,
/* This node is "/Android/media" */
PERM_ANDROID_MEDIA,
- /* This node is "/Android/user" */
- PERM_ANDROID_USER,
} perm_t;
/* Permissions structure to derive */
@@ -250,7 +248,7 @@
__u32 inode_ctr;
Hashmap* package_to_appid;
- Hashmap* appid_with_rw;
+ Hashmap* uid_with_rw;
};
/* Private data used by a single fuse handler. */
@@ -472,6 +470,8 @@
/* Legacy internal layout places users at top level */
node->perm = PERM_ROOT;
node->userid = strtoul(node->name, NULL, 10);
+ node->gid = multiuser_get_uid(node->userid, AID_SDCARD_R);
+ node->mode = 0771;
break;
case PERM_ROOT:
/* Assume masked off by default. */
@@ -483,14 +483,14 @@
} else if (fuse->split_perms) {
if (!strcasecmp(node->name, "DCIM")
|| !strcasecmp(node->name, "Pictures")) {
- node->gid = AID_SDCARD_PICS;
+ node->gid = multiuser_get_uid(node->userid, AID_SDCARD_PICS);
} else if (!strcasecmp(node->name, "Alarms")
|| !strcasecmp(node->name, "Movies")
|| !strcasecmp(node->name, "Music")
|| !strcasecmp(node->name, "Notifications")
|| !strcasecmp(node->name, "Podcasts")
|| !strcasecmp(node->name, "Ringtones")) {
- node->gid = AID_SDCARD_AV;
+ node->gid = multiuser_get_uid(node->userid, AID_SDCARD_AV);
}
}
break;
@@ -510,13 +510,6 @@
/* 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-
- * specific path. */
- node->perm = PERM_ANDROID_USER;
- node->gid = AID_SDCARD_ALL;
- node->mode = 0770;
}
break;
case PERM_ANDROID_DATA:
@@ -528,13 +521,6 @@
}
node->mode = 0770;
break;
- case PERM_ANDROID_USER:
- /* Root of a secondary user */
- node->perm = PERM_ROOT;
- node->userid = strtoul(node->name, NULL, 10);
- node->gid = AID_SDCARD_R;
- node->mode = 0771;
- break;
}
}
@@ -545,8 +531,7 @@
return true;
}
- appid_t appid = multiuser_get_app_id(hdr->uid);
- return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
+ return hashmapContainsKey(fuse->uid_with_rw, (void*) (uintptr_t) hdr->uid);
}
/* Kernel has already enforced everything we returned through
@@ -725,7 +710,7 @@
}
static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
- gid_t write_gid, derive_t derive, bool split_perms) {
+ gid_t write_gid, userid_t owner_user, derive_t derive, bool split_perms) {
pthread_mutex_init(&fuse->lock, NULL);
fuse->fd = fd;
@@ -757,10 +742,10 @@
* places user_id at the top directory level, with the actual roots
* just below that. Shared OBB path is also at top level. */
fuse->root.perm = PERM_LEGACY_PRE_ROOT;
- fuse->root.mode = 0771;
+ fuse->root.mode = 0711;
fuse->root.gid = AID_SDCARD_R;
fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
- fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
+ fuse->uid_with_rw = hashmapCreate(128, int_hash, int_equals);
snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
break;
@@ -769,9 +754,10 @@
* /Android/user and shared OBB path under /Android/obb. */
fuse->root.perm = PERM_ROOT;
fuse->root.mode = 0771;
- fuse->root.gid = AID_SDCARD_R;
+ fuse->root.userid = owner_user;
+ fuse->root.gid = multiuser_get_uid(owner_user, AID_SDCARD_R);
fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
- fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
+ fuse->uid_with_rw = hashmapCreate(128, int_hash, int_equals);
snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
break;
}
@@ -1692,7 +1678,7 @@
pthread_mutex_lock(&fuse->lock);
hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
- hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
+ hashmapForEach(fuse->uid_with_rw, remove_int_to_null, fuse->uid_with_rw);
FILE* file = fopen(kPackagesListFile, "r");
if (!file) {
@@ -1713,9 +1699,14 @@
char* token = strtok(gids, ",");
while (token != NULL) {
- if (strtoul(token, NULL, 10) == fuse->write_gid) {
- hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
- break;
+ // Current packages.list format is a bit funky; it blends per
+ // user GID membership into a single per-app line. Here we
+ // work backwards from the groups to build the per-user UIDs
+ // that have write permission.
+ gid_t gid = strtoul(token, NULL, 10);
+ if (multiuser_get_app_id(gid) == fuse->write_gid) {
+ uid_t uid = multiuser_get_uid(multiuser_get_user_id(gid), appid);
+ hashmapPut(fuse->uid_with_rw, (void*) (uintptr_t) uid, (void*) (uintptr_t) 1);
}
token = strtok(NULL, ",");
}
@@ -1724,7 +1715,7 @@
TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
hashmapSize(fuse->package_to_appid),
- hashmapSize(fuse->appid_with_rw));
+ hashmapSize(fuse->uid_with_rw));
fclose(file);
pthread_mutex_unlock(&fuse->lock);
return 0;
@@ -1849,7 +1840,7 @@
}
static int run(const char* source_path, const char* dest_path, uid_t uid,
- gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
+ gid_t gid, gid_t write_gid, userid_t owner_user, int num_threads, derive_t derive,
bool split_perms) {
int fd;
char opts[256];
@@ -1893,7 +1884,7 @@
goto error;
}
- fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
+ fuse_init(&fuse, fd, source_path, write_gid, owner_user, derive, split_perms);
umask(0);
res = ignite_fuse(&fuse, num_threads);
@@ -1914,6 +1905,7 @@
uid_t uid = 0;
gid_t gid = 0;
gid_t write_gid = AID_SDCARD_RW;
+ userid_t owner_user = 0;
int num_threads = DEFAULT_NUM_THREADS;
derive_t derive = DERIVE_NONE;
bool split_perms = false;
@@ -1922,7 +1914,7 @@
int fs_version;
int opt;
- while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
+ while ((opt = getopt(argc, argv, "u:g:w:o:t:dls")) != -1) {
switch (opt) {
case 'u':
uid = strtoul(optarg, NULL, 10);
@@ -1933,6 +1925,9 @@
case 'w':
write_gid = strtoul(optarg, NULL, 10);
break;
+ case 'o':
+ owner_user = strtoul(optarg, NULL, 10);
+ break;
case 't':
num_threads = strtoul(optarg, NULL, 10);
break;
@@ -1999,6 +1994,7 @@
sleep(1);
}
- res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
+ res = run(source_path, dest_path, uid, gid, write_gid, owner_user,
+ num_threads, derive, split_perms);
return res < 0 ? 1 : 0;
}
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 959dc22..4e54eb8 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -47,7 +47,6 @@
iftop \
ioctl \
ionice \
- load_policy \
log \
ls \
lsof \
@@ -58,8 +57,6 @@
prlimit \
renice \
restorecon \
- route \
- runcon \
sendevent \
setprop \
start \
@@ -103,6 +100,14 @@
$(TOOLS_H):
$(transform-generated-source)
+$(LOCAL_PATH)/getevent.c: $(intermediates)/input.h-labels.h
+
+INPUT_H_LABELS_H := $(intermediates)/input.h-labels.h
+$(INPUT_H_LABELS_H): PRIVATE_LOCAL_PATH := $(LOCAL_PATH)
+$(INPUT_H_LABELS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_LOCAL_PATH)/generate-input.h-labels.py > $@
+$(INPUT_H_LABELS_H): $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/generate-input.h-labels.py
+$(INPUT_H_LABELS_H):
+ $(transform-generated-source)
# We only want 'r' on userdebug and eng builds.
include $(CLEAR_VARS)
diff --git a/toolbox/generate-input.h-labels.py b/toolbox/generate-input.h-labels.py
new file mode 100755
index 0000000..ebb9588
--- /dev/null
+++ b/toolbox/generate-input.h-labels.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+#
+# 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.
+#
+# pylint: disable=bad-indentation,bad-continuation
+
+import os
+import re
+
+input_prop_list = []
+ev_list = []
+syn_list = []
+key_list = []
+rel_list = []
+abs_list = []
+sw_list = []
+msc_list = []
+led_list = []
+rep_list = []
+snd_list = []
+mt_tool_list = []
+ff_status_list = []
+ff_list = []
+
+r = re.compile(r'#define\s+(\S+)\s+((?:0x)?\d+)')
+
+with open('bionic/libc/kernel/uapi/linux/input.h', 'r') as f:
+ for line in f:
+ m = r.match(line)
+ if m:
+ name = m.group(1)
+ if name.startswith("INPUT_PROP_"):
+ input_prop_list.append(name)
+ elif name.startswith("EV_"):
+ ev_list.append(name)
+ elif name.startswith("SYN_"):
+ syn_list.append(name)
+ elif name.startswith("KEY_") or name.startswith("BTN_"):
+ key_list.append(name)
+ elif name.startswith("REL_"):
+ rel_list.append(name)
+ elif name.startswith("ABS_"):
+ abs_list.append(name)
+ elif name.startswith("SW_"):
+ sw_list.append(name)
+ elif name.startswith("MSC_"):
+ msc_list.append(name)
+ elif name.startswith("LED_"):
+ led_list.append(name)
+ elif name.startswith("REP_"):
+ rep_list.append(name)
+ elif name.startswith("SND_"):
+ snd_list.append(name)
+ elif name.startswith("MT_TOOL_"):
+ mt_tool_list.append(name)
+ elif name.startswith("FF_STATUS_"):
+ ff_status_list.append(name)
+ elif name.startswith("FF_"):
+ ff_list.append(name)
+
+def Dump(struct_name, values):
+ print 'static struct label %s[] = {' % (struct_name)
+ for value in values:
+ print ' LABEL(%s),' % (value)
+ print ' LABEL_END,'
+ print '};'
+
+Dump("input_prop_labels", input_prop_list)
+Dump("ev_labels", ev_list)
+Dump("syn_labels", syn_list)
+Dump("key_labels", key_list)
+Dump("rel_labels", rel_list)
+Dump("abs_labels", abs_list)
+Dump("sw_labels", sw_list)
+Dump("msc_labels", msc_list)
+Dump("led_labels", led_list)
+Dump("rep_labels", rep_list)
+Dump("snd_labels", snd_list)
+Dump("mt_tool_labels", mt_tool_list)
+Dump("ff_status_labels", ff_status_list)
+Dump("ff_labels", ff_list)
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index c58eb5d..30053af 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -12,7 +12,25 @@
#include <errno.h>
#include <unistd.h>
-#include "getevent.h"
+struct label {
+ const char *name;
+ int value;
+};
+
+#define LABEL(constant) { #constant, constant }
+#define LABEL_END { NULL, -1 }
+
+static struct label key_value_labels[] = {
+ { "UP", 0 },
+ { "DOWN", 1 },
+ { "REPEAT", 2 },
+ LABEL_END,
+};
+
+#include "input.h-labels.h"
+
+#undef LABEL
+#undef LABEL_END
static struct pollfd *ufds;
static char **device_names;
diff --git a/toolbox/getevent.h b/toolbox/getevent.h
deleted file mode 100644
index 0482d04..0000000
--- a/toolbox/getevent.h
+++ /dev/null
@@ -1,727 +0,0 @@
-#include <linux/input.h>
-
-struct label {
- const char *name;
- int value;
-};
-
-#define LABEL(constant) { #constant, constant }
-#define LABEL_END { NULL, -1 }
-
-static struct label input_prop_labels[] = {
- LABEL(INPUT_PROP_POINTER),
- LABEL(INPUT_PROP_DIRECT),
- LABEL(INPUT_PROP_BUTTONPAD),
- LABEL(INPUT_PROP_SEMI_MT),
- LABEL_END,
-};
-
-static struct label ev_labels[] = {
- LABEL(EV_SYN),
- LABEL(EV_KEY),
- LABEL(EV_REL),
- LABEL(EV_ABS),
- LABEL(EV_MSC),
- LABEL(EV_SW),
- LABEL(EV_LED),
- LABEL(EV_SND),
- LABEL(EV_REP),
- LABEL(EV_FF),
- LABEL(EV_PWR),
- LABEL(EV_FF_STATUS),
- LABEL_END,
-};
-
-static struct label syn_labels[] = {
- LABEL(SYN_REPORT),
- LABEL(SYN_CONFIG),
- LABEL(SYN_MT_REPORT),
- LABEL(SYN_DROPPED),
- LABEL_END,
-};
-
-static struct label key_labels[] = {
- LABEL(KEY_RESERVED),
- LABEL(KEY_ESC),
- LABEL(KEY_1),
- LABEL(KEY_2),
- LABEL(KEY_3),
- LABEL(KEY_4),
- LABEL(KEY_5),
- LABEL(KEY_6),
- LABEL(KEY_7),
- LABEL(KEY_8),
- LABEL(KEY_9),
- LABEL(KEY_0),
- LABEL(KEY_MINUS),
- LABEL(KEY_EQUAL),
- LABEL(KEY_BACKSPACE),
- LABEL(KEY_TAB),
- LABEL(KEY_Q),
- LABEL(KEY_W),
- LABEL(KEY_E),
- LABEL(KEY_R),
- LABEL(KEY_T),
- LABEL(KEY_Y),
- LABEL(KEY_U),
- LABEL(KEY_I),
- LABEL(KEY_O),
- LABEL(KEY_P),
- LABEL(KEY_LEFTBRACE),
- LABEL(KEY_RIGHTBRACE),
- LABEL(KEY_ENTER),
- LABEL(KEY_LEFTCTRL),
- LABEL(KEY_A),
- LABEL(KEY_S),
- LABEL(KEY_D),
- LABEL(KEY_F),
- LABEL(KEY_G),
- LABEL(KEY_H),
- LABEL(KEY_J),
- LABEL(KEY_K),
- LABEL(KEY_L),
- LABEL(KEY_SEMICOLON),
- LABEL(KEY_APOSTROPHE),
- LABEL(KEY_GRAVE),
- LABEL(KEY_LEFTSHIFT),
- LABEL(KEY_BACKSLASH),
- LABEL(KEY_Z),
- LABEL(KEY_X),
- LABEL(KEY_C),
- LABEL(KEY_V),
- LABEL(KEY_B),
- LABEL(KEY_N),
- LABEL(KEY_M),
- LABEL(KEY_COMMA),
- LABEL(KEY_DOT),
- LABEL(KEY_SLASH),
- LABEL(KEY_RIGHTSHIFT),
- LABEL(KEY_KPASTERISK),
- LABEL(KEY_LEFTALT),
- LABEL(KEY_SPACE),
- LABEL(KEY_CAPSLOCK),
- LABEL(KEY_F1),
- LABEL(KEY_F2),
- LABEL(KEY_F3),
- LABEL(KEY_F4),
- LABEL(KEY_F5),
- LABEL(KEY_F6),
- LABEL(KEY_F7),
- LABEL(KEY_F8),
- LABEL(KEY_F9),
- LABEL(KEY_F10),
- LABEL(KEY_NUMLOCK),
- LABEL(KEY_SCROLLLOCK),
- LABEL(KEY_KP7),
- LABEL(KEY_KP8),
- LABEL(KEY_KP9),
- LABEL(KEY_KPMINUS),
- LABEL(KEY_KP4),
- LABEL(KEY_KP5),
- LABEL(KEY_KP6),
- LABEL(KEY_KPPLUS),
- LABEL(KEY_KP1),
- LABEL(KEY_KP2),
- LABEL(KEY_KP3),
- LABEL(KEY_KP0),
- LABEL(KEY_KPDOT),
- LABEL(KEY_ZENKAKUHANKAKU),
- LABEL(KEY_102ND),
- LABEL(KEY_F11),
- LABEL(KEY_F12),
- LABEL(KEY_RO),
- LABEL(KEY_KATAKANA),
- LABEL(KEY_HIRAGANA),
- LABEL(KEY_HENKAN),
- LABEL(KEY_KATAKANAHIRAGANA),
- LABEL(KEY_MUHENKAN),
- LABEL(KEY_KPJPCOMMA),
- LABEL(KEY_KPENTER),
- LABEL(KEY_RIGHTCTRL),
- LABEL(KEY_KPSLASH),
- LABEL(KEY_SYSRQ),
- LABEL(KEY_RIGHTALT),
- LABEL(KEY_LINEFEED),
- LABEL(KEY_HOME),
- LABEL(KEY_UP),
- LABEL(KEY_PAGEUP),
- LABEL(KEY_LEFT),
- LABEL(KEY_RIGHT),
- LABEL(KEY_END),
- LABEL(KEY_DOWN),
- LABEL(KEY_PAGEDOWN),
- LABEL(KEY_INSERT),
- LABEL(KEY_DELETE),
- LABEL(KEY_MACRO),
- LABEL(KEY_MUTE),
- LABEL(KEY_VOLUMEDOWN),
- LABEL(KEY_VOLUMEUP),
- LABEL(KEY_POWER),
- LABEL(KEY_KPEQUAL),
- LABEL(KEY_KPPLUSMINUS),
- LABEL(KEY_PAUSE),
- LABEL(KEY_SCALE),
- LABEL(KEY_KPCOMMA),
- LABEL(KEY_HANGEUL),
- LABEL(KEY_HANGUEL),
- LABEL(KEY_HANJA),
- LABEL(KEY_YEN),
- LABEL(KEY_LEFTMETA),
- LABEL(KEY_RIGHTMETA),
- LABEL(KEY_COMPOSE),
- LABEL(KEY_STOP),
- LABEL(KEY_AGAIN),
- LABEL(KEY_PROPS),
- LABEL(KEY_UNDO),
- LABEL(KEY_FRONT),
- LABEL(KEY_COPY),
- LABEL(KEY_OPEN),
- LABEL(KEY_PASTE),
- LABEL(KEY_FIND),
- LABEL(KEY_CUT),
- LABEL(KEY_HELP),
- LABEL(KEY_MENU),
- LABEL(KEY_CALC),
- LABEL(KEY_SETUP),
- LABEL(KEY_SLEEP),
- LABEL(KEY_WAKEUP),
- LABEL(KEY_FILE),
- LABEL(KEY_SENDFILE),
- LABEL(KEY_DELETEFILE),
- LABEL(KEY_XFER),
- LABEL(KEY_PROG1),
- LABEL(KEY_PROG2),
- LABEL(KEY_WWW),
- LABEL(KEY_MSDOS),
- LABEL(KEY_COFFEE),
- LABEL(KEY_SCREENLOCK),
- LABEL(KEY_DIRECTION),
- LABEL(KEY_CYCLEWINDOWS),
- LABEL(KEY_MAIL),
- LABEL(KEY_BOOKMARKS),
- LABEL(KEY_COMPUTER),
- LABEL(KEY_BACK),
- LABEL(KEY_FORWARD),
- LABEL(KEY_CLOSECD),
- LABEL(KEY_EJECTCD),
- LABEL(KEY_EJECTCLOSECD),
- LABEL(KEY_NEXTSONG),
- LABEL(KEY_PLAYPAUSE),
- LABEL(KEY_PREVIOUSSONG),
- LABEL(KEY_STOPCD),
- LABEL(KEY_RECORD),
- LABEL(KEY_REWIND),
- LABEL(KEY_PHONE),
- LABEL(KEY_ISO),
- LABEL(KEY_CONFIG),
- LABEL(KEY_HOMEPAGE),
- LABEL(KEY_REFRESH),
- LABEL(KEY_EXIT),
- LABEL(KEY_MOVE),
- LABEL(KEY_EDIT),
- LABEL(KEY_SCROLLUP),
- LABEL(KEY_SCROLLDOWN),
- LABEL(KEY_KPLEFTPAREN),
- LABEL(KEY_KPRIGHTPAREN),
- LABEL(KEY_NEW),
- LABEL(KEY_REDO),
- LABEL(KEY_F13),
- LABEL(KEY_F14),
- LABEL(KEY_F15),
- LABEL(KEY_F16),
- LABEL(KEY_F17),
- LABEL(KEY_F18),
- LABEL(KEY_F19),
- LABEL(KEY_F20),
- LABEL(KEY_F21),
- LABEL(KEY_F22),
- LABEL(KEY_F23),
- LABEL(KEY_F24),
- LABEL(KEY_PLAYCD),
- LABEL(KEY_PAUSECD),
- LABEL(KEY_PROG3),
- LABEL(KEY_PROG4),
- LABEL(KEY_DASHBOARD),
- LABEL(KEY_SUSPEND),
- LABEL(KEY_CLOSE),
- LABEL(KEY_PLAY),
- LABEL(KEY_FASTFORWARD),
- LABEL(KEY_BASSBOOST),
- LABEL(KEY_PRINT),
- LABEL(KEY_HP),
- LABEL(KEY_CAMERA),
- LABEL(KEY_SOUND),
- LABEL(KEY_QUESTION),
- LABEL(KEY_EMAIL),
- LABEL(KEY_CHAT),
- LABEL(KEY_SEARCH),
- LABEL(KEY_CONNECT),
- LABEL(KEY_FINANCE),
- LABEL(KEY_SPORT),
- LABEL(KEY_SHOP),
- LABEL(KEY_ALTERASE),
- LABEL(KEY_CANCEL),
- LABEL(KEY_BRIGHTNESSDOWN),
- LABEL(KEY_BRIGHTNESSUP),
- LABEL(KEY_MEDIA),
- LABEL(KEY_SWITCHVIDEOMODE),
- LABEL(KEY_KBDILLUMTOGGLE),
- LABEL(KEY_KBDILLUMDOWN),
- LABEL(KEY_KBDILLUMUP),
- LABEL(KEY_SEND),
- LABEL(KEY_REPLY),
- LABEL(KEY_FORWARDMAIL),
- LABEL(KEY_SAVE),
- LABEL(KEY_DOCUMENTS),
- LABEL(KEY_BATTERY),
- LABEL(KEY_BLUETOOTH),
- LABEL(KEY_WLAN),
- LABEL(KEY_UWB),
- LABEL(KEY_UNKNOWN),
- LABEL(KEY_VIDEO_NEXT),
- LABEL(KEY_VIDEO_PREV),
- LABEL(KEY_BRIGHTNESS_CYCLE),
- LABEL(KEY_BRIGHTNESS_ZERO),
- LABEL(KEY_DISPLAY_OFF),
- LABEL(KEY_WIMAX),
- LABEL(KEY_RFKILL),
- LABEL(BTN_0),
- LABEL(BTN_1),
- LABEL(BTN_2),
- LABEL(BTN_3),
- LABEL(BTN_4),
- LABEL(BTN_5),
- LABEL(BTN_6),
- LABEL(BTN_7),
- LABEL(BTN_8),
- LABEL(BTN_9),
- LABEL(BTN_LEFT),
- LABEL(BTN_RIGHT),
- LABEL(BTN_MIDDLE),
- LABEL(BTN_SIDE),
- LABEL(BTN_EXTRA),
- LABEL(BTN_FORWARD),
- LABEL(BTN_BACK),
- LABEL(BTN_TASK),
- LABEL(BTN_JOYSTICK),
- LABEL(BTN_TRIGGER),
- LABEL(BTN_THUMB),
- LABEL(BTN_THUMB2),
- LABEL(BTN_TOP),
- LABEL(BTN_TOP2),
- LABEL(BTN_PINKIE),
- LABEL(BTN_BASE),
- LABEL(BTN_BASE2),
- LABEL(BTN_BASE3),
- LABEL(BTN_BASE4),
- LABEL(BTN_BASE5),
- LABEL(BTN_BASE6),
- LABEL(BTN_DEAD),
- LABEL(BTN_A),
- LABEL(BTN_B),
- LABEL(BTN_C),
- LABEL(BTN_X),
- LABEL(BTN_Y),
- LABEL(BTN_Z),
- LABEL(BTN_TL),
- LABEL(BTN_TR),
- LABEL(BTN_TL2),
- LABEL(BTN_TR2),
- LABEL(BTN_SELECT),
- LABEL(BTN_START),
- LABEL(BTN_MODE),
- LABEL(BTN_THUMBL),
- LABEL(BTN_THUMBR),
- LABEL(BTN_TOOL_PEN),
- LABEL(BTN_TOOL_RUBBER),
- LABEL(BTN_TOOL_BRUSH),
- LABEL(BTN_TOOL_PENCIL),
- LABEL(BTN_TOOL_AIRBRUSH),
- LABEL(BTN_TOOL_FINGER),
- LABEL(BTN_TOOL_MOUSE),
- LABEL(BTN_TOOL_LENS),
- LABEL(BTN_TOUCH),
- LABEL(BTN_STYLUS),
- LABEL(BTN_STYLUS2),
- LABEL(BTN_TOOL_DOUBLETAP),
- LABEL(BTN_TOOL_TRIPLETAP),
- LABEL(BTN_TOOL_QUADTAP),
- LABEL(BTN_GEAR_DOWN),
- LABEL(BTN_GEAR_UP),
- LABEL(KEY_OK),
- LABEL(KEY_SELECT),
- LABEL(KEY_GOTO),
- LABEL(KEY_CLEAR),
- LABEL(KEY_POWER2),
- LABEL(KEY_OPTION),
- LABEL(KEY_INFO),
- LABEL(KEY_TIME),
- LABEL(KEY_VENDOR),
- LABEL(KEY_ARCHIVE),
- LABEL(KEY_PROGRAM),
- LABEL(KEY_CHANNEL),
- LABEL(KEY_FAVORITES),
- LABEL(KEY_EPG),
- LABEL(KEY_PVR),
- LABEL(KEY_MHP),
- LABEL(KEY_LANGUAGE),
- LABEL(KEY_TITLE),
- LABEL(KEY_SUBTITLE),
- LABEL(KEY_ANGLE),
- LABEL(KEY_ZOOM),
- LABEL(KEY_MODE),
- LABEL(KEY_KEYBOARD),
- LABEL(KEY_SCREEN),
- LABEL(KEY_PC),
- LABEL(KEY_TV),
- LABEL(KEY_TV2),
- LABEL(KEY_VCR),
- LABEL(KEY_VCR2),
- LABEL(KEY_SAT),
- LABEL(KEY_SAT2),
- LABEL(KEY_CD),
- LABEL(KEY_TAPE),
- LABEL(KEY_RADIO),
- LABEL(KEY_TUNER),
- LABEL(KEY_PLAYER),
- LABEL(KEY_TEXT),
- LABEL(KEY_DVD),
- LABEL(KEY_AUX),
- LABEL(KEY_MP3),
- LABEL(KEY_AUDIO),
- LABEL(KEY_VIDEO),
- LABEL(KEY_DIRECTORY),
- LABEL(KEY_LIST),
- LABEL(KEY_MEMO),
- LABEL(KEY_CALENDAR),
- LABEL(KEY_RED),
- LABEL(KEY_GREEN),
- LABEL(KEY_YELLOW),
- LABEL(KEY_BLUE),
- LABEL(KEY_CHANNELUP),
- LABEL(KEY_CHANNELDOWN),
- LABEL(KEY_FIRST),
- LABEL(KEY_LAST),
- LABEL(KEY_AB),
- LABEL(KEY_NEXT),
- LABEL(KEY_RESTART),
- LABEL(KEY_SLOW),
- LABEL(KEY_SHUFFLE),
- LABEL(KEY_BREAK),
- LABEL(KEY_PREVIOUS),
- LABEL(KEY_DIGITS),
- LABEL(KEY_TEEN),
- LABEL(KEY_TWEN),
- LABEL(KEY_VIDEOPHONE),
- LABEL(KEY_GAMES),
- LABEL(KEY_ZOOMIN),
- LABEL(KEY_ZOOMOUT),
- LABEL(KEY_ZOOMRESET),
- LABEL(KEY_WORDPROCESSOR),
- LABEL(KEY_EDITOR),
- LABEL(KEY_SPREADSHEET),
- LABEL(KEY_GRAPHICSEDITOR),
- LABEL(KEY_PRESENTATION),
- LABEL(KEY_DATABASE),
- LABEL(KEY_NEWS),
- LABEL(KEY_VOICEMAIL),
- LABEL(KEY_ADDRESSBOOK),
- LABEL(KEY_MESSENGER),
- LABEL(KEY_DISPLAYTOGGLE),
- LABEL(KEY_SPELLCHECK),
- LABEL(KEY_LOGOFF),
- LABEL(KEY_DOLLAR),
- LABEL(KEY_EURO),
- LABEL(KEY_FRAMEBACK),
- LABEL(KEY_FRAMEFORWARD),
- LABEL(KEY_CONTEXT_MENU),
- LABEL(KEY_MEDIA_REPEAT),
- LABEL(KEY_10CHANNELSUP),
- LABEL(KEY_10CHANNELSDOWN),
- LABEL(KEY_IMAGES),
- LABEL(KEY_DEL_EOL),
- LABEL(KEY_DEL_EOS),
- LABEL(KEY_INS_LINE),
- LABEL(KEY_DEL_LINE),
- LABEL(KEY_FN),
- LABEL(KEY_FN_ESC),
- LABEL(KEY_FN_F1),
- LABEL(KEY_FN_F2),
- LABEL(KEY_FN_F3),
- LABEL(KEY_FN_F4),
- LABEL(KEY_FN_F5),
- LABEL(KEY_FN_F6),
- LABEL(KEY_FN_F7),
- LABEL(KEY_FN_F8),
- LABEL(KEY_FN_F9),
- LABEL(KEY_FN_F10),
- LABEL(KEY_FN_F11),
- LABEL(KEY_FN_F12),
- LABEL(KEY_FN_1),
- LABEL(KEY_FN_2),
- LABEL(KEY_FN_D),
- LABEL(KEY_FN_E),
- LABEL(KEY_FN_F),
- LABEL(KEY_FN_S),
- LABEL(KEY_FN_B),
- LABEL(KEY_BRL_DOT1),
- LABEL(KEY_BRL_DOT2),
- LABEL(KEY_BRL_DOT3),
- LABEL(KEY_BRL_DOT4),
- LABEL(KEY_BRL_DOT5),
- LABEL(KEY_BRL_DOT6),
- LABEL(KEY_BRL_DOT7),
- LABEL(KEY_BRL_DOT8),
- LABEL(KEY_BRL_DOT9),
- LABEL(KEY_BRL_DOT10),
- LABEL(KEY_NUMERIC_0),
- LABEL(KEY_NUMERIC_1),
- LABEL(KEY_NUMERIC_2),
- LABEL(KEY_NUMERIC_3),
- LABEL(KEY_NUMERIC_4),
- LABEL(KEY_NUMERIC_5),
- LABEL(KEY_NUMERIC_6),
- LABEL(KEY_NUMERIC_7),
- LABEL(KEY_NUMERIC_8),
- LABEL(KEY_NUMERIC_9),
- LABEL(KEY_NUMERIC_STAR),
- LABEL(KEY_NUMERIC_POUND),
- LABEL(KEY_CAMERA_FOCUS),
- LABEL(KEY_WPS_BUTTON),
- LABEL(KEY_TOUCHPAD_TOGGLE),
- LABEL(KEY_TOUCHPAD_ON),
- LABEL(KEY_TOUCHPAD_OFF),
- LABEL(KEY_CAMERA_ZOOMIN),
- LABEL(KEY_CAMERA_ZOOMOUT),
- LABEL(KEY_CAMERA_UP),
- LABEL(KEY_CAMERA_DOWN),
- LABEL(KEY_CAMERA_LEFT),
- LABEL(KEY_CAMERA_RIGHT),
- LABEL(BTN_TRIGGER_HAPPY1),
- LABEL(BTN_TRIGGER_HAPPY2),
- LABEL(BTN_TRIGGER_HAPPY3),
- LABEL(BTN_TRIGGER_HAPPY4),
- LABEL(BTN_TRIGGER_HAPPY5),
- LABEL(BTN_TRIGGER_HAPPY6),
- LABEL(BTN_TRIGGER_HAPPY7),
- LABEL(BTN_TRIGGER_HAPPY8),
- LABEL(BTN_TRIGGER_HAPPY9),
- LABEL(BTN_TRIGGER_HAPPY10),
- LABEL(BTN_TRIGGER_HAPPY11),
- LABEL(BTN_TRIGGER_HAPPY12),
- LABEL(BTN_TRIGGER_HAPPY13),
- LABEL(BTN_TRIGGER_HAPPY14),
- LABEL(BTN_TRIGGER_HAPPY15),
- LABEL(BTN_TRIGGER_HAPPY16),
- LABEL(BTN_TRIGGER_HAPPY17),
- LABEL(BTN_TRIGGER_HAPPY18),
- LABEL(BTN_TRIGGER_HAPPY19),
- LABEL(BTN_TRIGGER_HAPPY20),
- LABEL(BTN_TRIGGER_HAPPY21),
- LABEL(BTN_TRIGGER_HAPPY22),
- LABEL(BTN_TRIGGER_HAPPY23),
- LABEL(BTN_TRIGGER_HAPPY24),
- LABEL(BTN_TRIGGER_HAPPY25),
- LABEL(BTN_TRIGGER_HAPPY26),
- LABEL(BTN_TRIGGER_HAPPY27),
- LABEL(BTN_TRIGGER_HAPPY28),
- LABEL(BTN_TRIGGER_HAPPY29),
- LABEL(BTN_TRIGGER_HAPPY30),
- LABEL(BTN_TRIGGER_HAPPY31),
- LABEL(BTN_TRIGGER_HAPPY32),
- LABEL(BTN_TRIGGER_HAPPY33),
- LABEL(BTN_TRIGGER_HAPPY34),
- LABEL(BTN_TRIGGER_HAPPY35),
- LABEL(BTN_TRIGGER_HAPPY36),
- LABEL(BTN_TRIGGER_HAPPY37),
- LABEL(BTN_TRIGGER_HAPPY38),
- LABEL(BTN_TRIGGER_HAPPY39),
- LABEL(BTN_TRIGGER_HAPPY40),
- LABEL_END,
-};
-
-static struct label rel_labels[] = {
- LABEL(REL_X),
- LABEL(REL_Y),
- LABEL(REL_Z),
- LABEL(REL_RX),
- LABEL(REL_RY),
- LABEL(REL_RZ),
- LABEL(REL_HWHEEL),
- LABEL(REL_DIAL),
- LABEL(REL_WHEEL),
- LABEL(REL_MISC),
- LABEL_END,
-};
-
-static struct label abs_labels[] = {
- LABEL(ABS_X),
- LABEL(ABS_Y),
- LABEL(ABS_Z),
- LABEL(ABS_RX),
- LABEL(ABS_RY),
- LABEL(ABS_RZ),
- LABEL(ABS_THROTTLE),
- LABEL(ABS_RUDDER),
- LABEL(ABS_WHEEL),
- LABEL(ABS_GAS),
- LABEL(ABS_BRAKE),
- LABEL(ABS_HAT0X),
- LABEL(ABS_HAT0Y),
- LABEL(ABS_HAT1X),
- LABEL(ABS_HAT1Y),
- LABEL(ABS_HAT2X),
- LABEL(ABS_HAT2Y),
- LABEL(ABS_HAT3X),
- LABEL(ABS_HAT3Y),
- LABEL(ABS_PRESSURE),
- LABEL(ABS_DISTANCE),
- LABEL(ABS_TILT_X),
- LABEL(ABS_TILT_Y),
- LABEL(ABS_TOOL_WIDTH),
- LABEL(ABS_VOLUME),
- LABEL(ABS_MISC),
- LABEL(ABS_MT_SLOT),
- LABEL(ABS_MT_TOUCH_MAJOR),
- LABEL(ABS_MT_TOUCH_MINOR),
- LABEL(ABS_MT_WIDTH_MAJOR),
- LABEL(ABS_MT_WIDTH_MINOR),
- LABEL(ABS_MT_ORIENTATION),
- LABEL(ABS_MT_POSITION_X),
- LABEL(ABS_MT_POSITION_Y),
- LABEL(ABS_MT_TOOL_TYPE),
- LABEL(ABS_MT_BLOB_ID),
- LABEL(ABS_MT_TRACKING_ID),
- LABEL(ABS_MT_PRESSURE),
- LABEL(ABS_MT_DISTANCE),
- LABEL_END,
-};
-
-static struct label sw_labels[] = {
- LABEL(SW_LID),
- LABEL(SW_TABLET_MODE),
- LABEL(SW_HEADPHONE_INSERT),
- LABEL(SW_RFKILL_ALL),
- LABEL(SW_RADIO),
- LABEL(SW_MICROPHONE_INSERT),
- LABEL(SW_DOCK),
- LABEL(SW_LINEOUT_INSERT),
- LABEL(SW_JACK_PHYSICAL_INSERT),
- LABEL(SW_VIDEOOUT_INSERT),
- LABEL(SW_CAMERA_LENS_COVER),
- LABEL(SW_KEYPAD_SLIDE),
- LABEL(SW_FRONT_PROXIMITY),
- LABEL(SW_ROTATE_LOCK),
- LABEL_END,
-};
-
-static struct label msc_labels[] = {
- LABEL(MSC_SERIAL),
- LABEL(MSC_PULSELED),
- LABEL(MSC_GESTURE),
- LABEL(MSC_RAW),
- LABEL(MSC_SCAN),
- LABEL_END,
-};
-
-static struct label led_labels[] = {
- LABEL(LED_NUML),
- LABEL(LED_CAPSL),
- LABEL(LED_SCROLLL),
- LABEL(LED_COMPOSE),
- LABEL(LED_KANA),
- LABEL(LED_SLEEP),
- LABEL(LED_SUSPEND),
- LABEL(LED_MUTE),
- LABEL(LED_MISC),
- LABEL(LED_MAIL),
- LABEL(LED_CHARGING),
- LABEL_END,
-};
-
-static struct label rep_labels[] = {
- LABEL(REP_DELAY),
- LABEL(REP_PERIOD),
- LABEL_END,
-};
-
-static struct label snd_labels[] = {
- LABEL(SND_CLICK),
- LABEL(SND_BELL),
- LABEL(SND_TONE),
- LABEL_END,
-};
-
-#if 0
-static struct label id_labels[] = {
- LABEL(ID_BUS),
- LABEL(ID_VENDOR),
- LABEL(ID_PRODUCT),
- LABEL(ID_VERSION),
- LABEL_END,
-};
-
-static struct label bus_labels[] = {
- LABEL(BUS_PCI),
- LABEL(BUS_ISAPNP),
- LABEL(BUS_USB),
- LABEL(BUS_HIL),
- LABEL(BUS_BLUETOOTH),
- LABEL(BUS_VIRTUAL),
- LABEL(BUS_ISA),
- LABEL(BUS_I8042),
- LABEL(BUS_XTKBD),
- LABEL(BUS_RS232),
- LABEL(BUS_GAMEPORT),
- LABEL(BUS_PARPORT),
- LABEL(BUS_AMIGA),
- LABEL(BUS_ADB),
- LABEL(BUS_I2C),
- LABEL(BUS_HOST),
- LABEL(BUS_GSC),
- LABEL(BUS_ATARI),
- LABEL(BUS_SPI),
- LABEL_END,
-};
-#endif
-
-static struct label mt_tool_labels[] = {
- LABEL(MT_TOOL_FINGER),
- LABEL(MT_TOOL_PEN),
- LABEL(MT_TOOL_MAX),
- LABEL_END,
-};
-
-static struct label ff_status_labels[] = {
- LABEL(FF_STATUS_STOPPED),
- LABEL(FF_STATUS_PLAYING),
- LABEL(FF_STATUS_MAX),
- LABEL_END,
-};
-
-static struct label ff_labels[] = {
- LABEL(FF_RUMBLE),
- LABEL(FF_PERIODIC),
- LABEL(FF_CONSTANT),
- LABEL(FF_SPRING),
- LABEL(FF_FRICTION),
- LABEL(FF_DAMPER),
- LABEL(FF_INERTIA),
- LABEL(FF_RAMP),
- LABEL(FF_SQUARE),
- LABEL(FF_TRIANGLE),
- LABEL(FF_SINE),
- LABEL(FF_SAW_UP),
- LABEL(FF_SAW_DOWN),
- LABEL(FF_CUSTOM),
- LABEL(FF_GAIN),
- LABEL(FF_AUTOCENTER),
- LABEL_END,
-};
-
-static struct label key_value_labels[] = {
- { "UP", 0 },
- { "DOWN", 1 },
- { "REPEAT", 2 },
- LABEL_END,
-};
diff --git a/toolbox/load_policy.c b/toolbox/load_policy.c
deleted file mode 100644
index 90d48c4..0000000
--- a/toolbox/load_policy.c
+++ /dev/null
@@ -1,49 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int load_policy_main(int argc, char **argv)
-{
- int fd, rc;
- struct stat sb;
- void *map;
- const char *path;
-
- if (argc != 2) {
- fprintf(stderr, "usage: %s policy-file\n", argv[0]);
- exit(1);
- }
-
- path = argv[1];
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "Could not open %s: %s\n", path, strerror(errno));
- exit(2);
- }
-
- if (fstat(fd, &sb) < 0) {
- fprintf(stderr, "Could not stat %s: %s\n", path, strerror(errno));
- exit(3);
- }
-
- map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (map == MAP_FAILED) {
- fprintf(stderr, "Could not mmap %s: %s\n", path, strerror(errno));
- exit(4);
- }
-
- rc = security_load_policy(map, sb.st_size);
- if (rc < 0) {
- fprintf(stderr, "Could not load %s: %s\n", path, strerror(errno));
- exit(5);
- }
- munmap(map, sb.st_size);
- close(fd);
- exit(0);
-}
diff --git a/toolbox/newfs_msdos.c b/toolbox/newfs_msdos.c
index 01517fd..5b98a01 100644
--- a/toolbox/newfs_msdos.c
+++ b/toolbox/newfs_msdos.c
@@ -798,6 +798,7 @@
__unused int oflag,struct bpb *bpb)
{
struct hd_geometry geom;
+ u_long block_size;
if (ioctl(fd, BLKSSZGET, &bpb->bps)) {
fprintf(stderr, "Error getting bytes / sector (%s)\n", strerror(errno));
@@ -806,11 +807,18 @@
ckgeom(fname, bpb->bps, "bytes/sector");
- if (ioctl(fd, BLKGETSIZE, &bpb->bsec)) {
+ if (ioctl(fd, BLKGETSIZE, &block_size)) {
fprintf(stderr, "Error getting blocksize (%s)\n", strerror(errno));
exit(1);
}
+ if (block_size > UINT32_MAX) {
+ fprintf(stderr, "Error blocksize too large: %lu\n", block_size);
+ exit(1);
+ }
+
+ bpb->bsec = (u_int)block_size;
+
if (ioctl(fd, HDIO_GETGEO, &geom)) {
fprintf(stderr, "Error getting gemoetry (%s) - trying sane values\n", strerror(errno));
geom.heads = 64;
diff --git a/toolbox/route.c b/toolbox/route.c
deleted file mode 100644
index 3e10014..0000000
--- a/toolbox/route.c
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * 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.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <linux/route.h>
-
-static inline int set_address(const char *address, struct sockaddr *sa) {
- return inet_aton(address, &((struct sockaddr_in *)sa)->sin_addr);
-}
-
-/* current support the following routing entries */
-/* route add default dev wlan0 */
-/* route add default gw 192.168.1.1 dev wlan0 */
-/* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
-
-int route_main(int argc, char *argv[])
-{
- struct rtentry rt = {
- .rt_dst = {.sa_family = AF_INET},
- .rt_genmask = {.sa_family = AF_INET},
- .rt_gateway = {.sa_family = AF_INET},
- };
-
- errno = EINVAL;
- if (argc > 2 && !strcmp(argv[1], "add")) {
- if (!strcmp(argv[2], "default")) {
- /* route add default dev wlan0 */
- if (argc > 4 && !strcmp(argv[3], "dev")) {
- rt.rt_flags = RTF_UP;
- rt.rt_dev = argv[4];
- errno = 0;
- goto apply;
- }
-
- /* route add default gw 192.168.1.1 dev wlan0 */
- if (argc > 6 && !strcmp(argv[3], "gw") && !strcmp(argv[5], "dev")) {
- rt.rt_flags = RTF_UP | RTF_GATEWAY;
- rt.rt_dev = argv[6];
- if (set_address(argv[4], &rt.rt_gateway)) {
- errno = 0;
- }
- goto apply;
- }
- }
-
- /* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
- if (argc > 7 && !strcmp(argv[2], "-net") &&
- !strcmp(argv[4], "netmask")) {
- if (!strcmp(argv[6], "gw")) {
- rt.rt_flags = RTF_UP | RTF_GATEWAY;
- if (set_address(argv[3], &rt.rt_dst) &&
- set_address(argv[5], &rt.rt_genmask) &&
- set_address(argv[7], &rt.rt_gateway)) {
- errno = 0;
- }
- goto apply;
- } else if (!strcmp(argv[6], "dev")) {
- rt.rt_flags = RTF_UP;
- rt.rt_dev = argv[7];
- if (set_address(argv[3], &rt.rt_dst) &&
- set_address(argv[5], &rt.rt_genmask)) {
- errno = 0;
- }
- goto apply;
- }
- }
- }
-
-apply:
- if (!errno) {
- int s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s != -1 && (ioctl(s, SIOCADDRT, &rt) != -1 || errno == EEXIST)) {
- return 0;
- }
- }
- puts(strerror(errno));
- return errno;
-}
diff --git a/toolbox/runcon.c b/toolbox/runcon.c
deleted file mode 100644
index 4a57bf3..0000000
--- a/toolbox/runcon.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int runcon_main(int argc, char **argv)
-{
- int rc;
-
- if (argc < 3) {
- fprintf(stderr, "usage: %s context program args...\n", argv[0]);
- exit(1);
- }
-
- rc = setexeccon(argv[1]);
- if (rc < 0) {
- fprintf(stderr, "Could not set context to %s: %s\n", argv[1], strerror(errno));
- exit(2);
- }
-
- argv += 2;
- argc -= 2;
- execvp(argv[0], argv);
- fprintf(stderr, "Could not exec %s: %s\n", argv[0], strerror(errno));
- exit(3);
-}
diff --git a/toolbox/uptime.c b/toolbox/uptime.c
index 2dd8084..ebfb15e 100644
--- a/toolbox/uptime.c
+++ b/toolbox/uptime.c
@@ -29,71 +29,33 @@
* SUCH DAMAGE.
*/
-#include <sys/time.h>
-#include <linux/ioctl.h>
-#include <linux/rtc.h>
-#include <linux/android_alarm.h>
-#include <fcntl.h>
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
#include <time.h>
-#include <unistd.h>
static void format_time(int time, char* buffer) {
- int seconds, minutes, hours, days;
-
- seconds = time % 60;
+ int seconds = time % 60;
time /= 60;
- minutes = time % 60;
+ int minutes = time % 60;
time /= 60;
- hours = time % 24;
- days = time / 24;
+ int hours = time % 24;
+ int days = time / 24;
- if (days > 0)
- sprintf(buffer, "%d days, %02d:%02d:%02d", days, hours, minutes, seconds);
- else
+ if (days > 0) {
+ sprintf(buffer, "%d day%s, %02d:%02d:%02d", days, (days == 1) ? "" : "s", hours, minutes, seconds);
+ } else {
sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
+ }
}
-static int elapsedRealtimeAlarm(struct timespec *ts)
-{
- int fd, result;
-
- fd = open("/dev/alarm", O_RDONLY);
- if (fd < 0)
- return fd;
-
- result = ioctl(fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), ts);
- close(fd);
-
- return result;
-}
-
-int64_t elapsedRealtime()
-{
- struct timespec ts;
-
- int result = elapsedRealtimeAlarm(&ts);
- if (result < 0)
- result = clock_gettime(CLOCK_BOOTTIME, &ts);
-
- if (result == 0)
- return ts.tv_sec;
- return -1;
-}
-
-int uptime_main(int argc __attribute__((unused)),
- char *argv[] __attribute__((unused)))
-{
- float up_time, idle_time;
- char up_string[100], idle_string[100], sleep_string[100];
- int elapsed;
- struct timespec up_timespec;
-
+int uptime_main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) {
FILE* file = fopen("/proc/uptime", "r");
if (!file) {
fprintf(stderr, "Could not open /proc/uptime\n");
return -1;
}
+ float idle_time;
if (fscanf(file, "%*f %f", &idle_time) != 1) {
fprintf(stderr, "Could not parse /proc/uptime\n");
fclose(file);
@@ -101,18 +63,21 @@
}
fclose(file);
- if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) < 0) {
- fprintf(stderr, "Could not get monotonic time\n");
+ struct timespec up_timespec;
+ if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) == -1) {
+ fprintf(stderr, "Could not get monotonic time: %s\n", strerror(errno));
return -1;
}
- up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9;
+ float up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9;
- elapsed = elapsedRealtime();
- if (elapsed < 0) {
- fprintf(stderr, "elapsedRealtime failed\n");
+ struct timespec elapsed_timespec;
+ if (clock_gettime(CLOCK_BOOTTIME, &elapsed_timespec) == -1) {
+ fprintf(stderr, "Could not get boot time: %s\n", strerror(errno));
return -1;
}
+ int elapsed = elapsed_timespec.tv_sec;
+ char up_string[100], idle_string[100], sleep_string[100];
format_time(elapsed, up_string);
format_time((int)idle_time, idle_string);
format_time((int)(elapsed - up_time), sleep_string);