Android设备节点
Android 安全架构及权限控制机制剖析

Android 安全架构及权限控制机制剖析李洋, 信息安全顾问简介:Android 是业界流行的开源移动平台,受到广泛关注并为多个手机制造商作为手机的操作系统平台,因此,研究其安全架构及权限控制机制具有非常的重要性。
本文从Android 层次化安全架构入手,详细地介绍Android 平台的安全架构及其权限控制机制,涵盖Android 应用程序权限申请方法等,并从源代码实现层面来解析该机制。
标记本文!发布日期:2012 年8 月14 日级别:初级Android 层次化安全架构Android 作为一个移动设备的平台,其软件层次结构包括了一个操作系统(OS),中间件(MiddleWare)和应用程序(Application)。
根据Android 的软件框图,其软件层次结构自下而上分为以下几个层次:操作系统层(OS)各种库(Libraries)和Android 运行环境(RunTime)应用程序框架(Application Framework)应用程序(Application)以下分别介绍Andoid 各个层次的软件的重点及其相关技术:(1)操作系统层(OS)Android 使用Linux2.6 作为操作系统,Linux2.6 是一种标准的技术,Linux 也是一个开放的操作系统。
Android 对操作系统的使用包括核心和驱动程序两部分,Android 的Linux 核心为标准的Linux2.6 内核,Android 更多的是需要一些与移动设备相关的驱动程序。
主要的驱动如下所示:显示驱动(Display Driver):常用基于Linux 的帧缓冲(Frame Buffer)驱动Flash 内存驱动(Flash Memory Driver)照相机驱动(Camera Driver):常用基于Linux 的v4l(Video for )驱动。
音频驱动(Audio Driver):常用基于ALSA(Advanced Linux Sound Architecture,高级Linux 声音体系)驱动WiFi 驱动(Camera Driver):基于IEEE 802.11 标准的驱动程序键盘驱动(KeyBoard Driver)蓝牙驱动(Bluetooth Driver)Binder IPC 驱动:Andoid 一个特殊的驱动程序,具有单独的设备节点,提供进程间通讯的功能。
Android系统中闹钟设置程序设计源程序

---------------------------------------------------------------范文最新推荐------------------------------------------------------ Android系统中闹钟设置程序设计+源程序摘要Android 是Google 在2007 年11 月推出了一个专为移动设备设计的软件平台。
Android 是一个针对移动设备的程序集,它包括操作系统、用户界面、中间件和应用程序,拥有移动电话工作所需的全部软件。
本从Android系统出现的背景和意义出发,简单介绍了Java语言,之后又介绍了Android系统的核心模块和相关技术,并对Android系统的主要组件进行了研究。
搭建了Android系统的开发环境,创建了简单的Android应用,分析了Android工程的目录结构。
最后开始Android闹钟程序的编写,实现了时间的动态显示、闹钟的设臵、闹钟的删除、重复响起闹钟的设臵、时间到了的时候的提醒、退出闹钟程序时的提醒等功能。
11517关键词AndroidJava开发环境闹钟程序1 / 13毕业设计说明书(论文)外文摘要TitleThe setup program of alarm clock designed in the Android systemAbstractAndroid is a special software platform designed for mobile devices launched by Google in November 2007. Android is a set of procedures for mobile devices, including the operating system, user interface, middleware and applications, with all the necessary software needed by mobile phone. Starting from the background and significance of the Android system, a brief introduction to the Java language, and then introduced in the core modules and related technologies of the Android system, and the main components of Android system. Set up the development environment of Android system, create a simple application of Android, analysis directory structure of the Android project. Last began to write Android alarm clock program,realize functions of time dynamic display, set of the alarm clock, delete of the alarm clock , settings of---------------------------------------------------------------范文最新推荐------------------------------------------------------the re-sounded alarm, reminding when time came, reminding when the alarm clock program exit.KeywordsAndroidJavadevelopment environmentalarm clock program1.1 Android出现的背景和意义Android 是Google 在2007 年11 月推出了一个专为移动设备设计的软件平台。
创建设备文件_使用DEVICE_ATTR实例分析

