/***** Copyright 2008, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ****     http://www.apache.org/licenses/LICENSE-2.0 **** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License.*/#define LOG_TAG "su"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <dirent.h>#include <errno.h>#include <unistd.h>#include <time.h>#include <pwd.h>#include <private/android_filesystem_config.h>/* * SU can be given a specific command to exec. UID _must_ be * specified for this (ie argc => 3). * * Usage: * su 1000 * su 1000 ls -l */int main(int argc, char **argv){    struct passwd *pw;    int uid, gid, myuid;    if(argc < 2) {        uid = gid = 0;    } else {        pw = getpwnam(argv[1]);        if(pw == 0) {            uid = gid = atoi(argv[1]);        } else {            uid = pw->pw_uid;            gid = pw->pw_gid;        }    }    /* Until we have something better, only root and the shell can use su. */    myuid = getuid();    if (myuid != AID_ROOT && myuid != AID_SHELL) {        fprintf(stderr,"su: uid %d not allowed to su\n", myuid);        return 1;    }        if(setgid(gid) || setuid(uid)) {        fprintf(stderr,"su: permission denied\n");        return 1;    }    /* User specified command for exec. */    if (argc == 3 ) {        if (execlp(argv[2], argv[2], NULL) < 0) {            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],                    strerror(errno));            return -errno;        }    } else if (argc > 3) {        /* Copy the rest of the args from main. */        char *exec_args[argc - 1];        memset(exec_args, 0, sizeof(exec_args));        memcpy(exec_args, &argv[2], sizeof(exec_args));        if (execvp(argv[2], exec_args) < 0) {            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],                    strerror(errno));            return -errno;        }    }    /* Default exec shell. */    execlp("/system/bin/sh", "sh", NULL);    fprintf(stderr, "su: exec failed\n");    return 1;}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Failed to find an AVD compatible with
  2. Android常见知识汇总
  3. Android异步任务
  4. Android中 android:gravity 和 android:l
  5. Android接口定义语言(AIDL)
  6. Android xml资源文件中@、@android:type
  7. android动态墙纸有关问题
  8. Android架构组件三 Android Architecture
  9. ANDROID Porting系列一、ANDROID编译系统
  10. Android shape图形