Android 7.1 adds a system bottom service

Affected by the epidemic, I am currently busy with my work. I haven't had a detailed blog for a long time.I recently looked at the underlying service and tried to add a dungeon service. , Students who are interested in Java layer services can take a look

Android 7.1 adds a system service for interprocess communication (java layer)

This time add an underlying service, the purpose is for the application to get this service to communicate with the underlying service 

Divided into roughly a few steps

1. Add files

2. Compile the file

3. Type the file into the ROM

4. Add sepolicy permissions

5. Test permissions

 

One: add files

The files involved this time are shown below:

frameworks/base$ tree customcamera 显示文件夹结构

customcamera
├── customcamera //编写可执行文件 
│   ├── Android.mk
│   └── customcamera.cpp
└── libcustomcamera //服务的具体实现类
    ├── CustomCamera.cpp
    └── CustomCamera.h

2 directories, 4 files

1. Create a folder customcamera and two subfolders customcamera and libcustomcamera 

First create CustomCamera.cpp and CustomCamera.h because it is simple Demo code and not complicated

----- CustomCamera.h reads as follows:

#ifndef ANDROID_GUILH_ADD_SERVICE_H
#define ANDROID_GUILH_ADD_SERVICE_H
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/threads.h>
namespace android {

        class CustomCamera: public BBinder{// 从 BBinder 派生,实现本地接口

                public:

                static int instantiate();

                CustomCamera();

                virtual ~CustomCamera();

                virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);

        };

}; //namespace

#endif

------CustomCamera.cpp

#include "CustomCamera.h"
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
namespace android {
    static struct sigaction oldact;
	static pthread_key_t sigbuskey;
	// 把自己注册到系统中
	int CustomCamera::instantiate() {
	ALOGW("CustomCamera instantiate");
    
    int r = defaultServiceManager()->addService(
				String16("custom_camera"), new CustomCamera());//服务添加到 Binder Driver 中服务名为 custom_camera
		ALOGW("CustomCamera r = %d/n", r);
		return r;
	}

	// 构造函数
	CustomCamera::CustomCamera()
	{
		ALOGW("CustomCamera created");
		pthread_key_create(&sigbuskey, NULL);
	}

	// 析构函数
	CustomCamera::~CustomCamera()
	{
		pthread_key_delete(sigbuskey);
		ALOGW("CustomCamera  destroyed");
	}

	// 这个是服务具体的本地实现,功能实现都应该放在这里面,通过传入执行代码( code ) // 的不同来执行不同的操作,上层隐射为不同的 api 。

	status_t CustomCamera::onTransact(
			uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){
		switch(code) {
			case 0: {// 根据 code 的不同执行不同的操作
					pid_t pid = data.readInt32();
					int num = data.readInt32();
					num = num + 1000;
					reply->writeInt32(num);
					return NO_ERROR;
				}
				break;
			default:
				return BBinder::onTransact(code, data, reply, flags);
		}
	}};
//namespace

When the code is written to this step, you can now resume a makefile file in this folder to test whether the currently written CustomCamera.cpp is correct, or you can post-test, here is for post-testing.

Then create customcamera.cpp and Android.mk files in the customcamera folder.

------ customcamera.cpp content is as follows

#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <private/android_filesystem_config.h>
#include "../libcustomcamera/CustomCamera.h"

using namespace android;

int main(int argc, char** argv)

{

	sp<ProcessState> proc(ProcessState::self());
	sp<IServiceManager> sm = defaultServiceManager();//取得 ServiceManager
	ALOGW("CustomCamera ServiceManager: %p", sm.get());
	CustomCamera::instantiate();//把自己添加到 ServiceManager中
	ProcessState::self()->startThreadPool();//启动缓冲池
	IPCThreadState::self()->joinThreadPool();//这里是把服务添加到 Binder闭合循环进程中
}