创建设备⽂件_使⽤DEVICE_ATTR实例分析在android中操作驱动时,很多时候都是使⽤的sysfs⽂件系统来进⾏直接操作。
即通过操作⼀个节点⽂件,直接实现对kernel 层的数据操作。
这个时候,就不得不提⼀个宏:DEVICE_ATTR。
DEVICE_ATTR宏定义在include/linux/device.h中,所以⼀般需要添加头⽂件:#include原型是:#define DEVICE_ATTR(_name, _mode, _show, _store) \struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store);DEVICE_ATTR 宏声明有四个参数,分别是名称(_name)、权限位(_mode:⼀般使⽤S_IRUGO | S_IWUSR或者0664)、读函数(_show)、写函数(_store)。
其中读函数和写函数是读写功能函数的函数名。
调⽤DEVICE_ATTR⽣成的对应的⽂件在/sys/devices/⽬录中对应的device下⾯。
代码主要实现过程如下:static ssize_t test_czd_show(struct device *dev,struct device_attribute *attr, char *buf) //读函数的封装定义,⾃⼰根据需要添加相应的操作{return sprintf(buf, "%s\n", "czd:test code");}static ssize_t test_czd_store(struct device *dev,struct device_attribute *attr,const char *buf, size_t count) //写函数的封装定义,⾃⼰根据需要添加相应的操作{return count;}static DEVICE_ATTR(test_czd, S_IRUGO | S_IWUSR, test_czd_show, test_czd_store);//创建设备节点⽂件test_czd当你想要实现的接⼝名字是test_czd的时候,需要实现结构体struct attribute *dev_attrs[],其中成员变量的名字必须是&dev_attr_test_czd.attrstatic struct attribute *dev_attrs[] = {&dev_attr_test_czd.attr,NULL,};然后再封装:static struct attribute_group dev_attr_group = {.attrs = dev_attrs,};然后在probe函数中调⽤:sysfs_create_group(&client->dev.kobj, &dev_attr_group);//创建接⼝sysfs在remove函数中调⽤:sysfs_remove_group(&client->dev.kobj, &dev_attr_group);//在remove中移除接⼝sysfs原理:当我们将数据echo到接⼝中时,在上层实际上完成了⼀次write操作,对应到kernel调⽤了驱动中的store。
android accessibilitynodeinfo详解

android accessibilitynodeinfo详解AccessibilityNodeInfo是Android中的一个类,用于描述应用程序界面中的可访问节点信息。
它提供了一系列方法,用于获取和设置节点的各种属性和状态,以及与节点进行交互的方法。
AccessibilityNodeInfo的详细解释如下:1. 节点类型:AccessibilityNodeInfo提供了获取节点类型的方法,可以判断节点是什么类型的控件,比如按钮、文本框、复选框等。
通过节点类型,可以根据需要进行相应的操作。
2. 节点文本:AccessibilityNodeInfo提供了获取节点文本的方法,可以获取控件上显示的文本内容。
这对于需要读取或验证控件上的文本信息非常有用。
3. 节点状态:AccessibilityNodeInfo提供了获取节点状态的方法,可以获取控件的状态信息,比如是否可用、是否已选中等。
通过节点状态,可以根据需要进行相应的操作或判断。
4. 节点属性:AccessibilityNodeInfo提供了获取节点属性的方法,可以获取控件的各种属性,比如控件的位置、大小、背景颜色等。
通过节点属性,可以对控件进行定位或判断。
5. 节点操作:AccessibilityNodeInfo提供了一系列方法,用于与节点进行交互,比如点击、长按、滑动等。
通过这些方法,可以模拟用户的操作,实现对控件的操作。
6. 节点关系:AccessibilityNodeInfo提供了获取节点关系的方法,可以获取控件的父节点、子节点、兄弟节点等。
通过节点关系,可以在应用程序界面中进行节点的遍历和查找。
总之,AccessibilityNodeInfo是一个非常重要的类,它提供了丰富的方法和属性,可以帮助开发者实现对应用程序界面中的可访问节点进行获取、操作和判断等功能。
通过使用AccessibilityNodeInfo,可以使Android应用程序更加易于访问和使用。
高通Andriod开机流程与镜像说明

