本文原博客: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 <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdint.h>
#include <pwd.h>
#include <private/android_filesystem_config.h>
#include <cutils/properties.h>
#include <cutils/log.h>
#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 := sur
LOCAL_SRC_FILES := su.c
LOCAL_STATIC_LIBRARIES := \
    liblog \
    libc \
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := eng debug
LOCAL_FORCE_STATIC_EXECUTABLE := true
include $(BUILD_EXECUTABLE)
编译Android源代码,生成rom
假设你还不知道怎样编译Android源代码。请參照
在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()的使用
结束!

Android中怎样自己制作su的更多相关文章

  1. Android中部署自己的su

    本人博客原文 首先把你的自己的su的放到Android应用程序project的assets文件夹,为了和系统的su区分,我自己的su文件叫做sur. 另外我这里没有考虑x86架构的cpu的手机. 废话 ...

  2. 【Android 应用开发】Android中使用ViewPager制作广告栏效果 - 解决ViewPager占满全屏页面适配问题

    . 参考界面 : 携程app首页的广告栏, 使用ViewPager实现        自制页面效果图 : 源码下载地址: http://download.csdn.net/detail/han1202 ...

  3. Android中使用ViewPager制作广告栏效果 - 解决ViewPager占满全屏页面适配问题

    . 参考界面 : 携程app首页的广告栏, 使用ViewPager实现        自制页面效果图 : 源码下载地址: http://download.csdn.net/detail/han1202 ...

  4. Android中使用shape制作一个旋转的progressbar

    public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...

  5. Android中制作自定义dialog对话框的实例

    http://www.jb51.net/article/83319.htm   这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...

  6. 如何制作Jar包并在android中调用jar包

    android制作jar包: 新建android工程,然后右击,点击导出,选择导出类型为Java下的JAR file,在java file specification 中不要选择androidmani ...

  7. Android开发之《制作自己的su文件》

    目录结构  ─ hello ├── jni ├── Android.mk └── hello.c 编译步骤: # cd hello # export NDK_PROJECT_PATH=`pwd` # ...

  8. Android App中使用Gallery制作幻灯片播放效果

    http://www.jb51.net/article/83313.htm 我们有时候在iPhone手机上或者Windows上面看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的图片滚动效果 ...

  9. Android中插件开发篇之----应用换肤原理解析

    一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...

随机推荐

  1. python--内置常用模块

    一 . 简单的了解模块 你写的每一个py文件都是一个模块. 还有一些我们一直在使用的模块. buildins  内置模块. print, input. random  主要是和随机相关的的内容 ran ...

  2. (转)Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等

    本文转自http://blog.csdn.net/totogo2010/article/details/7714960 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 - ...

  3. Spring,Mybatis,Springmvc框架整合项目(第二部分)

    一.创建数据库表 打开Navicat Premium,点击左上角连接,选择mysql   输入你的数据库用户名和密码信息,可以先点击下测试连接,如果显示连接成功,说明能连接到数据库,然后点击确定.如果 ...

  4. C#自定义Excel操作类

    C#自定义Excel操作类,可以用于将DataTable导出到Excel文件,从Excel文件读取数据. using System; using System.IO; using System.Dat ...

  5. jquery 元素文本取值/赋值

    select元素选中option值 jq $("#select1 option:selected").text(); $("select id或class option: ...

  6. luogu2219 [HAOI2007]修筑绿化带

    和「理想的正方形」比较相似,需要先掌握那道题. 花坛外头每一边必须套上绿化带 #include <iostream> #include <cstdio> using names ...

  7. luogu1131 [ZJOI2007]时态同步

    num[x]表示x到达叶子最远路径. 每个子节点对答案的贡献是num[x] - (num[t] + edge[i].val) #include <iostream> #include &l ...

  8. J2EE 中间件 JVM 集群

    [转]J2EE 中间件 JVM 集群 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 jvm应用服务器weblogicjvm集群 1 前言 越来越多的关键任务和大型应用 ...

  9. python 字典 key 和value 互换

    这里是一个可能有用的通过字典解析实现的小技巧: 交换字典的键和值. >>> a_dict = {'a': 1, 'b': 2, 'c': 3} >>> {value ...

  10. 计算几何 I. 极角

    参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...