Listen to UEvents

1,334 views
Skip to first unread message

freakingtux

unread,
Jan 4, 2011, 11:40:55 AM1/4/11
to android...@googlegroups.com
Hi,

I am trying to create a Java service that listens to kernel netlink uevent in order for it to detect when a Usb device is plugged-in/out.(is there a more simple way to do this in 2.2. ?)

Basically I want a kernel uevent -> broadcast intent service

I found a few classes in the Android source that listen to netlink events. A few are daemons like vold and netd and these run as root(and it is not possible for a apk installed content to run as root ).

I also found android.os.UEventObserver used by the system_server(I think) to do things like dock detection those are found in
frameworks//base/services/java/com/android/server/ and are called DockObserver , HeadsetObserver, BatteryService and HDMIObserver.

UEventObserver spawns a thread that will call native code found in hardware/libhardware_legacy/uevent. This code basically opens the socket
and binds to receive broadcast messages.I believe this happens in the system_server who runs as user "system"

When I try to mimic this behavior I get (as expected?)  the following exception:
/ActivityManager(   62): Start proc com.sample.android.platform.core for broadcast com.sample.android.platform.core/com.sample.android.platform.service.PlatformServer: pid=244 uid=1000 gids={3002, 3001, 3003}
D/PlatformServer(  244):  Starting
I/UsbDeviceObserver(  244): new Device Observer
W/dalvikvm(  244): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  244): FATAL EXCEPTION: UEventObserver
E/AndroidRuntime(  244): java.lang.RuntimeException: Unable to open socket for UEventObserver
E/AndroidRuntime(  244):        at android.os.UEventObserver.native_setup(Native Method)
E/AndroidRuntime(  244):        at android.os.UEventObserver.access$000(UEventObserver.java:39)
E/AndroidRuntime(  244):        at android.os.UEventObserver$UEventThread.run(UEventObserver.java:97)

(you can see I run a system user by the 1000 system userid) and I have the following permission declared
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Hence my question(s):
system_server who runs as user "system" how can is bind to a port < 1024 ? did I miss something?
How can I get  notifications of events (like usb plugging) into my application? I plan to talk to the plugged usb device using libusb.

Greetings

P.S. I also found this thread on a similar subject
http://groups.google.com/group/android-ndk/browse_thread/thread/1f8447c39ddf8606

freakingtux

unread,
Jan 19, 2011, 10:37:27 AM1/19/11
to android...@googlegroups.com
On Tuesday, January 4, 2011 5:40:55 PM UTC+1, freakingtux wrote:

system_server who runs as user "system" how can is bind to a port < 1024 ? did I miss something?
How can I get  notifications of events (like usb plugging) into my application? I plan to talk to the plugged usb device using libusb.

I think I found part of the answer.

Android first boots and runs init (,/system/core/init/), and executes the rc script using the Android Init Language. Somewhere in the init
zygote is started  with argument --start-system-server. I can find refs here (frameworks/base/cmds/app_process/app_main.cpp) but was not fully able to follow the flow of the code.

later  zygote (who is running a root?) starts the system server in  /frameworks/base/cmds/runtime/main_runtime.cpp and gets the bind capabilities

static const char* ZYGOTE_ARGV[] = {
    "--setuid=1000",
    "--setgid=1000",
    "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003",
    /* CAP_SYS_TTY_CONFIG & CAP_SYS_RESOURCE & CAP_NET_BROADCAST &
     * CAP_NET_ADMIN & CAP_NET_RAW & CAP_NET_BIND_SERVICE  & CAP_KILL &
     * CAP_SYS_BOOT CAP_SYS_NICE
     */
    "--capabilities=96549920,96549920",
    "--runtime-init",
    "--nice-name=system_server",
    "com.android.server.SystemServer"
};



Reply all
Reply to author
Forward
0 new messages