⾼通Andriod开机流程与镜像说明Android镜像说明Android设备刷机时都需要ROM包,ROM包下⾯有很多的.img和其他的相关镜像⽂件,其中这⾥⾯包含了Android很多的分区,Android镜像⽂件是通过源码编译⽣成的,下⾯是ROM包各个镜像的作⽤:镜像⽂件说明boot.img boot分区,包括内核⽂件和虚拟内存盘Ramdisk,负责设备开机,可在recovery模式进⾏擦除,重新安装带有boot分区的新系统system.img system分区,包含Android系统的⽤户界⾯以及设置上的⼀些预装系统应⽤recovery.img recovery分区,替代启动分区,执⾏恢复和维护系统的⼀些操作userdata.img data分区,⽤于保存⽤户的数据,例如联系⼈,短信,设置偏好和应⽤程序存放的地⽅cache.img cache分区,⽤于放置系统频繁访问的数据和应⽤程序组件的分区persist.img persist分区包含了设备的传感器和信号部分的驱动程序,例如wifi,蓝⽛连接都有关系splash.img开机画⾯⽂件NON-HLOS.bin modem image负责处理通讯协议相关的基带镜像prog_emmc_firehose_8953_ddr.mbn QFIL软件烧录系统的时候,需要⽤到此⽂件,应该是关于EMMC、DDR的配置参数相关的sbl1.mbn硬件的初始化,并且保存加载其他模块信息的顺序tz.mbn trustzone是ARM TrustZone® 技术是系统范围的安全⽅法,基于安全需求和引导模式配置XPU,NAND MPU,它和其他模块代码运⾏在相互隔离的区域,主要实现底层很多安全性特性emmc_appsboot.mbn bootloader⽂件,进⼊fastboot模式相关⽂件rpm.mbn电源管理器,是⾼通MSM平台另外加的⼀块芯⽚,虽然与AP芯⽚打包在⼀起,但其是⼀个独⽴的ARM Core。
Android 的 init.rc 文件的语法详解

