Merge "logd: liblog: logcat: debuggerd: Add LOG_ID_CRASH"
diff --git a/include/sysutils/FrameworkListener.h b/include/sysutils/FrameworkListener.h
index f1a4b43..18049cd 100644
--- a/include/sysutils/FrameworkListener.h
+++ b/include/sysutils/FrameworkListener.h
@@ -36,6 +36,7 @@
public:
FrameworkListener(const char *socketName);
FrameworkListener(const char *socketName, bool withSeq);
+ FrameworkListener(int sock);
virtual ~FrameworkListener() {}
protected:
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index a5ffda2..01ed54e 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -39,6 +39,11 @@
init(socketName, false);
}
+FrameworkListener::FrameworkListener(int sock) :
+ SocketListener(sock, true) {
+ init(NULL, false);
+}
+
void FrameworkListener::init(const char *socketName UNUSED, bool withSeq) {
mCommands = new FrameworkCommandCollection();
errorRate = 0;
diff --git a/libutils/BlobCache.cpp b/libutils/BlobCache.cpp
index 0fb1d8e..660917b 100644
--- a/libutils/BlobCache.cpp
+++ b/libutils/BlobCache.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "BlobCache"
//#define LOG_NDEBUG 0
+#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
@@ -54,18 +55,18 @@
void BlobCache::set(const void* key, size_t keySize, const void* value,
size_t valueSize) {
if (mMaxKeySize < keySize) {
- ALOGV("set: not caching because the key is too large: %d (limit: %d)",
+ ALOGV("set: not caching because the key is too large: %zu (limit: %zu)",
keySize, mMaxKeySize);
return;
}
if (mMaxValueSize < valueSize) {
- ALOGV("set: not caching because the value is too large: %d (limit: %d)",
+ ALOGV("set: not caching because the value is too large: %zu (limit: %zu)",
valueSize, mMaxValueSize);
return;
}
if (mMaxTotalSize < keySize + valueSize) {
ALOGV("set: not caching because the combined key/value size is too "
- "large: %d (limit: %d)", keySize + valueSize, mMaxTotalSize);
+ "large: %zu (limit: %zu)", keySize + valueSize, mMaxTotalSize);
return;
}
if (keySize == 0) {
@@ -94,15 +95,15 @@
continue;
} else {
ALOGV("set: not caching new key/value pair because the "
- "total cache size limit would be exceeded: %d "
- "(limit: %d)",
+ "total cache size limit would be exceeded: %zu "
+ "(limit: %zu)",
keySize + valueSize, mMaxTotalSize);
break;
}
}
mCacheEntries.add(CacheEntry(keyBlob, valueBlob));
mTotalSize = newTotalSize;
- ALOGV("set: created new cache entry with %d byte key and %d byte value",
+ ALOGV("set: created new cache entry with %zu byte key and %zu byte value",
keySize, valueSize);
} else {
// Update the existing cache entry.
@@ -116,14 +117,14 @@
continue;
} else {
ALOGV("set: not caching new value because the total cache "
- "size limit would be exceeded: %d (limit: %d)",
+ "size limit would be exceeded: %zu (limit: %zu)",
keySize + valueSize, mMaxTotalSize);
break;
}
}
mCacheEntries.editItemAt(index).setValue(valueBlob);
mTotalSize = newTotalSize;
- ALOGV("set: updated existing cache entry with %d byte key and %d byte "
+ ALOGV("set: updated existing cache entry with %zu byte key and %zu byte "
"value", keySize, valueSize);
}
break;
@@ -133,7 +134,7 @@
size_t BlobCache::get(const void* key, size_t keySize, void* value,
size_t valueSize) {
if (mMaxKeySize < keySize) {
- ALOGV("get: not searching because the key is too large: %d (limit %d)",
+ ALOGV("get: not searching because the key is too large: %zu (limit %zu)",
keySize, mMaxKeySize);
return 0;
}
@@ -141,7 +142,7 @@
CacheEntry dummyEntry(dummyKey, NULL);
ssize_t index = mCacheEntries.indexOf(dummyEntry);
if (index < 0) {
- ALOGV("get: no cache entry found for key of size %d", keySize);
+ ALOGV("get: no cache entry found for key of size %zu", keySize);
return 0;
}
@@ -150,10 +151,10 @@
sp<Blob> valueBlob(mCacheEntries[index].getValue());
size_t valueBlobSize = valueBlob->getSize();
if (valueBlobSize <= valueSize) {
- ALOGV("get: copying %d bytes to caller's buffer", valueBlobSize);
+ ALOGV("get: copying %zu bytes to caller's buffer", valueBlobSize);
memcpy(value, valueBlob->getData(), valueBlobSize);
} else {
- ALOGV("get: caller's buffer is too small for value: %d (needs %d)",
+ ALOGV("get: caller's buffer is too small for value: %zu (needs %zu)",
valueSize, valueBlobSize);
}
return valueBlobSize;
@@ -229,7 +230,7 @@
}
const Header* header = reinterpret_cast<const Header*>(buffer);
if (header->mMagicNumber != blobCacheMagic) {
- ALOGE("unflatten: bad magic number: %d", header->mMagicNumber);
+ ALOGE("unflatten: bad magic number: %" PRIu32, header->mMagicNumber);
return BAD_VALUE;
}
if (header->mBlobCacheVersion != blobCacheVersion ||
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index 9ce370e..933e7aa 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -23,6 +23,7 @@
#include <utils/FileMap.h>
#include <utils/Log.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
@@ -39,37 +40,32 @@
/*static*/ long FileMap::mPageSize = -1;
-
-/*
- * Constructor. Create an empty object.
- */
+// Constructor. Create an empty object.
FileMap::FileMap(void)
: mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
mDataPtr(NULL), mDataLength(0)
{
}
-/*
- * Destructor.
- */
+// Destructor.
FileMap::~FileMap(void)
{
assert(mRefCount == 0);
- //printf("+++ removing FileMap %p %u\n", mDataPtr, mDataLength);
+ //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength);
mRefCount = -100; // help catch double-free
if (mFileName != NULL) {
free(mFileName);
}
-#ifdef HAVE_POSIX_FILEMAP
+#ifdef HAVE_POSIX_FILEMAP
if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
- ALOGD("munmap(%p, %d) failed\n", mBasePtr, (int) mBaseLength);
+ ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
}
#endif
#ifdef HAVE_WIN32_FILEMAP
if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
- ALOGD("UnmapViewOfFile(%p) failed, error = %ld\n", mBasePtr,
+ ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
GetLastError() );
}
if (mFileMapping != INVALID_HANDLE_VALUE) {
@@ -80,14 +76,12 @@
}
-/*
- * Create a new mapping on an open file.
- *
- * Closing the file descriptor does not unmap the pages, so we don't
- * claim ownership of the fd.
- *
- * Returns "false" on failure.
- */
+// Create a new mapping on an open file.
+//
+// Closing the file descriptor does not unmap the pages, so we don't
+// claim ownership of the fd.
+//
+// Returns "false" on failure.
bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
bool readOnly)
{
@@ -98,32 +92,32 @@
if (mPageSize == -1) {
SYSTEM_INFO si;
-
+
GetSystemInfo( &si );
mPageSize = si.dwAllocationGranularity;
}
DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
-
+
mFileHandle = (HANDLE) _get_osfhandle(fd);
mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
if (mFileMapping == NULL) {
- ALOGE("CreateFileMapping(%p, %lx) failed with error %ld\n",
+ ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
mFileHandle, protect, GetLastError() );
return false;
}
-
+
adjust = offset % mPageSize;
adjOffset = offset - adjust;
adjLength = length + adjust;
-
- mBasePtr = MapViewOfFile( mFileMapping,
+
+ mBasePtr = MapViewOfFile( mFileMapping,
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
0,
(DWORD)(adjOffset),
adjLength );
if (mBasePtr == NULL) {
- ALOGE("MapViewOfFile(%ld, %ld) failed with error %ld\n",
+ ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
adjOffset, adjLength, GetLastError() );
CloseHandle(mFileMapping);
mFileMapping = INVALID_HANDLE_VALUE;
@@ -142,7 +136,7 @@
assert(offset >= 0);
assert(length > 0);
- /* init on first use */
+ // init on first use
if (mPageSize == -1) {
#if NOT_USING_KLIBC
mPageSize = sysconf(_SC_PAGESIZE);
@@ -151,7 +145,7 @@
return false;
}
#else
- /* this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM */
+ // this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM
mPageSize = 4096;
#endif
}
@@ -168,19 +162,19 @@
ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
if (ptr == MAP_FAILED) {
- // Cygwin does not seem to like file mapping files from an offset.
- // So if we fail, try again with offset zero
- if (adjOffset > 0) {
- adjust = offset;
- goto try_again;
- }
-
- ALOGE("mmap(%ld,%ld) failed: %s\n",
- (long) adjOffset, (long) adjLength, strerror(errno));
+ // Cygwin does not seem to like file mapping files from an offset.
+ // So if we fail, try again with offset zero
+ if (adjOffset > 0) {
+ adjust = offset;
+ goto try_again;
+ }
+
+ ALOGE("mmap(%" PRId64 ",%zu) failed: %s\n",
+ adjOffset, adjLength, strerror(errno));
return false;
}
mBasePtr = ptr;
-#endif /* HAVE_POSIX_FILEMAP */
+#endif // HAVE_POSIX_FILEMAP
mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
mBaseLength = adjLength;
@@ -190,15 +184,13 @@
assert(mBasePtr != NULL);
- ALOGV("MAP: base %p/%d data %p/%d\n",
- mBasePtr, (int) mBaseLength, mDataPtr, (int) mDataLength);
+ ALOGV("MAP: base %p/%zu data %p/%zu\n",
+ mBasePtr, mBaseLength, mDataPtr, mDataLength);
return true;
}
-/*
- * Provide guidance to the system.
- */
+// Provide guidance to the system.
int FileMap::advise(MapAdvice advice)
{
#if HAVE_MADVISE
@@ -220,6 +212,6 @@
ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
return cc;
#else
- return -1;
+ return -1;
#endif // HAVE_MADVISE
}
diff --git a/logd/CommandListener.cpp b/logd/CommandListener.cpp
index 0bb233a..a7bf92b 100644
--- a/logd/CommandListener.cpp
+++ b/logd/CommandListener.cpp
@@ -24,6 +24,7 @@
#include <sys/socket.h>
#include <sys/types.h>
+#include <cutils/sockets.h>
#include <private/android_filesystem_config.h>
#include <sysutils/SocketClient.h>
@@ -32,7 +33,7 @@
CommandListener::CommandListener(LogBuffer *buf, LogReader * /*reader*/,
LogListener * /*swl*/)
- : FrameworkListener("logd")
+ : FrameworkListener(getLogSocket())
, mBuf(*buf) {
// registerCmd(new ShutdownCmd(buf, writer, swl));
registerCmd(new ClearCmd(buf));
@@ -283,3 +284,16 @@
return 0;
}
+
+int CommandListener::getLogSocket() {
+ static const char socketName[] = "logd";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_STREAM);
+ }
+
+ return sock;
+}
diff --git a/logd/CommandListener.h b/logd/CommandListener.h
index 1290519..cd1c306 100644
--- a/logd/CommandListener.h
+++ b/logd/CommandListener.h
@@ -31,6 +31,8 @@
virtual ~CommandListener() {}
private:
+ static int getLogSocket();
+
class ShutdownCmd : public LogCommand {
LogBuffer &mBuf;
LogReader &mReader;
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
index b835b4f..ed5b391 100644
--- a/logd/LogListener.cpp
+++ b/logd/LogListener.cpp
@@ -107,7 +107,15 @@
}
int LogListener::getLogSocket() {
- int sock = android_get_control_socket("logdw");
+ static const char socketName[] = "logdw";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_DGRAM);
+ }
+
int on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
return -1;
diff --git a/logd/LogReader.cpp b/logd/LogReader.cpp
index 60a3507..51aa2ad 100644
--- a/logd/LogReader.cpp
+++ b/logd/LogReader.cpp
@@ -17,13 +17,14 @@
#include <ctype.h>
#include <poll.h>
#include <sys/socket.h>
+
#include <cutils/sockets.h>
#include "LogReader.h"
#include "FlushCommand.h"
LogReader::LogReader(LogBuffer *logbuf)
- : SocketListener("logdr", true)
+ : SocketListener(getLogSocket(), true)
, mLogbuf(*logbuf)
{ }
@@ -167,3 +168,16 @@
}
LogTimeEntry::unlock();
}
+
+int LogReader::getLogSocket() {
+ static const char socketName[] = "logdr";
+ int sock = android_get_control_socket(socketName);
+
+ if (sock < 0) {
+ sock = socket_local_server(socketName,
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_SEQPACKET);
+ }
+
+ return sock;
+}
diff --git a/logd/LogReader.h b/logd/LogReader.h
index b267c75..91559a3 100644
--- a/logd/LogReader.h
+++ b/logd/LogReader.h
@@ -34,6 +34,8 @@
virtual bool onDataAvailable(SocketClient *cli);
private:
+ static int getLogSocket();
+
void doSocketDelete(SocketClient *cli);
};
diff --git a/logd/main.cpp b/logd/main.cpp
index 7346e2f..04eef4a 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -36,6 +36,35 @@
#include "LogListener.h"
#include "LogAudit.h"
+//
+// The service is designed to be run by init, it does not respond well
+// to starting up manually. When starting up manually the sockets will
+// fail to open typically for one of the following reasons:
+// EADDRINUSE if logger is running.
+// EACCESS if started without precautions (below)
+//
+// Here is a cookbook procedure for starting up logd manually assuming
+// init is out of the way, pedantically all permissions and selinux
+// security is put back in place:
+//
+// setenforce 0
+// rm /dev/socket/logd*
+// chmod 777 /dev/socket
+// # here is where you would attach the debugger or valgrind for example
+// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
+// sleep 1
+// chmod 755 /dev/socket
+// chown logd.logd /dev/socket/logd*
+// restorecon /dev/socket/logd*
+// setenforce 1
+//
+// If minimalism prevails, typical for debugging and security is not a concern:
+//
+// setenforce 0
+// chmod 777 /dev/socket
+// logd
+//
+
static int drop_privs() {
struct sched_param param;
memset(¶m, 0, sizeof(param));