And yes, both the example you found and what I'm pointing you to
here will run as "system", not root. That's because in both cases
the system service is added to the system_server process which
itself is running as "system". Note that *NONE* of the processes
housing system services is running as root. Here's are relevant ps
snippets from 4.2 (servicemanager and zygote don't run system
services, but they're relevant):
system 30 1 836 348 c0195c08 40036fc0 S
/system/bin/servicemanager
...
system 36 1 16704 5412 ffffffff 4006bfc0 S
/system/bin/surfaceflinger
...
root 37 1 166052 34808 ffffffff 400370e4 S zygote
...
media 39 1 28196 6872 ffffffff 4008afc0 S
/system/bin/mediaserver
...
system 272 37 254784 41392 ffffffff 40036fc0 S system_server
...
radio 403 37 195408 27788 ffffffff 40037ebc S
com.android.phone
In the cases where system services need some form of root access,
the architecture that's relied on is to have a native C daemon
running in the background and servicing a Java-based system service
through a socket. Ex.:
phone service -> rild
mount service -> vold
package manager ->installd
etc.
In all those cases, the system service on the left runs as "system"
and the daemon on the right runs as some other user. Further ps
snippets:
root 31 1 4008 824 ffffffff 4003e76c S
/system/bin/vold
root 33 1 8632 1232 ffffffff 4006a76c S
/system/bin/netd
...
radio 35 1 5468 836 ffffffff 4003776c S
/system/bin/rild
...
install 40 1 848 440 c021db90 40036d50 S
/system/bin/installd
...
I'm not 100% sure as to why this architecture has been chosen, but
that's the template you'll find replicated. So you might want to
stick with something like that.
If you want to be original then check out how "am", "pm" and "svc"
are implemented. They all use app_process to start a Dalvik VM off
the command line and proceed to talk to a system service through
Binder. You could, I guess, write one that actually acts as a system
service and that is started by init as a root process. Let me know
if you do that, I'd be curious to see the code. You could also check
out the surfaceflinger which is a system service written in C but
that doesn't run as root. You could add your own that does.
Hope this helps,
--
Karim Yaghmour
CEO - Opersys inc. / www.opersys.com
http://twitter.com/karimyaghmour