Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中如何自己制作su

Android中如何自己制作su

編輯:關於Android編程

本文原博客:http://hubingforever.blog.163.com/blog/static/171040579201372915716149/

在Android源碼的system\extras(比如Android4.0\system\extras)下新建一個目錄,比如su_robin目錄

在su_robin目錄下包含以三個文件: su.h文件

#ifndef SU_h #define SU_h 1 #ifdef LOG_TAG#undef LOG_TAG#endif#define LOG_TAG "robin" #define REQUESTOR "com.noshufou.android.su"#define REQUESTOR_DATA_PATH "/data/data/" REQUESTOR#define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR #define REQUESTOR_DATABASES_PATH REQUESTOR_DATA_PATH "/databases"#define REQUESTOR_DATABASE_PATH REQUESTOR_DATABASES_PATH "/permissions.sqlite" /* intent actions */#define ACTION_REQUEST REQUESTOR ".REQUEST"#define ACTION_RESULT REQUESTOR ".RESULT" #define DEFAULT_SHELL "/system/bin/sh" #ifdef SU_LEGACY_BUILD#define VERSION_EXTRA "l"#else#define VERSION_EXTRA ""#endif #define VERSION "3.1.1" VERSION_EXTRA#define VERSION_CODE 17 #define DATABASE_VERSION 6#define PROTO_VERSION 0 struct su_initiator { pid_t pid; unsigned uid; char bin[PATH_MAX]; char args[4096];}; struct su_request { unsigned uid; int login; int keepenv; char *shell; char *command; char **argv; int argc; int optind;}; struct su_context { struct su_initiator from; struct su_request to; mode_t umask;}; enum { DB_INTERACTIVE, DB_DENY, DB_ALLOW};#endif

注意,這裡的很多東西是多余。 su.c文件

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "su.h" int main(int argc, char *argv[]){ LOGI("hello !robin-su begin %d !",getuid()); printf("hello !robin-su begin"); int uid=0; int gid=0; if(setgid(gid) || setuid(uid)) { LOGI("robin su: permission denied\n"); fprintf(stderr,"su: permission denied\n"); return 1; } char command[1024]; LOGI("hello !robin-su end %d !",getuid()); printf("hello !robin-su end"); /* Default exec shell. */ execlp("/system/bin/sh", "sh", NULL); fprintf(stderr, "su: exec failed\n"); return 1;}

Android.mk文件

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) LOCAL_MODULE := surLOCAL_SRC_FILES := su.c LOCAL_STATIC_LIBRARIES := \ liblog \ libc \ LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)LOCAL_MODULE_TAGS := eng debugLOCAL_FORCE_STATIC_EXECUTABLE := true include $(BUILD_EXECUTABLE)

編譯Android源碼,生成rom 如果你還不知道如何編譯Android源碼,請參照 http://source.android.com/source/building-running.html 在out\target\product\generic\system\xbin目錄下找到生成的可執行文件sur,就是我們自己制作的su文件 通過adb push命令將其推送到手機上的system\xbin目錄, 通過執行系統原來自己帶的su切換到root用戶,對手機上sur文件執行chmod 6777 sur命令。 使文件屬性變為rwsrwsrwx,如下所示 rwsrwsrwx root root 58960 2013-08-29 11:31 sur 然後你可以通過執行系統原來自己帶的sur切換到root用戶 關於chmod 6777 sur命令請參考《chmod命令基本篇》和請參考《chmod命令高級篇( SetUID與SetGID標識位)》 關於setuid請參考《setuid()與setgid()的使用 》 結束!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved