Wednesday, October 19, 2011

Adding User or Group to Android

Today I had a requirement to add a group(GID) to android system.

I found out that android system has hardcoded GID's for few Groups which is defined during the build time.

Step1: vim $(Android_root_folder)/system/core/include/private/android_filesystem_config.h

Add the below line for "lp" group creation

eg: #define AID_LP 3006

static struct android_id_info android_ids[]={
{"lp", AID_LP, },
};

Step2: Build the android source code.

Step3: Test whether group is created on some file eg: chown root.lp <file_name>

Step4: ls -l on the files you chowned, you can see group gets added successfully.


Note: Like linux you cant find /etc/passwd or /etc/group files in case of android. All the group additions are hardcoded.

I am yet to figure out how we can add users to the groups.


Monday, June 20, 2011

Some Insight in to android permission granting by PackageManagerService

Android Maintains application permissions in the android filesystem:

1.  File where all permissions assigned to a application(3rd party or system apps) will be stored in
/data/system/packages.xml

PackageManagerService.java
mSettings.writeLP() method writes the permissions to the above location

2.  File where all the packages are displayed as list with packagename, path and uid is
/data/system/packages.list
eg: com.android.providers.downloads 10010 0 /data/data/com.android.providers.media

3.  Granting of permissions during install is done by
   PackageManagerService.grantPermissionsLP()

4. Usually during Package Installation, installer determines whether or not to grant the requested permission by checking the authorities that signed the applications certificates and, in some cases, asking the user

5. Protection-Level for permissions:
    -> Normal
    -> Dangerous
    -> Signature
    -> signatureorsystem

6. All built-in permissions such as INTERNET, RECEIVE_SMS etc... falls under Normal Protection level.

7. Dangerous: dangerous permissions requested by an application is displayed to the user and require confirmation before proceeding.

8. Built-in permissions with respective gids are maintained by the android system in
/etc/permissions/platform.xml:
eg: <permission name = "android.permission.INTERNET">
         <group gid="camera" />
     </permission>
      <assign-permission name="android.permission.SEND_SMS" uid="shell" />
    library: /system/framework/android.test.runner.jar
               /system/framework/javax.obex.jar

9. PackageManager pm = getPackageManager();
    -> readPermissions(): Reads for gids from /etc/permissions/platform.xml for a particular permissions(INTERNET, SEND_SMS) which is included in the AndroidManifest file of an application.
    -> mSettings.readLP(): ReadPermissions from /data/system/packages.xml

10. Android grant permission by assigning the application process having eg:INTERNET permission to gid="inet".

Note: Gid's assigned to an application can do the operations assigned to it by the android system so "inet" gid assigned to an application can access INTERNET from your android device

11. During application launch time the gids will be assigned to the application process ie when it is fork() you can see the same in dalvik/vm/native/dalvik_system_Zygote.c under forkAndSpecializeCommon() method.
 

Compiling JNI shared library inside android source !!!

Today I was writing my JNI app for my company project and I will share how to compile the same.

Compiling JNI Shared Library:

Step1: I moved my .c file inside external/demo_jni/ folder
Step2: Wrote my Android.mk file which goes like below

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
LOCAL_MODULE := Demo-jni
LOCAL_SRC_FILES:= Demo-jni.c
LOCAL_PRELINK_MODULE := false
LOCAL_SHARED_LIBRARIES := libcutils
include $(BUILD_SHARED_LIBRARY)

Step3: compile the entire source code using make command
cmd: make ARCH=arm CROSS_COMPILE=<Dir-Path-android-src>/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-

Step4: cd out/target/product/generic/obj/SHARED_LIBRARIES/

You will find .so shared library created in respective folder


Please drop a comment if you like it.

Thursday, June 16, 2011

Logging in Native Code of Android

Today when i was working on native c code my printf didnt get logged so I experimented on how to put logs in native code which gets logged in logcat.

Below is the steps which i followed, hope it might help someone.

Step1: Included following header and log statement to get printed in logcat
           #include<android/log.h>
           __android_log_print(ANDROID_LOG_INFO, "MYPROG", "Hello i am inside jni");

Step2: Included below statements inside my Android.mk
          LOCAL_SHARED_LIBRARIES := libcutils

I followed the below link
https://groups.google.com/group/android-ndk/browse_thread/thread/2e27ecb1fa834c1c

LOCAL_LDLIBS := -llog,  didnt work for me as mentioned in the above link. The reason might be I am including my source code(JNI code) inside android source build system rather than building via NDK . I was getting Linker error because it was not able to get the reference for "__android_log_print"

In your android source code you will get libcutils already built as shared library under out/target/product/generic/obj/SHARED_LIBRARIES/libcutils_intermediates, so only reference to this lib as in step 2: above will work.


Please provide me any suggestions :-)