Reconcile with jb-mr2-release - do not merge
Change-Id: I5bcebe96be1d96d4e9f694eef967ca076310cfa1
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
index df9b1d1..604c461 100644
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -137,6 +137,10 @@
#define VENDOR_ID_HARRIS 0x19A5
// OPPO's USB Vendor ID
#define VENDOR_ID_OPPO 0x22D9
+// Xiaomi's USB Vendor ID
+#define VENDOR_ID_XIAOMI 0x2717
+// BYD's USB Vendor ID
+#define VENDOR_ID_BYD 0x19D1
/** built-in vendor list */
@@ -192,6 +196,8 @@
VENDOR_ID_ANYDATA,
VENDOR_ID_HARRIS,
VENDOR_ID_OPPO,
+ VENDOR_ID_XIAOMI,
+ VENDOR_ID_BYD,
};
#define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
diff --git a/fs_mgr/Android.mk b/fs_mgr/Android.mk
index 7c66f6a..0ce07c1 100644
--- a/fs_mgr/Android.mk
+++ b/fs_mgr/Android.mk
@@ -8,6 +8,7 @@
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE:= libfs_mgr
+LOCAL_STATIC_LIBRARIES := liblogwrap
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
include $(BUILD_STATIC_LIBRARY)
@@ -27,7 +28,7 @@
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)/sbin
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
-LOCAL_STATIC_LIBRARIES := libfs_mgr libcutils libc
+LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils libc
include $(BUILD_EXECUTABLE)
diff --git a/fs_mgr/fs_mgr.c b/fs_mgr/fs_mgr.c
index 8f64757..fecc556 100644
--- a/fs_mgr/fs_mgr.c
+++ b/fs_mgr/fs_mgr.c
@@ -14,11 +14,6 @@
* limitations under the License.
*/
-/* TO DO:
- * 1. Re-direct fsck output to the kernel log?
- *
- */
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -36,6 +31,7 @@
#include <private/android_filesystem_config.h>
#include <cutils/partition_utils.h>
#include <cutils/properties.h>
+#include <logwrap/logwrap.h>
#include "fs_mgr_priv.h"
@@ -44,6 +40,8 @@
#define E2FSCK_BIN "/system/bin/e2fsck"
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
+
struct flag_list {
const char *name;
unsigned flag;
@@ -434,11 +432,15 @@
static void check_fs(char *blk_device, char *fs_type, char *target)
{
- pid_t pid;
int status;
int ret;
long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
+ char *e2fsck_argv[] = {
+ E2FSCK_BIN,
+ "-y",
+ blk_device
+ };
/* Check for the types of filesystems we know how to check */
if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
@@ -461,19 +463,13 @@
}
INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
- pid = fork();
- if (pid > 0) {
- /* Parent, wait for the child to return */
- waitpid(pid, &status, 0);
- } else if (pid == 0) {
- /* child, run checker */
- execlp(E2FSCK_BIN, E2FSCK_BIN, "-y", blk_device, (char *)NULL);
- /* Only gets here on error */
- ERROR("Cannot run fs_mgr binary %s\n", E2FSCK_BIN);
- } else {
+ ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
+ &status, true, LOG_KLOG, true);
+
+ if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */
- ERROR("Fork failed trying to run %s\n", E2FSCK_BIN);
+ ERROR("Failed trying to run %s\n", E2FSCK_BIN);
}
}
diff --git a/include/cutils/klog.h b/include/cutils/klog.h
index 1335543..ba728ac 100644
--- a/include/cutils/klog.h
+++ b/include/cutils/klog.h
@@ -17,12 +17,18 @@
#ifndef _CUTILS_KLOG_H_
#define _CUTILS_KLOG_H_
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
void klog_init(void);
void klog_set_level(int level);
void klog_close(void);
void klog_write(int level, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
+__END_DECLS
+
#define KLOG_ERROR(tag,x...) klog_write(3, "<3>" tag ": " x)
#define KLOG_WARNING(tag,x...) klog_write(4, "<4>" tag ": " x)
#define KLOG_NOTICE(tag,x...) klog_write(5, "<5>" tag ": " x)
diff --git a/init/Android.mk b/init/Android.mk
index 00d2144..ef62bce 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -35,7 +35,9 @@
LOCAL_STATIC_LIBRARIES := \
libfs_mgr \
+ liblogwrap \
libcutils \
+ liblog \
libc \
libselinux
diff --git a/init/init.c b/init/init.c
index 93b5997..f8b21e6 100755
--- a/init/init.c
+++ b/init/init.c
@@ -90,7 +90,7 @@
}
static int have_console;
-static char *console_name = "/dev/console";
+static char console_name[PROP_VALUE_MAX] = "/dev/console";
static time_t process_needs_restart;
static const char *ENV[32];
@@ -430,7 +430,7 @@
static void msg_start(const char *name)
{
- struct service *svc;
+ struct service *svc = NULL;
char *tmp = NULL;
char *args = NULL;
@@ -438,11 +438,13 @@
svc = service_find_by_name(name);
else {
tmp = strdup(name);
- args = strchr(tmp, ':');
- *args = '\0';
- args++;
+ if (tmp) {
+ args = strchr(tmp, ':');
+ *args = '\0';
+ args++;
- svc = service_find_by_name(tmp);
+ svc = service_find_by_name(tmp);
+ }
}
if (svc) {
@@ -547,11 +549,9 @@
static int console_init_action(int nargs, char **args)
{
int fd;
- char tmp[PROP_VALUE_MAX];
if (console[0]) {
- snprintf(tmp, sizeof(tmp), "/dev/%s", console);
- console_name = strdup(tmp);
+ snprintf(console_name, sizeof(console_name), "/dev/%s", console);
}
fd = open(console_name, O_RDWR);
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 4056e8a..5037705 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -131,7 +131,11 @@
LOCAL_CFLAGS += -DHAVE_MEMSET16 -DHAVE_MEMSET32
LOCAL_SRC_FILES += arch-x86/android_memset16.S arch-x86/android_memset32.S memory.c
else # !x86-atom
+ifeq ($(TARGET_ARCH),mips)
+LOCAL_SRC_FILES += arch-mips/android_memset.c
+else # !mips
LOCAL_SRC_FILES += memory.c
+endif # !mips
endif # !x86-atom
endif # !arm
diff --git a/libcutils/arch-mips/android_memset.c b/libcutils/arch-mips/android_memset.c
new file mode 100644
index 0000000..bbc99fe
--- /dev/null
+++ b/libcutils/arch-mips/android_memset.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cutils/memory.h>
+
+/* Use mips-assembler versions supplied by bionic/libc/arch-mips/string/memset.S: */
+void _memset16(uint16_t* dst, uint16_t value, size_t size);
+void _memset32(uint32_t* dst, uint32_t value, size_t size);
+
+void android_memset16(uint16_t* dst, uint16_t value, size_t size)
+{
+ _memset16(dst, value, size);
+}
+
+void android_memset32(uint32_t* dst, uint32_t value, size_t size)
+{
+ _memset32(dst, value, size);
+}
diff --git a/libcutils/klog.c b/libcutils/klog.c
index b586a57..812af3b 100644
--- a/libcutils/klog.c
+++ b/libcutils/klog.c
@@ -35,6 +35,9 @@
void klog_init(void)
{
static const char *name = "/dev/__kmsg__";
+
+ if (klog_fd >= 0) return; /* Already initialized */
+
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
klog_fd = open(name, O_WRONLY);
fcntl(klog_fd, F_SETFD, FD_CLOEXEC);
@@ -50,7 +53,7 @@
va_list ap;
if (level > klog_level) return;
- if (klog_fd < 0) return;
+ if (klog_fd < 0) klog_init();
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
diff --git a/libsparse/output_file.c b/libsparse/output_file.c
index b5ae419..5014e4a 100644
--- a/libsparse/output_file.c
+++ b/libsparse/output_file.c
@@ -675,6 +675,9 @@
} else {
out = output_file_new_normal();
}
+ if (!out) {
+ return NULL;
+ }
out->ops->open(out, fd);
diff --git a/logwrapper/Android.mk b/logwrapper/Android.mk
index b4b6b29..917bf37 100644
--- a/logwrapper/Android.mk
+++ b/logwrapper/Android.mk
@@ -30,5 +30,5 @@
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= logwrapper.c
LOCAL_MODULE := logwrapper
-LOCAL_STATIC_LIBRARIES := liblog liblogwrap
+LOCAL_STATIC_LIBRARIES := liblog liblogwrap libcutils
include $(BUILD_EXECUTABLE)
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
index 2be8736..8087f0a 100644
--- a/logwrapper/include/logwrap/logwrap.h
+++ b/logwrapper/include/logwrap/logwrap.h
@@ -43,7 +43,12 @@
* SIGQUIT while logwrap is running. This may force the end-user to
* send a signal twice to signal the caller (once for the child, and
* once for the caller)
- * logwrap: when true, log messages from the child
+ * log_target: Specify where to log the output of the child, either LOG_NONE,
+ * LOG_ALOG (for the Android system log) or LOG_KLOG (for the kernel
+ * log).
+ * abbreviated: If true, capture up to the first 100 lines and last 4K of
+ * output from the child. The abbreviated output is not dumped to
+ * the specified log until the child has exited.
*
* Return value:
* 0 when logwrap successfully run the child process and captured its status
@@ -52,8 +57,26 @@
* the return value of the child if it exited properly and status is NULL
*
*/
-int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
- bool logwrap);
+
+/* Values for the log_target parameter android_fork_exec_ext() */
+#define LOG_NONE 0
+#define LOG_ALOG 1
+#define LOG_KLOG 2
+
+int android_fork_execvp_ext(int argc, char* argv[], int *status, bool ignore_int_quit,
+ int log_target, bool abbreviated);
+
+/* Similar to above, except abbreviated logging is not available, and if logwrap
+ * is true, logging is to the Android system log, and if false, there is no
+ * logging.
+ */
+static inline int android_fork_execvp(int argc, char* argv[], int *status,
+ bool ignore_int_quit, bool logwrap)
+{
+ return android_fork_execvp_ext(argc, argv, status, ignore_int_quit,
+ (logwrap ? LOG_ALOG : LOG_NONE), false);
+}
+
__END_DECLS
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index bf91977..01cc9a1 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -31,8 +31,10 @@
#include <logwrap/logwrap.h>
#include "private/android_filesystem_config.h"
#include "cutils/log.h"
+#include <cutils/klog.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
+#define MIN(a,b) (((a)<(b))?(a):(b))
static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -48,8 +50,247 @@
_exit(-1); \
} while(0)
-static int parent(const char *tag, int parent_read, pid_t pid, int *chld_sts,
- bool logwrap) {
+#define MAX_KLOG_TAG 16
+
+/* This is a simple buffer that holds up to the first beginning_buf->buf_size
+ * bytes of output from a command.
+ */
+#define BEGINNING_BUF_SIZE 0x1000
+struct beginning_buf {
+ char *buf;
+ size_t alloc_len;
+ /* buf_size is the usable space, which is one less than the allocated size */
+ size_t buf_size;
+ size_t used_len;
+};
+
+/* This is a circular buf that holds up to the last ending_buf->buf_size bytes
+ * of output from a command after the first beginning_buf->buf_size bytes
+ * (which are held in beginning_buf above).
+ */
+#define ENDING_BUF_SIZE 0x1000
+struct ending_buf {
+ char *buf;
+ ssize_t alloc_len;
+ /* buf_size is the usable space, which is one less than the allocated size */
+ ssize_t buf_size;
+ ssize_t used_len;
+ /* read and write offsets into the circular buffer */
+ int read;
+ int write;
+};
+
+ /* A structure to hold all the abbreviated buf data */
+struct abbr_buf {
+ struct beginning_buf b_buf;
+ struct ending_buf e_buf;
+ int beginning_buf_full;
+};
+
+/* Collect all the various bits of info needed for logging in one place. */
+struct log_info {
+ int log_target;
+ char klog_fmt[MAX_KLOG_TAG * 2];
+ char *btag;
+ bool abbreviated;
+ struct abbr_buf a_buf;
+};
+
+/* Forware declaration */
+static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen);
+
+/* Return 0 on success, and 1 when full */
+static int add_line_to_linear_buf(struct beginning_buf *b_buf,
+ char *line, ssize_t line_len)
+{
+ size_t new_len;
+ char *new_buf;
+ int full = 0;
+
+ if ((line_len + b_buf->used_len) > b_buf->buf_size) {
+ full = 1;
+ } else {
+ /* Add to the end of the buf */
+ memcpy(b_buf->buf + b_buf->used_len, line, line_len);
+ b_buf->used_len += line_len;
+ }
+
+ return full;
+}
+
+static void add_line_to_circular_buf(struct ending_buf *e_buf,
+ char *line, ssize_t line_len)
+{
+ ssize_t free_len;
+ ssize_t needed_space;
+ char *new_buf;
+ int cnt;
+
+ if (e_buf->buf == NULL) {
+ return;
+ }
+
+ if (line_len > e_buf->buf_size) {
+ return;
+ }
+
+ free_len = e_buf->buf_size - e_buf->used_len;
+
+ if (line_len > free_len) {
+ /* remove oldest entries at read, and move read to make
+ * room for the new string */
+ needed_space = line_len - free_len;
+ e_buf->read = (e_buf->read + needed_space) % e_buf->buf_size;
+ e_buf->used_len -= needed_space;
+ }
+
+ /* Copy the line into the circular buffer, dealing with possible
+ * wraparound.
+ */
+ cnt = MIN(line_len, e_buf->buf_size - e_buf->write);
+ memcpy(e_buf->buf + e_buf->write, line, cnt);
+ if (cnt < line_len) {
+ memcpy(e_buf->buf, line + cnt, line_len - cnt);
+ }
+ e_buf->used_len += line_len;
+ e_buf->write = (e_buf->write + line_len) % e_buf->buf_size;
+}
+
+/* Log directly to the specified log */
+static void do_log_line(struct log_info *log_info, char *line) {
+ if (log_info->log_target == LOG_KLOG) {
+ klog_write(6, log_info->klog_fmt, line);
+ } else if (log_info->log_target == LOG_ALOG) {
+ ALOG(LOG_INFO, log_info->btag, "%s", line);
+ }
+}
+
+/* Log to either the abbreviated buf, or directly to the specified log
+ * via do_log_line() above.
+ */
+static void log_line(struct log_info *log_info, char *line, int len) {
+ if (log_info->abbreviated) {
+ add_line_to_abbr_buf(&log_info->a_buf, line, len);
+ } else {
+ do_log_line(log_info, line);
+ }
+}
+
+/*
+ * The kernel will take a maximum of 1024 bytes in any single write to
+ * the kernel logging device file, so find and print each line one at
+ * a time. The allocated size for buf should be at least 1 byte larger
+ * than buf_size (the usable size of the buffer) to make sure there is
+ * room to temporarily stuff a null byte to terminate a line for logging.
+ */
+static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
+{
+ char *line_start;
+ char c;
+ int line_len;
+ int i;
+
+ line_start = buf;
+ for (i = 0; i < buf_size; i++) {
+ if (*(buf + i) == '\n') {
+ /* Found a line ending, print the line and compute new line_start */
+ /* Save the next char and replace with \0 */
+ c = *(buf + i + 1);
+ *(buf + i + 1) = '\0';
+ do_log_line(log_info, line_start);
+ /* Restore the saved char */
+ *(buf + i + 1) = c;
+ line_start = buf + i + 1;
+ } else if (*(buf + i) == '\0') {
+ /* The end of the buffer, print the last bit */
+ do_log_line(log_info, line_start);
+ break;
+ }
+ }
+ /* If the buffer was completely full, and didn't end with a newline, just
+ * ignore the partial last line.
+ */
+}
+
+static void init_abbr_buf(struct abbr_buf *a_buf) {
+ char *new_buf;
+
+ memset(a_buf, 0, sizeof(struct abbr_buf));
+ new_buf = malloc(BEGINNING_BUF_SIZE);
+ if (new_buf) {
+ a_buf->b_buf.buf = new_buf;
+ a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE;
+ a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1;
+ }
+ new_buf = malloc(ENDING_BUF_SIZE);
+ if (new_buf) {
+ a_buf->e_buf.buf = new_buf;
+ a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
+ a_buf->e_buf.buf_size = ENDING_BUF_SIZE - 1;
+ }
+}
+
+static void free_abbr_buf(struct abbr_buf *a_buf) {
+ free(a_buf->b_buf.buf);
+ free(a_buf->e_buf.buf);
+}
+
+static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen) {
+ if (!a_buf->beginning_buf_full) {
+ a_buf->beginning_buf_full =
+ add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
+ }
+ if (a_buf->beginning_buf_full) {
+ add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
+ }
+}
+
+static void print_abbr_buf(struct log_info *log_info) {
+ struct abbr_buf *a_buf = &log_info->a_buf;
+
+ /* Add the abbreviated output to the kernel log */
+ if (a_buf->b_buf.alloc_len) {
+ print_buf_lines(log_info, a_buf->b_buf.buf, a_buf->b_buf.used_len);
+ }
+
+ /* Print an ellipsis to indicate that the buffer has wrapped or
+ * is full, and some data was not logged.
+ */
+ if (a_buf->e_buf.used_len == a_buf->e_buf.buf_size) {
+ do_log_line(log_info, "...\n");
+ }
+
+ if (a_buf->e_buf.used_len == 0) {
+ return;
+ }
+
+ /* Simplest way to print the circular buffer is allocate a second buf
+ * of the same size, and memcpy it so it's a simple linear buffer,
+ * and then cal print_buf_lines on it */
+ if (a_buf->e_buf.read < a_buf->e_buf.write) {
+ /* no wrap around, just print it */
+ print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read,
+ a_buf->e_buf.used_len);
+ } else {
+ /* The circular buffer will always have at least 1 byte unused,
+ * so by allocating alloc_len here we will have at least
+ * 1 byte of space available as required by print_buf_lines().
+ */
+ char * nbuf = malloc(a_buf->e_buf.alloc_len);
+ if (!nbuf) {
+ return;
+ }
+ int first_chunk_len = a_buf->e_buf.buf_size - a_buf->e_buf.read;
+ memcpy(nbuf, a_buf->e_buf.buf + a_buf->e_buf.read, first_chunk_len);
+ /* copy second chunk */
+ memcpy(nbuf + first_chunk_len, a_buf->e_buf.buf, a_buf->e_buf.write);
+ print_buf_lines(log_info, nbuf, first_chunk_len + a_buf->e_buf.write);
+ free(nbuf);
+ }
+}
+
+static int parent(const char *tag, int parent_read, pid_t pid,
+ int *chld_sts, int log_target, bool abbreviated) {
int status = 0;
char buffer[4096];
struct pollfd poll_fds[] = {
@@ -60,13 +301,32 @@
};
int rc = 0;
+ struct log_info log_info;
+
int a = 0; // start index of unprocessed data
int b = 0; // end index of unprocessed data
int sz;
bool found_child = false;
+ char tmpbuf[256];
- char *btag = basename(tag);
- if (!btag) btag = (char*) tag;
+ log_info.log_target = log_target;
+ log_info.abbreviated = abbreviated;
+ log_info.btag = basename(tag);
+ if (!log_info.btag) {
+ log_info.btag = (char*) tag;
+ }
+
+ if (abbreviated && (log_target == LOG_NONE)) {
+ abbreviated = 0;
+ }
+ if (abbreviated) {
+ init_abbr_buf(&log_info.a_buf);
+ }
+
+ if (log_target == LOG_KLOG) {
+ snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
+ "<6>%.*s: %%s", MAX_KLOG_TAG, log_info.btag);
+ }
while (!found_child) {
if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), -1)) < 0) {
@@ -82,11 +342,21 @@
// Log one line at a time
for (b = 0; b < sz; b++) {
if (buffer[b] == '\r') {
- buffer[b] = '\0';
+ if (abbreviated) {
+ /* The abbreviated logging code uses newline as
+ * the line separator. Lucikly, the pty layer
+ * helpfully cooks the output of the command
+ * being run and inserts a CR before NL. So
+ * I just change it to NL here when doing
+ * abbreviated logging.
+ */
+ buffer[b] = '\n';
+ } else {
+ buffer[b] = '\0';
+ }
} else if (buffer[b] == '\n') {
buffer[b] = '\0';
- if (logwrap)
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ log_line(&log_info, &buffer[a], b - a);
a = b + 1;
}
}
@@ -94,8 +364,7 @@
if (a == 0 && b == sizeof(buffer) - 1) {
// buffer is full, flush
buffer[b] = '\0';
- if (logwrap)
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ log_line(&log_info, &buffer[a], b - a);
b = 0;
} else if (a != b) {
// Keep left-overs
@@ -132,32 +401,44 @@
rc = -ECHILD;
}
- if (logwrap) {
- // Flush remaining data
- if (a != b) {
- buffer[b] = '\0';
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ // Flush remaining data
+ if (a != b) {
+ buffer[b] = '\0';
+ log_line(&log_info, &buffer[a], b - a);
+ }
+
+ /* All the output has been processed, time to dump the abbreviated output */
+ if (abbreviated) {
+ print_abbr_buf(&log_info);
+ }
+
+ if (WIFEXITED(status)) {
+ if (WEXITSTATUS(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf),
+ "%s terminated by exit(%d)\n", log_info.btag, WEXITSTATUS(status));
+ do_log_line(&log_info, tmpbuf);
}
- if (WIFEXITED(status)) {
- if (WEXITSTATUS(status))
- ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
- WEXITSTATUS(status));
- } else {
- if (WIFSIGNALED(status))
- ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
- WTERMSIG(status));
- else if (WIFSTOPPED(status))
- ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
- WSTOPSIG(status));
+ } else {
+ if (WIFSIGNALED(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf),
+ "%s terminated by signal %d\n", log_info.btag, WTERMSIG(status));
+ do_log_line(&log_info, tmpbuf);
+ } else if (WIFSTOPPED(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf),
+ "%s stopped by signal %d\n", log_info.btag, WSTOPSIG(status));
+ do_log_line(&log_info, tmpbuf);
}
}
err_waitpid:
err_poll:
+ if (abbreviated) {
+ free_abbr_buf(&log_info.a_buf);
+ }
return rc;
}
-static void child(int argc, char* argv[], bool logwrap) {
+static void child(int argc, char* argv[]) {
// create null terminated argv_child array
char* argv_child[argc + 1];
memcpy(argv_child, argv, argc * sizeof(char *));
@@ -169,8 +450,8 @@
}
}
-int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
- bool logwrap) {
+int android_fork_execvp_ext(int argc, char* argv[], int *status, bool ignore_int_quit,
+ int log_target, bool abbreviated) {
pid_t pid;
int parent_ptty;
int child_ptty;
@@ -230,7 +511,7 @@
dup2(child_ptty, 2);
close(child_ptty);
- child(argc, argv, logwrap);
+ child(argc, argv);
} else {
close(child_ptty);
if (ignore_int_quit) {
@@ -242,7 +523,7 @@
sigaction(SIGQUIT, &ignact, &quitact);
}
- rc = parent(argv[0], parent_ptty, pid, status, logwrap);
+ rc = parent(argv[0], parent_ptty, pid, status, log_target, abbreviated);
}
if (ignore_int_quit) {
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index ed71a29..d1c6240 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -17,8 +17,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
+#include <unistd.h>
#include <logwrap/logwrap.h>
+#include <cutils/klog.h>
#include "cutils/log.h"
@@ -30,36 +32,55 @@
void usage() {
fatal(
- "Usage: logwrapper [-d] BINARY [ARGS ...]\n"
+ "Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
"\n"
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
"the Android logging system. Tag is set to BINARY, priority is\n"
"always LOG_INFO.\n"
"\n"
+ "-a: Causes logwrapper to do abbreviated logging.\n"
+ " This logs up to the first 4K and last 4K of the command\n"
+ " being run, and logs the output when the command exits\n"
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
- " fault address is set to the status of wait()\n");
+ " fault address is set to the status of wait()\n"
+ "-k: Causes logwrapper to log to the kernel log instead of\n"
+ " the Android system log\n");
}
int main(int argc, char* argv[]) {
int seg_fault_on_exit = 0;
+ int log_target = LOG_ALOG;
+ bool abbreviated = false;
+ int ch;
int status = 0xAAAA;
int rc;
- if (argc < 2) {
+ while ((ch = getopt(argc, argv, "adk")) != -1) {
+ switch (ch) {
+ case 'a':
+ abbreviated = true;
+ break;
+ case 'd':
+ seg_fault_on_exit = 1;
+ break;
+ case 'k':
+ log_target = LOG_KLOG;
+ klog_set_level(6);
+ break;
+ case '?':
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 1) {
usage();
}
- if (strncmp(argv[1], "-d", 2) == 0) {
- seg_fault_on_exit = 1;
- argc--;
- argv++;
- }
-
- if (argc < 2) {
- usage();
- }
-
- rc = android_fork_execvp(argc - 1, &argv[1], &status, true, true);
+ rc = android_fork_execvp_ext(argc, &argv[0], &status, true,
+ log_target, abbreviated);
if (!rc) {
if (WIFEXITED(status))
rc = WEXITSTATUS(status);
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 89ec18a..c3ef503 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -37,7 +37,7 @@
export ANDROID_STORAGE /storage
export ASEC_MOUNTPOINT /mnt/asec
export LOOP_MOUNTPOINT /mnt/obb
- export BOOTCLASSPATH /system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar
+ export BOOTCLASSPATH /system/framework/core.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar
# Backward compatibility
symlink /system/etc /etc
diff --git a/toolbox/dmesg.c b/toolbox/dmesg.c
index e57f607..9c73b00 100644
--- a/toolbox/dmesg.c
+++ b/toolbox/dmesg.c
@@ -5,15 +5,30 @@
#include <sys/klog.h>
#include <string.h>
-#define KLOG_BUF_SHIFT 17 /* CONFIG_LOG_BUF_SHIFT from our kernel */
-#define KLOG_BUF_LEN (1 << KLOG_BUF_SHIFT)
+#define FALLBACK_KLOG_BUF_SHIFT 17 /* CONFIG_LOG_BUF_SHIFT from our kernel */
+#define FALLBACK_KLOG_BUF_LEN (1 << FALLBACK_KLOG_BUF_SHIFT)
int dmesg_main(int argc, char **argv)
{
- char buffer[KLOG_BUF_LEN + 1];
- char *p = buffer;
+ char *buffer;
+ char *p;
ssize_t ret;
- int n, op;
+ int n, op, klog_buf_len;
+
+ klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
+
+ if (klog_buf_len <= 0) {
+ klog_buf_len = FALLBACK_KLOG_BUF_LEN;
+ }
+
+ buffer = (char *)malloc(klog_buf_len + 1);
+
+ if (!buffer) {
+ perror("malloc");
+ return EXIT_FAILURE;
+ }
+
+ p = buffer;
if((argc == 2) && (!strcmp(argv[1],"-c"))) {
op = KLOG_READ_CLEAR;
@@ -21,7 +36,7 @@
op = KLOG_READ_ALL;
}
- n = klogctl(op, buffer, KLOG_BUF_LEN);
+ n = klogctl(op, buffer, klog_buf_len);
if (n < 0) {
perror("klogctl");
return EXIT_FAILURE;
diff --git a/toolbox/ls.c b/toolbox/ls.c
index e530521..5324511 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -28,6 +28,7 @@
#define LIST_LONG_NUMERIC (1 << 5)
#define LIST_CLASSIFY (1 << 6)
#define LIST_MACLABEL (1 << 7)
+#define LIST_INODE (1 << 8)
// fwd
static int listpath(const char *name, int flags);
@@ -127,22 +128,20 @@
return 0;
}
-static int listfile_size(const char *path, const char *filename, int flags)
+static int listfile_size(const char *path, const char *filename, struct stat *s,
+ int flags)
{
- struct stat s;
-
- if (lstat(path, &s) < 0) {
- fprintf(stderr, "lstat '%s' failed: %s\n", path, strerror(errno));
+ if(!s || !path) {
return -1;
}
/* blocks are 512 bytes, we want output to be KB */
if ((flags & LIST_SIZE) != 0) {
- printf("%lld ", s.st_blocks / 2);
+ printf("%lld ", s->st_blocks / 2);
}
if ((flags & LIST_CLASSIFY) != 0) {
- char filetype = mode2kind(s.st_mode);
+ char filetype = mode2kind(s->st_mode);
if (filetype != 'l') {
printf("%c ", filetype);
} else {
@@ -161,15 +160,18 @@
return 0;
}
-static int listfile_long(const char *path, int flags)
+static int listfile_long(const char *path, struct stat *s, int flags)
{
- struct stat s;
char date[32];
char mode[16];
char user[16];
char group[16];
const char *name;
+ if(!s || !path) {
+ return -1;
+ }
+
/* name is anything after the final '/', or the whole path if none*/
name = strrchr(path, '/');
if(name == 0) {
@@ -178,36 +180,32 @@
name++;
}
- if(lstat(path, &s) < 0) {
- return -1;
- }
-
- mode2str(s.st_mode, mode);
+ mode2str(s->st_mode, mode);
if (flags & LIST_LONG_NUMERIC) {
- sprintf(user, "%ld", s.st_uid);
- sprintf(group, "%ld", s.st_gid);
+ sprintf(user, "%ld", s->st_uid);
+ sprintf(group, "%ld", s->st_gid);
} else {
- user2str(s.st_uid, user);
- group2str(s.st_gid, group);
+ user2str(s->st_uid, user);
+ group2str(s->st_gid, group);
}
- strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime));
+ strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s->st_mtime));
date[31] = 0;
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
// MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK)
- switch(s.st_mode & S_IFMT) {
+ switch(s->st_mode & S_IFMT) {
case S_IFBLK:
case S_IFCHR:
printf("%s %-8s %-8s %3d, %3d %s %s\n",
mode, user, group,
- (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
+ (int) MAJOR(s->st_rdev), (int) MINOR(s->st_rdev),
date, name);
break;
case S_IFREG:
printf("%s %-8s %-8s %8lld %s %s\n",
- mode, user, group, s.st_size, date, name);
+ mode, user, group, s->st_size, date, name);
break;
case S_IFLNK: {
char linkto[256];
@@ -237,15 +235,18 @@
return 0;
}
-static int listfile_maclabel(const char *path, int flags)
+static int listfile_maclabel(const char *path, struct stat *s, int flags)
{
- struct stat s;
char mode[16];
char user[16];
char group[16];
char *maclabel = NULL;
const char *name;
+ if(!s || !path) {
+ return -1;
+ }
+
/* name is anything after the final '/', or the whole path if none*/
name = strrchr(path, '/');
if(name == 0) {
@@ -254,20 +255,16 @@
name++;
}
- if(lstat(path, &s) < 0) {
- return -1;
- }
-
lgetfilecon(path, &maclabel);
if (!maclabel) {
return -1;
}
- mode2str(s.st_mode, mode);
- user2str(s.st_uid, user);
- group2str(s.st_gid, group);
+ mode2str(s->st_mode, mode);
+ user2str(s->st_uid, user);
+ group2str(s->st_gid, group);
- switch(s.st_mode & S_IFMT) {
+ switch(s->st_mode & S_IFMT) {
case S_IFLNK: {
char linkto[256];
ssize_t len;
@@ -301,7 +298,9 @@
static int listfile(const char *dirname, const char *filename, int flags)
{
- if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL)) == 0) {
+ struct stat s;
+
+ if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL | LIST_INODE)) == 0) {
printf("%s\n", filename);
return 0;
}
@@ -316,12 +315,20 @@
pathname = filename;
}
+ if(lstat(pathname, &s) < 0) {
+ return -1;
+ }
+
+ if(flags & LIST_INODE) {
+ printf("%8llu ", s.st_ino);
+ }
+
if ((flags & LIST_MACLABEL) != 0) {
- return listfile_maclabel(pathname, flags);
+ return listfile_maclabel(pathname, &s, flags);
} else if ((flags & LIST_LONG) != 0) {
- return listfile_long(pathname, flags);
+ return listfile_long(pathname, &s, flags);
} else /*((flags & LIST_SIZE) != 0)*/ {
- return listfile_size(pathname, filename, flags);
+ return listfile_size(pathname, filename, &s, flags);
}
}
@@ -456,6 +463,7 @@
case 'Z': flags |= LIST_MACLABEL; break;
case 'a': flags |= LIST_ALL; break;
case 'F': flags |= LIST_CLASSIFY; break;
+ case 'i': flags |= LIST_INODE; break;
default:
fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]);
exit(1);