Android培訓班(30)

來源:互聯網
上載者:User

<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

在init.rc檔案裡第一個初始化的服務是sh服務,如下:

## Daemon processes to be run by init.

##

service console /system/bin/sh

console

 

sh服務是控制台服務,其實它是從NetBSD移植過來的,因此它的命令也是比較有限的,不過作為嵌入式系統,使用shell的機會不多。

sh服務的代碼在目錄:Android-2.0/system/core/sh

sh服務使用flex工具產生詞法分析代碼,使用bison產生文法分析代碼。

 

下面來分析這個服務主要代碼,先從main函數開始,如下:

int

main(int argc, char **argv)

{

struct jmploc jmploc;

struct stackmark smark;

volatile int state;

char *shinit;

 

下面這行代碼,就是我為了加入調試使用的。

/* caijs add debug */

out2str("caijs add debug");

 

#if PROFILE

monitor(4, etext, profile_buf, sizeof profile_buf, 50);

#endif

state = 0;

 

下面這段代碼設定執行命令異常的處理。

if (setjmp(jmploc.loc)) {

/*

* When a shell procedure is executed, we raise the

* exception EXSHELLPROC to clean up before executing

* the shell procedure.

*/

switch (exception) {

case EXSHELLPROC:

rootpid = getpid();

rootshell = 1;

minusc = NULL;

state = 3;

break;

 

case EXEXEC:

exitstatus = exerrno;

break;

 

case EXERROR:

exitstatus = 2;

break;

 

default:

break;

}

 

if (exception != EXSHELLPROC) {

if (state == 0 || iflag == 0 || ! rootshell)

exitshell(exitstatus);

}

reset();

if (exception == EXINT

#if ATTY

&& (! attyset() || equal(termval(), "emacs"))

#endif

) {

out2c();

flushout(&errout);

}

popstackmark(&smark);

FORCEINTON; /* enable interrupts */

if (state == 1)

goto state1;

else if (state == 2)

goto state2;

else if (state == 3)

goto state3;

else

goto state4;

}

handler = &jmploc;

#ifdef DEBUG

#if DEBUG == 2

debug = 1;

#endif

opentrace();

trputs("Shell args: "); trargs(argv);

#endif

 

 

rootpid = getpid();

這行代碼通過調用函數getpid 擷取進程標識。

 

rootshell = 1;

init();

初始化內部支援的命令。

 

setstackmark(&smark);

 

 

procargs(argc, argv);

這行代碼根據命令列進行處理。

 

 

下面代碼讀取配置參數。

if (argv[0] && argv[0][0] == -) {

state = 1;

read_profile("/etc/profile");

state1:

state = 2;

read_profile(".profile");

}

state2:

state = 3;

if (getuid() == geteuid() && getgid() == getegid()) {

if ((shinit = lookupvar("ENV")) != NULL && *shinit != ) {

state = 3;

read_profile(shinit);

}

}

state3:

state = 4;

if (sflag == 0 || minusc) {

static int sigs[] = {

SIGINT, SIGQUIT, SIGHUP,

#ifdef SIGTSTP

SIGTSTP,

#endif

SIGPIPE

};

#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))

int i;

 

for (i = 0; i < SIGSSIZE; i++)

setsignal(sigs[i], 0);

}

 

if (minusc)

evalstring(minusc, 0);

 

 

下面的代碼調用函數cmdloop 來進入shell的命令處理。

if (sflag || minusc == NULL) {

state4: /* XXX ??? - why isnt this before the "if" statement */

cmdloop(1);

}

#if PROFILE

monitor(0);

#endif

 

最後退出shell的執行。

exitshell(exitstatus);

/* NOTREACHED */

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.