Android 从上到下写一个例子 SELinux权限(一)

在  android-8.1.0_r1/frameworks/base/core/java/android/os  路径下新增IGodvService.aidl

package android.os;

/**
 * @hide
 */
interface IGodvService {
    int gov_ioctl(int which, int status);
}

在  android-8.1.0_r1/frameworks/base  路径下修改mk文件将新建的aidl文件添加进去 

  接下来mma  mma ANDROID_COMPILE_WITH_JACK=false

## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \
	core/java/android/os/IGodvService.aidl \
...

 在out目录下find一下  拷贝

godv@godv-OptiPlex-7070:~/godv/AOSP/android-8.1.0_r1/out/target$ find -name IGodvService.java
./common/obj/JAVA_LIBRARIES/framework_intermediates/core/java/android/os/IGodvService.java

cp common/obj/JAVA_LIBRARIES/framework_intermediates/core/java/android/os/IGodvService.java ~

在该文件的末尾有一个生成好了的接口

public int gov_ioctl(int which, int status) throws android.os.RemoteException;

编写服务

  在  android-8.1.0_r1/frameworks/base/services/core/java/com/android/server 下编译

GodvService.java

package com.android.server;

import android.os.IGodvService;
import android.os.IBinder;
import android.os.Binder;
import android.os.ServiceManager;
import android.util.Slog;

public class GodvService extends IGodvService.Stub {
    private static final String TAG = "godv";
    

    public GodvService(){
	godv_open();
	Slog.d(TAG, "GodvService::godv_open");
    }

    public int gov_ioctl(int which, int status) throws android.os.RemoteException {
        Slog.d(TAG, "GodvService::gov_ioctl");
        godv_ioctl(which, status);
    } 
    
    public native int godv_open();
    public native int godv_ioctl(int which, int status);
        
}

添加服务

在frameworks/base/services/java/com/android/server/SystemServer.java里面添加服务  编译

private void startOtherServices() {
    ...
    traceBeginAndSlog("StartGodvService");
    GodvService godv = new GodvService(context);
    ServiceManager.addService("godv", godv);
    traceEnd();
    ...
}

报错信息

  SELinux权限问题可以暂时使用一个命令关闭    setenforce 0

  关于 SELinux权限问题解决参考https://blog.csdn.net/we1less/article/details/119282763

E ServiceManager: add_service('godv',33) uid=1000 - PERMISSION DENIED
I zygote64: option[47]=Xfingerprint:Android/aosp_x86_64/generic_x86_64:8.1.0/OPM1.171019.011/godv06271014:eng/test-keys
D godv    : GodvService::godv_open
E SELinux : avc:  denied  { add } for service=godv pid=6508 uid=1000 scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=0

服务权限解决完成后相应的服务进行就会在系统中运行起来了

generic_x86_64:/ # service list | grep "godv"                                  
71	godv: [android.os.IGodvService]

在路径下out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar

有编译好的jar包导入到as中可以进行ndk开发

public class MainActivity extends AppCompatActivity {
    IGodvService service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IBinder godv = ServiceManager.getService("godv");
        service = IGodvService.Stub.asInterface(godv);

    }

    public void run(View view) {
        try {
            service.gov_ioctl(0,1);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

 运行一遍打个log

LIBRARIES/framework_intermediates$ adb logcat -s "godv"
--------- beginning of main
--------- beginning of system
--------- beginning of crash
09-06 22:03:59.721  1540  1540 D godv    : GodvService::godv_open
09-06 22:14:04.435  1540  1706 D godv    : GodvService::gov_ioctl
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值