Android初始化语言包含了四种类型的声明:Actions(行动)、 Commands(命令)、Services(服务)和Options(选项)。
所有这些都是以行为单位的,各种记号由空格来隔开。
C语言风格的反斜杠号可用于在记号间插入空格。
双引号也可用于防止字符串被空格分割成多个记号。
行末的反斜杠用于折行。
注释行以井号(#)开头(允许以空格开头)。
Actions和Services声明一个新的分组。
所有的命令或选项都属于最近申明的分组。
位于第一个分组之前的命令或选项将会被忽略。
Actions和Services有唯一的名字。
如果有重名的情况,第二个申明的将会被作为错误忽略。
(???我们是否应该以覆盖来代替忽略)Actions(行动)----------Actions其实就是一序列的Commands(命令)。
Actions都有一个trigger(触发器),它被用于决定action的执行时间。
当一个符合action触发条件的事件发生时,action会被加入到执行队列的末尾,除非它已经在队列里了。
队列中的每一个action都被依次提取出,而这个action中的每个command (命令)都将被依次执行。
Init在这些命令的执行期间还控制着其他的活动(设备节点的创建和注销、属性的设置、进程的重启)。
Actions的形式如下:on 《trigger》《command》《command》《command》Services(服务)----------Services(服务)是一个程序,他在初始化时启动,并在退出时重启(可选)。
Services(服务)的形式如下:service 《name》《pathname》 [ 《argument》 ]*《option》《option》...Options(选项)----------Options(选项)是一个Services(服务)的修正者。
他们影响Services(服务)在何时,并以何种方式运行。
Android设备开发文档说明书

1) What is Android?It is an open-sourced operating system that is used primarily on mobile devices, such as cell phones and tablets. It is a Linux kernel-based system that’s been equipped with rich components that allows developers to create and run apps that can perform both basic and advanced functions.2) What Is the Google Android SDK?The Google Android SDK is a toolset that developers need in order to write apps on Android enabled devices. It contains a graphical interface that emulates an Android driven handheld environment, allowing them to test and debug their codes.3) What is the Android Architecture?Android Architecture is made up of 4 key components:- Linux Kernel- Libraries- Android Framework- Android Applications4) Describe the Android Framework.The Android Framework is an important aspect of the Android Architecture. Here you can find all the classes and methods that developers would need in order to write applications on the Android environment.5) What is AAPT?AAPT is short for Android Asset Packaging Tool. This tool provides developers with the ability to deal with zip-compatible archives, which includes creating, extracting as well as viewing its contents.6) What is the importance of having an emulator within the Android environment?The emulator lets developers “play” around an interface that acts as if it were an actual mobile device. They can write and test codes, and even debug. Emulators are a safe place for testing codes especially if it is in the early design phase.7) What is the use of an activityCreator?An activityCreator is the first step towards the creation of a new Android project. It is made up of a shell script that will be used to create new file system structure necessary for writing codes within the Android IDE.8 ) Describe Activities.Activities are what you refer to as the window to a user interface. Just as you create windows in order to display output or to ask for an input in the form of dialog boxes, activities play the same role, though it may not always be in the form of a user interface.9) What are Intents?Intents displays notification messages to the user from within the Android enabled device. It can be used to alert the user of a particular state that occurred. Users can be made to respond to intents.10) Differentiate Activities from Services.Activities can be closed, or terminated anytime the user wishes. On the other hand, services are designed to run behind the scenes, and can act independently. Most services run continuously, regardless of whether there are certain or no activities being executed.11) What items are important in every Android project?These are the essential items that are present each time an Android project is created:- AndroidManifest.xml- build.xml- bin/- src/- res/- assets/12) What is the importance of XML-based layouts?The use of XML-based layouts provides a consistent and somewhat standard means of setting GUI definition format. In common practice, layout details are placed in XML files while other items are placed in source files.13) What are containers?Containers, as the name itself implies, holds objects and widgets together, depending on which specific items are needed and in what particular arrangement that is wanted. Containers may hold labels, fields, buttons, or even child containers, as examples.14) What is Orientation?Orientation, which can be set using setOrientation(), dictates if the LinearLayout is represented asa row or as a column. Values are set as either HORIZONTAL or VERTICAL.15) What is the importance of Android in the mobile market?Developers can write and register apps that will specifically run under the Android environment. This means that every mobile device that is Android enabled will be able to support and run these apps. With the growing popularity of Android mobile devices, developers can take advantage of this trend by creating and uploading their apps on the Android Market for distribution to anyone who wants to download it.16) What do you think are some disadvantages of Android?Given that Android is an open-source platform, and the fact that different Android operating systems have been released on different mobile devices, there’s no clear cut policy to how applications can adapt with various OS versions and upgrades. One app that runs on this particular version of Android OS may or may not run on another version. Another disadvantage is that since mobile devices such as phones and tabs come in different sizes and forms, it poses a challenge for developers to create apps that can adjust correctly to the right screen size and other varying features and specs.17) What is adb?Adb is short for Android Debug Bridge. It allows developers the power to execute remote shell commands. Its basic function is to allow and control communication towards and from the emulator port.18) What are the four essential states of an activity?- Active – if the activity is at the foreground- Paused – if the activity is at the background and still visible- Stopped – if the activity is not visible and therefore is hidden or obscured by another activity- Destroyed – when the activity process is killed or completed terminated19) What is ANR?ANR is short for Application Not Responding. This is actually a dialog that appears to the user whenever an application have been unresponsive for a long period of time.20) Which elements can occur only once and must be present?Among the different elements, the and elements must be present and can occur only once. The rest are optional, and can occur as many times as needed.21) How are escape characters used as attribute?Escape characters are preceded by double backslashes. For example, a newline character is created using ‘\\n’22) What is the importance of settings permissions in app development?Permissions allow certain restrictions to be imposed primarily to protect data and code. Without these, codes could be compromised, resulting to defects in functionality.23) What is the function of an intent filter?Because every component needs to indicate which intents they can respond to, intent filters are used to filter out intents that these components are willing to receive. One or more intent filters are possible, depending on the services and activities that is going to make use of it.24) Enumerate the three key loops when monitoring an activity- Entire lifetime – activity happens between onCreate and onDestroy- Visible lifetime – activity happens between onStart and onStop- Foreground lifetime – activity happens between onResume and onPause25) When is the onStop() method invoked?A call to onStop method happens when an activity is no longer visible to the user, either because another activity has taken over or if in front of that activity.26) Is there a case wherein other qualifiers in multiple resources take precedence over locale?Yes, there are actually instances wherein some qualifiers can take precedence over locale. There are two known exceptions, which are the MCC (mobile country code) and MNC (mobile network code) qualifiers.27) What are the different states wherein a process is based?There are 4 possible states:- foreground activity- visible activity- background activity- empty process28) How can the ANR be prevented?One technique that prevents the Android system from concluding a code that has been responsive for a long period of time is to create a child thread. Within the child thread, most of the actual workings of the codes can be placed, so that the main thread runs with minimal periods of unresponsive times.29) What role does Dalvik play in Android development?Dalvik serves as a virtual machine, and it is where every Android application runs. Through Dalvik, a device is able to execute multiple virtual machines efficiently through better memory management.30) What is the AndroidManifest.xml?This file is essential in every application. It is declared in the root directory and contains information about the application that the Android system must know before the codes can be executed.31) What is the proper way of setting up an Android-powered device for app development? The following are steps to be followed prior to actual application development in an Android-powered device:-Declare your application as "debuggable" in your Android Manifest.-Turn on "USB Debugging" on your device.-Set up your system to detect your device.32) Enumerate the steps in creating a bounded service through AIDL.1. create the .aidl file, which defines the programming interface2. implement the interface, which involves extending the inner abstract Stub class as well as implanting its methods.3. expose the interface, which involves implementing the service to the clients.33) What is the importance of Default Resources?When default resources, which contain default strings and files, are not present, an error will occur and the app will not run. Resources are placed in specially named subdirectories under the project res/ directory.34) When dealing with multiple resources, which one takes precedence?Assuming that all of these multiple resources are able to match the configuration of a device, the ‘locale’ qualifier almost always takes the highest precedence over the others.35) When does ANR occur?The ANR dialog is displayed to the user based on two possible conditions. One is when there is no response to an input event within 5 seconds, and the other is when a broadcast receiver is not done executing within 10 seconds.36) What is AIDL?AIDL, or Android Interface Definition Language, handles the interface requirements between a client and a service so both can communicate at the same level through interprocess communication or IPC. This process involves breaking down objects into primitives that Android can understand. This part is required simply because a process cannot access the memory of the other process.37) What data types are supported by AIDL?AIDL has support for the following data types:-string-charSequence-List-Map-all native Java data types like int,long, char and Boolean38) What is a Fragment?A fragment is a part or portion of an activity. It is modular in a sense that you can move around or combine with other fragments in a single activity. Fragments are also reusable.39) What is a visible activity?A visible activity is one that sits behind a foreground dialog. It is actually visible to the user, but not necessarily being in the foreground itself.40) When is the best time to kill a foreground activity?The foreground activity, being the most important among the other states, is only killed or terminated as a last resort, especially if it is already consuming too much memory. When a memory paging state has been reach by a foreground activity, then it is killed so that the user interface can retain its responsiveness to the user.41) Is it possible to use or add a fragment without using a user interface?Yes, it is possible to do that, such as when you want to create a background behavior for a particular activity. You can do this by using add(Fragment,string) method to add a fragment from the activity.42) How do you remove icons and widgets from the main screen of the Android device?To remove an icon or shortcut, press and hold that icon. You then drag it downwards to the lower part of the screen where a remove button appears.43) What are the core components under the Android application architecture?There are 5 key components under the Android application architecture:- services- intent- resource externalization- notifications- content providers44) What composes a typical Android application project?A project under Android development, upon compilation, becomes an .apk file. This apk file format is actually made up of the AndroidManifest.xml file, application code, resource files, and other related files.45) What is a Sticky Intent?A Sticky Intent is a broadcast from sendStickyBroadcast() method such that the intent floats around even after the broadcast, allowing others to collect data from it.46) Do all mobile phones support the latest Android operating system?Some Android-powered phone allows you to upgrade to the higher Android operating system version. However, not all upgrades would allow you to get the latest version. It depends largely on the capability and specs of the phone, whether it can support the newer features available under the latest Android version.47) What is portable wi-fi hotspot?Portable Wi-Fi Hotspot allows you to share your mobile internet connection to other wireless device. For example, using your Android-powered phone as a Wi-Fi Hotspot, you can use your laptop to connect to the Internet using that access point.48) What is an action?In Android development, an action is what the intent sender wants to do or expected to get as a response. Most application functionality is based on the intended action.49) What is the difference between a regular bitmap and a nine-patch image?In general, a Nine-patch image allows resizing that can be used as background or other image size requirements for the target device. The Nine-patch refers to the way you can resize the image: 4 corners that are unscaled, 4 edges that are scaled in 1 axis, and the middle one that can be scaled into both axes.50) What language is supported by Android for application development?The main language supported is Java programming language. Java is the most popular language for app development, which makes it ideal even for new Android developers to quickly learn to create and deploy applications in the Android environment.Guru99 Provides FREE ONLINE TUTORIAL on Various courses likeJava MIS MongoDB BigData CassandraWeb Services SQLite JSP Informatica AccountingSAP Training Python Excel ASP Net HBase ProjectTest Management Business Analyst Ethical Hacking PMP ManagementLive Project SoapUI Photoshop Manual Testing Mobile TestingData Warehouse R Tutorial Tableau DevOps AWSJenkins Agile Testing RPA JUnitSoftware EngineeringSelenium CCNA AngularJS NodeJS PLSQL。
ueventd.rc触发规则 -回复

ueventd.rc触发规则-回复ueventd.rc触发规则是指在Android系统中,通过ueventd.rc文件中的规则来触发某些操作或事件。
该文件位于/system/core/rootdir/ueventd.rc路径下,是Android系统启动过程中非常重要的一个配置文件。
ueventd.rc文件中包含了一系列的触发规则,用于定义设备的硬件特性、文件权限、设备节点的创建等相关信息。
这些规则在系统启动时会被读取,然后按照规则执行相应的操作。
在ueventd.rc文件中,每个规则都以“on”开头,接着是一个属性(如subsystem、uevent、ueventmatch等),然后是一系列的条件和对应的动作。
下面将逐一解释这些属性以及对应的含义和作用。
1. subsystem:指定了uevent的subsystem属性,用于匹配对应的子系统类型。
可以是block、input、sound、camera等等。
当有子系统事件触发时,这个属性就会被匹配。
2. uevent:用于匹配uevent事件,通常是根据设备节点的路径进行匹配。
当该路径上发生了uevent事件时,uevent属性就会被匹配。
3. ueventmatch:用于匹配uevent的属性值。
可以是一个或多个属性的组合。
当事件上的属性值与该属性匹配时,ueventmatch属性就会被匹配。
例如,可以使用SUBSYSTEM=block来匹配子系统为block的设备。
4. device:指定了要创建的设备节点的名称。
可以是字符设备、块设备或者目录。
5. perms:指定了要创建的设备节点的权限。
可以是四位的八进制数,或者是文件权限的字符表示形式,如0666、rw-rw-rw-等。
6. chown:指定了要创建的设备节点的属主和属组。
可以是用户ID 和组ID的十进制或十六进制表示,也可以是用户和组的名称。
例如,可以使用root system来指定属主为root,属组为system。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1 第1章 Android设备节点的创建 在编写linux设备驱动程序的时候,很多时候都是利用mknod命令来手动创建设备节点的,带上名字和主次设备号就可以在/dev目录下生成设备节点。同样Android沿用了linux内核,很多设备驱动的节点是又是什么时候创建的呢? 在kernel自解压模块加载完成之后,会去运行android第一个应用程序init。在init.c的main函数中。 System/core/init/init.c int main(int argc, char **argv) { …… action_for_each_trigger("boot", action_add_queue_tail); …… } 在init进程解析init.rc脚本完成后,在on boot的最后两句是class start main和core,其中class Start是命令,在keyword.h中定义了class_start对应的function实际就是do_class_start。 System/core/init/builtins.c int do_class_start(int nargs, char **args) { service_for_each_class(args[1], service_start_if_not_disabled); return 0; } System/core/init/init_parser.c void service_for_each_class(const char *classname, void (*func)(struct service *svc)) { …… list_for_each(node, &service_list) { svc = node_to_item(node, struct service, slist); if (!strcmp(svc->classname, classname)) { func(svc); } }
在之前解析init.rc脚本的时候,service会被放在service_list的链表里。接下来就是要执行service_for_each_class的func(svc),也就是service_start_if_not_disabled。 System/core/init/builtins.c static void service_start_if_not_disabled(struct service *svc) { if (!(svc->flags & SVC_DISABLED)) { service_start(svc, NULL); } } Android的service大都是编译成可执行文件以命令的格式,我们注意到在init.rc中又这么个2
service值得关注下。 service ueventd /sbin/ueventd class core critical Android的服务不是选项不是disabled并且带core和main的选项的服务都是需要开机自动加载的服务。而ueventd是由system/core/init/ueventd.c编译而成的。 System/core/init/ueventd.c int ueventd_main(int argc, char **argv) { …… ueventd_parse_config_file("/ueventd.rc"); …… snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware); ueventd_parse_config_file(tmp); …… device_init(); …… while(1) { ufd.revents = 0; nr = poll(&ufd, 1, -1); if (nr <= 0) continue; if (ufd.revents == POLLIN) handle_device_fd(); } } Ueventd的main函数做的事情比较多,首先是要解析根文件系统下的ueventd.rc以及ueventd. ${hardware}.rc。 System/core/init/ueventd_parser.c int ueventd_parse_config_file(const char *fn) { char *data; data = read_file(fn, 0); if (!data) return -1; parse_config(fn, data); DUMP(); return 0; } static void parse_config(const char *fn, char *s) { …… state.parse_line = parse_line_device; …… for (;;) { 3
…… switch (token) { case T_EOF: state.parse_line(&state, 0, 0); return; case T_NEWLINE: if (nargs) { tate.parse_line(&state, 0, 0); …… } 解析过程中,会去循环解析这个文件,对解析的结果调用state.parse_line,对应的回调函数就是parse_line_device。 System/core/init/ueventd_parser.c static void parse_line_device(struct parse_state* state, int nargs, char **args) { set_device_permission(nargs, args); } 在set_device_permission(nargs, args)中会获取设备的mode,uid,gid。给设备增加操作权限。 然后通过device_init给设备添加创建NETLINK套接字。 System/core/init/ueventd.c int ueventd_main(int argc, char **argv) { …… ueventd_parse_config_file("/ueventd.rc"); …… snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware); ueventd_parse_config_file(tmp); …… device_init(); …… while(1) { ufd.revents = 0; nr = poll(&ufd, 1, -1); if (nr <= 0) continue; if (ufd.revents == POLLIN) handle_device_fd(); } } 通过轮询的方式查看设备的uevent的变化,在设备注册产生kobject_uevent。在设备处理时候调用了handle_device_fd()。 System/core/init/devices.c void handle_device_fd() { 4
…… char msg[UEVENT_MSG_LEN+2]; int n; while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ continue;
msg[n] = '\0'; msg[n+1] = '\0';
struct uevent uevent; parse_event(msg, &uevent);
handle_device_event(&uevent); handle_firmware_event(&uevent); } } 接收的uevent的小心不能超过1024个字节,如果超出就算溢出将不会处理。如果接收的uevent有效,解析这个uevent会根据设备的类型来解析。之后handle_device_event会处理设备的event。而handle_firmware_event则是和某些设备需要firmware回去处理firmware的加载。 System/core/init/devices.c static void handle_device_event(struct uevent *uevent) { if (!strcmp(uevent->action,"add")) fixup_sys_perms(uevent->path);
if (!strncmp(uevent->subsystem, "block", 5)) { handle_block_device_event(uevent); } else if (!strncmp(uevent->subsystem, "platform", 8)) { handle_platform_device_event(uevent); } else { handle_generic_device_event(uevent); } } 如果event的subsystem为block,说明是个块设备,就会交由handle_block_device_event去处理。或者subsystem是platform设备的话,则有handle_platform_device_event来处理。如果两者都不是的话,说明是一个普通设备,handle_generic_device_event会来处理。块设备和平台设备的处理方式比较特殊,先看一般通用设备的处理过程。 System/core/init/devices.c static void handle_generic_device_event(struct uevent *uevent) { …… name = parse_device_name(uevent, 64);