454// Evaluate the expressions in argv, returning an array of char*
455// results.  If any evaluate to NULL, free the rest and return NULL.
456// The caller is responsible for freeing the returned array and the
457// strings it contains.
458char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
459    char** args = (char**)malloc(argc * sizeof(char*));
460    int i = 0;
461    for (i = 0; i < argc; ++i) {
462        args[i] = Evaluate(state, argv[i]);
463        if (args[i] == NULL) {
464            int j;
465            for (j = 0; j < i; ++j) {
466                free(args[j]);
467            }
468            free(args);
469            return NULL;
470        }
471    }
472    return args;
473}
argv의 entry들을 하나씩 Evaluate으로 패스. 

 

패스된 argv=expr을 받아서

35char* Evaluate(State* state, Expr* expr) {
36    Value* v = expr->fn(expr->name, state, expr->argc, expr->argv);
37    if (v == NULL) return NULL;
38    if (v->type != VAL_STRING) {
39        ErrorAbort(state, "expecting string, got value type %d", v->type);
40        FreeValue(v);
41        return NULL;
42    }
43    char* result = v->data;
44    free(v);
45    return result;
46}


Value *v에 expr->fn(expr->name, state, expr->argc, expr->argv) 를 assign시킴.

Value은 다음과 같은 구조체.

51typedef struct {
52    int type;
53    ssize_t size;
54    char* data;
55} Value;

 

expr은 Expr 구조체이며,

60struct Expr {
61    Function fn;
62    char* name;
63    int argc;
64    Expr** argv;
65    int start, end;

 

expr->fn은 Function 타입, Function은

57typedef Value* (*Function)(const char* name, State* state,
58                           int argc, Expr* argv[]);

형태로 정의됨.

type인 string인지 확인하고 아닐 경우, 에러 메세지를 state에 저장하고, string일 경우, 정상 진행.

마지막에 v->data를 리턴함.

이런 값들을 모아서 ReadVarArgs는 args 배열로 리턴.

 

 

expr.h

30typedef struct Expr Expr;
31
32typedef struct {
33    // Optional pointer to app-specific data; the core of edify never
34    // uses this value.
35    void* cookie;
36
37    // The source of the original script.  Must be NULL-terminated,
38    // and in writable memory (Evaluate may make temporary changes to
39    // it but will restore it when done).
40    char* script;
41
42    // The error message (if any) returned if the evaluation aborts.
43    // Should be NULL initially, will be either NULL or a malloc'd
44    // pointer after Evaluate() returns.
45    char* errmsg;
46} State;
47
48#define VAL_STRING  1  // data will be NULL-terminated; size doesn't count null
49#define VAL_BLOB    2
50
51typedef struct {
52    int type;
53    ssize_t size;
54    char* data;
55} Value;
56
57typedef Value* (*Function)(const char* name, State* state,
58                           int argc, Expr* argv[]);
59
60struct Expr {
61    Function fn;
62    char* name;
63    int argc;
64    Expr** argv;
65    int start, end;
66};

 

Posted by code cat

출처:http://kimyow.blog.me/50116380850

이 글이 가장 인기있는 글이네요.

지금와서 다시 읽어보니, 내용이 너무 두서없는 것 같아, 좀 더 이해가 쉽도록 수정을 했습니다. signing 개념 이해에 많은 도움이 되길 바라겠습니다.

=================================================================


Platform key signing 을 하는 이유는 뭘까?


왜 굳이 Platform key 로 signing 을 해야 할까?


일반적인 apk 의 경우, (일반 개발자, 3rd party 가 개발한 APK) 자신이 만든 apk 를, data 영역에 install 할 수 있으며, 또는 rooting 한 폰의 system 영역에 설치할 수 있고, 이를 실행할 수 있다.


여기서 일반적인 apk 란, 통상적을 제공되는 sdk 에서 개발 된 apk 를 말한다.

(Vendor가 아닌, 일반적인 모든 개발자가 Eclipse 등 SDK를 사용하여 개발한 Application)


Full image build 과정에서 생성되는 System image(Framework.jar)를 사용하면, 소위말하는 Internal or Hidden API 를 사용하여 build 할 수 있다.


SDK 로 제공되는 android.jar 에는 포함되지 않는 API(method) 들이 많다. 

이는 Android가 일반 개발자에게는 open 하지 않는 기능들이라고 봐도 좋다.


오직 Device 를 만들어서 파는 Vendor(삼성/LG)들만이 사용할 수 있다.


Settings.apk 의 경우,


타 모델의 apk 또는 source 를 구한다 한 들, 쉽게 자신의 phone 에 설치 & 실행이 안 될 것이다.

(Setting 만이 가지고 있는 특정 기능을 포기한 후 build 한다면, 가능하다.)


이미 당신이 가지고 있는 폰은 Vendor 가 User mode 로 build 한, 특정 key 로 signing 된 image 를 사용하기 때문이다.

( Debug or Engineer 모드로 build 된 image 가 탑재된 상태로 시장에 출시되지 않겠죠? )


User mode build 시, Vendor 는 android 에서 제공되는 known key (보통, \build\target\product\security\ 안에 있다.) 로 signing 하지 않으며, 각자의 unique 한 key 를 만들어서 signing 한다. 개발 편의를 위해서 default key 로 signing 하기도 하는데, 이는 eclipse 가 build 시 signing 하는 key 와 동일하다.


즉, Vendor 입장에서는 개발 단계와 양산 단계의 signing key 가 다른 것이다.


System 권한이 필요한 Setting 의 경우, Platform key 로 signing 후 설치된다. System과 동일한 uid 를 가져야 동일한 권한으로 data/code 사용이 가능하며 이를 위해서는 system 과 동일한 platform key 로 signing 되어야 한다.


하지만, 이미 당신이 가지고 있는 device(phone) 의 platform key 를 가지고 있지 않은 이상, 아무리 source build 를 성공해도 만들어진 Settings.apk 를 설치하기란 불가능하다.

(설치하려고 하면, INSTALL_FAILED_SHARED_USER_INCOMPATIBLE error 가 뜰 것이다.)


물론, 위에서 언급했듯이 Setting 만의 특정 기능을 뺀다면 가능하다.


Setting 만의 특정 기능이란, 타 application 을 memory 상에서 깨끗하게 kill 할 수 있는 기능이다.


이는 위에서 언급한 internal api 를 사용해야 하므로, 3rd party application 에선 사용이 불가능하며, system 권한/자격이 필요한 기능이다.


System 권한, 자격을 포기한다면, 굳이 platform key 로 signing 할 필요 없으며, 일반적인 application 으로써, 어떤 device 든지 기 설치되어 있는 settings.apk 만 확실히 제거한 후, 당신이 빌드한 새로운 settings.apk를 재설치해서 사용이 가능하다.


이 system 자격, 권한을 포기한다는 의미가, AndroidManifest.xml 에서 속성으로 명시된,

android:sharedUserId="android.uid.system" 항목을 삭제하는 것이다.


위 의미는 system process 와 같은 uid 를 사용하므로써, system process 의 code & data 를 똑 같이 공유할 수 있는 권한을 해당 application 에게 준다는 의미이다.


따라서 위 항목을 지우고 build 한 apk 를 설치하면 아주 자~~알 설치될 것이다.


하지만,~~ Settings > Applications >Manage applications 에서 "Force stop" 버튼을 클릭 하는 순간 unexpected error, 즉 setting application 이 죽을 것이다.


System 자격이 있는 넘이 호출해야 할 함수를 자격이 없는 application이 호출했기 때문에 Permission error 가 나는 것이다.


대표적인 Internal api method 인, ActivityManager 의 forceStopPackage() 를 예로 들어보자.- Internal API이므로, 일반 개발자에게는 open 되어 있지 않은 함수다.


보통 Market에서 download 받아 설치하는 일반적인 system application의 경우(TaskManager), ActivityManager.killBackgroundProcesses() 로 application 을 kill하는데, 이는 application 우선순위가 Background 이하인 application 만 kill 하는 제약을 가지고 있다.(Visible, Foreground, Service 우선순위는 완벽하게 kill 할 수 없다.)


이에 반해 ActivityManager.forceStopPackage() 함수는 아무런 제약 사항없이 대상이 되는 application 을 메모리상에서 깨끗히 지우는 기능을 한다.


이 함수를 실행하기 위해서는 permission 이 필요한데, 

<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>

라고 AndroidManifest.xml 에 명시해줘야 한다.


이렇게 해서 build 된 apk 를 install 한 후, 실행 하면 하기와 같은 exception 이 발생한다.


==================================================================

 

07-21 10:38:13.480: WARN/ActivityManager(1981): Permission Denial: forceStopPackage() from pid=3565, uid=10110 requires android.permission.FORCE_STOP_PACKAGES

07-21 10:38:13.503: ERROR/AndroidRuntime(3565): FATAL EXCEPTION: main

07-21 10:38:13.503: ERROR/AndroidRuntime(3565): java.lang.SecurityException: Permission Denial: forceStopPackage() from pid=3565, uid=10110 requires android.permission.FORCE_STOP_PACKAGES

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.os.Parcel.readException(Parcel.java:1260)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.os.Parcel.readException(Parcel.java:1248)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.app.ActivityManagerProxy.forceStopPackage(ActivityManagerNative.java:2564)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.app.ActivityManager.forceStopPackage(ActivityManager.java:968)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at com.lge.lmk.activities.RunningProcessActivity$4.onClick(RunningProcessActivity.java:850)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.os.Handler.dispatchMessage(Handler.java:99)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.os.Looper.loop(Looper.java:123)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at android.app.ActivityThread.main(ActivityThread.java:4668)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at java.lang.reflect.Method.invokeNative(Native Method)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at java.lang.reflect.Method.invoke(Method.java:521)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

07-21 10:38:13.503: ERROR/AndroidRuntime(3565):     at dalvik.system.NativeStart.main(Native Method)

==================================================================

 


"android.permission.FORCE_STOP_PACKAGES" Permission 을 명시해 줬는데 왜 Permission error가 날까?


이 함수를 call 하는 application쪽에 System process 의 권한이 없기 때문이다.


system process 와 동일한 권한을 갖기위해서는?


AndroidManifest.xml 파일의 manifest attribute 에 하기와 같이 지정해 주면 된다.

android:sharedUserId="android.uid.system"


이 application process 의 uid 를 system 과 공유함으로써, system 과 동일한 권한을 갖겠다는 의미다.


참 편리하게 system 권한을 가질 수 있다고 생각할 수 있지만, 이는 어디까지 platform(system)  과 signature 가 같을 때의 경우에만 해당된다.


다시한번 언급하자면,

모든 사용자가 구입한 폰은 각 Vendor 측의 고유 key로 signing 된, User mode 로 빌드 된 image 가 탑재 되었고, 각 모델별로 고유한 platform key 로 signing 되었기 때문에, 아무리 rooting 을 했건, 뭐를 했건간에, User mode 로 빌드 된 image의 key 를 바꿀 순 없다.

(Vendor 측에 아는 사람이 있어 debug 나, engineer 모드로 빌드된 image 를 구할 수 있다면??)


전체 이미지 빌드 타임에 system 과 data 영역에 들어갈 모든 APK는, 싹 다 각각 Android.mk에 명시된 key 값으로 signing 한다.


때문에, Signature 가 Platform과 맞지 않은 Settings application 을 폰에 탑재하는 순간, 아래와 같은 logcat msg 와 함께, 해당 apk 는 launcher 에서 볼 수 없을 것이다.(PackageManager 가 package loading 실패)

====================================================================

07-21 10:56:36.046: WARN/PackageManager(1981): Package com.test.application shared user changed from <nothing> to android.uid.system; replacing with new

07-21 10:56:36.054: WARN/PackageManager(1981): Signature mismatch for shared user : SharedUserSetting{462749e8 android.uid.system/1000}

07-21 10:56:36.054: ERROR/PackageManager(1981): Package com.test.application has no signatures that match those in shared user android.uid.system; ignoring!

 

====================================================================


또는,(아래는 살짝 다른 유형의 error 다.)

====================================================================

 


08-09 09:47:06.054: ERROR/PackageManager(1973): Package com.test.application signatures do not match the previously installed version; ignoring!

08-09 09:47:06.257: INFO/IQClient(2030): submitHW03 -  status- 2 level- 74

====================================================================

 


system 쪽과 signature mis-match 가 나서 이 어플을 loading 할 수 없다는 의미이다.


해결방법은 해당 어플도 똑같이 platform signing 을 해주는 방법밖에 없다.


그래야 system 권한을 얻을 수 있다는 의미이다.


하기와 같이 platform key 를 어디선가 구해와서 signing 해주면 된다.

(당연히 아래 platform key 가 당신의 device 에 flashing 되어 있는 image 의 signed platform key 와 동일해야 한다.)


Platform key 는 어디서 구할까...??? 해당 모델의 Vendor 만이 알 수 있다.


java -jar signapk.jar platform.x509.pem platform.pk8 Unsigned.apk Signed.apk


위 명령어로 생성된 Signed.apk 를 설치해주면 System 권한으로 internal api 사용이 가능하며 원하는 모든 기능이 정상 동작 될 것이다.

Posted by code cat


xref: /system/core/libmincrypt/sha.c

286void SHA_init(SHA_CTX* ctx) {
287    ctx->state[0] = 0x67452301;
288    ctx->state[1] = 0xEFCDAB89;
289    ctx->state[2] = 0x98BADCFE;
290    ctx->state[3] = 0x10325476;
291    ctx->state[4] = 0xC3D2E1F0;
292    ctx->count = 0;
293}


/system/core/include/mincrypt/sha.h

37typedef struct SHA_CTX {
38    uint64_t count;
39    uint32_t state[5];
40#if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
41    union {
42        uint8_t b[64];
43        uint32_t w[16];
44    } buf;
45#else
46    uint8_t buf[64];
47#endif
48} SHA_CTX;


xref: /system/core/libmincrypt/sha.c

147void SHA_update(SHA_CTX* ctx, const void* data, int len) {
148    int i = ctx->count % sizeof(ctx->buf);
149    const uint8_t* p = (const uint8_t*)data;
150
151    ctx->count += len;
152
153    while (len > sizeof(ctx->buf) - i) {
154        memcpy(&ctx->buf.b[i], p, sizeof(ctx->buf) - i);
155        len -= sizeof(ctx->buf) - i;
156        p += sizeof(ctx->buf) - i;
157        SHA1_Transform(ctx);
158        i = 0;
159    }
160
161    while (len--) {
162        ctx->buf.b[i++] = *p++;
163        if (i == sizeof(ctx->buf)) {
164            SHA1_Transform(ctx);
165            i = 0;
166        }
167    }
168}


Posted by code cat
Download Provider

Download Provider

Contents


High-level requirements

Requirements for Android

Req# Description Detailed Requirements Status
DLMGR_A_1 The Download Manager MUST satisfy the basic download needs of OTA Updates.   YES done in 1.0
DLMGR_A_2 The Download Manager MUST satisfy the basic download needs of Market.   YES done in 1.0
DLMGR_A_3 The Download Manager MUST satisfy the basic download needs of Gmail.   YES done in 1.0
DLMGR_A_4 The Download Manager MUST satisfy the basic download needs of the Browser.   YES done in 1.0
DLMGR_A_5 Downloads MUST happen and continue in the background, independently of the application in front.   YES done in 1.0
DLMGR_A_6 Downloads MUST be reliable even on unreliable network connections, within the capabilities of the underlying protocol and of the server.   YESne in 1.0
DLMGR_A_7 User-accessible files MUST be stored on the external storage card, and only files that are explicitly supported by an installed application must be downloaded.  /td> YES done in 1.0
DLMGR_A_8 OMA downloads SHOULD be supported.   NO deferred beyond Cupcake (not enough time)
DLMGR_A_9 The Download Manager SHOULD only download when using high-speed (3G/wifi) links, or only when not roaming (if that can be detected).   NO erred beyond Cupcake (not enough time)
DLMGR_A_10 Downloads SHOULD be user-controllable (if allowed by the initiating application), including pause, resume, and restart of failed downloads. 4001 NO

Requirements for Cupcake

Req# Description Detailed Requirements Status
DLMGR_C_1 The Download Manager SHOULD resume downloads where the socket got cleanly closed while the download was incomplete (common behavior on proxies and Google servers) 5 YES done in Cupcake
DLMGR_C_2 The Download Manager SHOULD retry/resume downloads instead of aborting when the server returns a HTTP code 503 2006 YES done in Cupcake
DLMGR_C_3 The Download Manager SHOULD randomize the retry delay 2007 YES done in Cupcake
DLMGR_C_4 The Download Manager SHOULD use the retry-after header (delta-seconds) for 503 responses 2008 YES done in Cupcake
DLMGR_C_5 The Download Manager MAY hide columns that aren't strictly necessary 1010 YES done in Cupcake
DLMGR_C_6 The Download Manager SHOULD allow the initiating app to pause an ongoing download 1011 YES done in Cupcake
DLMGR_C_7 The Download Manager SHOULD not display multiple notification icons for completed downloads. 4002 NO deferred beyond Cupcake (no precise cifications, need framework support)
DLMGR_C_8 The Download Manager SHOULD delete old files from /cache 3006 NO deferred beyond Cupcake (no precise specifications)
DLMGR_C_9 The Download Manager SHOULD handle redirects 2009 YES done in Cupcake

Requirements for Donut

Req# Description Detailed Requirements Status

Technology Evaluation

ALSO See also future directions for possible additional technical changes.

Possible scope for Cupcake

Because there is no testing environment in place in the 1.0 code, and because the schedule between 1.0 and Cupcake doesn't leave enough time to develop a testing environment solid enough to be able to test the database upgrades from 1.0 database scheme to a new scheme, any work done in Cupcake will have to work within the existing 1.0 database scheme.

Reducing the number of classes

Each class in the system has a measurable RAM cost (because of the associated Class objects), and therefore reducing the number of classes when possible or relevant can reduce the memory requirements. That being said, classes that extend system classes and are necessary for the operation of the download manager can't be removed.

Class Comments
com.android.providers.downloads.Constants Only contains constants, can be merged into another class.
com.android.providers.downloads.DownloadFileInfo Should be merged with DownloadInfo.
com.android.providers.downloads.DownloadInfo Once we only store information in RAM about downloads that are explicitly active, can be merged with DownloadThread/td>
com.android.providers.downloads.DownloadNotification Looks like it could be merged with DownloadProvider or DownloadService.
com.android.providers.downloads.DownloadNotification.NotificationItem Can probably be eliminated by using queries intelligently.
com.android.providers.downloads.DownloadProvider Extends ContentProvider, can't be eliminated.
com.android.providers.downloads.DownloadProvider.DatabaseHelper Can probably be eliminated by re-implementing by hand the logic of SQLiteOpenHelper.
com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper Can be eliminated once Cursor is read-only system-wide.
com.android.providers.downloads.DownloadReceiver Extends BroadcastReceiver, can't be eliminated.
com.android.providers.downloads.DownloadService Extends Service, unlikely that this can be eliminated. TBD.
com.android.providers.downloads.DownloadService.DownloadManagerContentObserver Extends ContentObserver, can be eliminated if the download manager can be re-hitected to not depend on ContentObserver any more.
com.android.providers.downloads.DownloadService.MediaScannerConnection Can probably be merged into another class.
com.android.providers.downloads.DownloadService.UpdateThread Can probably be made to implement Runnable instead and merged into another class, can be minated if the download manager can be re-architected to not depend on ContentObserver any more.
com.android.providers.downloads.DownloadThread Can probably be made to implement Runnable instead. Unclear whether this can be eliminated as we will probably d one object that represents an ongoing download (unless the entire state can be stored on the stack with primitive types, which is unlikely).
com.android.providers.downloads.Helpers Can't be instantiated, can be merged into another class.
com.android.providers.downloads.Helpers.Lexer Keeps state about an ongoing lex, can probably be merged into another class by making the lexer synchronized, ce the operation is short-lived.

Reducing the list of visible columns

Security in the download provider is primarily enforced with two separate mechanisms:

  • Column restrictions, such that only a small number of the download provider's columns can be read or queried by applications.
  • UID restrictions, such that only the application that initiated a download can access information about that download.

The first mechanism is expected to be fairly robust (the implementation is quite simple, based on projection maps, which are highly structured), but the second one relies on arbitrary strings (URIs and SQL fragments) passed by applications and is therefore at a higher risk of being compromised. Therefore, sensitive information stored in unrestricted columns (for which the first mechanism doesn't apply) is at a greater risk than other information.

Here's the list of columns that can currently be read/queried, with comments:

Column Notes
_ID Needs to be visible so that the app can uniquely identify downloads. No security concern: those numbers are sequential and aren't hard to guess.
_DATA Probably should not be visible to applications. WARNING Security concern: This holds filenames, including those of private files. While file permissions are posed to kick in and protect the files, hiding private filenames deeper in would probably be a reasonable idea.
MIMETYPE Needs to be visible so that app can display the icon matching the mime type. Intended to be visible by 3rd-party download UIs. TODO Security TBD before we lement support for 3rd-party UIs.
VISIBILITY Needs to be visible in case an app has both visible and invisible downloads. No obvious security concern.
DESTINATION Needs to be visible in case an app has multiple destinations and wants to distinguish between them. Also used internally by the download manager. No obvious urity concern.
STATUS Needs to be visible (1004). No obvious security concern.
LAST_MODIFICATION Needs to be visible, e.g. so that apps can sort downloads by date of last activity, or discard old downloads. No obvious security concern.
NOTIFICATION_PACKAGE Allows individual apps running under shared UIDs to identify their own downloads. No security concern: can be queried through package manager.
NOTIFICATION_CLASS See NOTIFICATION_PACKAGE.
TOTAL_BYTES Needs to be visible so that the app can display a progress bar. No obvious security concern. Intended to be visible by 3rd-party download UIs.
CURRENT_BYTES See TOTAL_BYTES.
TITLE Intended to be visible by 3rd-party download UIs. TODO Security and Privacy TBD before we implement support for 3rd-party UIs.
DESCRIPTION See TITLE.

Hiding the URI column

The URI column is visible to the initiating application, which is a mild security risk. It should be hidden, but the OTA update mechanism relies on it to check duplicate downloads and to play the download that's currently ongoing in the settings app. If another string column was exposed to the initiating applications, the OTA update mechanism could use that one, and URI could n be hidden. For Cupcake, without changing the database schema, the ENTITY column could be re-used as it's currently unused.

Handling redirects

There are two important aspects to handle redirects:

  • Storing the intermediate URIs in the provider.
  • Protecting against redirect loops.

If the URI column gets hidden, it could be used to store the intermediate URIs. After 1.0 the only available integer columns were METHOD and CONTROL. CONTROL was re-exposed to applications and can't be used. METHOD is slated to be re-used for 503 retry-after delays. It could be split into two halves, one for retry-after and one for the redirect nt. It would make more sense to count the redirect loop with FAILED_CONNECTIONS, but since there's already quite some code using it it'd take a bit more effort. Ideally handling of redirects ld be delayed until a future release, with a cleanup of the database schema (going along with the cleanup of the handling of filenames).

Because of the pattern used to read/write DownloadInfo and DownloadProvider, it's impractical to store multiple small integers into a large one. Therefore, since there are no eger columns left in the database, redirects will have to wait beyond Cupcake.

ContentProvider for download UI

In order to allow a UI that can "see" all the relevant downloads, there'll need to be a separate URI (or set of URIs) in the content provider: trying to use the exact same URIs for regular download control for UI purposes (distinguishing them based on the permissions of the caller) will break down if a same app (or actually a same UID) tries to do both. It'll also break down if the system process tries to do ular download activities, since it has all permissions.

Beyond that, there's little technical challenge: there are already mechanisms in place to restrict the list of columns that can be inserted, queried and updated (inserting of course makes no sense through UI channel), they just need to be duplicated for the case of the UI. The download provider also knows how to check the permissions of its caller, there isn't anything new here.

Getting rid of OTHER_UID

Right now OTHER_UID is used by checkin/update to allow the settings app to display the name of an ongoing OTA update, and by Market to allow the system to install the new apks. It is however a gerous feature, at least because it touches a part of the code that is critical to the download manager security (separation of applications).

Getting rid of OTHER_UID would be beneficial for the download manager, but the existing functionality has to be worked around. At this point, the idea that I consider the most likely would be have checkin and market implement =ContentProvider= wrappers around their downloads, and expose those content providers to whichever app they want, with whichever security mechanism they wish to have.

Only using SDK APIs.

It'd be good if the download manager could be built against the SDK as much as possible.

Here's the list of APIs as of Nov 5 2008 that aren't in the current public API but that are used by the download manager:

com.google.android.collect.Lists
android.drm.mobile1.DrmRawContent
android.media.IMediaScannerService
android.net.http.AndroidHttpClient
android.os.FileUtils
android.provider.DrmStore

Schedule

  • No future milestones currently defined

Detailed Requirements

<> Codes 3xx weren't considered relevant when those requirements were written

Req# History Status Feature Description Notes
1xxx N/A   API    
1001 1.0 YES   Download Manager API The download manager provides an API that allows applications to initiate downloads.  
1002 1.0 YES   Cookies The download manager API allows applications to pass cookies to the download manager.  
1003 1.0 YES   Security The download manager provides a mechanism that prevents arbitrary applications from accessing meta-data about rent and past downloads. In 1.0, known holes between apps
1004 1.0 YES   Status to Initiator The download manager allows the application initiating a download to query the status of that download.  
1005 1.0 YES   Cancel by Initiator The download manager allows the application initiating a download to cancel that download. &p;
1006 1.0 YES   Notify Initiator The download manager notifies the application initiating a download when the download completes (with cess or failure)  
1007 1.0 YES   Fire and Forget The download manager does not rely on the initiating application when saving the file to a shared filesystem a  
1008 1.0 YES   Short User-Readable Description The download manager allows the initiating application to optionally provide a user-readable rt description of the download  
1009 1.0 YES   Long User-Readable Description The download manager allows the initiating application to optionally provide a user-readable l description of the download  
1010 Cupcake YES Cupcake P3 YES Restrict column access The download provider doesn't allow access to columns that aren't strictly essary.  
1011 Cupcake YES Cupcake P2 YES Pause downloads The download provider allows the application initiating a download to pause that download.td>  
2xxx N/A   HTTP    
2001 1.0 YES   HTTP Support The download manager supports all the features of HTTP 1.1 (RFC 2616) that are relevant to file downloads.
2002 1.0 YES   Resume Downloads The download manager resumes downloads that get interrupted.  
2003 1.0 YES   Flaky Downloads The download manager has mechanism that prevent excessive retries of downloads that consistently fail or get errupted.  
2004 1.0 YES   Resume after Reboot The download manager resumes downloads across device reboots.  
2005 Cupcake YES Cupcake P2 YES Resume after socket closed The download manager resumes incomplete downloads after the socket gets anly closed This is necessary in order to reliably download through GFEs, though it pushes us further away from being able to download from servers that don't implement pipelining.
2006 Cupcake YES Cupcake P2 YES Resume after 503 The download manager resumes or retries downloads when the server returns an HTTP code  
2007 Cupcake YES Cupcake P2 YES Random retry delay The download manager uses partial randomness when retrying downloads on an exponential koff pattern.  
2008 Cupcake YES Cupcake P2 YES Retry-after delta-seconds The download manager uses the retry-after header in a 503 response to decide n to retry the request Handling of absolute dates will be covered by a separate requirement.
2009 Cupcake YES Cupcake P2 YES Redirects The download manager handles common redirects.  
25xx N/A   HTTP/Conditions    
3xxx N/A   Storage    
3001 1.0 YES   File Storage The download manager stores the results of downloads in persistent storage.  
3002 1.0 YES   Max File Size The download manager is able to handle large files (order of magnitude: 50MB)  
3003 1.0 YES   Destination File Permissions The download manager restricts access to the internal storage to applications with the ropriate permissions  
3004 1.0 YES   Initiator File Access The download manager allows the initiating application to access the destination file   d>
3005 1.0 YES   File Types The download manager does not save files that can't be displayed by any currently installed application sp;
3006 Cupcake NO Cupcake P3 NO Old Files in /cache The download manager deletes old files in /cache  
35xx N/A   Storage/Filename    
4xxx N/A   UI    
4001 1.0 NO   Download Manager UI The download manager provides a UI that lets user get information about current downloads and control them.td> Didn't get spec on time to be able to even consider it.
4002 Cupcake NO Cupcake P2 NO Single Notification Icon The download manager displays a single icon in the notification bar regardless of number of ongoing and completed downloads. No spec in Cupcake timeframe.
5xxx N/A   MIME    
52xx N/A   MIME/DRM    
5201 1.0 YES   DRM The download manager respects the DRM information it receives with the responses  
54xx N/A   MIME/OMA    
60xx N/A   Misc    
65xx N/A   Misc/Browser    

System Architecture

+----------------------+    +--------------------------------------+    +-------------+
|                      |    |                                      |    |             |
|   Download Manager   |    |  Browser / Gmail / Market / Updater  |    |  Viewer App |
|                      |    |                                      |    |             |
+----------------------+    +--------------------------------------+    +-------------+
    ^    |    ^                                             ^                ^
    |    |    |                                             |                |
    |    |    |                                             |                |
    |    |    |      +---------------------------+          |                |
    |    |    |      |                           |          |                |
    |    |    |      |                           |          |                |
    |    |    +-------- - - - - - - - - - - - - ------------+                |
    |    |           |                           |                           |
    |    |           |                           |                           |
    |    +------------- - - - - - - - - - - - - -----------------------------+
    |                |                           |
    |                |                           |
    +--------------->|     Android framework     |
                     |                           |
                     |                           |
                     +---------------------------+

          Application                        Download Manager                         Viewer App
               |                                     |                                     |
               |         initiate download           |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |        content provider URI         |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           query download            |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |              Cursor                 |                                     |
               |                                     |                                     |
               |       register ContentObserver      |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |     ContentObserver notification    |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |    intent "notification clicked"    |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |      intent "download complete"     |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |            intent "view"            |
               |                                     |------------------------------------>|
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           update download           |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |         open downloaded file        |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |         ParcelFileDescriptor        |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           delete download           |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               v                                     v                                     v

Internal Product Dependencies

  • [reverse dependency] GMail depends on the download manager to download attachments.
  • [reverse dependency] OTA Update depends on the download manager to download system images.
  • [reverse dependency] Vending Machine depends on the download manager to download content.
  • [reverse dependency] Browser depends on the download manager to download non-browsable.
  • Download Manager depends on a notification system to let the user know when downloads complete or otherwise require attention.
  • Download Manager depends on a mechanism to share files with another app (letting apps access its files, or accessing apps' files).
  • Download Manager depends on the ability to associate individual downloads with separate applications and to restrict apps to only access their own downloads.
  • Download Manager depends on an HTTP stack that predictably processes all relevant kinds of HTTP requests and responses.
  • Download Manager depends on a connectivity manager that reports accurate information about the status of the different data connections.

Interface Documentation

WARNING Since none of those APIs are public, they are all subject to change. If you're working in the Android source tree, do NOT use the explicit values, ONLY use the symbolic stants, unless you REALLY know what you're doing and are willing to deal with the consequences; you've been warned.

The various constants that are meant to be used by applications are all defined in the android.provider.Downloads class. Whenever possible, the constants should be used instead of the explicit ues.

Permissions

Constant name Permission name Access restrictions Description
Downloads.PERMISSION_ACCESS "android.permission.ACCESS_DOWNLOAD_MANAGER" Signature or System Applications that want to access the nload Manager MUST have this permission.
Downloads.PERMISSION_ACCESS_ADVANCED "android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" Signature or System This permission protects e legacy APIs that new applications SHOULD NOT use.
Downloads.PERMISSION_CACHE "android.permission.ACCESS_CACHE_FILESYSTEM" Signature This permission allows an app to access the /cache esystem, and is only needed by the Update code. Other applications SHOULD NOT use this permission
Downloads.PERMISSION_SEND_INTENTS "android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" Signature The download manager holds this mission, and the receivers through which applications get intents of completed downloads SHOULD require this permission from the sender

Content Provider

The primary interface that applications use to communicate with the download manager is exposed as a ContentProvider.

URIs

The URIs for the Content Provider are:

Constant name URI Description
Downloads.CONTENT_URI Uri.parse("content://downloads/download") The URI of the whole Content Provider, used to insert new rows, or to query all rows.td>
N/A ContentUris.withAppendedId(CONTENT_URI, <id>) The URI of an individual download. <id> is the value of the Download._ID umn

Columns

The following columns are available:

Constant name Column name SQL Type Access Description Notes
Downloads._ID "_id" Integer Read   Inherited from BaseColumns._ID.
Downloads.URI "uri" Text Init   Used to be readable.
Downloads.APP_DATA "entity" Text Init/Read/Modify   Actual column name will change in the future.
Downloads.NO_INTEGRITY "no_integrity" Boolean Init   Used to be readable.
Downloads.FILENAME_HINT "hint" Text Init   Used to be readable.
Downloads._DATA "_data" Text Read   Used to be Downloads.FILENAME and "filename".
Downloads.MIMETYPE "mimetype" Text Init/Read    
Downloads.DESTINATION "destination" Integer Init   See Destination codes for ails of legal values. Used to be readable.
Downloads.VISIBILITY "visibility" Integer Init/Read/Modify   See Visibility codes
Downloads.CONTROL "control" Integer Init/Read/Modify   See Control codes for ails of legal values.
Downloads.STATUS "status" Integer Read   See Status codes for details of possible ues.
Downloads.LAST_MODIFICATION "lastmod" Bigint Read    
Downloads.NOTIFICATION_PACKAGE "notificationpackage" Text Init/Read    
Downloads.NOTIFICATION_CLASS "notificationclass" Text Init/Read    
Downloads.NOTIFICATION_EXTRAS "notificationextras" Text Init    
Downloads.COOKIE_DATA "cookiedata" Text Init    
Downloads.USER_AGENT "useragent" Text Init    
Downloads.REFERER "referer" Text Init    
Downloads.TOTAL_BYTES "total_bytes" Integer Read   Might gain Init access in the future.
Downloads.CURRENT_BYTES "current_bytes" Integer Read    
Downloads.OTHER_UID "otheruid" Integer Init   Requires the android.permission.ESS_DOWNLOAD_MANAGER_ADVANCED permission. Used to be readable and writable. Might disappear entirely if possible.
Downloads.TITLE "title" String Init/Read/Modify    
Downloads.DESCRIPTION "description" String Init/Read/Modify    

Destination Values

Constants name Constant value Description Notes
Downloads.DESTINATION_EXTERNAL 0 Saves the file to the SD card. Default value. Fails is SD card is not present.
Downloads.DESTINATION_CACHE_PARTITION 1 Saves the file to the internal cache partition. Requires the "android.permission.ESS_DOWNLOAD_MANAGER_ADVANCED" permission
Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE 2 Saves the file to the internal cache partition. The download can get deleted at any e by the download manager when it needs space.

Visibility Values

Constants name Constant value Description Notes
Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED 0 The download is visible in download UIs, and it shows up in the notification area during download after completion. Default value for external downloads.
Downloads.VISIBILITY_VISIBLE 1 The download is visible in download UIs, and it shows up in the notification area during download but not after pletion.  
Downloads.VISIBILITY_HIDDEN 2 The download is hidden from download UIs and doesn't show up in the notification area. Default value for ernal downloads.

Control Values

Constants name Constant value Description Notes
Downloads.CONTROL_RUN 0 The download is allowed to run. Default value.
Downloads.CONTROL_PAUSED 0 The download is paused.  

Status Values

<> <>

Constants name Constant value Description Notes
Downloads.STATUS_PENDING 190 Download hasn't started.  
Downloads.STATUS_PENDING_PAUSED 191 Download hasn't started and can't start immediately (network unavailable, paused by user). Not rently used.
Downloads.STATUS_RUNNING 192 Download has started and is running  
Downloads.STATUS_RUNNING_PAUSED 193 Download has started, but can't run at the moment (network unavailable, paused by user).  
Downloads.STATUS_SUCCESS 200 Download completed successfully.  
Downloads.STATUS_BAD_REQUEST 400 Couldn't initiate the request, or server response 400.  
Downloads.STATUS_NOT_ACCEPTABLE 406 No handler to view the file (external downloads), or server response 406. External downloads are nt to be user-visible, and are aborted if there's no application to handle the relevant MIME type.
Downloads.STATUS_LENGTH_REQUIRED 411 The download manager can't know the length of the download. Because of the unreliability of cell works, the download manager only performs downloads when it can verify that it has received all the data for a download, except if the initiating app sets the Downloads.NO_INTEGRITY flag.
Downloads.STATUS_PRECONDITION_FAILED 412 The download manager can't resume an interrupted download, because it didn't receive enough information m the server to be able to resume.  
Downloads.STATUS_CANCELED 490 The download was canceled by a cause outside the Download Manager. Formerly known as Downloads.TUS_CANCELLED. Might be impossible to observe in 1.0.
Downloads.STATUS_UNKNOWN_ERROR 491 The download was aborted because of an unknown error. Formerly known as Downloads.STATUS_ERROR. Typically the result of a runtime exception that is not explicitly handled.
Downloads.STATUS_FILE_ERROR 492 The download was aborted because the data couldn't be saved. Most commonly happens when the filesystem is l.
Downloads.STATUS_UNHANDLED_REDIRECT 493 The download was aborted because the server returned a redirect code 3xx that the download manager doesn't dle. The download manager currently handles 301, 302 and 307.
Downloads.STATUS_TOO_MANY_REDIRECTS 494 The download was aborted because the download manager received too many redirects while trying to find the ual file.  
Downloads.STATUS_UNHANDLED_HTTP_CODE 495 The download was aborted because the server returned a status code that the download manager doesn't handle.td> Standard codes 3xx, 4xx and 5xx don't trigger this.
Downloads.STATUS_HTTP_DATA_ERROR 496 The download was aborted because of an unrecoverable error trying to get data over the network. ically this happens when the download manager received several I/O Exceptions in a row while the network is available and without being able to download any data.
  4xx standard HTTP/1.1 4xx codes returned by the server are used as-is. Don't rely on non-standard values being passed through, especially the her values that would collide with download manager codes.
  5xx standard HTTP/1.1 5xx codes returned by the server are used as-is. Don't rely on non-standard values being passed through.

Status Helper Functions

Function signature Description
public static boolean Downloads.isStatusInformational(int status) Returns whether the status code matches a download that hasn't completed yet.
public static boolean Downloads.isStatusSuspended(int status) Returns whether the download hasn't completed yet but isn't currently making progress.
public static boolean Downloads.isStatusSuccess(int status) Returns whether the download successfully completed.
public static boolean Downloads.isStatusError(int status) Returns whether the download failed.
public static boolean Downloads.isStatusClientError(int status) Returns whether the download failed because of a client error.
public static boolean Downloads.isStatusServerError(int status) Returns whether the download failed because of a server error.
public static boolean Downloads.isStatusCompleted(int status) Returns whether the download completed (with no distinction between success and failure).

Intents

The download manager sends an intent broadcast Downloads.DOWNLOAD_COMPLETED_ACTION when a download completes.

The download manager sends an intent broadcast Downloads.NOTIFICATION_CLICKED_ACTION when the user clicks a download notification that doesn't match a download that can be opened (e.g. because notification is for several downloads at a time, or because it's for an incomplete download, or because it's for a private download).

The download manager starts an activity with Intent.ACTION_VIEW when the user clicks a download notification that matches a download that can be opened.

Differences between 1.0 and Cupcake

WARNING These are the differences for apps built from source in the Android source tree, i.e. they don't cover any of the cases of binary incompatibility.

  • Cursors returned by the content provider are now read-only.
  • SQL "where" statements are now verified and must follow a rigid syntax (the most visible aspect being that all parameters much be single-quoted).
  • Columns and constants that were unused or ineffective are gone: METHOD, NO_SYSTEM_FILES, DESTINATION_DATA_CACHE, OTA_UPDATE.
  • OTHER_UID and DESTINATION_CACHE_PARTITION require android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED.
  • The list of columns that can be queried and read is limited: _ID, APP_DATA, _DATA, MIMETYPE, CONTROL, STATUS, T_MODIFICATION, NOTIFICATION_PACKAGE, NOTIFICATION_CLASS, TOTAL_BYTES, CURRENT_BYTES, TITLE, DESCRIPTION.
  • The list of columns that can be initialized by apps is limited: URI, APP_DATA, NO_INTEGRITY, FILENAME_HINT, MIMETYPE, DESTINATION, VISIBILITY, CONTROL, STATUS, LAST_MODIFICATION, NOTIFICATION_PACKAGE, NOTIFICATION_CLASS, NOTIFICATION_EXTRAS, COOKIE_DATA, USER_AGENT, REFERER, OTHER_UID, TITLE, DESCRIPTION.
  • The list of columns that can be updated by apps is limited: APP_DATA, VISIBILITY, CONTROL, TITLE, DESCRIPTION.
  • Downloads to the SD card default to have notifications that are visible after completion, internal downloads default to notifications that are always hidden.
  • The constant FILENAME was renamed Downloads._DATA.
  • Downloads can be paused and resumed by writing to the CONTROL column. Downloads.CONTROL_RUN (Default) makes the download go, Downloads.CONTROL_PAUSED pauses it.
  • New column APP_DATA that is untouched by the download manager, used to store app-specific info about the downloads.
  • Minor differences unlikely to affect applications:
    • The notification class/package must now match the UID.
    • The backdoor to see the entire provider (which was intended to implement UIs) is gone.
    • Private column names were removed from the public API.

Writing code that works on both the 1.0 and Cupcake versions of the download manager.

If you're not 100% sure that you need to be reading this chapter, don't read it.

Basic rule: don't use features that only exist in one of the two implementations (POST, downloads to /data...).

Also, don't use columns in 1.0 that are protected or hidden in Cupcake.

Unfortunately, that's not always entirely possible.

Areas of concern:

  • Some columns were renamed. FILENAME became Downloads._DATA ("_data"), and ENTITY became APP_DATA ("entity").
    • The difference can be used to distinguish between 1.0 and Cupcake, though reflection.
    • The difference prevents from using any of the symbolic constants directly in source code: if the same binary wants to run on both 1.0 and Cupcake, it will have to hard-code the values of those stants or use reflection to get to them.
  • URI column accessible in 1.0 but protected in Cupcake. Code that relies on being able to re-read its URI should be using APP_DATA in Cupcake, but that column doesn't exist as such in 1.0.
    • If the code detects that it's running on Cupcake, write URI to APP_DATA in addition to URI, and query and read from the appropriate column.
    • Since the underlying column for APP_DATA exists in both 1.0 and Cupcake even though it has different names, it can actually be used in both cases (see note above about renamed columns).
  • Some of the error codes have been renumbered. STATUS_UNHANDLED_HTTP_CODE and STATUS_HTTP_DATA_ERROR were bumped up from 494 and 495 to 495 and 496.
  • Backward compatibility is not guaranteed: the download manager APIs weren't meant to be backward compatible yet. As such it's impossible to guarantee that code that uses the Cupcake download manager l be binary-compatible with future versions.
    • I intend to eventually change the column name for APP_DATA to "app_data". Because of that, code should use reflection to get to that name instead of hard-coding "entity", so that it always gets the ht value for the string.
    • I intend to refine the handling of filenames and content URIs, exposing separate columns for situations where a download can be accessed both as a file and through a content URI (e.g. stuff that is ognized by the media scanner). Unfortunately at this point this feature isn't clear in my mind. I'd recommend using reflection to look for the Downloads._DATA column, and if it isn't there to look for the ENAME column (which has the advantage of also dealing with the difference between 1.0 and Cupcake).
    • I intend to renumber the error codes, especially those in the 4xx range, and especially those below 490 (which overlap with standard HTTP error codes but will probably be separated). Reflection would rove the probability to getting to them in the future. Unfortunately, the names of the constants are likely to change in the process, in order to disambiguate codes coming from HTTP from those generated ally. I might try to stick to the following pattern: where a constant is currently named STATUS_XXX, its locally-generated version in the future might be named STATUS_LOCAL_XXX while the current constant e might disappear. Using reflection to try to get to the possible new name instead of using the old name might improve the probability of compatibility in the future. That being said, it is critically ortant to properly handle the full ranges or error codes, especially the 4xx range, as "expected" errors, and it is far preferable to not try to distinguish between those codes at all: use the functions nloads.isError and Downloads.isClientError to easily recognize those entire ranges. In order of probability, the 1xx range is the second most likely to be affected.

Functional Specification

All the details about what the product does.

TODO

Release notes for Cupcake

  • HTTP codes 301, 302, 303 and 307 (redirects) are now supported
  • HTTP code 503 is now handled, with support for retry-after in delay-seconds
  • Downloads that were cleanly interrupted are now resumed instead of failing
  • Applications can now pause their downloads
  • Retry delays are now randomized
  • Connectivity is now checked on all interfaces
  • Downloads with invalid characters in file name can now be saved
  • Various security fixes
  • Minor API changes (see API differences between 1.0 and Cupcake)

Product Architecture

How the tasks are split between the different modules that implement the product/feature.

TODO To be completed

Class
com.android.providers.downloads.Constants
com.android.providers.downloads.DownloadFileInfo
com.android.providers.downloads.DownloadInfo
com.android.providers.downloads.DownloadNotification
com.android.providers.downloads.DownloadNotification.NotificationItem
com.android.providers.downloads.DownloadProvider extends ContentProvider
com.android.providers.downloads.DownloadProvider.DatabaseHelper extends SQLiteOpenHelper
com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper extends CursorWrapper implements CrossProcessCursor
com.android.providers.downloads.DownloadReceiver extends BroadcastReceiver
com.android.providers.downloads.DownloadService extends Service
com.android.providers.downloads.DownloadService.DownloadManagerContentObserver extends ContentObserver
com.android.providers.downloads.DownloadService.MediaScannerConnection implements ServiceConnection
com.android.providers.downloads.DownloadService.UpdateThread extends Thread
com.android.providers.downloads.DownloadThread extends Thread
com.android.providers.downloads.Helpers
com.android.providers.downloads.Helpers.Lexer

The download manager is built primarily around a ContentProvider and a Service. The ContentProvider part is the front end, i.e. applications communicate with the download manager through the provider. The vice part is the back end, which contains the actual download logic, running as a background process.

As a first approach, the provider is essentially a canonical provider backed by a SQLite3 database. The biggest difference between the download provider and a "plain" provider is that the download provider ressively validates its inputs, for security reasons.

The service is a background process that performs the actual downloads as requested by the applications. The service doesn't offer any bindable interface, the service object exists strictly so that the tem knows how to prioritize the download manager's process against other processes when memory is tight.

Communication between the provider and the service is done through public Android APIs, so that the two components are deeply decoupled (they could in fact run in different processes). The download manager rts the service whenever a change is made that can start or restart a download. The service observes and queries the provider for changes, and updates the provider as the download progresses.

There are a few secondary classes that provide auxiliary functions.

A Receiver listens to several broadcasts. Is receives some system broadcasts when the system boots (so that the download manager can resume downloads that were interrupted when the system was turned off) or n the connectivity changes (so that the download manager can restart downloads that were interrupted when connectivity was lost). It also receives intents when the user selects a download notification.

Finally, some helper classes provide support functions.

Most significantly, DownloadThread is responsible for performing the actual downloads as part of the DownloadService's functionality, while UpdateThread is responsible for updating the DownloadInfo whenever DownloadProvider data changes.

DownloadInfo and DownloadFileInfo hold pure data structures, with little or no actual logic.

Lexer takes care of validating the snippets of SQL data that are received from applications, to avoid cases of SQL injection.

The service keeps a copy of the provider data in RAM, so that it can determine what changed in the provider when it receives a change notification through the ContentObserver. That data is kept in an array DownloadInfo structures.

Each DownloadThread performs the operations for a single download (or, more precisely, for a single HTTP transaction). Each DownloadThread is backed by a DownloadInfo object. which is in fact on of the ects kept by the DownloadService. While a download is running, the DownloadService can influence the download by writing data into the relevant DownloadInfo object, and the DownloadThread checks that object appropriate times during the download.

Because the DownloadService updates the DownloadInfo objects asynchronously from everything else (it uses a dedicated thread for that purpose), a lot of care has to be taken when upgrading the DownloadInfo ect. In fact, only the DownloadService's updateThread function can update that object, and it should be considered read-only to every other bit of code. Even within the updateThread function, some care must taken to ensure that the DownloadInfos don't get out of sync with the provider.

On the other hand, the DownloadService's updateThread function does upgrade the DownloadInfo when it spawns new DownloadThreads (and in a few more circumstances), and when it does that it must also update DownloadProvider (or risk seeing its DownloadInfo data get overwritten).

Because of all that, all code outside of the DowloadService's updateThread must neither read from DownloadProvider nor write to the DownloadInfo objects under any circumstances. The DownloadService's ateFunction is responsible for copying data from the DownloadProvider to the DownloadInfo objects, and must ensure that the DownloadProvider remains in sync with the information it writes into the nloadInfo objects.

Implementation Documentation

How individual modules are implemented.

TODO To be completed

Database formats

Android 1.0, format version 31, table downloads:

Column Type
"_id" INTEGER PRIMARY KEY AUTOINCREMENT
"uri" TEXT
"method" INTEGER
"entity" TEXT
"no_integrity" BOOLEAN
"hint" TEXT
"otaupdate" BOOLEAN
"_data" TEXT
"mimetype" TEXT
"destination" INTEGER
"no_system" BOOLEAN
"visibility" INTEGER
"control" INTEGER
"status" INTEGER
"numfailed" INTEGER
"lastmod" BIGINT
"notificationpackage" TEXT
"notificationclass" TEXT
"notificationextras" TEXT
"cookiedata" TEXT
"useragent" TEXT
"referer" TEXT
"total_bytes" INTEGER
"current_bytes" INTEGER
"etag" TEXT
"uid" INTEGER
"otheruid" INTEGER
"title" TEXT
"description" TEXT
"scanned" BOOLEAN

Cupcake, format version 100: Same as format version 31.

Future Directions

WARNING This section is for informative purposes only.

API

  • Expose Download Manager to 3rd party apps - security, robustness .
  • Validate application-provided user agent, cookies, etc... to protect against e.g. header injection.
  • Allow trust-and-verify MIME type.
  • Extract response string from HTTP responses, extract entity from failed responses
  • If app fails to be notified, retry later - don't give up because of a single failure.
  • Support data: URIs .
  • Download files to app-provided content provider .
  • Don't pass HTTP codes above about 490 to the initiating app (figure out what the threshold should be).
  • Allow initiating app to specify that it wants wifi-only downloads .
  • Provide SQL "where" clauses for the different categories of status codes, matching isStatusXXX().
  • There has to be a mechanism by which old downloads are automatically purged from /cache if there's not enough space .
  • Clicking the notification for a completed download should go through the initiating app instead of directly opening the file (but what about fire and forget?).
  • Clean up the difference between pending_network (waiting for network) and running_paused (user paused the download).
  • Allow any app to access the downloaded file (but no other column) of user-downloaded files .
  • Provider should return more errors and throw fewer exceptions if possible.
  • Add option to auto-dismiss notifications for completed downloads after a given time .
  • Delete filename in case of failure - better handle the separation between filename and content URI.
  • Allow the initiating application to specify that it wants to restart downloads from scratch if they're interrupted and can't be resumed .
  • Save images directly from browser without re-downloading data .
  • Give applications the ability to explicitly specify the full target filename (not just a hint).
  • Give applications the ability to download multiple files into multiple set locations as if they were a single "package".
  • Give applications the ability to download files only if they haven't been already downloaded.
  • Give applications the ability to download files that have already been downloaded only if there's a newer version.
  • Set-cookie in the response.
  • basic auth .
  • app-provided prompts for basic auth, ssl, redirects.
  • File should be hidden from initiating application when DRM.
  • Delay writing of app-visible filename column until file visible (split user-visible vs private files?).
  • Separate locally-generated status codes from standard HTTP codes.
  • Allow app to specify it doesn't want to resume downloads across reboots (because they might require additional work).
  • Allow app to prioritize user-initiated downloads.
  • Allow app to specify length of download (full trust, or trust-and-verify).
  • Support POST.
  • Support PUT.
  • Support plugins for additional protocols and download descriptors.
  • Rename columns to have an appropriate COLUMN_ prefix.

HTTP Handling

  • Fail download immediately on authoritative unresolved hostnames .
  • The download manager should use the browser's user-agent by default.
  • Redirect with HTTP Refresh headers (download current and target content with non-zero refresh).
  • Handle content-encoding header.
  • Handle transfer-encoding header.
  • Find other ways to validate interrupted downloads (signature, last-mod/if-modified-since) .
  • Make downloads time out in case of long time with no activity.

File names

  • Protect against situations where there's already a "downloads" directory on SD card.
  • Deal with filenames with invalid characters.
  • Refine the logic that builds filenames to better match desktop browsers - drop the query string.
  • URI-decode filenames generated from URIs.
  • Better deal with filenames that end in '.'.
  • Deal with URIs that end in '/' or '?'.
  • Investigate how to better deal with filenames that have multiple extensions.

UI

  • Prompt for redirects across domains or cancel.
  • Prompt for redirects from SSL or cancel.
  • Prompt for basic auth or cancel.
  • Prompt for SSL with untrusted/invalid/expired certificates or cancel.
  • Reduce number of icons in the title bar, possibly as low as 1 (animated if there are ongoing downloads, fixed if all downloads have completed) .
  • UI to cancel visible downloads.
  • UI to pause visible downloads.
  • Reorder downloads.
  • View SSL certificates.
  • Indicate secure downloads.

Handling of specific MIME types

  • Parse HTML for redirects with meta tag.
  • Handle charsets and transcoding of text files.
  • Deal with multiparts.
  • Support OMA downloads with DD and data in same multipart, i.e. combined delivery.
  • Assume application/octet-stream for http responses with no mime type.
  • Download anything if an app supports application/octet-stream.
  • Download any text/* if an application supports text/plain.
  • Should the media scanner be invoked on DRM downloads?
  • Refresh header with timer should be followed if content is not downloadable.
  • Support OMA downloads.
  • Support MIDP-OTA downloads.
  • Support Sprint MCD downloads.
  • Sniff content when receiving MIME-types known to be inaccurately sent by misconfigured servers.

Management of downloads based on environment

  • If the device routinely connects over wifi, delay non-interactive downloads by a certain amount of time in case wifi becomes available
  • Turn on wifi if possible
  • Fall back to cell when wifi is available but download doesn't proceed
  • Be smarter about spurious losses (i.e. exceptions while network appears up) when the active network changes (e.g. turn on wifi while downloading over cell).
  • Investigate the use of wifi locks, especially when performing non-resumable downloads.
  • Poll network state (and maybe even try to connect) even without notifications from the connectivity manager (in case the notifications go AWOL or get inconsistent) .
  • Pause when conditions degrade .
  • Pause when roaming.
  • Throttle or pause when user is active.
  • Pause on slow networks (2G).
  • Pause when battery is low.
  • Throttle to not overwhelm the link.
  • Pause when sync is active.
  • Deal with situations where the active connection is down but there's another connection available
  • Download files at night when the user is not explicitly waiting.

Management of simultaneous downloads

  • Pipeline requests on limited number of sockets, run downloads sequentially .
  • Manage bandwidth to not starve foreground tasks.
  • Run unsized downloads on their own (on a per-filesystem basis) to avoid failing multiple of them because of a full filesystem .

Minor functional changes, edge cases

  • The database could be somewhat checked when it's opened.
  • [DownloadProvider.java] When upgrading the database, the numbering of ids should restart where it left off.
  • [DownloadProvider.java] Handle errors when failing to start the service.
  • [DownloadProvider.java] Explicitly populate all database columns that have documented default values, investigate whether that can be done at the SQL level.
  • [DownloadProvider.java] It's possible that the last update time should be updated by the Sevice logic, not by the content provider.
  • When relevant, combine logged messages on fewer lines.
  • [DownloadService.java] Trim the database in the provider, not in the service. Notify application when trimming. Investigate why the row count seems off by one. Enforce on an ongoing basis.
  • [DownloadThread.java] When download is restarted and MIME type wasn't provided by app, don't re-use MIME type.
  • [DownloadThread.java] Deal with mistmatched file data sizes (between database and filesystem) when resuming a download, or with missing files that should be here.
  • [DownloadThread.java] Validate that the response content-length can be properly parsed (i.e. presence of a string doesn't guarantee correctness).
  • [DownloadThread.java] Be finer-grained with the way file permissions are managed in /cache - don't 0644 everything .
  • Truncate files before deleting them, in case they're still open cross-process.
  • Restart from scratch downloads that had very little progress .
  • Deal with situations where /data is full as it prevents the database from growing (DoS) .
  • Wait until file scanned to notify that download is completed.
  • Missing some detailed logging about IOExceptions.
  • Allow to disable LOGD debugging independently from system setting.
  • Pulling the battery during a download corrupts files (lots of zeros written) .
  • Should keep a bit of "emergency" database storage to initiate the download of an OTA update, in a file that is pre-allocated whenever possible (how to know it's an OTA update?).
  • Figure out how to hook up into dumpsys and event log.
  • Use the event log to log download progress.
  • Use /cache to stage downloads that eventually go to the sd card, to avoid having sd files open too long in case the use pulls the card and to avoid partial files for too long.
  • Maintain per-application usage statistics.
  • There might be corner cases where the notifications are slightly off because different notifications might be using PendingIntents that can't be distinguished (differing only by their extras).

Architecture and Implementation

  • The content:// Uri of individual downloads could be cached instead of being re-built whenever it's needed.
  • [DownloadProvider.java] Solidify extraction of id from URI
  • [DownloadProvider.java] Use ContentURIs.parseId(uri) to extra the id from various functions.
  • [DownloadProvider.java] Use StringBuilder to build the various queries.
  • [DownloadService.java] Cache interface to the media scanner service more aggressively.
  • [DownloadService.java] Investigate why unbinding from the media scanner service sometimes throws an exception
  • [DownloadService.java] Handle exceptions in the service's UpdateThread - mark that there's no thread left.
  • [DownloadService.java] At the end of UpdateThread, closing the cursor should be done even if there's an exception. Also log the exception, as we'd be in an inconsistent state.
  • [DownloadProvider.java] Investigate whether the download provider should aggressively cache the result of getContext() and getContext().getContentResolver()
  • Document the database columns that are most likely to stay unchanged throughout versions, to increase the chance being able to perform downgrades.
  • [DownloadService.java] Sanity-check the ordering of the local cache when adding/removing rows.
  • [DownloadService.java] Factor the code that checks for DRM types into a separate function.
  • [DownloadService.java] Factor the code that notifies applications into a separate function (codepath with early 406 failure)
  • [DownloadService.java] Check for errors when spawning download threads.
  • [DownloadService.java] Potential race condition when a download completes at the same time as it gets deleted through the content provider - see deleteDownload().
  • [DownloadService.java] Catch all exceptions in scanFile - don't trust a remote process to the point where we'd let it crash us.
  • [DownloadService.java] Limit number of attempts to scan a file.
  • [DownloadService.java] Keep less data in RAM, especially about completed downloads. Investigating cutting unused columns if necessary
  • [DownloadThread.java] Don't let exceptions out of run() - that'd kill the service, which'd accomplish no good.
  • [DownloadThread.java] Keep track of content-length responses in a long, not in a string that we keep parsing .
  • [DownloadThread.java] Use variable-size buffer to avoid thousands of operations on large downloads
  • [Helpers.java] Deal with atomicity of checking/creating file.
  • [Helpers.java] Handle differences between content-location separators and filesystem separators.
  • Optimize database queries: use projections to reduce number of columns and get constant column numbers.
  • Index last-mod date in DB, because of ordered searches. Investigate whether other columns need to be indexed (Hidden?)
  • Deal with the fact that sqlite INTEGER matches java long (63-bit) .
  • Use a single HTTP client for the entire download manager.
  • Could use fewer alarms - currently setting new alarm each time database updated .
  • Obsolete columns should be removed from the database .
  • Assign relevant names to threads.
  • Investigate and handle the different subclasses of IOException appropriately .
  • There's potentially a race condition around read-modify-write cycles in the database, between the Service's updateFromProvider thread and the worker threads (and possibly more). Those should be hronized appropriately, and the provider should be hardened to prevent asynchronous changes to sensitive data (or to synchronize when there's no other way, though I'd rather avoid that) .
  • Temporary file leaks when downloads are deleted while the service isn't running .
  • Increase priority of updaterThread while in the critical section (to avoid issues of priority inheritance with the main thread).
  • Explicitly specify which interface to use for a given download (to get better sync with the connection manager).
  • Cancel the requests on more kinds of errors instead of trusting the garbage collector.
  • Issues with the fact that parseInt can throw exceptions on invalid server headers.

Code style, refactoring

  • Fix lint warnings
  • Make sure that comments fit in 80 columns to match style guide
  • Unify code style when dealing with lines longer than 100 characters
  • [Constants.java] constants should be organized by logical groups to improve readability.
  • Use fewer internal classes (Helpers, Constants...) .

Browser changes

  • Move download UI outside of browser, so that browser doesn't need to access the provider.
  • Make browser sniff zips and jars to see if they're apks.
  • Live handoff of browser-initiated downloads (download in browser, browser update download UI, hand over to download manager on retry).
Posted by code cat

Bionic 이 지원안하고 안 할려는 리스트:

 

1. C++ exception (크고 느린 코드 생성원인)

2. pthread cancellation (C 코드 복잡성만 증가. 이런거에 의존하지 말고 multi-thread 코딩을 잘해라)

3. init 함수가 던진 c++ exception에 대한 pthread_once()지원 안함  (init함수가 하는 fork()도)

4. locale & wide characters (i18n을 위해 대신 ICU)

5. 유저 계정 관련 함수(getpwd 등)

Posted by code cat

a port of BSD C library to linux kernel with the following additions/changes:

- no support fo locales

- no support for wide chars (multi-byte characters)

- own smallish implementation of pthreads based on Linux futexes

- support fo x86, ARM, and ARM thumb cpu instruction sets and kernel interfaces

 

To add new syscalls:

Bionic provides the 'gensyscalls.py' script to automatically generate syscall stubs from the list defined in the file 'SYSCALLS.TXT'

To add a new syscall:

1. SYSCALL.TXT 수정

2. syscall에 대한 새로운 라인 추가:

return_type syscall_name(parameters)    syscall_number

3. syscall function과 entry name을 구분 짓고 싶으면:

return_type funcname:syscall_name(parameters)    syscall_number

4. 추가로 syscall number가 ARM 과 x86끼리 다르면,

return_type funcname[:syscall_name](parameters)    arm_number, x86_number

5. 플랫폼에 syscall이 구현안되었다고 나타내려면 -1을 사용하라.

void __set_tls(void*)    arm_number, -1     (예제)

 

자세한 내용은 SYSCALLS.TXT를 참조하고, 'checksyscalls.py'를 사용하여, syscall number를 제대로 사용하였는지 체크할 수도 있다.(리눅스 커널 헤더들안의 숫자와 비교한다.)

'안드로이드 > 프레임워크' 카테고리의 다른 글

[Android] OTA DownloadProvider document  (0) 2013.10.18
Bionic libc (3)  (0) 2012.11.08
Bionic libc  (0) 2012.10.30
안드로이드 init.rc (oom_adj값)  (0) 2012.09.27
Dalvik Virtual Machine 와 odex  (0) 2012.08.13
Posted by code cat

출처: wikipeida (http://en.wikipedia.org/wiki/Bionic_(software))

The Bionic libc is a derivation of the BSD standard C library code that was originally developed by Google for the Android embedded system operating system. Bionic has several major Linux-specific features and development continues independent of other code bases. The publicly-stated goals for Bionic are:[1][2]
BSD license: Android uses a Linux kernel which is under the GNU General Public License (GPL), but Google wished to isolate Android applications from the effects of the GPL.
Small size: Bionic is much smaller than GNU libc (glibc) and somewhat smaller than uClibc.
Speed: Bionic is designed for CPUs at relatively low clock frequencies.

Bionic lacks many features found in full libc implementations, such as wide character and C++ exception handling support.[1][3][4] Also some functions defined in the Bionic header are still unimplemented, which may trigger unexpected behavior in some cases.[1][4][5][6]

As of Android Jelly Bean (4.1), bionic does not include support for Glibc's FORTIFY_SOURCE. FORTIFY_SOURCE is a feature where unsafe string and memory functions (such as strcpy, strcat, and memcpy) are replaced with safer variants (similar to BSD's lstrcpy and lstrcat) when buffer sizes can be determined at compile time. Future releases of the library plan to support -DFORTIFY_SOURCE=1.[7]

The recommended way of directly using and extending Bionic is with the Android Native Development Kit.

'안드로이드 > 프레임워크' 카테고리의 다른 글

Bionic libc (3)  (0) 2012.11.08
Bionic libc (2)  (0) 2012.11.08
안드로이드 init.rc (oom_adj값)  (0) 2012.09.27
Dalvik Virtual Machine 와 odex  (0) 2012.08.13
안드로이드 바인더 ipc  (0) 2011.10.23
Posted by code cat

init.rc에 oom_adj 값에 대해 찾아보았다.



여기에 사용되는 값은 막 지어낸 게 아니라  linux/include/linux/oom.h 를 참조하는 값이며, 다음과 같이 되어 있다. 


#define OOM_DISABLE     (-17) /* inclusive */

#define OOM_ADJUST_MIN  (-16) 

#define OOM_ADJUST_MAX 15



init 프로세스와 그 child들은 init.rc에서 다음과 같이 정의 되어 있다.

write /proc/1/oom_adj -16

oom_adj를 따로 설정 안 해주는 service들이나 'exec'로 실행되는 바이너리들은 -16을 갖는다.

'안드로이드 > 프레임워크' 카테고리의 다른 글

Bionic libc (2)  (0) 2012.11.08
Bionic libc  (0) 2012.10.30
Dalvik Virtual Machine 와 odex  (0) 2012.08.13
안드로이드 바인더 ipc  (0) 2011.10.23
안드로이드 프레임워크 *Java에서 전처리기 사용하기*  (0) 2011.06.06
Posted by code cat

출처: Android Open Source code, dalvik/doc/dexopt.hml

Dalvik Optimization and Verification


Dalvik Optimization and Verification with dexopt


  • class 데이터(특히 bytecode), 는 시스템 메모리 사용을 최소화하기 위해 프로세스간 공유함
  • app을 띄울때 응답성을 높이기 위해 overhead를 줄임
  • class 데이터를 각 개개별의 파일에 저장할 경우 낭비가 심하여, 이를 방지함
  • class 데이터 파싱을 하면 class 로딩 시에 많은 overhead가 발생하므로, C 타입의 data value로 처리하는게 유리함
  • Bytecode verification은 필요하나 느리므로, app의 실행외에 verify 하는 방향으로 추진
  • Bytecode 최적화는 속도와 배터리 수명을 위해 매우 중요함
  • 보안을 위해 프로세스는 공유 코드를 못 고침

일반적인 VM implementation은 compressed archive 에서 (uncompressed) individual classes를 꺼내와 heap에 저장함.
이는 곧, 모든 프로세스마다 각 class들의 카피를 가지고 있고, app이 시작할때 코드의 압축을 풀어야 하기 때문에 느림.  반면에 로컬 heap에 bytecode를 가지고 있으면, rewrite 하기 쉽다는 장점이 있음
 

결론적으로 다음과 같은 결과물이 필요하다.


  • multiple class들은 하나의 "DEX" file로 통합됨
  • DEX 파일은 read-only로 맵핑되며 프로세스간에 공유됨
  • Byte ordering 과 word alignment는 local system에 맞게 변경가능함
  • Bytecode verification은 필요하나 "pre-verify"를 함
  • bytecode를 rewrite해야 하는 최적화는 반드시 선행되어야 함

VM Operation


어플리케이션 코드는 어플이름.jar 혹은 어플이름.apk 로 시스템이 전달되는데, 이는 단지 어플이름.zip 에 meta-data를 첨가한 것이다.  Dalvik DEX data 파일은 항상 clases.dex라고 표기된다.

bytecode는 zip 형태로 데이터가 압축되어 있고, 파일의 시작이 word-aligned가 아닐 수 있으므로, 메모리 맵핑되거나 zip 파일로부터 곧바로 실행이 불가능하다.  그러므로 classes.dex를 압축해제 하고 필요한 다른 액션(예를 들어 realignment, optimization, verification등)을 취해줘야 한다.  그럼 누가 이런 결과물을 만들어내고 저장해야 하는가??


Preparation


"preapred" DEX file을 생성하는데는 적어도 3가지의 방법이 있다.


  1. VM 이 'Just-In-Time'(보통 JIT)을 통해 dalvik-cache 디렉터리에 결과물을 저장. desktop이나 engineering-only 디바이스에 적합하다.(dalvik-cache의 퍼미션 문제로 양산품에는 적용불가함)
  2. dalvik-cache에 쓸 권한을 가진 시스템 인스톨러가 어플리케이션이 처음 추가되었을 때 한다. 
  3. classes.dex를 제외한 관련 jar나 apk파일들을 이용해  빌드시스템이 선행적으로 진행한다. optimized DEX 파일은 오리지널 zip 아카이브 옆에 놓이며, 시스템 이미지의 일부분이 된다.

dalvik-cache 디렉터리는 $ANDROID_DATA/data/dalvik-cache이다.  이 디렉터리 안의 파일 이름은 source DEX의 full path를 사용하며, 이 디렉터리는 system을 owner로 0771 권한을 가지고 있다.  안의 optimized DEX 파일들은 system이 owner이며 application 이 gropu으로, 0644 권한을 가지고 있다.




'안드로이드 > 프레임워크' 카테고리의 다른 글

Bionic libc (2)  (0) 2012.11.08
Bionic libc  (0) 2012.10.30
안드로이드 init.rc (oom_adj값)  (0) 2012.09.27
안드로이드 바인더 ipc  (0) 2011.10.23
안드로이드 프레임워크 *Java에서 전처리기 사용하기*  (0) 2011.06.06
Posted by code cat
출처: 인사이드 안드로이드(위키북스출판)

RPC code & RPC data
서비스 클라이언트는 서비스서버에 존재하는 서비스의 함수를 사용하기 위해,
각 함수에 해당하는 식별자를 바인더 IPC데이터에 담아 전달 <-- RPC 코드
함수의 인자 역시 IPC데이터에 담아 전달                           <-- RPC 데이터

즉, 서비스 서버가 가진 서비스의 함수를 호출할려면, PRC코드를 알아야 함.

바인더 어드레싱
context manager : 다양한 서비를 모두 목록화해서 관리함.  서비스마다 핸들(바인더 IPC의 목적지 주소로 사용)이라는 번호값을 할당. 서비스의 추가/검색등의 관리기능을 수행.  %context manager의 핸들값은 0으로 지정되어 있음
바인더 드라이버는 IPC 데이터의 핸들을 가지고 서비스 서버를 찾는데, 이러한 과정을 바인더 어드레싱이라 함.

바인더 어드레싱을 위해 서비스 서버는 자신이 가진 서비스에 대한 접근 정보를 컨텍스트 매니저에 등록해야함.
서비스서버는 ADD_SERVICE라는 RPC코드와 등록할 서비스 이름(RPC데이터), 그리고 핸들을 0으로 지정하고IPC데이터에 실어 바인더 드라이버에 전달함.
바인더 드라이버는 먼저 핸들 0에 해당하는 바인더 노드를 찾음. 핸들 0에 해당하는 바인더 노드를 context manager를 의미하기 때문에 서비스 서버는 IPC데이터를 context manager에 전달함.
이후 바인더 드라이버는 서비스서버의 서비스 A에 해당하는 바인더 노드를 하나 생성함.  그리고 contenxt manager가 생성된 바인더 노드를 알수 있게 바인더 노드 참조 데이터를 생성해 해당 노드를 연결함.
참조 데이터는 생성된 순서대로 번호가 매겨지고, 이 번호는 ipc데이터에 담겨 context manger로 전달됨
context manager는 ipc데이터에 들어있는 서비스 이름과 바인더 노드의 번호를 서비스 리스트에 등록.

 %바인더 노드는 바인더 드라이버내 자료구조이며, 서비스마다 하나씩 존재함. 그리고 프로세스마다 바인더 노드들을 리스트로 가진다.(바인더 노드는 ipc의 대상이 되는 프로세스를 찾기 위한 식별자 정도로 이해하면 됨)

프로세스 관점에서의 서비스 사용
클라이언트던 서버 입장이던 다음과 같은 공통 작업을 거친다.
1. 바인더 드라이버 열기 open()함수 호출
2. IPC 데이터/응답 데이터 수신 버퍼 확보 mmap()함수 호출
3. 데이터 송신을 위한 ioctl()함수 호출

바인더 드라이버 관점에서의 서비스 사용
...

'안드로이드 > 프레임워크' 카테고리의 다른 글

Bionic libc (2)  (0) 2012.11.08
Bionic libc  (0) 2012.10.30
안드로이드 init.rc (oom_adj값)  (0) 2012.09.27
Dalvik Virtual Machine 와 odex  (0) 2012.08.13
안드로이드 프레임워크 *Java에서 전처리기 사용하기*  (0) 2011.06.06
Posted by code cat