在介绍完Android M之前的日志系统的实现之后,我们现在来看看现在最新的Android L的日志机制。Android L与之前版本最大的变化,就是日志保存的位置由Kernel的Ringer Buffer(前一篇文章已经介绍过)改成用户层Buffer来存储,用户层的Buffer的读定存储设置管理,都由logd这个服务来实现,现在我们来一探究竟。分析完logd之后,再花一篇文章,简单的过一下上层写日志和logcat读日志的流程。这样整个安卓日志系统就有一个大概轮廓了。
1,在系统启动到init处理的时候,会解析init.rc启动logd service如下:
service logd /system/bin/logd
class core
socket logd stream 0666 logd logd
socket logdr seqpacket 0666 logd logd
socket logdw dgram 0222 logd logd
group root system
同时会创建和初始化3个socket::logd, logdr, logdw。分别是用来监听命令,处理读log,和处理写log。
socket logd stream 0666 logd logd
在init中解析socket的处理如下:
service_start(struct service *svc, const char *dynamic_args)@init.cpp
for (si = svc->sockets; si; si = si->next) {
//读取socket类型,stream或者dgram
int socket_type = (
!strcmp(si->type, "stream") ? SOCK_STREAM :
(!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
//创建socket
int s = create_socket(si->name, socket_type,
si->perm, si->uid, si->gid, si->socketcon ?: scon);
if (s >= 0) {
//发布socket,把创建的socketFd写到环境变量,让其它Sokect的Server端通过android_get_control_socket(mSocketName)来获得socketFd.
publish_socket(si->name, s);
}
}
核心是create_socket,来看这里的实现,代码位于init/util.cpp
int create_socket(const char *name, int type, mode_t perm, uid_t uid,
gid_t gid, const char *socketcon)
{
struct sockaddr_un addr;
int fd, ret;
char *filecon;
//调用系统调用socket来创建一个PF_UNIX的socket
fd = socket(PF_UNIX, type, 0);
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
name);
//把这个socket绑定到addr上,这个addr就与/dev/socket/*有关了
ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
这个init基本上就把Socket的Server端的初始化工作准备好了。
2,logd启动之后,会获得相应的socket,并监听socket。
以logdw为例,main()#logd/main.cpp
// LogListener listens on /dev/socket/logdw for client
// initiated log messages. New log entries are added to LogBuffer
// and LogReader is notified to send updates to connected clients.
LogListener *swl = new LogListener(logBuf, reader);
// Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
if (swl->startListener(300)) {
exit(1);
}
LogListener继承成SocketListener,而startListener正是其父类SocketListener的方法。
先看New LogListener(LogBuf, reader)
LogListener::LogListener(LogBuffer *buf, LogReader *reader) :
//同时会构造一个父类SocketListener,getLogSocket()是通过logdw这个名字返回一个SocketFd
SocketListener(getLogSocket(), false),
logbuf(buf),
reader(reader) {
}
接下来看SocketListener的构造函数,也就是把相关参数传过来进行赋值传递。
SocketListener.cpp
SocketListener::SocketListener(int socketFd, bool listen) {
init(NULL, socketFd, listen, false);
}
=》
void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) {
mListen = listen;
mSocketName = socketName;
mSock = socketFd;
mUseCmdNum = useCmdNum;
pthread_mutex_init(&mClientsLock, NULL);
mClients = new SocketClientCollection();
}
再回到上面,logd/main.cpp中main()。创建完LogListener,紧接着就swl->startListener(300);这个startListener直接由SocketListener实现,我们直接来看SocketListener.cpp
int SocketListener::startListener(int backlog) {
if (!mSocketName && mSock == -1) {
} else if (mSocketName) {
//获得SocketFd
if ((mSock = android_get_control_socket(mSocketName)) < 0) {
}
SLOGV("got mSock = %d for %s", mSock, mSocketName);
fcntl(mSock, F_SETFD, FD_CLOEXEC);
}
//调用listen的系统调用,监听SocketFd。此时mListen为NULL,应该不会调用listen??TODO,有编译器有关??
if (mListen && listen(mSock, backlog) < 0) {
} else if (!mListen)
//创建SocketClient,并放到mClients的,mClients是存储所有SocketClient的List容器。
mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
//创建PID为mThread的线程,线程执行函数是thradStart,并启动 。
if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
SLOGE("pthread_create (%s)", strerror(errno));
return -1;
}
return 0;
}
来看thread执行函数threadStart
void *SocketListener::threadStart(void *obj) {
SocketListener *me = reinterpret_cast(obj);
me->runListener();
pthread_exit(NULL);
return NULL;
}
runListener有点长,主要做了以下几个事情。
void SocketListener::runListener() {
...
rc = select(max + 1, &read_fds, NULL, NULL, NULL);
...
c = accept(mSock, &addr, &alen);
...
/* Process the pending list, since it is owned by the thread, * there is no need to lock it */ while (!pendingList.empty()) { /* Pop the first item from the list */ it = pendingList.begin(); SocketClient* c = *it; pendingList.erase(it); /* Process it, if false is returned, remove from list */ if (!onDataAvailable(c)) {//这个数据处理函数,由继承SocketListener的类来实现,在这里就是指LogListener.cpp release(c, false); } c->decRef(); }
这些都是UNIX线程通信的系统调用。这样Socket的Server就准备好了。
总结一下,在unix Socket通信中Server端一般有以下几个步骤
The steps involved in establishing a socket on the server side are as follows:
1,Create a socket with the socket() system call
2,Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3,Listen for connections with the listen() system call
4,Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
Send and receive data
对于logdw,1,2步骤在init里面完成,3,4步是LogListener的父类SocketListener里面完成。
3,Logdw是如何处理来自liblog的请求处理的。
这样我们可以大致来看看整个sokect通信框图(草图)
在第2小节中,具体的数据处理是由onDataAvailable()完成,这个函数是LogListener.cpp来实现,
第1步,读取数据,并存在Socket定义的MSG相关结构体内
char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
+ LOGGER_ENTRY_MAX_PAYLOAD]; struct iovec iov = { buffer, sizeof(buffer) };
memset(buffer, 0, sizeof(buffer));
//存放Client的进程信息
char control[CMSG_SPACE(sizeof(struct ucred))];
struct msghdr hdr = {
NULL,
0,
&iov,//真正存放LOG message
1,
control,
sizeof(control),
0,
};
int socket = cli->getSocket();
ssize_t n = recvmsg(socket, &hdr, 0);
这里有必要说一下msghdr这个结构体:
msghdr是用于Socket在两个进程之间通讯定义的消息头
struct msghdr {
void *msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void *msg_control; /* ancillary data, see below */
size_t msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
msg_control:是一个指向cmsghdr 结构体的指针,
struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by unsigned char cmsg_data[]; */
};
msg_controllen :参见下图,即cmsghdr 结构体可能不止一个;
对于CMSG在LogListener.cpp里面是control变量,char control[CMSG_SPACE(sizeof(struct ucred))];也就是说CMSG是存放Client的PID,UID,GID信息的。
struct ucred {
pid_t pid; /* process ID of the sending process */
uid_t uid; /* user ID of the sending process */
gid_t gid; /* group ID of the sending process */
};
第2步,解析CMSG里面进程相关信息,并检查权限
struct ucred *cred = NULL;
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
while (cmsg != NULL) {
if (cmsg->cmsg_level == SOL_SOCKET
&& cmsg->cmsg_type == SCM_CREDENTIALS) {
cred = (struct ucred *)CMSG_DATA(cmsg);
break;
}
cmsg = CMSG_NXTHDR(&hdr, cmsg);
}
if (cred == NULL) {
return false;
}
if (cred->uid == AID_LOGD) {
// ignore log messages we send to ourself.
// Such log messages are often generated by libraries we depend on
// which use standard Android logging.
return false;
}
第3步,处理真正的Log信息,从第1步可以知道,Log信息是存放在iov指向的buffer里面,即对buffer处理就是处理Log信息
android_log_header_t *header = reinterpret_cast(buffer);
if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX || header->id == LOG_ID_KERNEL) {
return false;
}
char *msg = ((char *)buffer) + sizeof(android_log_header_t);
n -= sizeof(android_log_header_t);
// NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
// truncated message to the logs.
if (logbuf->log((log_id_t)header->id, header->realtime,
cred->uid, cred->pid, header->tid, msg,
((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX) >= 0) {
reader->notifyNewLog();
}
return true;
首先调用 logbuf->log()创建一条Log,然后调用reader->nofifyNewLog()把Log存储到buffer中。
至logd的实现,基本上分析完成。关于LogBuffer和LogReader,读者可以自己深入分析。
msghdr部分参考了:http://blog.csdn.net/jnu_simba/article/details/9079627