android usb挂载分析---MountService启动
在android usb挂载分析----vold启动,我们的vold模块已经启动了,通信的机制也已经建立起来了,接下来我们分析一下MountService的启动,也就是我们FrameWork层的启动,首先看下其大概流程:
MountService的启动在SystemServer.java中,有如下代码:
- try {
- /*
- * NotificationManagerService is dependant on MountService,
- * (for media / usb notifications) so we must start MountService first.
- */
- Slog.i(TAG, "Mount Service");
- ServiceManager.addService("mount", new MountService(context));
- } catch (Throwable e) {
- Slog.e(TAG, "Failure starting Mount Service", e);
- }
这里new 了一个MountService,并把service添加到了ServiceManager,我们看下MountService的构造函数:
- /**
- * Constructs a new MountService instance
- *
- * @param context Binder context for this service
- */
- public MountService(Context context) {
- mContext = context;
- // XXX: This will go away soon in favor of IMountServiceObserver
- mPms = (PackageManagerService) ServiceManager.getService("package");//获取包管理服务
- mContext.registerReceiver(mBroadcastReceiver,
- new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);//注册广播接收器
- mHandlerThread = new HandlerThread("MountService");//处理消息
- mHandlerThread.start();
- mHandler = new MountServiceHandler(mHandlerThread.getLooper());
- // Add OBB Action Handler to MountService thread.
- mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
- /*
- * Vold does not run in the simulator, so pretend the connector thread
- * ran and did its thing.
- */
- if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
- mReady = true;
- mUmsEnabling = true;
- return;
- }
- /*
- * Create the connection to vold with a maximum queue of twice the
- * amount of containers we'd ever expect to have. This keeps an
- * "asec list" from blocking a thread repeatedly.
- */
- mConnector = new NativeDaemonConnector(this, "vold",
- PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
- mReady = false;
- Thread thread = new Thread(mConnector, VOLD_TAG);
- thread.start();
- }
后面new 了一个NativeDaemonConnector,注意这里传递了一个"vold"字符串,跟我们在vold启动的时候传给CommandListener是一样的。NativeDaemonConnector实现了Runnable接口
接下来调用 thread.start()启动线程,我们看下它的run函数
- public void run() {
- while (true) {
- try {
- listenToSocket();
- } catch (Exception e) {
- Slog.e(TAG, "Error in NativeDaemonConnector", e);
- SystemClock.sleep(5000);
- }
- }
- }
在循环中调用listenToSocket函数,看下这个函数
- private void listenToSocket() throws IOException {
- LocalSocket socket = null;
- try {
- socket = new LocalSocket();
- LocalSocketAddress address = new LocalSocketAddress(mSocket, //这里mSocket=“vold"
- LocalSocketAddress.Namespace.RESERVED); //注意这里的RESERVED
- socket.connect(address); //连接到vold模块监听的套接字处
- mCallbacks.onDaemonConnected(); //实现在MountService中
- InputStream inputStream = socket.getInputStream();
- mOutputStream = socket.getOutputStream();
- byte[] buffer = new byte[BUFFER_SIZE];
- int start = 0;
- while (true) {
- int count = inputStream.read(buffer, start, BUFFER_SIZE - start); //读取消息
- if (count < 0) break;
- // Add our starting point to the count and reset the start.
- count += start;
- start = 0;
- for (int i = 0; i < count; i++) {
- if (buffer[i] == 0) {
- String event = new String(buffer, start, i - start);
- if (LOCAL_LOGD) Slog.d(TAG, String.format("RCV <- {%s}", event));
- String[] tokens = event.split(" ");
- try {
- int code = Integer.parseInt(tokens[0]);
- if (code >= ResponseCode.UnsolicitedInformational) {
- try {
- if (!mCallbacks.onEvent(code, event, tokens)) {//实现在MountService中
- Slog.w(TAG, String.format(
- "Unhandled event (%s)", event));
- }
- } catch (Exception ex) {
- Slog.e(TAG, String.format(
- "Error handling '%s'", event), ex);
- }
- }
- try {
- mResponseQueue.put(event);
- } catch (InterruptedException ex) {
- Slog.e(TAG, "Failed to put response onto queue", ex);
- }
- } catch (NumberFormatException nfe) {
- Slog.w(TAG, String.format("Bad msg (%s)", event));
- }
- start = i + 1;
- }
- }
- // We should end at the amount we read. If not, compact then
- // buffer and read again.
- if (start != count) {
- final int remaining = BUFFER_SIZE - start;
- System.arraycopy(buffer, start, buffer, 0, remaining);
- start = remaining;
- } else {
- start = 0;
- }
- }
- } catch (IOException ex) {
- Slog.e(TAG, "Communications error", ex);
- throw ex;
- } finally {
- synchronized (this) {
- if (mOutputStream != null) {
- try {
- mOutputStream.close();
- } catch (IOException e) {
- Slog.w(TAG, "Failed closing output stream", e);
- }
- mOutputStream = null;
- }
- }
- try {
- if (socket != null) {
- socket.close();
- }
- } catch (IOException ex) {
- Slog.w(TAG, "Failed closing socket", ex);
- }
- }
- }
onDaemonConnected的实现在MountServices中,将向下下发volume list消息 获取到了磁盘的标签,挂载点与状态,调用connect函数连接到vold模块,connetc最终调用native函数connectLocal(
system/core/libcutils/socket_local_client.c)进行连接工作,我们看下他的jni层代码,最后调用的:
- int socket_local_client_connect(int fd, const char *name, int namespaceId,
- int type)
- {
- struct sockaddr_un addr;
- socklen_t alen;
- size_t namelen;
- int err;
- err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
- if (err < 0) {
- goto error;
- }
- if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
- goto error;
- }
- return fd;
- error:
- return -1;
- }
- /**
- * connect to peer named "name"
- * returns fd or -1 on error
- */
我们再跟进socket_make_sockaddr_un函数,这时namespaceId传的ANDROID_SOCKET_NAMESPACE_RESERVED,所以会执行下面几句:
- case ANDROID_SOCKET_NAMESPACE_RESERVED:
- namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
- /* unix_path_max appears to be missing on linux */
- if (namelen > sizeof(*p_addr)
- - offsetof(struct sockaddr_un, sun_path) - 1) {
- goto error;
- }
- strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX); // ANDROID_RESERVED_SOCKET_PREFIX="/dev/socket/"
- strcat(p_addr->sun_path, name);
- break;
注意在前面 connect 函数中的套接字的构造,使用了AF_LOCAL:
- int socket_local_client(const char *name, int namespaceId, int type)
- {
- int s;
- s = socket(<span style="color:#ff0000;">AF_LOCAL</span>, type, 0);
- if(s < 0) return -1;
- if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
- close(s);
- return -1;
- }
- return s;
- }
这样,就建立了一条从FrameWork层到vold层的通信链路,后面FrameWork层就等待Vold发送消息过来了。。。
FrameWork层的通信也ok了,就可以等待U盘挂载了。。
android usb挂载分析---MountService启动的更多相关文章
- android usb挂载分析----vold启动
http://blog.csdn.net/new_abc/article/details/7396733 前段时间做了下usb挂载的,现在出了几个bug,又要把流程给梳理下,顺便也把相关的知识总结下, ...
- android usb挂载分析---vold处理内核消息
android usb挂载分析---vold处理内核消息 分类: u盘挂载2012-03-29 22:25 3215人阅读 评论(0) 收藏 举报 androidactioniteratordiskd ...
- android usb挂载分析
http://blog.csdn.net/new_abc/article/details/7409018
- [Openwrt 扩展上篇]USB挂载&U盘启动&Samba共享
最近偷懒,没学习,反想起自己的路由刷了Openwrt,正好闲置了一个硬盘想拿来做个网络硬盘,于是开始了折腾....这里将不谈论如何刷Openwrt,如何ssh,如何添加PPOE,如何添加相对应服务的包 ...
- [Openwrt 项目开发笔记]:USB挂载& U盘启动(三)
[Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一篇中,我结合Netgear Wndr370 ...
- Appium Android Bootstrap源代码分析之启动执行
通过前面的两篇文章<Appium Android Bootstrap源代码分析之控件AndroidElement>和<Appium Android Bootstrap源代码分析之命令 ...
- android文件系统挂载分析(1)---正常开机挂载
未完,更新中 ... "android"系列分为三部分: 1.正常开机挂载 2.encryption 3.dm-verity 我们知道android有很多分区,如"sys ...
- Appium Android Bootstrap源码分析之启动运行
通过前面的两篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>和<Appium Android Bootstrap源码分析之命令解析 ...
- Android USB驱动源码分析(-)
Android USB驱动中,上层应用协议里最重要的一个文件是android/kernel/drivers/usb/gadget/android.c.这个文件实现USB的上层应用协议. 首先包含了一些 ...
随机推荐
- htaccess 实现网址缩短
访问 :app.xxx.com/a 解析到:app.xxx.com/index.php/app/a <IfModule mod_rewrite.c> RewriteEngine on Re ...
- sqldeveloper连接mysql
sqldeveloper连接mysql 腑镜诏 钌d 汁叁ㄧ勋 佚蔡弥噙 不仅仅是越南帮和戴爷的事情还有谢婉莹的条 炔验遒其 倒扇油┣ 砹笄谤 几句话孟飞的信心大增兴奋不已换句话说 谠诂k厝 ...
- onPostCreate——Activity彻底运行起来之后的回调
记得之前想要在Activity布局完成,彻底跑起来之后,再获取当前Activity的窗口中,某个View的宽高,之前用的办法很土,弄个Handler,发个Message出来,使用sendMessage ...
- 笨方法学python--多行,转义序列
1 输入多行字符串的方法有2个,一个是使用换行符 \n.另一个是使用 "三引号". 2 针对不同的符号,有很多这样的"转义序列"(escape sequence ...
- Jquery-获取勾选的checkbox的同级节点数量
// 获取勾选的标签值得上一个兄弟节点 var groups = []; $('input[name="group_name"]:checked').siblings(" ...
- sql-删除delete涉及到三个表,这个时候就要使用from,比如这样
delete y from dbo.XZXK_BANJIE b ,YJDC_YELLOWRED_CONTENT y , dbo.XZXK_SHOULI s where b.shoulioid=s.sh ...
- win10怎么启用网络发现,网络发现已关闭怎么办
脑和电脑之间传输文件的方式很多,其中一种就是使用局域网,在网络中我们的电脑应该可以被其他电脑发现是非常方便使用文件共享的,尤其是在使用家庭组网络的时候,那么win10里面怎么启用网络发现呢? 工具/原 ...
- 3.1 cron表达式
1.Cron在线生成网址: http://cron.qqe2.com/ http://www.pdtools.net/tools/becron.jsp#cron 2.Cron 概要 3. ...
- Openlays 3 绘制基本图形
<body> <div id="menu"> <label>几何图形类型:</label> <select id=" ...
- http://www.iteye.com/job/topic/1133159
Lucene 的索引体系是一个写独占,读共享的结构,这意味着,我们在使用多线程进行添加索引时,性能并不会得到明显的提升,所以任何时刻只能有一个线程对索引进行写 入操作,而保障这个操作的安全性则是来自于 ...