------Android.mk content

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := ../libcustomcamera/CustomCamera.cpp \
        customcamera.cpp \

LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS -Wl,-Map=test.map -g -Wno-unused-parameter -Wno-unused-variable

LOCAL_CONLYFLAGS := -std=c11

LOCAL_SHARED_LIBRARIES := libcutils \
                libsysutils \
        libutils \
        libbinder \
        libui \
        libgui \
        liblog

LOCAL_LDLIBS := -llog

LOCAL_MODULE := custom_camera

LOCAL_MODULE_TAGS := optional

LOCAL_C_INCLUDES += $(LOCAL_PATH)


#编译为可执行文件
include $(BUILD_EXECUTABLE)

The above documents have been written.

 

2. Compile the file

Then return to the root directory of the source code and execute as follows

~:source build/envsetup.sh 

~:lunch XX<需要编译的代码>

Then go back to the frameworks / base / customcamera / customcamera folder to perform mm operation and compile the module

At this point you should see the following content: successfully compiled successfully  

Then go to the corresponding directory and find that: custom_camera already exists 

 

3. Hit the ROM

1. Because we created the file Android.mk under framework / base, the system will automatically copy it to system / bin in the out directory when the system is fully compiled.

This will bring the compiled custom_camera to the system when playing ROM.

2. If we compiled custom_camera in advance, put custom_camera under a file, and add the copy code in the corresponding mk of the system.

For example, mine is Qualcomm's code addition path is device / qcom / msmxxx / msmxxx.mk

The sense of this sentence is to indicate that if the system is compiled, copy this file to out / target / product / msmxxxx / system / bin / custom_camera

PRODUCT_COPY_FILES += \
    out/target/product/msm8953_64/system/bin/custom_camera:system/bin/custom_camera

4. Start the service

The path of the code file to add the startup service is qcom / msmXXX / init.target.rc Other platforms may be other.rc But the logic is the same

Modify permissions first

    # 修改权限
     chmod 0777 /system/bin/custom_camera
     chmod 0660 /sys/devices/soc.0/78b7000.i2c/i2c-3/3-0020/input/input0/secure_touch_enable
     chmod 0440 /sys/devices/soc.0/78b7000.i2c/i2c-3/3-0020/input/input0/secure_touch
     chmod 0660 /sys/devices/soc.0/78b8000.i2c/i2c-4/4-0020/input/input0/secure_touch_enable

 

#启动我们的服务,开机启动
service custom_camera /system/bin/custom_camera
    class core
    user root
    group root
    oneshot

 

V. Testing services 

//在应用程序中测试服务的代码:

    private void test(){

                try{

                        IBinder binder = ServiceManager.getService("guilh.add");// 取得服务

                        Parcel data = Parcel.obtain();

                        Parcel reply = Parcel.obtain();

                        if(binder == null)

                                Log.d(TAG,"failed to get service");

                        data.writeInt(Process.myPid());// 固定操作

                        data.writeInt(100);// 传入参数

                        binder.transact(0, data, reply, 0);// 执行远程调用

                        Log.d(TAG,"result="+reply.readInt());// 验证结果

                }catch(Exception e){

                         Log.d(TAG,e.toString());

                }

Six. Add selinux permissions

Android 7.1 adds an inter-process communication system service (java layer)  . The latter part of the article is introduced, so I wo n’t repeat it here.

The main design is as follows

 


#主要是修改 neverallow 内容
#-mcu_service
system/sepolicy/domain.te


#/system/bin/custom_camera  u:object_r:custom_camera_service_exec:s0
system/sepolicy/file_contexts


# custom_camera               u:custom_camera_service_exec:s0
system/sepolicy/custom_camera.te # 拷贝别的te修改名字就行


# custom_camera               u:object_r:custom_camera_service_service:s0
system/sepolicy/service_contexts #  与 file_contexts 相对应

 

26 original articles published · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/ChaoLi_Chen/article/details/105615126