/************************************************************************* * i.mx6 su.c Test * Description: * Today suddenly want to analyze Su's source code, look at its working mechanism. * 2016-8-10 Shenzhen Nanshan Ping Shan village Zengjianfeng **************************************************** ********************/First, SU source code modification:/** * * Copyright, the Android Open Source Project * * * Licensed under the Apache License, Version 2. 0 (the "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * *http://www.apache.org/licenses/LICENSE-2.0* * * unless required by applicable law or agreed to writing, software * Distributed under the License is distributed on a "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. */ #defineLog_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 is given a specific command to exec. UID _must_ is * specified for this (ie ARGC = 3). * Usage: * Su-ls-l * su*/ intMainintargcChar**argv) { structpasswd *PW; intuid, GID, myuid; /*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(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; } } //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; } Two, permissions: chmod4775/system/xbin/Su III, call:Private StaticString cmdlist[] = { "su 0 netcfg can0 down", "su 0 IP link set can0 type can bitrate 1000000 triple-sampling on", "su 0 netcfg can0 up", };
i.mx6 SU.C Test